View source
<?php
namespace Drupal\KernelTests\Core\Config;
use Drupal\Core\Config\InstallStorage;
use Drupal\Core\Config\PreExistingConfigException;
use Drupal\Core\Config\UnmetDependenciesException;
use Drupal\KernelTests\KernelTestBase;
class ConfigInstallTest extends KernelTestBase {
protected static $modules = [
'system',
];
protected function setUp() : void {
parent::setUp();
unset($GLOBALS['hook_config_test']);
}
public function testModuleInstallation() {
$default_config = 'config_test.system';
$default_configuration_entity = 'config_test.dynamic.dotted.default';
$config = $this
->config($default_config);
$this
->assertTrue($config
->isNew());
$config = $this
->config($default_configuration_entity);
$this
->assertTrue($config
->isNew());
$this
->assertFalse(\Drupal::service('config.typed')
->hasConfigSchema('config_schema_test.someschema'), 'Configuration schema for config_schema_test.someschema does not exist.');
$this
->installModules([
'config_test',
]);
\Drupal::configFactory()
->reset($default_config);
\Drupal::configFactory()
->reset($default_configuration_entity);
$config = $this
->config($default_config);
$this
->assertFalse($config
->isNew());
$config = $this
->config($default_configuration_entity);
$this
->assertFalse($config
->isNew());
$this
->assertFalse(isset($GLOBALS['hook_config_test']['load']));
$this
->assertTrue(isset($GLOBALS['hook_config_test']['presave']));
$this
->assertTrue(isset($GLOBALS['hook_config_test']['insert']));
$this
->assertFalse(isset($GLOBALS['hook_config_test']['update']));
$this
->assertFalse(isset($GLOBALS['hook_config_test']['predelete']));
$this
->assertFalse(isset($GLOBALS['hook_config_test']['delete']));
$this
->enableModules([
'config_schema_test',
]);
$this
->installConfig([
'config_schema_test',
]);
$this
->assertTrue(\Drupal::service('config.typed')
->hasConfigSchema('config_schema_test.someschema'), 'Configuration schema for config_schema_test.someschema exists.');
$this
->config('core.extension')
->set('module', [])
->save();
\Drupal::service('config.manager')
->uninstall('module', 'config_test');
$this
->assertFalse(\Drupal::service('config.typed')
->hasConfigSchema('config_schema_test.someschema'), 'Configuration schema for config_schema_test.someschema does not exist.');
}
public function testCollectionInstallationNoCollections() {
$this
->enableModules([
'config_collection_install_test',
]);
$this
->installConfig([
'config_collection_install_test',
]);
$active_storage = \Drupal::service('config.storage');
$this
->assertEquals([], $active_storage
->getAllCollectionNames());
}
public function testCollectionInstallationCollections() {
$collections = [
'another_collection',
'collection.test1',
'collection.test2',
];
\Drupal::state()
->set('config_collection_install_test.collection_names', $collections);
$this
->enableModules([
'config_collection_install_test',
]);
$this
->installConfig([
'config_collection_install_test',
]);
$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']);
}
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());
}
\Drupal::service('config.installer')
->installCollectionDefaultConfig('entity');
$this
->assertEquals($collections, $active_storage
->getAllCollectionNames());
$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);
\Drupal::service('config.manager')
->uninstall('module', 'config_collection_install_test');
$this
->assertEquals([
'entity',
], $active_storage
->getAllCollectionNames());
\Drupal::service('config.manager')
->uninstall('module', 'config_test');
$this
->assertEquals([], $active_storage
->getAllCollectionNames());
}
public function testCollectionInstallationCollectionConfigEntity() {
$collections = [
'entity',
];
\Drupal::state()
->set('config_collection_install_test.collection_names', $collections);
$this
->installModules([
'config_test',
'config_collection_install_test',
]);
$active_storage = \Drupal::service('config.storage');
$this
->assertEquals($collections, $active_storage
->getAllCollectionNames());
$collection_storage = $active_storage
->createCollection('entity');
$name = 'config_test.dynamic.dotted.default';
$data = $active_storage
->read($name);
$this
->assertTrue(isset($data['uuid']));
$data = $collection_storage
->read($name);
$this
->assertSame([
'label' => 'entity',
], $data);
}
public function testDependencyChecking() {
$this
->installModules([
'config_test',
]);
try {
$this
->installModules([
'config_install_dependency_test',
]);
$this
->fail('Expected UnmetDependenciesException not thrown.');
} catch (UnmetDependenciesException $e) {
$this
->assertEquals('config_install_dependency_test', $e
->getExtension());
$this
->assertEquals([
'config_test.dynamic.other_module_test_with_dependency' => [
'config_other_module_config_test',
'config_test.dynamic.dotted.english',
],
], $e
->getConfigObjects());
$this
->assertEquals('Configuration objects provided by <em class="placeholder">config_install_dependency_test</em> have unmet dependencies: <em class="placeholder">config_test.dynamic.other_module_test_with_dependency (config_other_module_config_test, config_test.dynamic.dotted.english)</em>', $e
->getMessage());
}
try {
$this
->installModules([
'config_install_double_dependency_test',
]);
$this
->fail('Expected UnmetDependenciesException not thrown.');
} catch (UnmetDependenciesException $e) {
$this
->assertEquals('config_install_double_dependency_test', $e
->getExtension());
$this
->assertEquals([
'config_test.dynamic.other_module_test_with_dependency' => [
'config_other_module_config_test',
'config_test.dynamic.dotted.english',
],
], $e
->getConfigObjects());
$this
->assertEquals('Configuration objects provided by <em class="placeholder">config_install_double_dependency_test</em> have unmet dependencies: <em class="placeholder">config_test.dynamic.other_module_test_with_dependency (config_other_module_config_test, config_test.dynamic.dotted.english)</em>', $e
->getMessage());
}
$this
->installModules([
'config_test_language',
]);
try {
$this
->installModules([
'config_install_dependency_test',
]);
$this
->fail('Expected UnmetDependenciesException not thrown.');
} catch (UnmetDependenciesException $e) {
$this
->assertEquals('config_install_dependency_test', $e
->getExtension());
$this
->assertEquals([
'config_test.dynamic.other_module_test_with_dependency' => [
'config_other_module_config_test',
],
], $e
->getConfigObjects());
$this
->assertEquals('Configuration objects provided by <em class="placeholder">config_install_dependency_test</em> have unmet dependencies: <em class="placeholder">config_test.dynamic.other_module_test_with_dependency (config_other_module_config_test)</em>', $e
->getMessage());
}
$this
->installModules([
'config_other_module_config_test',
]);
$this
->installModules([
'config_install_dependency_test',
]);
$entity = \Drupal::entityTypeManager()
->getStorage('config_test')
->load('other_module_test_with_dependency');
$this
->assertNotEmpty($entity, 'The config_test.dynamic.other_module_test_with_dependency configuration has been created during install.');
$this
->assertSame('config_install_dependency_test', $entity
->getDependencies()['module'][0]);
}
public function testLanguage() {
$this
->installModules([
'config_test_language',
]);
$storage = new InstallStorage();
$data = $storage
->read('config_test.dynamic.dotted.english');
$this
->assertTrue(!isset($data['langcode']));
$this
->assertEquals('en', $this
->config('config_test.dynamic.dotted.english')
->get('langcode'));
$data = $storage
->read('config_test.dynamic.dotted.french');
$this
->assertEquals('fr', $data['langcode']);
$this
->assertEquals('fr', $this
->config('config_test.dynamic.dotted.french')
->get('langcode'));
}
public function testIdMisMatch() {
$this
->expectWarning();
$this
->expectWarningMessage('The configuration name "config_test.dynamic.no_id_match" does not match the ID "does_not_match"');
$this
->installModules([
'config_test_id_mismatch',
]);
}
protected function installModules(array $modules) {
$this->container
->get('module_installer')
->install($modules);
$this->container = \Drupal::getContainer();
}
}