View source
<?php
namespace Drupal\Tests\Core\Theme;
use Drupal\Core\Theme\ActiveTheme;
use Drupal\Core\Theme\Registry;
use Drupal\Tests\UnitTestCase;
class RegistryTest extends UnitTestCase {
protected $registry;
protected $cache;
protected $lock;
protected $moduleHandler;
protected $themeHandler;
protected $themeInitialization;
protected $themeManager;
public static $functions = [];
protected function setUp() {
parent::setUp();
$this->cache = $this
->createMock('Drupal\\Core\\Cache\\CacheBackendInterface');
$this->lock = $this
->createMock('Drupal\\Core\\Lock\\LockBackendInterface');
$this->moduleHandler = $this
->createMock('Drupal\\Core\\Extension\\ModuleHandlerInterface');
$this->themeHandler = $this
->createMock('Drupal\\Core\\Extension\\ThemeHandlerInterface');
$this->themeInitialization = $this
->createMock('Drupal\\Core\\Theme\\ThemeInitializationInterface');
$this->themeManager = $this
->createMock('Drupal\\Core\\Theme\\ThemeManagerInterface');
$this
->setupTheme();
}
protected function tearDown() {
parent::tearDown();
static::$functions = [];
}
public function testGetRegistryForModule() {
$test_theme = new ActiveTheme([
'name' => 'test_theme',
'path' => 'core/modules/system/tests/themes/test_theme/test_theme.info.yml',
'engine' => 'twig',
'owner' => 'twig',
'stylesheets_remove' => [],
'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',
'stylesheets_remove' => [],
'libraries_override' => [],
'libraries_extend' => [],
'libraries' => [],
'extension' => '.twig',
'base_theme_extensions' => [],
]);
$this->themeManager
->expects($this
->exactly(2))
->method('getActiveTheme')
->willReturnOnConsecutiveCalls($test_theme, $test_stable);
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';
$this->moduleHandler
->expects($this
->exactly(2))
->method('getImplementations')
->with('theme')
->will($this
->returnValue([
'theme_test',
]));
$this->moduleHandler
->expects($this
->atLeastOnce())
->method('getModuleList')
->willReturn([]);
$registry = $this->registry
->get();
$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']);
$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']);
}
public function testPostProcessExtension($defined_functions, $hooks, $expected) {
static::$functions['user'] = $defined_functions;
$theme = $this
->prophesize(ActiveTheme::class);
$theme
->getBaseThemeExtensions()
->willReturn([]);
$theme
->getName()
->willReturn('test');
$theme
->getEngine()
->willReturn('twig');
$this->moduleHandler
->expects($this
->atLeastOnce())
->method('getModuleList')
->willReturn([]);
$class = new \ReflectionClass(Registry::class);
$reflection_method = $class
->getMethod('postProcessExtension');
$reflection_method
->setAccessible(TRUE);
$reflection_method
->invokeArgs($this->registry, [
&$hooks,
$theme
->reveal(),
]);
$this
->assertArrayEquals($expected, $hooks);
}
public function providerTestPostProcessExtension() {
$data = [];
$data['base_hook_with_autodiscovered_suggestions'] = [
'defined_functions' => [
'test_preprocess_test_hook__suggestion',
'test_preprocess_test_hook__suggestion__another',
],
'hooks' => [
'test_hook' => [
'preprocess functions' => [
'explicit_preprocess_test_hook',
],
],
],
'expected' => [
'test_hook' => [
'preprocess functions' => [
'explicit_preprocess_test_hook',
],
],
'test_hook__suggestion' => [
'preprocess functions' => [
'explicit_preprocess_test_hook',
'test_preprocess_test_hook__suggestion',
],
'base hook' => 'test_hook',
],
'test_hook__suggestion__another' => [
'preprocess functions' => [
'explicit_preprocess_test_hook',
'test_preprocess_test_hook__suggestion',
'test_preprocess_test_hook__suggestion__another',
],
'base hook' => 'test_hook',
],
],
];
$data['base_hook_with_explicit_suggestions'] = [
'defined_functions' => [],
'hooks' => [
'test_hook' => [
'preprocess functions' => [
'explicit_preprocess_test_hook',
],
],
'test_hook__suggestion__another' => [
'base hook' => 'test_hook',
'preprocess functions' => [
'explicit_preprocess_test_hook__suggestion__another',
],
'incomplete preprocess functions' => TRUE,
],
],
'expected' => [
'test_hook' => [
'preprocess functions' => [
'explicit_preprocess_test_hook',
],
],
'test_hook__suggestion__another' => [
'preprocess functions' => [
'explicit_preprocess_test_hook',
'explicit_preprocess_test_hook__suggestion__another',
],
'base hook' => 'test_hook',
],
],
];
$data['base_hook_with_explicit_suggestions_and_intermediary_preprocess_function'] = [
'defined_functions' => [
'test_preprocess_test_hook__suggestion',
],
'hooks' => [
'test_hook' => [
'preprocess functions' => [
'explicit_preprocess_test_hook',
],
],
'test_hook__suggestion__another' => [
'base hook' => 'test_hook',
'preprocess functions' => [
'explicit_preprocess_test_hook__suggestion__another',
],
'incomplete preprocess functions' => TRUE,
],
],
'expected' => [
'test_hook' => [
'preprocess functions' => [
'explicit_preprocess_test_hook',
],
],
'test_hook__suggestion' => [
'preprocess functions' => [
'explicit_preprocess_test_hook',
'test_preprocess_test_hook__suggestion',
],
'base hook' => 'test_hook',
],
'test_hook__suggestion__another' => [
'preprocess functions' => [
'explicit_preprocess_test_hook',
'test_preprocess_test_hook__suggestion',
'explicit_preprocess_test_hook__suggestion__another',
],
'base hook' => 'test_hook',
],
],
];
$data['child_hook_without_magic_naming'] = [
'defined_functions' => [],
'hooks' => [
'test_hook' => [
'preprocess functions' => [
'explicit_preprocess_test_hook',
],
],
'child_hook' => [
'base hook' => 'test_hook',
'preprocess functions' => [
'explicit_preprocess_child_hook',
],
'incomplete preprocess functions' => TRUE,
],
],
'expected' => [
'test_hook' => [
'preprocess functions' => [
'explicit_preprocess_test_hook',
],
],
'child_hook' => [
'preprocess functions' => [
'explicit_preprocess_test_hook',
'explicit_preprocess_child_hook',
],
'base hook' => 'test_hook',
],
],
];
$data['child_hook_with_suggestions'] = [
'defined_functions' => [
'test_preprocess_child_hook__suggestion',
'test_preprocess_child_hook__suggestion__another',
],
'hooks' => [
'test_hook' => [
'preprocess functions' => [
'explicit_preprocess_test_hook',
],
],
'child_hook' => [
'base hook' => 'test_hook',
'preprocess functions' => [
'explicit_preprocess_child_hook',
],
'incomplete preprocess functions' => TRUE,
],
],
'expected' => [
'test_hook' => [
'preprocess functions' => [
'explicit_preprocess_test_hook',
],
],
'child_hook' => [
'preprocess functions' => [
'explicit_preprocess_test_hook',
'explicit_preprocess_child_hook',
],
'base hook' => 'test_hook',
],
'child_hook__suggestion' => [
'preprocess functions' => [
'explicit_preprocess_test_hook',
'explicit_preprocess_child_hook',
'test_preprocess_child_hook__suggestion',
],
'base hook' => 'test_hook',
],
'child_hook__suggestion__another' => [
'preprocess functions' => [
'explicit_preprocess_test_hook',
'explicit_preprocess_child_hook',
'test_preprocess_child_hook__suggestion',
'test_preprocess_child_hook__suggestion__another',
],
'base hook' => 'test_hook',
],
],
];
$data['suggestion_with_alternate_base_hook'] = [
'defined_functions' => [
'test_preprocess_test_hook__suggestion',
],
'hooks' => [
'test_hook' => [
'preprocess functions' => [
'explicit_preprocess_test_hook',
],
],
'alternate_base_hook' => [
'preprocess functions' => [
'explicit_preprocess_alternate_base_hook',
],
],
'test_hook__suggestion__another' => [
'base hook' => 'alternate_base_hook',
'preprocess functions' => [
'explicit_preprocess_test_hook__suggestion__another',
],
'incomplete preprocess functions' => TRUE,
],
],
'expected' => [
'test_hook' => [
'preprocess functions' => [
'explicit_preprocess_test_hook',
],
],
'alternate_base_hook' => [
'preprocess functions' => [
'explicit_preprocess_alternate_base_hook',
],
],
'test_hook__suggestion' => [
'preprocess functions' => [
'explicit_preprocess_test_hook',
'test_preprocess_test_hook__suggestion',
],
'base hook' => 'test_hook',
],
'test_hook__suggestion__another' => [
'preprocess functions' => [
'explicit_preprocess_alternate_base_hook',
'explicit_preprocess_test_hook',
'test_preprocess_test_hook__suggestion',
'explicit_preprocess_test_hook__suggestion__another',
],
'base hook' => 'alternate_base_hook',
],
],
];
$data['missing_base_hook'] = [
'defined_functions' => [],
'hooks' => [
'child_hook' => [
'base hook' => 'test_hook',
'preprocess functions' => [
'explicit_preprocess_child_hook',
],
'incomplete preprocess functions' => TRUE,
],
],
'expected' => [
'child_hook' => [
'preprocess functions' => [
'explicit_preprocess_child_hook',
],
'base hook' => 'test_hook',
],
],
];
return $data;
}
protected function setupTheme() {
$this->registry = $this
->getMockBuilder(Registry::class)
->setMethods([
'getPath',
])
->setConstructorArgs([
$this->root,
$this->cache,
$this->lock,
$this->moduleHandler,
$this->themeHandler,
$this->themeInitialization,
])
->getMock();
$this->registry
->expects($this
->any())
->method('getPath')
->willReturnCallback(function ($module) {
if ($module == 'theme_test') {
return 'core/modules/system/tests/modules/theme_test';
}
});
$this->registry
->setThemeManager($this->themeManager);
}
}
namespace Drupal\Core\Theme;
use Drupal\Tests\Core\Theme\RegistryTest;
function get_defined_functions() {
return RegistryTest::$functions ?: \get_defined_functions();
}