function RegistryTest::testGetRegistryForModule

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Theme/RegistryTest.php \Drupal\Tests\Core\Theme\RegistryTest::testGetRegistryForModule()
  2. 8.9.x core/tests/Drupal/Tests/Core/Theme/RegistryTest.php \Drupal\Tests\Core\Theme\RegistryTest::testGetRegistryForModule()
  3. 10 core/tests/Drupal/Tests/Core/Theme/RegistryTest.php \Drupal\Tests\Core\Theme\RegistryTest::testGetRegistryForModule()

Tests getting the theme registry defined by a module.

File

core/tests/Drupal/Tests/Core/Theme/RegistryTest.php, line 127

Class

RegistryTest
@coversDefaultClass \Drupal\Core\Theme\Registry[[api-linebreak]] @group Theme

Namespace

Drupal\Tests\Core\Theme

Code

public function testGetRegistryForModule() : void {
  $test_theme = new ActiveTheme([
    'name' => 'test_theme',
    'path' => 'core/modules/system/tests/themes/test_theme/test_theme.info.yml',
    'engine' => 'twig',
    'owner' => 'twig',
    'libraries_override' => [],
    'libraries_extend' => [],
    'libraries' => [],
    'extension' => '.twig',
    'base_theme_extensions' => [],
  ]);
  $test_stable = new ActiveTheme([
    'name' => 'test_stable',
    'path' => 'core/tests/fixtures/test_stable/test_stable.info.yml',
    'engine' => 'twig',
    'owner' => 'twig',
    'libraries_override' => [],
    'libraries_extend' => [],
    'libraries' => [],
    'extension' => '.twig',
    'base_theme_extensions' => [],
  ]);
  $this->themeManager
    ->expects($this->exactly(2))
    ->method('getActiveTheme')
    ->willReturnOnConsecutiveCalls($test_theme, $test_stable);
  // Include the module and theme files so that hook_theme can be called.
  include_once $this->root . '/core/modules/system/tests/modules/theme_test/theme_test.module';
  include_once $this->root . '/core/tests/fixtures/test_stable/test_stable.theme';
  $themeTestTheme = new ThemeTestHooks();
  $this->moduleHandler
    ->expects($this->exactly(2))
    ->method('invoke')
    ->with('theme_test', 'theme')
    ->willReturn($themeTestTheme->theme(NULL, NULL, NULL, NULL));
  $this->moduleHandler
    ->expects($this->atMost(50))
    ->method('invokeAllWith')
    ->willReturnCallback(fn(string $hook, callable $callback) => $callback('pi', 'theme_test'));
  $this->moduleHandler
    ->expects($this->exactly(2))
    ->method('getModuleList')
    ->willReturn([
    'theme_test' => NULL,
  ]);
  $this->moduleList
    ->expects($this->exactly(2))
    ->method('getPath')
    ->with('theme_test')
    ->willReturn('core/modules/system/tests/modules/theme_test');
  $registry = $this->registry
    ->get();
  // Ensure that the registry entries from the module are found.
  $this->assertArrayHasKey('theme_test', $registry);
  $this->assertArrayHasKey('theme_test_template_test', $registry);
  $this->assertArrayHasKey('theme_test_template_test_2', $registry);
  $this->assertArrayHasKey('theme_test_suggestion_provided', $registry);
  $this->assertArrayHasKey('theme_test_specific_suggestions', $registry);
  $this->assertArrayHasKey('theme_test_suggestions', $registry);
  $this->assertArrayHasKey('theme_test_foo', $registry);
  $this->assertArrayHasKey('theme_test_render_element', $registry);
  $this->assertNotContains('test_stable_preprocess_theme_test_render_element', $registry['theme_test_render_element']['preprocess functions']);
  // The second call will initialize with the second theme. Ensure that this
  // returns a different object and the discovery for the second theme's
  // preprocess function worked.
  $other_registry = $this->registry
    ->get();
  $this->assertNotSame($registry, $other_registry);
  $this->assertContains('test_stable_preprocess_theme_test_render_element', $other_registry['theme_test_render_element']['preprocess functions']);
}

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