Version 1
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Render / Element / RenderElementTypesTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Render\Element;
4
5 use Drupal\Component\Utility\Html;
6 use Drupal\Core\Url;
7 use Drupal\KernelTests\KernelTestBase;
8
9 /**
10  * Tests the markup of core render element types passed to drupal_render().
11  *
12  * @group Common
13  */
14 class RenderElementTypesTest extends KernelTestBase {
15
16   /**
17    * Modules to enable.
18    *
19    * @var array
20    */
21   public static $modules = ['system', 'router_test'];
22
23   protected function setUp() {
24     parent::setUp();
25     $this->installConfig(['system']);
26     \Drupal::service('router.builder')->rebuild();
27   }
28
29   /**
30    * Asserts that an array of elements is rendered properly.
31    *
32    * @param array $elements
33    *   The render element array to test.
34    * @param string $expected_html
35    *   The expected markup.
36    * @param string $message
37    *   Assertion message.
38    */
39   protected function assertElements(array $elements, $expected_html, $message) {
40     $actual_html = (string) \Drupal::service('renderer')->renderRoot($elements);
41
42     $out = '<table><tr>';
43     $out .= '<td valign="top"><pre>' . Html::escape($expected_html) . '</pre></td>';
44     $out .= '<td valign="top"><pre>' . Html::escape($actual_html) . '</pre></td>';
45     $out .= '</tr></table>';
46     $this->verbose($out);
47
48     $this->assertIdentical($actual_html, $expected_html, Html::escape($message));
49   }
50
51   /**
52    * Tests system #type 'container'.
53    */
54   public function testContainer() {
55     // Basic container with no attributes.
56     $this->assertElements([
57       '#type' => 'container',
58       '#markup' => 'foo',
59     ], "<div>foo</div>\n", "#type 'container' with no HTML attributes");
60
61     // Container with a class.
62     $this->assertElements([
63       '#type' => 'container',
64       '#markup' => 'foo',
65       '#attributes' => [
66         'class' => ['bar'],
67       ],
68     ], '<div class="bar">foo</div>' . "\n", "#type 'container' with a class HTML attribute");
69
70     // Container with children.
71     $this->assertElements([
72       '#type' => 'container',
73       'child' => [
74         '#markup' => 'foo',
75       ],
76     ], "<div>foo</div>\n", "#type 'container' with child elements");
77   }
78
79   /**
80    * Tests system #type 'html_tag'.
81    */
82   public function testHtmlTag() {
83     // Test void element.
84     $this->assertElements([
85       '#type' => 'html_tag',
86       '#tag' => 'meta',
87       '#value' => 'ignored',
88       '#attributes' => [
89         'name' => 'description',
90         'content' => 'Drupal test',
91       ],
92     ], '<meta name="description" content="Drupal test" />' . "\n", "#type 'html_tag', void element renders properly");
93
94     // Test non-void element.
95     $this->assertElements([
96       '#type' => 'html_tag',
97       '#tag' => 'section',
98       '#value' => 'value',
99       '#attributes' => [
100         'class' => ['unicorns'],
101       ],
102     ], '<section class="unicorns">value</section>' . "\n", "#type 'html_tag', non-void element renders properly");
103
104     // Test empty void element tag.
105     $this->assertElements([
106       '#type' => 'html_tag',
107       '#tag' => 'link',
108     ], "<link />\n", "#type 'html_tag' empty void element renders properly");
109
110     // Test empty non-void element tag.
111     $this->assertElements([
112       '#type' => 'html_tag',
113       '#tag' => 'section',
114     ], "<section></section>\n", "#type 'html_tag' empty non-void element renders properly");
115   }
116
117   /**
118    * Tests system #type 'more_link'.
119    */
120   public function testMoreLink() {
121     $elements = [
122       [
123         'name' => "#type 'more_link' anchor tag generation without extra classes",
124         'value' => [
125           '#type' => 'more_link',
126           '#url' => Url::fromUri('https://www.drupal.org'),
127         ],
128         'expected' => '//div[@class="more-link"]/a[@href="https://www.drupal.org" and text()="More"]',
129       ],
130       [
131         'name' => "#type 'more_link' anchor tag generation with different link text",
132         'value' => [
133           '#type' => 'more_link',
134           '#url' => Url::fromUri('https://www.drupal.org'),
135           '#title' => 'More Titles',
136         ],
137         'expected' => '//div[@class="more-link"]/a[@href="https://www.drupal.org" and text()="More Titles"]',
138       ],
139       [
140         'name' => "#type 'more_link' anchor tag generation with attributes on wrapper",
141         'value' => [
142           '#type' => 'more_link',
143           '#url' => Url::fromUri('https://www.drupal.org'),
144           '#theme_wrappers' => [
145             'container' => [
146               '#attributes' => [
147                 'title' => 'description',
148                 'class' => ['more-link', 'drupal', 'test'],
149               ],
150             ],
151           ],
152         ],
153         'expected' => '//div[@title="description" and contains(@class, "more-link") and contains(@class, "drupal") and contains(@class, "test")]/a[@href="https://www.drupal.org" and text()="More"]',
154       ],
155       [
156         'name' => "#type 'more_link' anchor tag with a relative path",
157         'value' => [
158           '#type' => 'more_link',
159           '#url' => Url::fromRoute('router_test.1'),
160         ],
161         'expected' => '//div[@class="more-link"]/a[@href="' . Url::fromRoute('router_test.1')->toString() . '" and text()="More"]',
162       ],
163       [
164         'name' => "#type 'more_link' anchor tag with a route",
165         'value' => [
166           '#type' => 'more_link',
167           '#url' => Url::fromRoute('router_test.1'),
168         ],
169         'expected' => '//div[@class="more-link"]/a[@href="' . \Drupal::urlGenerator()->generate('router_test.1') . '" and text()="More"]',
170       ],
171       [
172         'name' => "#type 'more_link' anchor tag with an absolute path",
173         'value' => [
174           '#type' => 'more_link',
175           '#url' => Url::fromRoute('system.admin_content'),
176           '#options' => ['absolute' => TRUE],
177         ],
178         'expected' => '//div[@class="more-link"]/a[@href="' . Url::fromRoute('system.admin_content')->setAbsolute()->toString() . '" and text()="More"]',
179       ],
180       [
181         'name' => "#type 'more_link' anchor tag to the front page",
182         'value' => [
183           '#type' => 'more_link',
184           '#url' => Url::fromRoute('<front>'),
185         ],
186         'expected' => '//div[@class="more-link"]/a[@href="' . Url::fromRoute('<front>')->toString() . '" and text()="More"]',
187       ],
188     ];
189
190     foreach ($elements as $element) {
191       $xml = new \SimpleXMLElement(\Drupal::service('renderer')->renderRoot($element['value']));
192       $result = $xml->xpath($element['expected']);
193       $this->assertTrue($result, '"' . $element['name'] . '" input rendered correctly by drupal_render().');
194     }
195   }
196
197   /**
198    * Tests system #type 'system_compact_link'.
199    */
200   public function testSystemCompactLink() {
201     $elements = [
202       [
203         'name' => "#type 'system_compact_link' when admin compact mode is off",
204         'value' => [
205           '#type' => 'system_compact_link',
206         ],
207         'expected' => '//div[@class="compact-link"]/a[contains(@href, "admin/compact/on?") and text()="Hide descriptions"]',
208       ],
209       [
210         'name' => "#type 'system_compact_link' when adding extra attributes",
211         'value' => [
212           '#type' => 'system_compact_link',
213           '#attributes' => [
214             'class' => ['kittens-rule'],
215           ],
216         ],
217         'expected' => '//div[@class="compact-link"]/a[contains(@href, "admin/compact/on?") and @class="kittens-rule" and text()="Hide descriptions"]',
218       ],
219     ];
220
221     foreach ($elements as $element) {
222       $xml = new \SimpleXMLElement(\Drupal::service('renderer')->renderRoot($element['value']));
223       $result = $xml->xpath($element['expected']);
224       $this->assertTrue($result, '"' . $element['name'] . '" is rendered correctly by drupal_render().');
225     }
226
227     // Set admin compact mode on for additional tests.
228     \Drupal::request()->cookies->set('Drupal_visitor_admin_compact_mode', TRUE);
229
230     $element = [
231       'name' => "#type 'system_compact_link' when admin compact mode is on",
232       'value' => [
233         '#type' => 'system_compact_link',
234       ],
235       'expected' => '//div[@class="compact-link"]/a[contains(@href, "admin/compact?") and text()="Show descriptions"]',
236     ];
237
238     $xml = new \SimpleXMLElement(\Drupal::service('renderer')->renderRoot($element['value']));
239     $result = $xml->xpath($element['expected']);
240     $this->assertTrue($result, '"' . $element['name'] . '" is rendered correctly by drupal_render().');
241   }
242
243 }