6bf2faaa984b9b57db5cd25620ea1dd1159b1e90
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Render / Element / TableTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Render\Element;
4
5 use Drupal\KernelTests\KernelTestBase;
6
7 /**
8  * Tests built-in table theme functions.
9  *
10  * @group Theme
11  */
12 class TableTest extends KernelTestBase {
13
14   /**
15    * Modules to enable.
16    *
17    * @var array
18    */
19   public static $modules = ['system', 'form_test'];
20
21   /**
22    * Tableheader.js provides 'sticky' table headers, and is included by default.
23    */
24   public function testThemeTableStickyHeaders() {
25     $header = ['one', 'two', 'three'];
26     $rows = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
27     $table = [
28       '#type' => 'table',
29       '#header' => $header,
30       '#rows' => $rows,
31       '#sticky' => TRUE,
32     ];
33     $this->render($table);
34     // Make sure tableheader.js was attached.
35     $tableheader = $this->xpath("//script[contains(@src, 'tableheader.js')]");
36     $this->assertEqual(count($tableheader), 1);
37     $this->assertRaw('sticky-enabled');
38   }
39
40   /**
41    * If $sticky is FALSE, no tableheader.js should be included.
42    */
43   public function testThemeTableNoStickyHeaders() {
44     $header = ['one', 'two', 'three'];
45     $rows = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
46     $attributes = [];
47     $caption = NULL;
48     $colgroups = [];
49     $table = [
50       '#type' => 'table',
51       '#header' => $header,
52       '#rows' => $rows,
53       '#attributes' => $attributes,
54       '#caption' => $caption,
55       '#colgroups' => $colgroups,
56       '#sticky' => FALSE,
57     ];
58     $this->render($table);
59     // Make sure tableheader.js was not attached.
60     $tableheader = $this->xpath("//script[contains(@src, 'tableheader.js')]");
61     $this->assertEqual(count($tableheader), 0);
62     $this->assertNoRaw('sticky-enabled');
63   }
64
65   /**
66    * Tests that the table header is printed correctly even if there are no rows,
67    * and that the empty text is displayed correctly.
68    */
69   public function testThemeTableWithEmptyMessage() {
70     $header = [
71       'Header 1',
72       [
73         'data' => 'Header 2',
74         'colspan' => 2,
75       ],
76     ];
77     $table = [
78       '#type' => 'table',
79       '#header' => $header,
80       '#rows' => [],
81       '#empty' => 'Empty row.',
82     ];
83
84     // Enable the Classy theme.
85     \Drupal::service('theme_handler')->install(['classy']);
86     $this->config('system.theme')->set('default', 'classy')->save();
87
88     $this->render($table);
89     $this->removeWhiteSpace();
90     $this->assertRaw('<thead><tr><th>Header 1</th><th colspan="2">Header 2</th></tr>', 'Table header found.');
91     $this->assertRaw('<tr class="odd"><td colspan="3" class="empty message">Empty row.</td>', 'Colspan on #empty row found.');
92   }
93
94   /**
95    * Tests that the 'no_striping' option works correctly.
96    */
97   public function testThemeTableWithNoStriping() {
98     $rows = [
99       [
100         'data' => [1],
101         'no_striping' => TRUE,
102       ],
103     ];
104     $table = [
105       '#type' => 'table',
106       '#rows' => $rows,
107     ];
108     $this->render($table);
109     $this->assertNoRaw('class="odd"', 'Odd/even classes were not added because $no_striping = TRUE.');
110     $this->assertNoRaw('no_striping', 'No invalid no_striping HTML attribute was printed.');
111   }
112
113   /**
114    * Test that the 'footer' option works correctly.
115    */
116   public function testThemeTableFooter() {
117     $footer = [
118       [
119         'data' => [1],
120       ],
121       ['Foo'],
122     ];
123
124     $table = [
125       '#type' => 'table',
126       '#rows' => [],
127       '#footer' => $footer,
128     ];
129
130     $this->render($table);
131     $this->removeWhiteSpace();
132     $this->assertRaw('<tfoot><tr><td>1</td></tr><tr><td>Foo</td></tr></tfoot>', 'Table footer found.');
133   }
134
135   /**
136    * Tests that the 'header' option in cells works correctly.
137    */
138   public function testThemeTableHeaderCellOption() {
139     $rows = [
140       [
141         ['data' => 1, 'header' => TRUE],
142         ['data' => 1, 'header' => FALSE],
143         ['data' => 1],
144       ],
145     ];
146     $table = [
147       '#type' => 'table',
148       '#rows' => $rows,
149     ];
150     $this->render($table);
151     $this->removeWhiteSpace();
152     $this->assertRaw('<th>1</th><td>1</td><td>1</td>', 'The th and td tags was printed correctly.');
153   }
154
155   /**
156    * Tests that the 'responsive-table' class is applied correctly.
157    */
158   public function testThemeTableResponsive() {
159     $header = ['one', 'two', 'three'];
160     $rows = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
161     $table = [
162       '#type' => 'table',
163       '#header' => $header,
164       '#rows' => $rows,
165       '#responsive' => TRUE,
166     ];
167     $this->render($table);
168     $this->assertRaw('responsive-enabled', 'The responsive-enabled class was printed correctly.');
169   }
170
171   /**
172    * Tests that the 'responsive-table' class is not applied without headers.
173    */
174   public function testThemeTableNotResponsiveHeaders() {
175     $rows = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
176     $table = [
177       '#type' => 'table',
178       '#rows' => $rows,
179       '#responsive' => TRUE,
180     ];
181     $this->render($table);
182     $this->assertNoRaw('responsive-enabled', 'The responsive-enabled class is not applied without table headers.');
183   }
184
185   /**
186    * Tests that 'responsive-table' class only applied when responsive is TRUE.
187    */
188   public function testThemeTableNotResponsiveProperty() {
189     $header = ['one', 'two', 'three'];
190     $rows = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
191     $table = [
192       '#type' => 'table',
193       '#header' => $header,
194       '#rows' => $rows,
195       '#responsive' => FALSE,
196     ];
197     $this->render($table);
198     $this->assertNoRaw('responsive-enabled', 'The responsive-enabled class is not applied without the "responsive" property set to TRUE.');
199   }
200
201   /**
202    * Tests 'priority-medium' and 'priority-low' classes.
203    */
204   public function testThemeTableResponsivePriority() {
205     $header = [
206       // Test associative header indices.
207       'associative_key' => ['data' => 1, 'class' => [RESPONSIVE_PRIORITY_MEDIUM]],
208       // Test non-associative header indices.
209       ['data' => 2, 'class' => [RESPONSIVE_PRIORITY_LOW]],
210       // Test no responsive priorities.
211       ['data' => 3],
212     ];
213     $rows = [[4, 5, 6]];
214     $table = [
215       '#type' => 'table',
216       '#header' => $header,
217       '#rows' => $rows,
218       '#responsive' => TRUE,
219     ];
220     $this->render($table);
221     $this->assertRaw('<th class="priority-medium">1</th>', 'Header 1: the priority-medium class was applied correctly.');
222     $this->assertRaw('<th class="priority-low">2</th>', 'Header 2: the priority-low class was applied correctly.');
223     $this->assertRaw('<th>3</th>', 'Header 3: no priority classes were applied.');
224     $this->assertRaw('<td class="priority-medium">4</td>', 'Cell 1: the priority-medium class was applied correctly.');
225     $this->assertRaw('<td class="priority-low">5</td>', 'Cell 2: the priority-low class was applied correctly.');
226     $this->assertRaw('<td>6</td>', 'Cell 3: no priority classes were applied.');
227   }
228
229   /**
230    * Tests header elements with a mix of string and render array values.
231    */
232   public function testThemeTableHeaderRenderArray() {
233     $header = [
234        [
235         'data' => [
236           '#markup' => 'one',
237         ],
238       ],
239       'two',
240        [
241         'data' => [
242           '#type' => 'html_tag',
243           '#tag' => 'b',
244           '#value' => 'three',
245         ],
246       ],
247     ];
248     $rows = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
249     $table = [
250       '#type' => 'table',
251       '#header' => $header,
252       '#rows' => $rows,
253       '#responsive' => FALSE,
254     ];
255     $this->render($table);
256     $this->removeWhiteSpace();
257     $this->assertRaw('<thead><tr><th>one</th><th>two</th><th><b>three</b></th></tr>', 'Table header found.');
258   }
259
260   /**
261    * Tests row elements with a mix of string and render array values.
262    */
263   public function testThemeTableRowRenderArray() {
264     $header = ['one', 'two', 'three'];
265     $rows = [
266       [
267         '1-one',
268         [
269           'data' => '1-two',
270         ],
271         '1-three',
272       ],
273       [
274          [
275           'data' => [
276             '#markup' => '2-one',
277           ],
278         ],
279         '2-two',
280          [
281           'data' => [
282             '#type' => 'html_tag',
283             '#tag' => 'b',
284             '#value' => '2-three',
285           ],
286         ],
287       ],
288     ];
289     $table = [
290       '#type' => 'table',
291       '#header' => $header,
292       '#rows' => $rows,
293       '#responsive' => FALSE,
294     ];
295     $this->render($table);
296     $this->removeWhiteSpace();
297     $this->assertRaw('<tbody><tr><td>1-one</td><td>1-two</td><td>1-three</td></tr>', 'Table row 1 found.');
298     $this->assertRaw('<tr><td>2-one</td><td>2-two</td><td><b>2-three</b></td></tr></tbody>', 'Table row 2 found.');
299   }
300
301   /**
302    * Tests that the select/checkbox label is being generated and escaped.
303    */
304   public function testThemeTableTitle() {
305     $form = \Drupal::formBuilder()->getForm('\Drupal\form_test\Form\FormTestTableForm');
306     $this->render($form);
307     $this->assertEscaped('Update <em>kitten</em>');
308     $this->assertRaw('Update my favourite fruit is <strong>bananas</strong>');
309   }
310
311 }