From 35afef409624f2f2122e61f5f50ff3029337f73c Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Tue, 1 Aug 2023 22:37:20 +1000 Subject: [PATCH] Refactored install tests. --- .../CivicthemeDevIgnoreFilter.php | 40 ++++++++++++ .../contrib/civictheme/.circleci/build.sh | 6 +- .../Functional/CivicthemeBrowserTestBase.php | 55 +++++------------ ....php => CivicthemeInstallOptionalTest.php} | 7 ++- .../CivicthemeInstallRequiredTest.php | 28 +++++++++ .../civictheme/theme-settings.provision.inc | 61 ++++++++++++++++--- phpcs.xml | 3 + 7 files changed, 147 insertions(+), 53 deletions(-) rename docroot/themes/contrib/civictheme/tests/src/Functional/{CivicthemeInstallTest.php => CivicthemeInstallOptionalTest.php} (62%) create mode 100644 docroot/themes/contrib/civictheme/tests/src/Functional/CivicthemeInstallRequiredTest.php diff --git a/docroot/modules/custom/civictheme_dev/src/Plugin/ConfigFilter/CivicthemeDevIgnoreFilter.php b/docroot/modules/custom/civictheme_dev/src/Plugin/ConfigFilter/CivicthemeDevIgnoreFilter.php index ca9b1c677..d62388681 100644 --- a/docroot/modules/custom/civictheme_dev/src/Plugin/ConfigFilter/CivicthemeDevIgnoreFilter.php +++ b/docroot/modules/custom/civictheme_dev/src/Plugin/ConfigFilter/CivicthemeDevIgnoreFilter.php @@ -53,6 +53,46 @@ public function filterWrite($name, array $data) { } } + // Exclude hidden fields added by the optional modules. + if (fnmatch('core.entity_view_display.*', $name)) { + unset($data['hidden']['search_api_excerpt']); + } + + // Exclude processing for some properties of the blocks. + if (fnmatch('block.block.civictheme_*', $name)) { + // Unset dependencies. + unset($data['dependencies']['content']); + + if (!empty($data['dependencies']['module'])) { + foreach ($data['dependencies']['module'] as $key => $module_name) { + if (in_array($module_name, ['block_content'])) { + unset($data['dependencies']['module'][$key]); + } + } + if (empty($data['dependencies']['module'])) { + unset($data['dependencies']['module']); + } + } + } + + // Exclude processing for some properties of the views. + if (fnmatch('views.view.civictheme_*', $name) && !str_contains($name, 'example') && !str_contains($name, 'test')) { + if (!empty($data['display'])) { + foreach (array_keys($data['display']) as $display_name) { + if (!empty($data['display'][$display_name]['display_options']['display_extenders'])) { + foreach (array_keys($data['display'][$display_name]['display_options']['display_extenders']) as $extender_name) { + if (str_starts_with($extender_name, 'simple_sitemap')) { + unset($data['display'][$display_name]['display_options']['display_extenders'][$extender_name]); + } + } + if (empty($data['display'][$display_name]['display_options']['display_extenders'])) { + unset($data['display'][$display_name]['display_options']['display_extenders']); + } + } + } + } + } + return $data; } diff --git a/docroot/themes/contrib/civictheme/.circleci/build.sh b/docroot/themes/contrib/civictheme/.circleci/build.sh index 1b23fa016..a0573449f 100755 --- a/docroot/themes/contrib/civictheme/.circleci/build.sh +++ b/docroot/themes/contrib/civictheme/.circleci/build.sh @@ -167,17 +167,17 @@ ln -s "$(pwd)"/* "${BUILD_DIR}/web/themes/${THEME}" && rm "${BUILD_DIR}/web/them ######################################## echo " > Enabling theme ${THEME} dependent modules." -"${BUILD_DIR}/vendor/bin/drush" php:eval "require_once dirname(\Drupal::getContainer()->get('theme_handler')->rebuildThemeData()['civictheme']->getPathname()) . '/theme-settings.provision.inc'; civictheme_enable_modules();" +"${BUILD_DIR}/vendor/bin/drush" -r "${BUILD_DIR}/web" php:eval "require_once dirname(\Drupal::getContainer()->get('theme_handler')->rebuildThemeData()['civictheme']->getPathname()) . '/theme-settings.provision.inc'; civictheme_enable_modules();" echo " > Enabling theme ${THEME}." "${BUILD_DIR}/vendor/bin/drush" -r "${BUILD_DIR}/web" theme:install "${THEME}" -y "${BUILD_DIR}/vendor/bin/drush" -r "${BUILD_DIR}/web" cr echo " > Setting theme ${THEME} as default." -"${BUILD_DIR}/vendor/bin/drush" config:set system.theme default "${THEME}" -y +"${BUILD_DIR}/vendor/bin/drush" -r "${BUILD_DIR}/web" config:set system.theme default "${THEME}" -y echo " > Provisioning content from theme defaults." -"${BUILD_DIR}/vendor/bin/drush" php:eval -v "require_once dirname(\Drupal::getContainer()->get('theme_handler')->rebuildThemeData()['civictheme']->getPathname()) . '/theme-settings.provision.inc'; civictheme_provision_cli();" +"${BUILD_DIR}/vendor/bin/drush" -r "${BUILD_DIR}/web" php:eval "require_once dirname(\Drupal::getContainer()->get('theme_handler')->rebuildThemeData()['civictheme']->getPathname()) . '/theme-settings.provision.inc'; civictheme_provision_cli();" ######################################## diff --git a/docroot/themes/contrib/civictheme/tests/src/Functional/CivicthemeBrowserTestBase.php b/docroot/themes/contrib/civictheme/tests/src/Functional/CivicthemeBrowserTestBase.php index 087a03678..cb690ce73 100644 --- a/docroot/themes/contrib/civictheme/tests/src/Functional/CivicthemeBrowserTestBase.php +++ b/docroot/themes/contrib/civictheme/tests/src/Functional/CivicthemeBrowserTestBase.php @@ -11,46 +11,6 @@ */ abstract class CivicthemeBrowserTestBase extends BrowserTestBase { - /** - * {@inheritdoc} - */ - protected static $modules = [ - 'block', - 'block_content', - 'ckeditor', - 'components', - 'config', - 'content_moderation', - 'datetime_range', - 'field', - 'field_group', - 'file', - 'help', - 'image', - 'inline_form_errors', - 'layout_builder', - 'layout_builder_restrictions', - 'layout_discovery', - 'layout_test', - 'linkit', - 'media', - 'media_library', - 'menu_block', - 'migrate', - 'node', - 'options', - 'paragraphs', - 'path_alias', - 'pathauto', - 'redirect', - 'rest', - 'shortcut', - 'system', - 'taxonomy', - 'user', - 'webform', - ]; - /** * {@inheritdoc} */ @@ -63,6 +23,13 @@ abstract class CivicthemeBrowserTestBase extends BrowserTestBase { */ protected $customTheme = 'civictheme'; + /** + * Whether to install optional dependencies. + * + * @var bool + */ + protected $optionalDependencies = TRUE; + /** * {@inheritdoc} */ @@ -71,6 +38,14 @@ public function setUp(): void { $container = $this->container; + require_once dirname($container->get('theme_handler')->rebuildThemeData()[$this->customTheme]->getPathname()) . '/theme-settings.provision.inc'; + $modules = _civictheme_get_theme_dependencies($this->customTheme, $this->optionalDependencies); + $container->get('module_installer')->install($modules); + + // Refresh container after installing modules. + $this->container = \Drupal::getContainer(); + $container = $this->container; + // Ensure the default theme is installed. $container->get('theme_installer')->install([$this->customTheme], TRUE); diff --git a/docroot/themes/contrib/civictheme/tests/src/Functional/CivicthemeInstallTest.php b/docroot/themes/contrib/civictheme/tests/src/Functional/CivicthemeInstallOptionalTest.php similarity index 62% rename from docroot/themes/contrib/civictheme/tests/src/Functional/CivicthemeInstallTest.php rename to docroot/themes/contrib/civictheme/tests/src/Functional/CivicthemeInstallOptionalTest.php index ed083b029..59b7e3d63 100644 --- a/docroot/themes/contrib/civictheme/tests/src/Functional/CivicthemeInstallTest.php +++ b/docroot/themes/contrib/civictheme/tests/src/Functional/CivicthemeInstallOptionalTest.php @@ -3,14 +3,15 @@ namespace Drupal\Tests\civictheme\Functional; /** - * Class CivicthemeInstallTest. + * Class CivicthemeInstallOptionalTest. * - * Tests the installation of the Civictheme. + * Tests the installation of the CivicTheme with required and optional + * dependencies. * * @group civictheme:functional * @group site:functional */ -class CivicthemeInstallTest extends CivicthemeBrowserTestBase { +class CivicthemeInstallOptionalTest extends CivicthemeBrowserTestBase { /** * Test that a theme can be installed. diff --git a/docroot/themes/contrib/civictheme/tests/src/Functional/CivicthemeInstallRequiredTest.php b/docroot/themes/contrib/civictheme/tests/src/Functional/CivicthemeInstallRequiredTest.php new file mode 100644 index 000000000..b9aa5ae6d --- /dev/null +++ b/docroot/themes/contrib/civictheme/tests/src/Functional/CivicthemeInstallRequiredTest.php @@ -0,0 +1,28 @@ +drupalCreateUser(['administer site configuration']); + $this->drupalLogin($adminUser); + } + +} diff --git a/docroot/themes/contrib/civictheme/theme-settings.provision.inc b/docroot/themes/contrib/civictheme/theme-settings.provision.inc index 8bc034b73..4fefa2dd5 100644 --- a/docroot/themes/contrib/civictheme/theme-settings.provision.inc +++ b/docroot/themes/contrib/civictheme/theme-settings.provision.inc @@ -17,6 +17,7 @@ use Drupal\block\Entity\Block; use Drupal\civictheme\CivicthemeConfigImporter; use Drupal\civictheme\CivicthemeConfigManager; use Drupal\civictheme\CivicthemeConstants; +use Drupal\Core\Config\FileStorage; use Drupal\Core\File\FileSystemInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Url; @@ -1016,23 +1017,69 @@ function civictheme_provision_permissions_map() { */ /** - * Helper to install modules required by CivicTheme. + * Install modules required by CivicTheme. + * + * This should be run before the theme is installed. + * + * @code + * drush php:eval "require_once dirname(\Drupal::getContainer()->get('theme_handler')->rebuildThemeData()['civictheme']->getPathname()) . '/theme-settings.provision.inc'; civictheme_enable_modules();" + * @endcode * * @see https://www.drupal.org/node/2652542 * @SuppressWarnings(PHPMD.StaticAccess) + * @SuppressWarnings(PHPMD.BooleanArgumentFlag) + * + * @param bool $include_optional + * Whether to include optional dependencies. Defaults to TRUE. + * + * @throws \Drupal\Core\Extension\ExtensionNameLengthException + * @throws \Drupal\Core\Extension\MissingDependencyException + */ +function civictheme_enable_modules($include_optional = TRUE) { + $dependencies = _civictheme_get_theme_dependencies('civictheme', $include_optional); + \Drupal::getContainer()->get('module_installer')->install($dependencies); +} + +/** + * Get dependencies of a theme. + * + * @param string $theme_name + * The name of the theme. + * @param bool $include_optional + * Whether to include optional dependencies. Defaults to TRUE. + * + * @return string[] + * Array of dependency names. + * + * @SuppressWarnings(PHPMD.BooleanArgumentFlag) */ -function civictheme_enable_modules() { +function _civictheme_get_theme_dependencies(string $theme_name, $include_optional = TRUE) { /** @var \Drupal\Core\Extension\ThemeHandler $theme_handler */ $theme_handler = \Drupal::getContainer()->get('theme_handler'); $theme_data = $theme_handler->rebuildThemeData(); - if (!empty($theme_data['civictheme'])) { - $modules = array_keys($theme_data['civictheme']->module_dependencies); - /** @var \Drupal\Core\Extension\ModuleInstaller $module_installer */ - $module_installer = \Drupal::getContainer()->get('module_installer'); - $module_installer->install($modules); + // Merge dependencies from the theme's info file and from the 'optional' + // config. We are deliberately not including dependencies from the 'install' + // config to make sure that any unlisted required dependencies in the + // theme's .info file would trigger an error. + $dependencies = array_keys($theme_data['civictheme']->module_dependencies); + + if ($include_optional) { + $theme_path = $theme_data[$theme_name]->subpath; + $config_storage = new FileStorage($theme_path . '/config/optional'); + + foreach ($config_storage->listAll() as $name) { + $config_dependencies = $config_storage->read($name)['dependencies'] ?? []; + if (!empty($config_dependencies)) { + $dependencies = array_merge($dependencies, $config_dependencies['module'] ?? []); + } + } } + $dependencies = array_unique($dependencies); + $dependencies = array_diff($dependencies, [$theme_name]); + + return $dependencies; } /** diff --git a/phpcs.xml b/phpcs.xml index 759b8d1d2..c4333ed7c 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -36,6 +36,9 @@ .storybook/* webpack/* + + civictheme/build/* + docroot\/modules\/custom\/cs_generated_content\/generated_content\/node\/civictheme_page_variations\/.*