3 namespace Drupal\KernelTests\Config;
5 use Drupal\Core\Config\FileStorage;
6 use Drupal\Core\Config\InstallStorage;
7 use Drupal\Core\Config\StorageInterface;
8 use Drupal\KernelTests\AssertConfigTrait;
9 use Drupal\KernelTests\FileSystemModuleDiscoveryDataProviderTrait;
10 use Drupal\KernelTests\KernelTestBase;
13 * Tests that the installed config matches the default config.
17 class DefaultConfigTest extends KernelTestBase {
19 use AssertConfigTrait;
20 use FileSystemModuleDiscoveryDataProviderTrait;
25 protected static $timeLimit = 500;
30 public static $modules = ['system', 'user'];
33 * The following config entries are changed on module install.
35 * Comparing them does not make sense.
37 * @todo Figure out why simpletest.settings is not installed.
41 public static $skippedConfig = [
42 'locale.settings' => ['path: '],
43 'syslog.settings' => ['facility: '],
44 'simpletest.settings' => TRUE,
50 protected function setUp() {
53 // @todo ModuleInstaller calls system_rebuild_module_data which is part of
54 // system.module, see https://www.drupal.org/node/2208429.
55 include_once $this->root . '/core/modules/system/system.module';
57 // Set up the state values so we know where to find the files when running
58 // drupal_get_filename().
59 // @todo Remove as part of https://www.drupal.org/node/2186491
60 system_rebuild_module_data();
64 * Tests if installed config is equal to the exported config.
66 * @dataProvider coreModuleListDataProvider
68 public function testModuleConfig($module) {
69 // System and user are required in order to be able to install some of the
70 // other modules. Therefore they are put into static::$modules, which though
71 // doesn't install config files, so import those config files explicitly.
75 $this->installConfig([$module]);
79 $module_path = drupal_get_path('module', $module) . '/';
81 /** @var \Drupal\Core\Extension\ModuleInstallerInterface $module_installer */
82 $module_installer = $this->container->get('module_installer');
84 // Work out any additional modules and themes that need installing to create
85 // an optional config.
86 $optional_config_storage = new FileStorage($module_path . InstallStorage::CONFIG_OPTIONAL_DIRECTORY, StorageInterface::DEFAULT_COLLECTION);
87 $modules_to_install = [$module];
88 $themes_to_install = [];
89 foreach ($optional_config_storage->listAll() as $config_name) {
90 $data = $optional_config_storage->read($config_name);
91 if (isset($data['dependencies']['module'])) {
92 $modules_to_install = array_merge($modules_to_install, $data['dependencies']['module']);
94 if (isset($data['dependencies']['theme'])) {
95 $themes_to_install = array_merge($themes_to_install, $data['dependencies']['theme']);
98 $module_installer->install(array_unique($modules_to_install));
99 $this->container->get('theme_installer')->install($themes_to_install);
101 // Test configuration in the module's config/install directory.
102 $module_config_storage = new FileStorage($module_path . InstallStorage::CONFIG_INSTALL_DIRECTORY, StorageInterface::DEFAULT_COLLECTION);
103 $this->doTestsOnConfigStorage($module_config_storage);
105 // Test configuration in the module's config/optional directory.
106 $this->doTestsOnConfigStorage($optional_config_storage);
110 * Tests that default config matches the installed config.
112 * @param \Drupal\Core\Config\StorageInterface $default_config_storage
113 * The default config storage to test.
115 protected function doTestsOnConfigStorage(StorageInterface $default_config_storage) {
116 /** @var \Drupal\Core\Config\ConfigManagerInterface $config_manager */
117 $config_manager = $this->container->get('config.manager');
119 // Just connect directly to the config table so we don't need to worry about
121 $active_config_storage = $this->container->get('config.storage');
123 foreach ($default_config_storage->listAll() as $config_name) {
124 if ($active_config_storage->exists($config_name)) {
125 // If it is a config entity re-save it. This ensures that any
126 // recalculation of dependencies does not cause config change.
127 if ($entity_type = $config_manager->getEntityTypeIdByName($config_name)) {
128 $entity_storage = $config_manager
130 ->getStorage($entity_type);
131 $id = $entity_storage->getIDFromConfigName($config_name, $entity_storage->getEntityType()
132 ->getConfigPrefix());
133 $entity_storage->load($id)->calculateDependencies()->save();
135 $result = $config_manager->diff($default_config_storage, $active_config_storage, $config_name);
136 $this->assertConfigDiff($result, $config_name, static::$skippedConfig);