b9f09ec034bcfd087df0ed0c370eefe1c62fbfbc
[yaffs-website] / web / core / modules / system / tests / src / Kernel / Theme / ThemeTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Kernel\Theme;
4
5 use Drupal\KernelTests\KernelTestBase;
6 use Drupal\Component\Render\MarkupInterface;
7 use Drupal\test_theme\ThemeClass;
8
9 /**
10  * Tests low-level theme functions.
11  *
12  * @group Theme
13  */
14 class ThemeTest extends KernelTestBase {
15
16   /**
17    * {@inheritdoc}
18    */
19   public static $modules = ['theme_test', 'node', 'system'];
20
21   /**
22    * {@inheritdoc}
23    */
24   protected function setUp() {
25     parent::setUp();
26     \Drupal::service('theme_handler')->install(['test_theme']);
27   }
28
29   /**
30    * Test attribute merging.
31    *
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
38    */
39   public function testAttributeMerging() {
40     $theme_test_render_element = [
41       'elements' => [
42         '#attributes' => ['data-foo' => 'bar'],
43       ],
44       'attributes' => [
45         'id' => 'bazinga',
46       ],
47     ];
48     $this->assertThemeOutput('theme_test_render_element', $theme_test_render_element, '<div id="bazinga" data-foo="bar" data-variables-are-preprocessed></div>' . "\n");
49   }
50
51   /**
52    * Test that ThemeManager renders the expected data types.
53    */
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());
64       }
65       elseif (is_string($output)) {
66         $this->assertIdentical($output, '', 'A string will be return when the theme returns an empty string.');
67       }
68     }
69
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.');
74   }
75
76   /**
77    * Test function theme_get_suggestions() for SA-CORE-2009-003.
78    */
79   public function testThemeSuggestions() {
80     // Set the front page as something random otherwise the CLI
81     // test runner fails.
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.');
101   }
102
103   /**
104    * Test the listInfo() function.
105    */
106   public function testListThemes() {
107     $theme_handler = $this->container->get('theme_handler');
108     $theme_handler->install(['test_subtheme']);
109     $themes = $theme_handler->listInfo();
110
111     // Check if ThemeHandlerInterface::listInfo() retrieves enabled themes.
112     $this->assertIdentical(1, $themes['test_theme']->status, 'Installed theme detected');
113
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'];
118
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.');
126   }
127
128   /**
129    * Tests child element rendering for 'render element' theme hooks.
130    */
131   public function testDrupalRenderChildren() {
132     $element = [
133       '#theme' => 'theme_test_render_element_children',
134       'child' => [
135         '#markup' => 'Foo',
136       ],
137     ];
138     $this->assertThemeOutput('theme_test_render_element_children', $element, 'Foo', 'drupal_render() avoids #theme recursion loop when rendering a render element.');
139
140     $element = [
141       '#theme_wrappers' => ['theme_test_render_element_children'],
142       'child' => [
143         '#markup' => 'Foo',
144       ],
145     ];
146     $this->assertThemeOutput('theme_test_render_element_children', $element, 'Foo', 'drupal_render() avoids #theme_wrappers recursion loop when rendering a render element.');
147   }
148
149   /**
150    * Tests theme can provide classes.
151    */
152   public function testClassLoading() {
153     new ThemeClass();
154   }
155
156   /**
157    * Tests drupal_find_theme_templates().
158    */
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.');
163   }
164
165 }