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 // @todo https://www.drupal.org/node/2308745 Rest has an implicit dependency
85 // on the Node module remove once solved.
86 if (in_array($module, ['rest', 'hal'])) {
87 $module_installer->install(['node']);
90 // Work out any additional modules and themes that need installing to create
91 // an optional config.
92 $optional_config_storage = new FileStorage($module_path . InstallStorage::CONFIG_OPTIONAL_DIRECTORY, StorageInterface::DEFAULT_COLLECTION);
93 $modules_to_install = [$module];
94 $themes_to_install = [];
95 foreach ($optional_config_storage->listAll() as $config_name) {
96 $data = $optional_config_storage->read($config_name);
97 if (isset($data['dependencies']['module'])) {
98 $modules_to_install = array_merge($modules_to_install, $data['dependencies']['module']);
100 if (isset($data['dependencies']['theme'])) {
101 $themes_to_install = array_merge($themes_to_install, $data['dependencies']['theme']);
104 $module_installer->install(array_unique($modules_to_install));
105 $this->container->get('theme_installer')->install($themes_to_install);
107 // Test configuration in the module's config/install directory.
108 $module_config_storage = new FileStorage($module_path . InstallStorage::CONFIG_INSTALL_DIRECTORY, StorageInterface::DEFAULT_COLLECTION);
109 $this->doTestsOnConfigStorage($module_config_storage);
111 // Test configuration in the module's config/optional directory.
112 $this->doTestsOnConfigStorage($optional_config_storage);
116 * Tests that default config matches the installed config.
118 * @param \Drupal\Core\Config\StorageInterface $default_config_storage
119 * The default config storage to test.
121 protected function doTestsOnConfigStorage(StorageInterface $default_config_storage) {
122 /** @var \Drupal\Core\Config\ConfigManagerInterface $config_manager */
123 $config_manager = $this->container->get('config.manager');
125 // Just connect directly to the config table so we don't need to worry about
127 $active_config_storage = $this->container->get('config.storage');
129 foreach ($default_config_storage->listAll() as $config_name) {
130 if ($active_config_storage->exists($config_name)) {
131 // If it is a config entity re-save it. This ensures that any
132 // recalculation of dependencies does not cause config change.
133 if ($entity_type = $config_manager->getEntityTypeIdByName($config_name)) {
134 $entity_storage = $config_manager
136 ->getStorage($entity_type);
137 $id = $entity_storage->getIDFromConfigName($config_name, $entity_storage->getEntityType()
138 ->getConfigPrefix());
139 $entity_storage->load($id)->calculateDependencies()->save();
141 $result = $config_manager->diff($default_config_storage, $active_config_storage, $config_name);
142 $this->assertConfigDiff($result, $config_name, static::$skippedConfig);