3 namespace Drupal\KernelTests\Core\Render\Element;
5 use Drupal\KernelTests\KernelTestBase;
8 * Tests built-in table theme functions.
12 class TableTest extends KernelTestBase {
19 public static $modules = ['system', 'form_test'];
22 * Tableheader.js provides 'sticky' table headers, and is included by default.
24 public function testThemeTableStickyHeaders() {
25 $header = ['one', 'two', 'three'];
26 $rows = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
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');
41 * If $sticky is FALSE, no tableheader.js should be included.
43 public function testThemeTableNoStickyHeaders() {
44 $header = ['one', 'two', 'three'];
45 $rows = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
53 '#attributes' => $attributes,
54 '#caption' => $caption,
55 '#colgroups' => $colgroups,
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');
66 * Tests that the table header is printed correctly even if there are no rows,
67 * and that the empty text is displayed correctly.
69 public function testThemeTableWithEmptyMessage() {
81 '#empty' => 'Empty row.',
84 // Enable the Classy theme.
85 \Drupal::service('theme_handler')->install(['classy']);
86 $this->config('system.theme')->set('default', 'classy')->save();
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.');
95 * Tests that the 'no_striping' option works correctly.
97 public function testThemeTableWithNoStriping() {
101 'no_striping' => TRUE,
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.');
114 * Test that the 'footer' option works correctly.
116 public function testThemeTableFooter() {
127 '#footer' => $footer,
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.');
136 * Tests that the 'header' option in cells works correctly.
138 public function testThemeTableHeaderCellOption() {
141 ['data' => 1, 'header' => TRUE],
142 ['data' => 1, 'header' => FALSE],
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.');
156 * Tests that the 'responsive-table' class is applied correctly.
158 public function testThemeTableResponsive() {
159 $header = ['one', 'two', 'three'];
160 $rows = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
163 '#header' => $header,
165 '#responsive' => TRUE,
167 $this->render($table);
168 $this->assertRaw('responsive-enabled', 'The responsive-enabled class was printed correctly.');
172 * Tests that the 'responsive-table' class is not applied without headers.
174 public function testThemeTableNotResponsiveHeaders() {
175 $rows = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
179 '#responsive' => TRUE,
181 $this->render($table);
182 $this->assertNoRaw('responsive-enabled', 'The responsive-enabled class is not applied without table headers.');
186 * Tests that 'responsive-table' class only applied when responsive is TRUE.
188 public function testThemeTableNotResponsiveProperty() {
189 $header = ['one', 'two', 'three'];
190 $rows = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
193 '#header' => $header,
195 '#responsive' => FALSE,
197 $this->render($table);
198 $this->assertNoRaw('responsive-enabled', 'The responsive-enabled class is not applied without the "responsive" property set to TRUE.');
202 * Tests 'priority-medium' and 'priority-low' classes.
204 public function testThemeTableResponsivePriority() {
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.
216 '#header' => $header,
218 '#responsive' => TRUE,
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.');
230 * Tests header elements with a mix of string and render array values.
232 public function testThemeTableHeaderRenderArray() {
242 '#type' => 'html_tag',
248 $rows = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
251 '#header' => $header,
253 '#responsive' => FALSE,
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.');
261 * Tests row elements with a mix of string and render array values.
263 public function testThemeTableRowRenderArray() {
264 $header = ['one', 'two', 'three'];
276 '#markup' => '2-one',
282 '#type' => 'html_tag',
284 '#value' => '2-three',
291 '#header' => $header,
293 '#responsive' => FALSE,
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.');
302 * Tests that the select/checkbox label is being generated and escaped.
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>');