function FunctionalTestSetupTrait::installDefaultThemeFromClassProperty

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Test/FunctionalTestSetupTrait.php \Drupal\Core\Test\FunctionalTestSetupTrait::installDefaultThemeFromClassProperty()
  2. 8.9.x core/lib/Drupal/Core/Test/FunctionalTestSetupTrait.php \Drupal\Core\Test\FunctionalTestSetupTrait::installDefaultThemeFromClassProperty()
  3. 11.x core/lib/Drupal/Core/Test/FunctionalTestSetupTrait.php \Drupal\Core\Test\FunctionalTestSetupTrait::installDefaultThemeFromClassProperty()

Installs the default theme defined by `static::$defaultTheme` when needed.

To install a test theme outside of the testing environment, add

$settings['extension_discovery_scan_tests'] = TRUE;

to your settings.php.

Parameters

\Symfony\Component\DependencyInjection\ContainerInterface $container: The container.

Throws

\Exception If the test case does not initialize default theme.

2 calls to FunctionalTestSetupTrait::installDefaultThemeFromClassProperty()
BrowserTestBase::installDrupal in core/tests/Drupal/Tests/BrowserTestBase.php
Installs Drupal into the test site.
InstallerTestBase::setUp in core/tests/Drupal/FunctionalTests/Installer/InstallerTestBase.php
1 method overrides FunctionalTestSetupTrait::installDefaultThemeFromClassProperty()
StandardRecipeInstallTest::installDefaultThemeFromClassProperty in core/tests/Drupal/FunctionalTests/Core/Recipe/StandardRecipeInstallTest.php
Installs the default theme defined by `static::$defaultTheme` when needed.

File

core/lib/Drupal/Core/Test/FunctionalTestSetupTrait.php, line 441

Class

FunctionalTestSetupTrait
Defines a trait for shared functional test setup functionality.

Namespace

Drupal\Core\Test

Code

protected function installDefaultThemeFromClassProperty(ContainerInterface $container) {
  // Use the install profile to determine the default theme if configured and
  // not already specified.
  $profile = $container->getParameter('install_profile');
  if (!empty($profile)) {
    $default_sync_path = $container->get('extension.list.profile')
      ->getPath($profile) . '/config/sync';
    $profile_config_storage = new FileStorage($default_sync_path, StorageInterface::DEFAULT_COLLECTION);
    if (!isset($this->defaultTheme) && $profile_config_storage->exists('system.theme')) {
      $this->defaultTheme = $profile_config_storage->read('system.theme')['default'];
    }
    $default_install_path = $container->get('extension.list.profile')
      ->getPath($profile) . '/' . InstallStorage::CONFIG_INSTALL_DIRECTORY;
    $profile_config_storage = new FileStorage($default_install_path, StorageInterface::DEFAULT_COLLECTION);
    if (!isset($this->defaultTheme) && $profile_config_storage->exists('system.theme')) {
      $this->defaultTheme = $profile_config_storage->read('system.theme')['default'];
    }
  }
  // Require a default theme to be specified at this point.
  if (!isset($this->defaultTheme)) {
    throw new \Exception('Drupal\\Tests\\BrowserTestBase::$defaultTheme is required. See https://www.drupal.org/node/3083055, which includes recommendations on which theme to use.');
  }
  // Ensure the default theme is installed.
  $container->get('theme_installer')
    ->install([
    $this->defaultTheme,
  ], TRUE);
  $system_theme_config = $container->get('config.factory')
    ->getEditable('system.theme');
  if ($system_theme_config->get('default') !== $this->defaultTheme) {
    $system_theme_config->set('default', $this->defaultTheme)
      ->save();
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.