Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / tests / Drupal / KernelTests / Config / DefaultConfigTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Config;
4
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;
11
12 /**
13  * Tests that the installed config matches the default config.
14  *
15  * @group Config
16  */
17 class DefaultConfigTest extends KernelTestBase {
18
19   use AssertConfigTrait;
20   use FileSystemModuleDiscoveryDataProviderTrait;
21
22   /**
23    * {@inheritdoc}
24    */
25   protected static $timeLimit = 500;
26
27   /**
28    * {@inheritdoc}
29    */
30   public static $modules = ['system', 'user'];
31
32   /**
33    * The following config entries are changed on module install.
34    *
35    * Comparing them does not make sense.
36    *
37    * @todo Figure out why simpletest.settings is not installed.
38    *
39    * @var array
40    */
41   public static $skippedConfig = [
42     'locale.settings' => ['path: '],
43     'syslog.settings' => ['facility: '],
44     'simpletest.settings' => TRUE,
45   ];
46
47   /**
48    * {@inheritdoc}
49    */
50   protected function setUp() {
51     parent::setUp();
52
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';
56
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();
61   }
62
63   /**
64    * Tests if installed config is equal to the exported config.
65    *
66    * @dataProvider coreModuleListDataProvider
67    */
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.
72     switch ($module) {
73       case 'system':
74       case 'user':
75         $this->installConfig([$module]);
76         break;
77     }
78
79     $module_path = drupal_get_path('module', $module) . '/';
80
81     /** @var \Drupal\Core\Extension\ModuleInstallerInterface $module_installer */
82     $module_installer = $this->container->get('module_installer');
83
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']);
93       }
94       if (isset($data['dependencies']['theme'])) {
95         $themes_to_install = array_merge($themes_to_install, $data['dependencies']['theme']);
96       }
97     }
98     $module_installer->install(array_unique($modules_to_install));
99     $this->container->get('theme_installer')->install($themes_to_install);
100
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);
104
105     // Test configuration in the module's config/optional directory.
106     $this->doTestsOnConfigStorage($optional_config_storage);
107   }
108
109   /**
110    * Tests that default config matches the installed config.
111    *
112    * @param \Drupal\Core\Config\StorageInterface $default_config_storage
113    *   The default config storage to test.
114    */
115   protected function doTestsOnConfigStorage(StorageInterface $default_config_storage) {
116     /** @var \Drupal\Core\Config\ConfigManagerInterface $config_manager */
117     $config_manager = $this->container->get('config.manager');
118
119     // Just connect directly to the config table so we don't need to worry about
120     // the cache layer.
121     $active_config_storage = $this->container->get('config.storage');
122
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
129             ->getEntityManager()
130             ->getStorage($entity_type);
131           $id = $entity_storage->getIDFromConfigName($config_name, $entity_storage->getEntityType()
132             ->getConfigPrefix());
133           $entity_storage->load($id)->calculateDependencies()->save();
134         }
135         $result = $config_manager->diff($default_config_storage, $active_config_storage, $config_name);
136         $this->assertConfigDiff($result, $config_name, static::$skippedConfig);
137       }
138     }
139   }
140
141 }