View source
<?php
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;
class DefaultConfigTest extends KernelTestBase {
use AssertConfigTrait;
use FileSystemModuleDiscoveryDataProviderTrait;
protected static $timeLimit = 500;
protected static $modules = [
'system',
'user',
'path_alias',
];
public static $skippedConfig = [
'locale.settings' => [
'path: ',
],
'syslog.settings' => [
'facility: ',
],
'simpletest.settings' => TRUE,
];
public function testModuleConfig($module) {
$this
->assertExtensionConfig($module, 'module');
}
public function testThemeConfig($theme) {
$this
->assertExtensionConfig($theme, 'theme');
}
protected function assertExtensionConfig(string $name, string $type) {
$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");
}
$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'));
}
$modules_to_install = array_diff(array_unique($modules_to_install), [
'core',
]);
$this->container
->get('module_installer')
->install($modules_to_install);
$this->container
->get('theme_installer')
->install(array_unique($themes_to_install));
$this
->doTestsOnConfigStorage($extension_config_storage, $name);
$this
->doTestsOnConfigStorage($optional_config_storage, $name);
}
public 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);
unset($themes_keyed['engines']);
return array_map(function ($theme) {
return [
$theme,
];
}, $themes_keyed);
}
protected function doTestsOnConfigStorage(StorageInterface $default_config_storage, $module) {
$config_manager = $this->container
->get('config.manager');
$active_config_storage = $this->container
->get('config.storage');
foreach ($default_config_storage
->listAll() as $config_name) {
if ($active_config_storage
->exists($config_name)) {
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();
}
$result = $config_manager
->diff($default_config_storage, $active_config_storage, $config_name);
$this
->assertNull($this
->assertConfigDiff($result, $config_name, static::$skippedConfig));
}
else {
$info = $this->container
->get('extension.list.module')
->getExtensionInfo($module);
if (!isset($info[ExtensionLifecycle::LIFECYCLE_IDENTIFIER]) || $info[ExtensionLifecycle::LIFECYCLE_IDENTIFIER] !== ExtensionLifecycle::EXPERIMENTAL) {
$this
->fail("{$config_name} provided by {$module} does not exist after installing all dependencies");
}
}
}
}
}