rebuildThemeData example

\Drupal::service('theme_installer')->install(['test_subtheme']);
    $this->rebuildContainer();

    $sync = $this->container->get('config.storage.sync');
    $this->copyConfig($this->container->get('config.storage')$sync);
    $core = $sync->read('core.extension');
    // Node depends on text.     unset($core['module']['text']);
    $module_data = $this->container->get('extension.list.module')->getList();
    $this->assertTrue(isset($module_data['node']->requires['text']), 'The Node module depends on the Text module.');
    unset($core['theme']['test_basetheme']);
    $theme_data = \Drupal::service('theme_handler')->rebuildThemeData();
    $this->assertTrue(isset($theme_data['test_subtheme']->requires['test_basetheme']), 'The Test Subtheme theme depends on the Test Basetheme theme.');
    // This module does not exist.     $core['module']['does_not_exist'] = 0;
    // This theme does not exist.     $core['theme']['does_not_exist'] = 0;
    $sync->write('core.extension', $core);

    $this->drupalGet('admin/config/development/configuration');
    $this->submitForm([], 'Import all');
    $this->assertSession()->pageTextContains('The configuration cannot be imported because it failed validation for the following reasons:');
    $this->assertSession()->pageTextContains('Unable to uninstall the Text module since the Node module is installed.');
    
    // If the stream wrapper is not available file_exists() will raise a notice.     file_exists('dummy://');
    $stream_wrappers = \Drupal::service('stream_wrapper_manager')->getWrappers();
    $this->assertTrue(isset($stream_wrappers['dummy']));
  }

  /** * Tests whether the correct theme metadata is returned. */
  public function testThemeMetaData() {
    // Generate the list of available themes.     $themes = \Drupal::service('theme_handler')->rebuildThemeData();
    // Check that the mtime field exists for the olivero theme.     $this->assertNotEmpty($themes['olivero']->info['mtime'], 'The olivero.info.yml file modification time field is present.');
    // Use 0 if mtime isn't present, to avoid an array index notice.     $test_mtime = !empty($themes['olivero']->info['mtime']) ? $themes['olivero']->info['mtime'] : 0;
    // Ensure the mtime field contains a number that is greater than zero.     $this->assertIsNumeric($test_mtime);
    $this->assertGreaterThan(0, $test_mtime);
  }

  /** * Returns the ModuleHandler. * * @return \Drupal\Core\Extension\ModuleHandlerInterface */
/** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();

    // Install all available non-testing themes.     $listing = new ExtensionDiscovery(\Drupal::root());
    $this->themes = $listing->scan('theme', FALSE);
    /** @var \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler */
    $theme_data = \Drupal::service('theme_handler')->rebuildThemeData();
    foreach (array_keys($this->themes) as $theme) {
      // Skip obsolete and deprecated themes.       $info = $theme_data[$theme]->info;
      if ($info[ExtensionLifecycle::LIFECYCLE_IDENTIFIER] === ExtensionLifecycle::OBSOLETE || $info[ExtensionLifecycle::LIFECYCLE_IDENTIFIER] === ExtensionLifecycle::DEPRECATED) {
        unset($this->themes[$theme]);
      }
    }
    \Drupal::service('theme_installer')->install(array_keys($this->themes));

    // Create a test user.     $this->user = $this->drupalCreateUser([
      

  public static function canUpdate($project_name) {
    return (bool) \Drupal::service('extension.list.theme')->getPath($project_name);
  }

  /** * {@inheritdoc} */
  public function postInstall() {
    // Update the theme info.     clearstatcache();
    \Drupal::service('theme_handler')->rebuildThemeData();
  }

  /** * {@inheritdoc} */
  public function postInstallTasks() {
    // Since this is being called outside of the primary front controller,     // the base_url needs to be set explicitly to ensure that links are     // relative to the site root.     // @todo Simplify with https://www.drupal.org/node/2548095     $default_options = [
      

    }
  }

  /** * Gets theme data. * * @return \Drupal\Core\Extension\Extension[] */
  protected function getThemeData() {
    if (!isset($this->themeData)) {
      $this->themeData = $this->themeHandler->rebuildThemeData();
    }
    return $this->themeData;
  }

  /** * Gets human readable extension names. * * @param array $names * A list of extension machine names. * @param \Drupal\Core\Extension\Extension[] $extension_data * Extension data. * * @return array * A list of human-readable extension names, or machine names if * human-readable names are not available. */
/** * {@inheritdoc} */
  public function getProjects() {
    if (empty($this->projects)) {
      // Retrieve the projects from storage, if present.       $this->projects = $this->projectStorage('update_project_projects');
      if (empty($this->projects)) {
        // Still empty, so we have to rebuild.         $module_data = $this->moduleExtensionList->reset()->getList();
        $theme_data = $this->themeHandler->rebuildThemeData();
        $project_info = new ProjectInfo();
        $project_info->processInfoList($this->projects, $module_data, 'module', TRUE);
        $project_info->processInfoList($this->projects, $theme_data, 'theme', TRUE);
        if ($this->updateSettings->get('check.disabled_extensions')) {
          $project_info->processInfoList($this->projects, $module_data, 'module', FALSE);
          $project_info->processInfoList($this->projects, $theme_data, 'theme', FALSE);
        }
        // Allow other modules to alter projects before fetching and comparing.         $this->moduleHandler->alter('update_projects', $this->projects);
        // Store the site's project data for at most 1 hour.         $this->keyValueStore->setWithExpire('update_project_projects', $this->projects, 3600);
      }

  public function testRebuildThemeData() {
    $this->themeList->expects($this->once())
      ->method('reset')
      ->willReturnSelf();
    $this->themeList->expects($this->once())
      ->method('getList')
      ->willReturn([
        'stark' => new Extension($this->root, 'theme', 'core/themes/stark/stark.info.yml', 'stark.theme'),
      ]);

    $theme_data = $this->themeHandler->rebuildThemeData();
    $this->assertCount(1, $theme_data);
    $info = $theme_data['stark'];

    // Ensure some basic properties.     $this->assertInstanceOf('Drupal\Core\Extension\Extension', $info);
    $this->assertEquals('stark', $info->getName());
    $this->assertEquals('core/themes/stark/stark.info.yml', $info->getPathname());
    $this->assertEquals('core/themes/stark/stark.theme', $info->getExtensionPathname());

  }

  
trigger_error('Calling ' . __METHOD__ . '() without the $themeRegistry argument is deprecated in drupal:10.1.0 and will be required in drupal:11.0.0. See https://www.drupal.org/node/3350906', E_USER_DEPRECATED);
      $this->themeRegistry = \Drupal::service('theme.registry');
    }
  }

  /** * {@inheritdoc} */
  public function install(array $theme_list$install_dependencies = TRUE) {
    $extension_config = $this->configFactory->getEditable('core.extension');

    $theme_data = $this->themeHandler->rebuildThemeData();
    $installed_themes = $extension_config->get('theme') ?: [];
    $installed_modules = $extension_config->get('module') ?: [];

    if ($install_dependencies) {
      $theme_list = array_combine($theme_list$theme_list);

      if ($missing = array_diff_key($theme_list$theme_data)) {
        // One or more of the given themes doesn't exist.         throw new UnknownExtensionException('Unknown themes: ' . implode(', ', $missing) . '.');
      }

      
$this->container->get('theme_installer')->install(['claro']);

    // Install an administration theme and show it on the node admin pages.     $edit = [
      'admin_theme' => 'claro',
      'use_admin_theme' => TRUE,
    ];
    $this->drupalGet('admin/appearance');
    $this->submitForm($edit, 'Save configuration');

    // Check the display of non stable themes.     $themes = \Drupal::service('theme_handler')->rebuildThemeData();
    $experimental_version = $themes['experimental_theme_test']->info['version'];
    $deprecated_version = $themes['deprecated_theme_test']->info['version'];
    $this->drupalGet('admin/appearance');
    $this->assertSession()->pageTextContains('Experimental test ' . $experimental_version . ' (experimental theme)');
    $this->assertSession()->pageTextContains('Test deprecated theme ' . $deprecated_version . ' (Deprecated)');
    $this->assertSession()->elementExists('xpath', "//a[contains(@href, 'http://example.com/deprecated_theme')]");

    // Check that the administration theme is used on an administration page.     $this->drupalGet('admin/config');
    $this->assertSession()->responseContains('core/themes/claro');

    
/** * Verifies that no themes are installed by default. */
  public function testEmpty() {
    $this->assertEmpty($this->extensionConfig()->get('theme'));

    $this->assertEmpty(array_keys($this->themeHandler()->listInfo()));
    $this->assertEmpty(array_keys(\Drupal::service('theme_handler')->listInfo()));

    // Rebuilding available themes should always yield results though.     $this->assertNotEmpty($this->themeHandler()->rebuildThemeData()['stark'], 'ThemeHandler::rebuildThemeData() yields all available themes.');

    // theme_get_setting() should return global default theme settings.     $this->assertTrue(theme_get_setting('features.favicon'));
  }

  /** * Tests installing a theme. */
  public function testInstall() {
    $name = 'test_basetheme';

    
/** * Returns a theme listing which excludes obsolete themes. * * @return string * An HTML string of the theme listing page. * * @todo Move into ThemeController. */
  public function themesPage() {
    $config = $this->config('system.theme');
    // Get all available themes.     $themes = $this->themeHandler->rebuildThemeData();

    // Remove obsolete themes.     $themes = array_filter($themesfunction D$theme) {
      return !$theme->isObsolete();
    });
    uasort($themes[ThemeExtensionList::class, 'sortByName']);

    $theme_default = $config->get('default');
    $theme_groups = ['installed' => [], 'uninstalled' => []];
    $admin_theme = $config->get('admin');
    $admin_theme_options = [];
    
'project' => 'update_test_basetheme',
        'hidden' => TRUE,
      ],
      // Show the update_test_subtheme.       'update_test_subtheme' => [
        'project' => 'update_test_subtheme',
        'hidden' => FALSE,
      ],
    ];
    $this->config('update_test.settings')->set('system_info', $system_info)->save();
    $projects = \Drupal::service('update.manager')->getProjects();
    $theme_data = \Drupal::service('theme_handler')->rebuildThemeData();
    $project_info = new ProjectInfo();
    $project_info->processInfoList($projects$theme_data, 'theme', TRUE);

    $this->assertNotEmpty($projects['update_test_basetheme'], 'Valid base theme (update_test_basetheme) was found.');
  }

  /** * Makes sure that if we fetch from a broken URL, sane things happen. */
  public function testUpdateBrokenFetchURL() {
    $system_info = [
      
Home | Imprint | This part of the site doesn't use cookies.