setActiveTheme example


  protected function activateTheme($theme_name) {
    $this->container->get('theme_installer')->install([$theme_name]);

    /** @var \Drupal\Core\Theme\ThemeInitializationInterface $theme_initializer */
    $theme_initializer = $this->container->get('theme.initialization');

    /** @var \Drupal\Core\Theme\ThemeManagerInterface $theme_manager */
    $theme_manager = $this->container->get('theme.manager');

    $theme_manager->setActiveTheme($theme_initializer->getActiveThemeByName($theme_name));

    $this->libraryDiscovery->clearCachedDefinitions();

    $this->assertSame($theme_name$theme_manager->getActiveTheme()->getName());
  }

  /** * Asserts that the specified asset is in the given library. * * @param string $asset * The asset file with the path for the file. * @param string $extension * The extension in which the $library is defined. * @param string $library_name * Name of the library. * @param string $sub_key * The library sub key where the given asset is defined. * @param string $message * (optional) A message to display with the assertion. * * @internal */
protected static $modules = ['user', 'system'];

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();
    $this->installEntitySchema('user');

    // Set up a test theme that prints the user's mail field.     \Drupal::service('theme_installer')->install(['user_test_theme']);
    \Drupal::theme()->setActiveTheme(\Drupal::service('theme.initialization')->initTheme('user_test_theme'));
    // Clear the theme registry.     $this->container->set('theme.registry', NULL);
  }

  /** * Tests account's available fields. */
  public function testUserFields() {
    // Create the user to test the user fields.     $user = User::create([
      'name' => 'foobar',
      


  /** * Ensure that components in modules without schema fail validation. * * The theme sdc_theme_test_enforce_schema_invalid is set as enforcing schemas * but provides a component without schema. */
  public function testInvalidDefinitionTheme(): void {
    \Drupal::service('theme_installer')->install(['sdc_theme_test_enforce_schema_invalid']);
    $active_theme = \Drupal::service('theme.initialization')->initTheme('sdc_theme_test_enforce_schema_invalid');
    \Drupal::service('theme.manager')->setActiveTheme($active_theme);
    $this->expectException(InvalidComponentException::class);
    $this->manager->getDefinitions();
  }

}

  public function testElementInfoByTheme() {
    /** @var \Drupal\Core\Theme\ThemeInitializationInterface $theme_initializer */
    $theme_initializer = $this->container->get('theme.initialization');

    /** @var \Drupal\Core\Theme\ThemeManagerInterface $theme_manager */
    $theme_manager = $this->container->get('theme.manager');

    /** @var \Drupal\Core\Render\ElementInfoManagerInterface $element_info */
    $element_info = $this->container->get('plugin.manager.element_info');

    $theme_manager->setActiveTheme($theme_initializer->getActiveThemeByName('starterkit_theme'));
    $this->assertEquals(60, $element_info->getInfo('textfield')['#size']);

    $theme_manager->setActiveTheme($theme_initializer->getActiveThemeByName('test_theme'));
    $this->assertEquals(40, $element_info->getInfo('textfield')['#size']);
  }

}
$styles = [
      'default' => '//div[@class="views-row"]',
      // Olivero does not use the 'views-col' class.       'grid' => '//div[contains(@class, "views-col") or contains(@class, "views-view-grid__item-inner")]',
      'html_list' => '//div[@class="item-list"]//li',
    ];

    $themes = ['olivero', 'stable9', 'stark', 'claro'];

    foreach ($themes as $theme) {
      \Drupal::service('theme_installer')->install([$theme]);
      \Drupal::theme()->setActiveTheme(\Drupal::service('theme.initialization')->initTheme($theme));
      foreach ($styles as $type => $xpath) {
        $view = Views::getView('test_page_display');
        $view->storage->invalidateCaches();
        $view->initDisplay();
        $view->setDisplay('empty_row');
        $view->displayHandlers->get('empty_row')->default_display->options['style']['type'] = $type;
        $view->initStyle();
        $this->executeView($view);
        $output = $view->preview();
        $output = \Drupal::service('renderer')->renderRoot($output);
        $this->setRawContent($output);
        

  protected $defaultTheme = 'stark';

  /** * Tests if the theme has been altered. */
  public function testDrupalAlter() {
    // This test depends on Olivero, so make sure that it is always the current     // active theme.     \Drupal::service('theme_installer')->install(['olivero']);
    \Drupal::theme()->setActiveTheme(\Drupal::service('theme.initialization')->initTheme('olivero'));

    $array = ['foo' => 'bar'];
    $entity = new \stdClass();
    $entity->foo = 'bar';

    // Verify alteration of a single argument.     $array_copy = $array;
    $array_expected = ['foo' => 'Drupal theme'];
    \Drupal::moduleHandler()->alter('drupal_alter', $array_copy);
    \Drupal::theme()->alter('drupal_alter', $array_copy);
    $this->assertEquals($array_expected$array_copy, 'Single array was altered.');

    
/** * Ensures Twig template cache setting can be overridden. */
  public function testTwigCacheOverride() {
    $extension = twig_extension();
    $theme_installer = $this->container->get('theme_installer');
    $theme_installer->install(['test_theme']);
    $this->config('system.theme')->set('default', 'test_theme')->save();

    // The registry still works on theme globals, so set them here.     \Drupal::theme()->setActiveTheme(\Drupal::service('theme.initialization')->getActiveThemeByName('test_theme'));

    // Reset the theme registry, so that the new theme is used.     $this->container->set('theme.registry', NULL);

    // Load array of Twig templates.     // reset() is necessary to invalidate caches tagged with 'theme_registry'.     $registry = $this->container->get('theme.registry');
    $registry->reset();
    $templates = $registry->getRuntime();

    // Get the template filename and the cache filename for

  protected function setUp($import_test_views = TRUE): void {
    parent::setUp($import_test_views);

    $this->installEntitySchema('node');
    $this->installEntitySchema('user');
    $this->installSchema('history', ['history']);
    // Use history_test_theme because its marker is wrapped in a span so it can     // be easily targeted with xpath.     \Drupal::service('theme_installer')->install(['history_test_theme']);
    \Drupal::theme()->setActiveTheme(\Drupal::service('theme.initialization')->initTheme('history_test_theme'));
  }

  /** * Tests the handlers. */
  public function testHandlers() {
    $nodes = [];
    $node = Node::create([
      'title' => 'n1',
      'type' => 'default',
    ]);
    
else {
        $theme_keys = [$theme];
      }

      // Save the name of the current theme (if any), so that we can temporarily       // override the current theme and allow theme_get_setting() to work       // without having to pass the theme name to it.       $default_active_theme = $this->themeManager->getActiveTheme();
      $default_theme = $default_active_theme->getName();
      /** @var \Drupal\Core\Theme\ThemeInitialization $theme_initialization */
      $theme_initialization = \Drupal::service('theme.initialization');
      $this->themeManager->setActiveTheme($theme_initialization->getActiveThemeByName($theme));

      // Process the theme and all its base themes.       foreach ($theme_keys as $theme) {
        // Include the theme-settings.php file.         $theme_path = $this->themeHandler->getTheme($theme)->getPath();
        $theme_settings_file = $theme_path . '/theme-settings.php';
        $theme_file = $theme_path . '/' . $theme . '.theme';
        $filenames = [$theme_settings_file$theme_file];
        foreach ($filenames as $filename) {
          if (file_exists($filename)) {
            require_once $filename;

            
/** * Ensures that all core module and theme library files exist. */
  public function testCoreLibraryCompleteness() {
    // First verify all libraries with no active theme.     $this->verifyLibraryFilesExist($this->getAllLibraries());

    // Then verify all libraries for each core theme. This may seem like     // overkill but themes can override and extend other extensions' libraries     // and these changes are only applied for the active theme.     foreach ($this->allThemes as $theme) {
      $this->themeManager->setActiveTheme($this->themeInitialization->getActiveThemeByName($theme));
      $this->libraryDiscovery->clearCachedDefinitions();

      $this->verifyLibraryFilesExist($this->getAllLibraries());
    }
  }

  /** * Checks that all the library files exist. * * @param array[] $library_definitions * An array of library definitions, keyed by extension, then by library, and * so on. */
$this->libraryDiscovery = $this->container->get('library.discovery');
  }

  /** * Ensures that Stable 9 overrides all relevant core library assets. */
  public function testStable9LibraryOverrides() {
    // First get the clean library definitions with no active theme.     $libraries_before = $this->getAllLibraries();
    $libraries_before = $this->removeVendorAssets($libraries_before);

    $this->themeManager->setActiveTheme($this->themeInitialization->getActiveThemeByName('stable9'));
    $this->libraryDiscovery->clearCachedDefinitions();

    // Now get the library definitions with Stable 9 as the active theme.     $libraries_after = $this->getAllLibraries();
    $libraries_after = $this->removeVendorAssets($libraries_after);

    foreach ($libraries_before as $extension => $libraries) {
      foreach ($libraries as $library_name => $library) {
        // Allow skipping libraries.         if (in_array("$extension/$library_name", $this->librariesToSkip)) {
          continue;
        }
// The hash is the second segment of the filename.     if (!isset($file_parts[1])) {
      throw new BadRequestHttpException('Invalid filename');
    }
    $received_hash = $file_parts[1];

    // Now build the asset groups based on the libraries. It requires the full     // set of asset groups to extract and build the aggregate for the group we     // want, since libraries may be split across different asset groups.     $theme = $request->query->get('theme');
    $active_theme = $this->themeInitialization->initTheme($theme);
    $this->themeManager->setActiveTheme($active_theme);

    $attached_assets = new AttachedAssets();
    $include_string = UrlHelper::uncompressQueryParameter($request->query->get('include'));

    if (!$include_string) {
      throw new BadRequestHttpException('The libraries to include are encoded incorrectly.');
    }
    $attached_assets->setLibraries(explode(',', $include_string));

    if ($request->query->has('exclude')) {
      $exclude_string = UrlHelper::uncompressQueryParameter($request->query->get('exclude'));
      
Home | Imprint | This part of the site doesn't use cookies.