function ConfigInstallTest::testCollectionInstallationCollections

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Config/ConfigInstallTest.php \Drupal\KernelTests\Core\Config\ConfigInstallTest::testCollectionInstallationCollections()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Config/ConfigInstallTest.php \Drupal\KernelTests\Core\Config\ConfigInstallTest::testCollectionInstallationCollections()
  3. 10 core/tests/Drupal/KernelTests/Core/Config/ConfigInstallTest.php \Drupal\KernelTests\Core\Config\ConfigInstallTest::testCollectionInstallationCollections()

Tests config objects in collections are installed as expected.

File

core/tests/Drupal/KernelTests/Core/Config/ConfigInstallTest.php, line 102

Class

ConfigInstallTest
Tests installation of configuration objects in installation functionality.

Namespace

Drupal\KernelTests\Core\Config

Code

public function testCollectionInstallationCollections() : 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);
    // Install the test module.
    $this->enableModules([
        'config_collection_install_test',
    ]);
    $this->installConfig([
        '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']);
    }
    // Tests that clashing configuration in collections is detected.
    try {
        \Drupal::service('module_installer')->install([
            'config_collection_clash_install_test',
        ]);
        $this->fail('Expected PreExistingConfigException not thrown.');
    } catch (PreExistingConfigException $e) {
        $this->assertEquals('config_collection_clash_install_test', $e->getExtension());
        $this->assertEquals([
            'another_collection' => [
                'config_collection_install_test.test',
            ],
            'collection.test1' => [
                'config_collection_install_test.test',
            ],
            'collection.test2' => [
                'config_collection_install_test.test',
            ],
        ], $e->getConfigObjects());
        $this->assertEquals('Configuration objects (another_collection/config_collection_install_test.test, collection/test1/config_collection_install_test.test, collection/test2/config_collection_install_test.test) provided by config_collection_clash_install_test already exist in active configuration', $e->getMessage());
    }
    // Test that the we can use the config installer to install all the
    // available default configuration in a particular collection for enabled
    // extensions.
    \Drupal::service('config.installer')->installCollectionDefaultConfig('entity');
    // The 'entity' collection will not exist because the 'config_test' module
    // is not enabled.
    $this->assertEquals($collections, $active_storage->getAllCollectionNames());
    // Enable the 'config_test' module and try again.
    $this->enableModules([
        'config_test',
    ]);
    \Drupal::service('config.installer')->installCollectionDefaultConfig('entity');
    $collections[] = 'entity';
    $this->assertEquals($collections, $active_storage->getAllCollectionNames());
    $collection_storage = $active_storage->createCollection('entity');
    $data = $collection_storage->read('config_test.dynamic.dotted.default');
    $this->assertSame([
        'label' => 'entity',
    ], $data);
    // Test that the config manager uninstalls configuration from collections
    // as expected.
    \Drupal::state()->set('config_events_test.all_events', []);
    $this->container
        ->get('config.manager')
        ->uninstall('module', 'config_collection_install_test');
    $all_events = \Drupal::state()->get('config_events_test.all_events');
    $this->assertArrayHasKey(ConfigCollectionEvents::DELETE_IN_COLLECTION, $all_events);
    // The delete-in-collection event has been triggered 3 times.
    $this->assertCount(3, $all_events[ConfigCollectionEvents::DELETE_IN_COLLECTION]['config_collection_install_test.test']);
    $event_collections = [];
    foreach ($all_events[ConfigCollectionEvents::DELETE_IN_COLLECTION]['config_collection_install_test.test'] as $event) {
        $event_collections[] = $event['original_config_data']['collection'];
    }
    $this->assertSame([
        'another_collection',
        'collection.test1',
        'collection.test2',
    ], $event_collections);
    $this->assertEquals([
        'entity',
    ], $active_storage->getAllCollectionNames());
    \Drupal::state()->set('config_events_test.all_events', []);
    $this->container
        ->get('config.manager')
        ->uninstall('module', 'config_test');
    $this->assertEquals([], $active_storage->getAllCollectionNames());
    $all_events = \Drupal::state()->get('config_events_test.all_events');
    $this->assertArrayHasKey(ConfigCollectionEvents::DELETE_IN_COLLECTION, $all_events);
    $this->assertCount(1, $all_events[ConfigCollectionEvents::DELETE_IN_COLLECTION]['config_test.dynamic.dotted.default']);
}

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