3 namespace Drupal\Tests\system\Kernel\Theme;
5 use Drupal\KernelTests\KernelTestBase;
6 use Drupal\Component\Render\MarkupInterface;
7 use Drupal\test_theme\ThemeClass;
10 * Tests low-level theme functions.
14 class ThemeTest extends KernelTestBase {
19 public static $modules = ['theme_test', 'node', 'system'];
24 protected function setUp() {
26 \Drupal::service('theme_handler')->install(['test_theme']);
30 * Test attribute merging.
32 * Render arrays that use a render element and templates (and hence call
33 * template_preprocess()) must ensure the attributes at different occasions
34 * are all merged correctly:
35 * - $variables['attributes'] as passed in to the theme hook implementation.
36 * - the render element's #attributes
37 * - any attributes set in the template's preprocessing function
39 public function testAttributeMerging() {
40 $theme_test_render_element = [
42 '#attributes' => ['data-foo' => 'bar'],
48 $this->assertThemeOutput('theme_test_render_element', $theme_test_render_element, '<div id="bazinga" data-foo="bar" data-variables-are-preprocessed></div>' . "\n");
52 * Test that ThemeManager renders the expected data types.
54 public function testThemeDataTypes() {
55 // theme_test_false is an implemented theme hook so \Drupal::theme() service
56 // should return a string or an object that implements MarkupInterface,
57 // even though the theme function itself can return anything.
58 $foos = ['null' => NULL, 'false' => FALSE, 'integer' => 1, 'string' => 'foo', 'empty_string' => ''];
59 foreach ($foos as $type => $example) {
60 $output = \Drupal::theme()->render('theme_test_foo', ['foo' => $example]);
61 $this->assertTrue($output instanceof MarkupInterface || is_string($output), format_string('\Drupal::theme() returns an object that implements MarkupInterface or a string for data type @type.', ['@type' => $type]));
62 if ($output instanceof MarkupInterface) {
63 $this->assertIdentical((string) $example, $output->__toString());
65 elseif (is_string($output)) {
66 $this->assertIdentical($output, '', 'A string will be return when the theme returns an empty string.');
70 // suggestionnotimplemented is not an implemented theme hook so \Drupal::theme() service
71 // should return FALSE instead of a string.
72 $output = \Drupal::theme()->render(['suggestionnotimplemented'], []);
73 $this->assertIdentical($output, FALSE, '\Drupal::theme() returns FALSE when a hook suggestion is not implemented.');
77 * Test function theme_get_suggestions() for SA-CORE-2009-003.
79 public function testThemeSuggestions() {
80 // Set the front page as something random otherwise the CLI
82 $this->config('system.site')->set('page.front', '/nobody-home')->save();
83 $args = ['node', '1', 'edit'];
84 $suggestions = theme_get_suggestions($args, 'page');
85 $this->assertEqual($suggestions, ['page__node', 'page__node__%', 'page__node__1', 'page__node__edit'], 'Found expected node edit page suggestions');
86 // Check attack vectors.
87 $args = ['node', '\\1'];
88 $suggestions = theme_get_suggestions($args, 'page');
89 $this->assertEqual($suggestions, ['page__node', 'page__node__%', 'page__node__1'], 'Removed invalid \\ from suggestions');
90 $args = ['node', '1/'];
91 $suggestions = theme_get_suggestions($args, 'page');
92 $this->assertEqual($suggestions, ['page__node', 'page__node__%', 'page__node__1'], 'Removed invalid / from suggestions');
93 $args = ['node', "1\0"];
94 $suggestions = theme_get_suggestions($args, 'page');
95 $this->assertEqual($suggestions, ['page__node', 'page__node__%', 'page__node__1'], 'Removed invalid \\0 from suggestions');
96 // Define path with hyphens to be used to generate suggestions.
97 $args = ['node', '1', 'hyphen-path'];
98 $result = ['page__node', 'page__node__%', 'page__node__1', 'page__node__hyphen_path'];
99 $suggestions = theme_get_suggestions($args, 'page');
100 $this->assertEqual($suggestions, $result, 'Found expected page suggestions for paths containing hyphens.');
104 * Test the listInfo() function.
106 public function testListThemes() {
107 $theme_handler = $this->container->get('theme_handler');
108 $theme_handler->install(['test_subtheme']);
109 $themes = $theme_handler->listInfo();
111 // Check if ThemeHandlerInterface::listInfo() retrieves enabled themes.
112 $this->assertIdentical(1, $themes['test_theme']->status, 'Installed theme detected');
114 // Check if ThemeHandlerInterface::listInfo() returns disabled themes.
115 // Check for base theme and subtheme lists.
116 $base_theme_list = ['test_basetheme' => 'Theme test base theme'];
117 $sub_theme_list = ['test_subsubtheme' => 'Theme test subsubtheme', 'test_subtheme' => 'Theme test subtheme'];
119 $this->assertIdentical($themes['test_basetheme']->sub_themes, $sub_theme_list, 'Base theme\'s object includes list of subthemes.');
120 $this->assertIdentical($themes['test_subtheme']->base_themes, $base_theme_list, 'Subtheme\'s object includes list of base themes.');
121 // Check for theme engine in subtheme.
122 $this->assertIdentical($themes['test_subtheme']->engine, 'twig', 'Subtheme\'s object includes the theme engine.');
123 // Check for theme engine prefix.
124 $this->assertIdentical($themes['test_basetheme']->prefix, 'twig', 'Base theme\'s object includes the theme engine prefix.');
125 $this->assertIdentical($themes['test_subtheme']->prefix, 'twig', 'Subtheme\'s object includes the theme engine prefix.');
129 * Tests child element rendering for 'render element' theme hooks.
131 public function testDrupalRenderChildren() {
133 '#theme' => 'theme_test_render_element_children',
138 $this->assertThemeOutput('theme_test_render_element_children', $element, 'Foo', 'drupal_render() avoids #theme recursion loop when rendering a render element.');
141 '#theme_wrappers' => ['theme_test_render_element_children'],
146 $this->assertThemeOutput('theme_test_render_element_children', $element, 'Foo', 'drupal_render() avoids #theme_wrappers recursion loop when rendering a render element.');
150 * Tests theme can provide classes.
152 public function testClassLoading() {
157 * Tests drupal_find_theme_templates().
159 public function testFindThemeTemplates() {
160 $registry = $this->container->get('theme.registry')->get();
161 $templates = drupal_find_theme_templates($registry, '.html.twig', drupal_get_path('theme', 'test_theme'));
162 $this->assertEqual($templates['node__1']['template'], 'node--1', 'Template node--1.tpl.twig was found in test_theme.');