function CheckpointStorageTest::testRevertWithCollections

Same name in other branches
  1. 10 core/tests/Drupal/KernelTests/Core/Config/Storage/Checkpoint/CheckpointStorageTest.php \Drupal\KernelTests\Core\Config\Storage\Checkpoint\CheckpointStorageTest::testRevertWithCollections()

File

core/tests/Drupal/KernelTests/Core/Config/Storage/Checkpoint/CheckpointStorageTest.php, line 204

Class

CheckpointStorageTest
Tests CheckpointStorage operations.

Namespace

Drupal\KernelTests\Core\Config\Storage\Checkpoint

Code

public function testRevertWithCollections() : void {
    $collections = [
        'another_collection',
        'collection.test1',
        'collection.test2',
    ];
    // Set the event listener to return three possible collections.
    // @see \Drupal\config_collection_install_test\EventSubscriber
    \Drupal::state()->set('config_collection_install_test.collection_names', $collections);
    $checkpoint_storage = $this->container
        ->get('config.storage.checkpoint');
    $checkpoint_storage->checkpoint('A');
    // Install the test module.
    $this->assertTrue($this->container
        ->get('module_installer')
        ->install([
        'config_collection_install_test',
    ]));
    $checkpoint_storage = $this->container
        ->get('config.storage.checkpoint');
    
    /** @var \Drupal\Core\Config\StorageInterface $active_storage */
    $active_storage = \Drupal::service('config.storage');
    $this->assertEquals($collections, $active_storage->getAllCollectionNames());
    foreach ($collections as $collection) {
        $collection_storage = $active_storage->createCollection($collection);
        $data = $collection_storage->read('config_collection_install_test.test');
        $this->assertEquals($collection, $data['collection']);
    }
    $check2 = $checkpoint_storage->checkpoint('B');
    $importer = $this->getConfigImporter($checkpoint_storage);
    $storage_comparer = $importer->getStorageComparer();
    $config_changelist = $storage_comparer->createChangelist()
        ->getChangelist();
    $this->assertSame([], $config_changelist['create']);
    $this->assertSame([
        'core.extension',
    ], $config_changelist['update']);
    $this->assertSame([], $config_changelist['delete']);
    $this->assertSame([], $config_changelist['rename']);
    foreach ($collections as $collection) {
        $config_changelist = $storage_comparer->getChangelist(NULL, $collection);
        $this->assertSame([], $config_changelist['create']);
        $this->assertSame([], $config_changelist['update']);
        $this->assertSame([
            'config_collection_install_test.test',
        ], $config_changelist['delete'], $collection);
        $this->assertSame([], $config_changelist['rename']);
    }
    $importer->import();
    $this->assertSame([], $importer->getErrors());
    $checkpoint_storage = $this->container
        ->get('config.storage.checkpoint');
    
    /** @var \Drupal\Core\Config\StorageInterface $active_storage */
    $active_storage = \Drupal::service('config.storage');
    $this->assertEmpty($active_storage->getAllCollectionNames());
    foreach ($collections as $collection) {
        $collection_storage = $active_storage->createCollection($collection);
        $this->assertFalse($collection_storage->read('config_collection_install_test.test'));
    }
    $checkpoint_storage->setCheckpointToReadFrom($check2);
    $importer = $this->getConfigImporter($checkpoint_storage);
    $storage_comparer = $importer->getStorageComparer();
    $config_changelist = $storage_comparer->createChangelist()
        ->getChangelist();
    $this->assertSame([], $config_changelist['create']);
    $this->assertSame([
        'core.extension',
    ], $config_changelist['update']);
    $this->assertSame([], $config_changelist['delete']);
    $this->assertSame([], $config_changelist['rename']);
    foreach ($collections as $collection) {
        $config_changelist = $storage_comparer->getChangelist(NULL, $collection);
        $this->assertSame([
            'config_collection_install_test.test',
        ], $config_changelist['create']);
        $this->assertSame([], $config_changelist['update']);
        $this->assertSame([], $config_changelist['delete'], $collection);
        $this->assertSame([], $config_changelist['rename']);
    }
    $importer->import();
    $this->assertSame([], $importer->getErrors());
    $this->assertTrue($this->container
        ->get('module_handler')
        ->moduleExists('config_collection_install_test'));
    
    /** @var \Drupal\Core\Config\StorageInterface $active_storage */
    $active_storage = \Drupal::service('config.storage');
    $this->assertEquals($collections, $active_storage->getAllCollectionNames());
    foreach ($collections as $collection) {
        $collection_storage = $active_storage->createCollection($collection);
        $data = $collection_storage->read('config_collection_install_test.test');
        $this->assertEquals($collection, $data['collection']);
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.