3 namespace Drupal\Tests\views\Functional\Plugin;
5 use Drupal\Tests\views\Functional\ViewTestBase;
6 use Drupal\views\Entity\View;
9 * Tests the table style views plugin.
13 class StyleTableTest extends ViewTestBase {
16 * Views used by this test.
20 public static $testViews = ['test_table'];
25 protected function setUp($import_test_views = TRUE) {
26 parent::setUp($import_test_views);
28 $this->enableViewsTestModule();
32 * Test table caption/summary/description.
34 public function testAccessibilitySettings() {
35 $this->drupalGet('test-table');
37 $result = $this->xpath('//caption/child::text()');
38 $this->assertTrue(count($result), 'The caption appears on the table.');
39 $this->assertEqual(trim($result[0]->getText()), 'caption-text');
41 $result = $this->xpath('//summary/child::text()');
42 $this->assertTrue(count($result), 'The summary appears on the table.');
43 $this->assertEqual(trim($result[0]->getText()), 'summary-text');
45 $result = $this->xpath('//caption/details/child::text()');
46 $this->assertTrue(count($result), 'The table description appears on the table.');
47 $this->assertEqual(trim($result[0]->getText()), 'description-text');
49 // Remove the caption and ensure the caption is not displayed anymore.
50 $view = View::load('test_table');
51 $display = &$view->getDisplay('default');
52 $display['display_options']['style']['options']['caption'] = '';
55 $this->drupalGet('test-table');
56 $result = $this->xpath('//caption/child::text()');
57 $this->assertFalse(trim($result[0]->getText()), 'Ensure that the caption disappears.');
59 // Remove the table summary.
60 $display = &$view->getDisplay('default');
61 $display['display_options']['style']['options']['summary'] = '';
64 $this->drupalGet('test-table');
65 $result = $this->xpath('//summary/child::text()');
66 $this->assertFalse(count($result), 'Ensure that the summary disappears.');
68 // Remove the table description.
69 $display = &$view->getDisplay('default');
70 $display['display_options']['style']['options']['description'] = '';
73 $this->drupalGet('test-table');
74 $result = $this->xpath('//caption/details/child::text()');
75 $this->assertFalse(count($result), 'Ensure that the description disappears.');
79 * Test table fields in columns.
81 public function testFieldInColumns() {
82 $this->drupalGet('test-table');
84 // Ensure that both columns are in separate tds.
85 // Check for class " views-field-job ", because just "views-field-job" won't
86 // do: "views-field-job-1" would also contain "views-field-job".
87 // @see Drupal\system\Tests\Form\ElementTest::testButtonClasses().
88 $result = $this->xpath('//tbody/tr/td[contains(concat(" ", @class, " "), " views-field-job ")]');
89 $this->assertTrue(count($result), 'Ensure there is a td with the class views-field-job');
90 $result = $this->xpath('//tbody/tr/td[contains(concat(" ", @class, " "), " views-field-job-1 ")]');
91 $this->assertTrue(count($result), 'Ensure there is a td with the class views-field-job-1');
93 // Combine the second job-column with the first one, with ', ' as separator.
94 $view = View::load('test_table');
95 $display = &$view->getDisplay('default');
96 $display['display_options']['style']['options']['columns']['job_1'] = 'job';
97 $display['display_options']['style']['options']['info']['job']['separator'] = ', ';
100 // Ensure that both columns are properly combined.
101 $this->drupalGet('test-table');
103 $result = $this->xpath('//tbody/tr/td[contains(concat(" ", @class, " "), " views-field-job views-field-job-1 ")]');
104 $this->assertTrue(count($result), 'Ensure that the job column class names are joined into a single column');
106 $result = $this->xpath('//tbody/tr/td[contains(., "Drummer, Drummer")]');
107 $this->assertTrue(count($result), 'Ensure the job column values are joined into a single column');
111 * Test that a number with the value of "0" is displayed in the table.
113 public function testNumericFieldVisible() {
114 // Adds a new datapoint in the views_test_data table to have a person with
116 $data_set = $this->dataSet();
117 $query = db_insert('views_test_data')
118 ->fields(array_keys($data_set[0]));
120 'name' => 'James McCartney',
123 'created' => gmmktime(6, 30, 10, 1, 1, 2000),
128 $this->drupalGet('test-table');
130 $result = $this->xpath('//tbody/tr/td[contains(., "Baby")]');
131 $this->assertTrue(count($result), 'Ensure that the baby is found.');
133 $result = $this->xpath('//tbody/tr/td[text()=0]');
134 $this->assertTrue(count($result), 'Ensure that the baby\'s age is shown');
138 * Test that empty columns are hidden when empty_column is set.
140 public function testEmptyColumn() {
141 // Empty the 'job' data.
142 \Drupal::database()->update('views_test_data')
143 ->fields(['job' => ''])
146 $this->drupalGet('test-table');
148 // Test that only one of the job columns still shows.
149 $result = $this->xpath('//thead/tr/th/a[text()="Job"]');
150 $this->assertEqual(count($result), 1, 'Ensure that empty column header is hidden.');
152 $result = $this->xpath('//tbody/tr/td[contains(concat(" ", @class, " "), " views-field-job-1 ")]');
153 $this->assertEqual(count($result), 0, 'Ensure the empty table cells are hidden.');
157 * Tests grouping by a field.
159 public function testGrouping() {
160 /** @var \Drupal\views\ViewEntityInterface $view */
161 $view = \Drupal::entityTypeManager()->getStorage('view')->load('test_table');
162 // Get a reference to the display configuration so we can alter some
163 // specific style options.
164 $display = &$view->getDisplay('default');
165 // Set job as the grouping field.
166 $display['display_options']['style']['options']['grouping'][0] = [
169 'rendered_strip' => FALSE,
171 // Clear the caption text, the rendered job field will be used as a caption.
172 $display['display_options']['style']['options']['caption'] = '';
173 $display['display_options']['style']['options']['summary'] = '';
174 $display['display_options']['style']['options']['description'] = '';
177 // Add a record containing unsafe markup to be sure it's filtered out.
178 $unsafe_markup = '<script>alert("Rapper");</script>';
179 $unsafe_markup_data = [
180 'name' => 'Marshall',
182 'job' => $unsafe_markup,
183 'created' => gmmktime(0, 0, 0, 2, 15, 2001),
186 $database = $this->container->get('database');
187 $database->insert('views_test_data')
188 ->fields(array_keys($unsafe_markup_data))
189 ->values($unsafe_markup_data)
192 $this->drupalGet('test-table');
193 $expected_captions = [
198 'Job: ' . $unsafe_markup,
201 // Ensure that we don't find the caption containing unsafe markup.
202 $this->assertNoRaw($unsafe_markup, "Didn't find caption containing unsafe markup.");
204 // Ensure that all expected captions are found.
205 foreach ($expected_captions as $raw_caption) {
206 $this->assertEscaped($raw_caption);
209 $display = &$view->getDisplay('default');
210 // Remove the label from the grouping field.
211 $display['display_options']['fields']['job']['label'] = '';
214 $this->drupalGet('test-table');
215 $expected_captions = [
223 // Ensure that we don't find the caption containing unsafe markup.
224 $this->assertNoRaw($unsafe_markup, "Didn't find caption containing unsafe markup.");
226 // Ensure that all expected captions are found.
227 foreach ($expected_captions as $raw_caption) {
228 $this->assertEscaped($raw_caption);