1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
<?php
declare(strict_types=1);
namespace Drupal\KernelTests\Config;
use Drupal\Core\Config\Entity\ConfigEntityDependency;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Config\InstallStorage;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Extension\ExtensionLifecycle;
use Drupal\KernelTests\AssertConfigTrait;
use Drupal\KernelTests\FileSystemModuleDiscoveryDataProviderTrait;
use Drupal\KernelTests\KernelTestBase;
/**
* Tests that the installed config matches the default config.
*
* @group Config
* @group #slow
*/
class DefaultConfigTest extends KernelTestBase {
use AssertConfigTrait;
use FileSystemModuleDiscoveryDataProviderTrait;
/**
* {@inheritdoc}
*/
protected static $timeLimit = 500;
/**
* {@inheritdoc}
*/
protected static $modules = ['system', 'user', 'path_alias'];
/**
* The following config entries are changed on module install.
*
* Comparing them does not make sense.
*
* @var array
*/
public static $skippedConfig = [
'locale.settings' => ['path: '],
'syslog.settings' => ['facility: '],
];
/**
* Tests if installed config is equal to the exported config.
*
* @dataProvider moduleListDataProvider
*/
public function testModuleConfig(string $module): void {
$this->assertExtensionConfig($module, 'module');
}
/**
* Tests if installed config is equal to the exported config.
*
* @dataProvider themeListDataProvider
*/
public function testThemeConfig($theme): void {
$this->assertExtensionConfig($theme, 'theme');
}
/**
* Tests that the config provided by the extension is correct.
*
* @param string $name
* Extension name.
* @param string $type
* Extension type, either 'module' or 'theme'.
*
* @internal
*/
protected function assertExtensionConfig(string $name, string $type): void {
// Parse .info.yml file for module/theme $name. Since it's not installed at
// this point we can't retrieve it from the 'module_handler' service.
switch ($name) {
case 'test_deprecated_theme':
$file_name = DRUPAL_ROOT . '/core/modules/system/tests/themes/' . $name . '/' . $name . '.info.yml';
break;
case 'deprecated_module':
$file_name = DRUPAL_ROOT . '/core/modules/system/tests/modules/' . $name . '/' . $name . '.info.yml';
break;
default:
$file_name = DRUPAL_ROOT . '/core/' . $type . 's/' . $name . '/' . $name . '.info.yml';
}
$info = \Drupal::service('info_parser')->parse($file_name);
// Test we have a parsed info.yml file.
$this->assertNotEmpty($info);
// Skip deprecated extensions.
if (isset($info[ExtensionLifecycle::LIFECYCLE_IDENTIFIER])
&& $info[ExtensionLifecycle::LIFECYCLE_IDENTIFIER] === ExtensionLifecycle::DEPRECATED) {
$this->markTestSkipped("The $type '$name' is deprecated.");
}
// System and user are required in order to be able to install some of the
// other modules. Therefore they are put into static::$modules, which though
// doesn't install config files, so import those config files explicitly. Do
// this for all tests in case optional configuration depends on it.
$this->installConfig(['system', 'user']);
$extension_path = \Drupal::service('extension.path.resolver')->getPath($type, $name) . '/';
$extension_config_storage = new FileStorage($extension_path . InstallStorage::CONFIG_INSTALL_DIRECTORY, StorageInterface::DEFAULT_COLLECTION);
$optional_config_storage = new FileStorage($extension_path . InstallStorage::CONFIG_OPTIONAL_DIRECTORY, StorageInterface::DEFAULT_COLLECTION);
if (empty($optional_config_storage->listAll()) && empty($extension_config_storage->listAll())) {
$this->markTestSkipped("$name has no configuration to test");
}
// Work out any additional modules and themes that need installing to create
// an optional config.
$modules_to_install = $type !== 'theme' ? [$name] : [];
$themes_to_install = $type === 'theme' ? [$name] : [];
foreach ($optional_config_storage->listAll() as $config_name) {
$data = $optional_config_storage->read($config_name);
$dependency = new ConfigEntityDependency($config_name, $data);
$modules_to_install = array_merge($modules_to_install, $dependency->getDependencies('module'));
$themes_to_install = array_merge($themes_to_install, $dependency->getDependencies('theme'));
}
// Remove core and standard because they cannot be installed.
$modules_to_install = array_diff(array_unique($modules_to_install), ['core', 'standard']);
$this->container->get('module_installer')->install($modules_to_install);
$this->container->get('theme_installer')->install(array_unique($themes_to_install));
// Test configuration in the extension's config/install directory.
$this->doTestsOnConfigStorage($extension_config_storage, $name, $type);
// Test configuration in the extension's config/optional directory.
$this->doTestsOnConfigStorage($optional_config_storage, $name, $type);
}
/**
* A data provider that lists every theme in core.
*
* Also adds a deprecated theme with config.
*
* @return string[][]
* An array of theme names to test, with both key and value being the name
* of the theme.
*/
public static function themeListDataProvider() {
$prefix = dirname(__DIR__, 4) . DIRECTORY_SEPARATOR . 'themes';
$theme_dirs = array_keys(iterator_to_array(new \FilesystemIterator($prefix)));
$theme_names = array_map(function ($path) use ($prefix) {
return str_replace($prefix . DIRECTORY_SEPARATOR, '', $path);
}, $theme_dirs);
$themes_keyed = array_combine($theme_names, $theme_names);
// Engines is not a theme.
unset($themes_keyed['engines']);
// Add a deprecated theme with config.
$themes_keyed['test_deprecated_theme'] = 'test_deprecated_theme';
return array_map(function ($theme) {
return [$theme];
}, $themes_keyed);
}
/**
* A data provider that lists every module in core.
*
* Also adds a deprecated module with config.
*
* @return string[][]
* An array of module names to test, with both key and value being the name
* of the module.
*/
public static function moduleListDataProvider(): array {
$modules_keyed = self::coreModuleListDataProvider();
// Add a deprecated module with config.
$modules_keyed['deprecated_module'] = ['deprecated_module'];
return $modules_keyed;
}
/**
* Tests that default config matches the installed config.
*
* @param \Drupal\Core\Config\StorageInterface $default_config_storage
* The default config storage to test.
* @param string $extension
* The extension that is being tested.
* @param string $type
* The extension type to test.
*/
protected function doTestsOnConfigStorage(StorageInterface $default_config_storage, $extension, string $type = 'module'): void {
/** @var \Drupal\Core\Config\ConfigManagerInterface $config_manager */
$config_manager = $this->container->get('config.manager');
// Just connect directly to the config table so we don't need to worry about
// the cache layer.
$active_config_storage = $this->container->get('config.storage');
/** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */
$config_factory = $this->container->get('config.factory');
foreach ($default_config_storage->listAll() as $config_name) {
if ($active_config_storage->exists($config_name)) {
// If it is a config entity re-save it. This ensures that any
// recalculation of dependencies does not cause config change.
if ($entity_type = $config_manager->getEntityTypeIdByName($config_name)) {
$entity_storage = $config_manager
->getEntityTypeManager()
->getStorage($entity_type);
$id = $entity_storage->getIDFromConfigName($config_name, $entity_storage->getEntityType()
->getConfigPrefix());
$entity_storage->load($id)->calculateDependencies()->save();
}
else {
// Ensure simple configuration is re-saved so any schema sorting is
// applied.
$config_factory->getEditable($config_name)->save();
}
$result = $config_manager->diff($default_config_storage, $active_config_storage, $config_name);
// ::assertConfigDiff will throw an exception if the configuration is
// different.
$this->assertNull($this->assertConfigDiff($result, $config_name, static::$skippedConfig));
}
else {
$data = $default_config_storage->read($config_name);
$dependency = new ConfigEntityDependency($config_name, $data);
if ($dependency->hasDependency('module', 'standard')) {
// Skip configuration with a dependency on the standard profile. Such
// configuration has probably been removed from the standard profile
// and needs its own test.
continue;
}
$info = $this->container->get("extension.list.$type")->getExtensionInfo($extension);
if (!isset($info[ExtensionLifecycle::LIFECYCLE_IDENTIFIER]) || $info[ExtensionLifecycle::LIFECYCLE_IDENTIFIER] !== ExtensionLifecycle::EXPERIMENTAL) {
$this->fail("$config_name provided by $extension does not exist after installing all dependencies");
}
}
}
}
}
|