Skip to content

Commit

Permalink
Refactored install tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSkrypnyk committed Aug 1, 2023
1 parent cd28c66 commit 35afef4
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
6 changes: 3 additions & 3 deletions docroot/themes/contrib/civictheme/.circleci/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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();"

########################################

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand All @@ -63,6 +23,13 @@ abstract class CivicthemeBrowserTestBase extends BrowserTestBase {
*/
protected $customTheme = 'civictheme';

/**
* Whether to install optional dependencies.
*
* @var bool
*/
protected $optionalDependencies = TRUE;

/**
* {@inheritdoc}
*/
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Drupal\Tests\civictheme\Functional;

/**
* Class CivicthemeInstallOptionalTest.php.
*
* Tests the installation of the CivicTheme with only required dependencies.
*
* @group civictheme:functional
* @group site:functional
*/
class CivicthemeInstallRequiredTest extends CivicthemeBrowserTestBase {

/**
* {@inheritdoc}
*/
protected $optionalDependencies = FALSE;

/**
* Test that a theme can be installed.
*/
public function testThemeInstall() {
$adminUser = $this->drupalCreateUser(['administer site configuration']);
$this->drupalLogin($adminUser);
}

}
61 changes: 54 additions & 7 deletions docroot/themes/contrib/civictheme/theme-settings.provision.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
<exclude-pattern>.storybook/*</exclude-pattern>
<exclude-pattern>webpack/*</exclude-pattern>

<!-- Exclude CivicTheme isolated build files. -->
<exclude-pattern>civictheme/build/*</exclude-pattern>

<!-- Exclude array-heavy files without any logic. -->
<exclude-pattern>docroot\/modules\/custom\/cs_generated_content\/generated_content\/node\/civictheme_page_variations\/.*</exclude-pattern>

Expand Down

0 comments on commit 35afef4

Please sign in to comment.