View source
<?php
namespace Drupal\Tests\thunder\Functional;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Config\Schema\SchemaCheckTrait;
class InstalledConfigurationTest extends ThunderTestBase {
use SchemaCheckTrait;
protected static $modules = [
'thunder_testing_demo',
'thunder_google_analytics',
'thunder_ivw',
'adsense',
'thunder_riddle',
'harbourmaster',
'simple_gmap',
];
protected $defaultTheme = 'stable';
protected static $ignoreCoreConfigs = [
'checklistapi.progress.update_helper_checklist',
'system.site',
'core.extension',
'system.performance',
'system.theme',
'system.action.user_add_role_action.administrator',
'system.action.user_add_role_action.editor',
'system.action.user_add_role_action.restricted_editor',
'system.action.user_add_role_action.seo',
'system.action.user_remove_role_action.administrator',
'system.action.user_remove_role_action.editor',
'system.action.user_remove_role_action.restricted_editor',
'system.action.user_remove_role_action.seo',
'system.action.user_add_role_action.harbourmaster',
'system.action.user_remove_role_action.harbourmaster',
'core.entity_view_mode.access_token.token',
'core.entity_view_mode.block.token',
'core.entity_view_mode.content_moderation_state.token',
'core.entity_view_mode.crop.token',
'core.entity_view_mode.file.token',
'core.entity_view_mode.menu_link_content.token',
'core.entity_view_mode.node.token',
'core.entity_view_mode.paragraph.token',
'core.entity_view_mode.taxonomy_term.token',
'core.entity_view_mode.user.token',
'core.entity_view_mode.path_alias.token',
'core.entity_view_mode.search_api_task.token',
'tour.tour.search-api-index',
'tour.tour.search-api-index-fields',
'tour.tour.search-api-index-form',
'tour.tour.search-api-index-processors',
'tour.tour.search-api-server',
'tour.tour.search-api-server-form',
];
protected static $ignoreConfigKeys = [
'scheduler.settings' => [
'lightweight_cron_access_key' => TRUE,
],
'system.date' => [
'timezone' => [
'default' => TRUE,
],
],
'system.file' => [
'path' => [
'temporary' => TRUE,
],
],
'update.settings' => [
'notification' => [
'emails' => TRUE,
],
],
'system.logging' => [
'error_level' => TRUE,
],
'system.mail' => [
'interface' => [
'default' => TRUE,
],
],
'views.view.content' => [
'status' => TRUE,
],
'views.view.glossary' => [
'dependencies' => [
'config' => TRUE,
],
'display' => [
'page_1' => [
'cache_metadata' => [
'max-age' => TRUE,
],
],
'attachment_1' => [
'cache_metadata' => [
'max-age' => TRUE,
],
],
'default' => [
'cache_metadata' => [
'max-age' => TRUE,
],
],
],
],
'views.view.watchdog' => [
'display' => [
'page' => [
'cache_metadata' => [
'max-age' => TRUE,
],
],
'default' => [
'cache_metadata' => [
'max-age' => TRUE,
],
],
],
],
'views.view.files' => [
'display' => [
'page_1' => [
'cache_metadata' => [
'max-age' => TRUE,
],
],
'page_2' => [
'cache_metadata' => [
'max-age' => TRUE,
],
],
'default' => [
'cache_metadata' => [
'max-age' => TRUE,
],
],
],
],
'views.view.moderated_content' => [
'display' => [
'moderated_content' => [
'cache_metadata' => [
'max-age' => TRUE,
'tags' => TRUE,
],
],
'default' => [
'cache_metadata' => [
'max-age' => TRUE,
'tags' => TRUE,
],
],
],
],
'diff.settings' => [
'general_settings' => [
'layout_plugins' => [
'visual_inline' => [
'enabled' => TRUE,
],
],
],
],
'core.entity_form_display.node.article.default' => [
'content' => [
'field_ivw' => TRUE,
],
'dependencies' => [
'config' => TRUE,
'module' => TRUE,
],
],
'core.entity_form_display.node.article.bulk_edit' => [
'hidden' => [
'field_ivw' => TRUE,
],
],
'core.entity_form_display.taxonomy_term.channel.default' => [
'content' => [
'field_ivw' => TRUE,
],
'dependencies' => [
'config' => TRUE,
'module' => TRUE,
],
],
'core.entity_view_display.taxonomy_term.channel.default' => [
'hidden' => [
'field_ivw' => TRUE,
],
],
'core.entity_view_display.node.article.default' => [
'hidden' => [
'field_ivw' => TRUE,
],
],
'core.entity_view_display.node.article.rss' => [
'hidden' => [
'field_ivw' => TRUE,
],
],
'core.entity_view_display.node.article.search_index' => [
'hidden' => [
'field_ivw' => TRUE,
],
],
'core.entity_view_display.node.article.teaser' => [
'hidden' => [
'field_ivw' => TRUE,
],
],
'core.entity_form_display.media.nexx_video.default' => [
'content' => [
'path' => TRUE,
'moderation_state' => TRUE,
],
],
'core.entity_form_display.paragraph.nexx_video.default' => [
'content' => [
'moderation_state' => TRUE,
],
],
'paragraphs.paragraphs_type.nexx_video' => [
'icon_uuid' => TRUE,
'description' => TRUE,
],
];
protected static $configPathSeparator = '::';
protected static $ignoreConfigListValues = [
'user.role.editor' => [
'permissions' => [
'access riddle_browser entity browser pages',
],
],
'user.role.seo' => [
'permissions' => [
'access riddle_browser entity browser pages',
],
],
'user.role.authenticated' => [
'permissions' => [
'opt-in or out of google analytics tracking',
],
],
];
protected static $ignoreConfigs = [];
protected function setDefaultTheme($defaultTheme) {
\Drupal::service('theme_installer')
->install([
$defaultTheme,
]);
$themeConfig = \Drupal::configFactory()
->getEditable('system.theme');
$themeConfig
->set('default', $defaultTheme);
$themeConfig
->save();
}
protected function cleanupConfigurations(array $configurations, $configurationName) {
$optionalStorage = \Drupal::service('config_update.extension_optional_storage');
$configCleanup = [];
$ignoreListRules = [];
if (array_key_exists($configurationName, static::$ignoreConfigKeys)) {
$configCleanup = static::$ignoreConfigKeys[$configurationName];
}
if (array_key_exists($configurationName, static::$ignoreConfigListValues)) {
foreach (static::$ignoreConfigListValues[$configurationName] as $keyPath => $ignoreValues) {
$ignoreListRules[] = [
'key_path' => explode(static::$configPathSeparator, $keyPath),
'ignore_values' => $ignoreValues,
];
}
}
if ($optionalStorage
->exists($configurationName)) {
$configCleanup = NestedArray::mergeDeep($configCleanup, [
'dependencies' => TRUE,
]);
}
if (empty($configCleanup) && empty($ignoreListRules)) {
return $configurations;
}
foreach ($configurations as $index => $arrayToOverwrite) {
$configurations[$index] = NestedArray::mergeDeep($arrayToOverwrite, $configCleanup);
foreach ($ignoreListRules as $ignoreRule) {
$list = $this
->cleanupConfigList(NestedArray::getValue($configurations[$index], $ignoreRule['key_path']), $ignoreRule['ignore_values']);
NestedArray::setValue($configurations[$index], $ignoreRule['key_path'], $list);
}
}
return $configurations;
}
protected function cleanupConfigList(array $list, array $ignoreValues) {
$cleanList = $list;
if (!empty($cleanList)) {
foreach ($ignoreValues as $ignoreValue) {
if (!in_array($ignoreValue, $cleanList)) {
$cleanList[] = $ignoreValue;
}
}
}
else {
$cleanList = $ignoreValues;
}
sort($cleanList);
return $cleanList;
}
public function testInstalledConfiguration() {
$this
->setDefaultTheme($this->defaultTheme);
$configUpdate = \Drupal::service('config_update.config_update');
$typedConfigManager = \Drupal::service('config.typed');
$activeStorage = \Drupal::service('config.storage');
$installStorage = \Drupal::service('config_update.extension_storage');
$optionalStorage = \Drupal::service('config_update.extension_optional_storage');
$activeList = $activeStorage
->listAll();
$installList = $installStorage
->listAll();
$optionalList = $optionalStorage
->listAll();
$installListDiff = array_diff($installList, $activeList);
$this
->assertEquals([], $installListDiff, "All required configurations should be installed.");
$activeList = array_diff($activeList, static::$ignoreCoreConfigs);
$activeListDiff = array_diff($activeList, $installList, $optionalList);
$this
->assertEquals([], $activeListDiff, "All active configurations should be defined in Yaml files.");
$configDiffer = \Drupal::service('config_update.config_diff');
$differentConfigNames = [];
$schemaCheckFail = [];
foreach ($activeList as $activeConfigName) {
if (in_array($activeConfigName, static::$ignoreConfigs)) {
continue;
}
$activeConfig = $configUpdate
->getFromActive('', $activeConfigName);
$fileConfig = $configUpdate
->getFromExtension('', $activeConfigName);
if ($typedConfigManager
->hasConfigSchema($activeConfigName)) {
if ($this
->checkConfigSchema($typedConfigManager, $activeConfigName, $activeConfig) !== TRUE) {
$schemaCheckFail['active'][] = $activeConfigName;
}
if ($this
->checkConfigSchema($typedConfigManager, $activeConfigName, $fileConfig) !== TRUE) {
$schemaCheckFail['file'][] = $activeConfigName;
}
}
else {
$schemaCheckFail['no-schema'][] = $activeConfigName;
}
list($activeConfig, $fileConfig) = $this
->cleanupConfigurations([
$activeConfig,
$fileConfig,
], $activeConfigName);
if (!$configDiffer
->same($fileConfig, $activeConfig)) {
$differentConfigNames[] = $activeConfigName;
}
}
if (!empty($differentConfigNames) || !empty($schemaCheckFail)) {
$errorOutput = [
'configuration-diff' => $differentConfigNames,
'schema-check' => $schemaCheckFail,
];
throw new \Exception('Configuration difference is found: ' . print_r($errorOutput, TRUE));
}
}
}