3bbe6bf97c3c965c87dc434f9bd87ad39ff8ca9d
[yaffs-website] / web / core / modules / views / tests / src / Functional / Plugin / StyleTableTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Functional\Plugin;
4
5 use Drupal\Tests\views\Functional\ViewTestBase;
6 use Drupal\views\Entity\View;
7
8 /**
9  * Tests the table style views plugin.
10  *
11  * @group views
12  */
13 class StyleTableTest extends ViewTestBase {
14
15   /**
16    * Views used by this test.
17    *
18    * @var array
19    */
20   public static $testViews = ['test_table'];
21
22   /**
23    * {@inheritdoc}
24    */
25   protected function setUp($import_test_views = TRUE) {
26     parent::setUp($import_test_views);
27
28     $this->enableViewsTestModule();
29   }
30
31   /**
32    * Test table caption/summary/description.
33    */
34   public function testAccessibilitySettings() {
35     $this->drupalGet('test-table');
36
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');
40
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');
44
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');
48
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'] = '';
53     $view->save();
54
55     $this->drupalGet('test-table');
56     $result = $this->xpath('//caption/child::text()');
57     $this->assertFalse(trim($result[0]->getText()), 'Ensure that the caption disappears.');
58
59     // Remove the table summary.
60     $display = &$view->getDisplay('default');
61     $display['display_options']['style']['options']['summary'] = '';
62     $view->save();
63
64     $this->drupalGet('test-table');
65     $result = $this->xpath('//summary/child::text()');
66     $this->assertFalse(count($result), 'Ensure that the summary disappears.');
67
68     // Remove the table description.
69     $display = &$view->getDisplay('default');
70     $display['display_options']['style']['options']['description'] = '';
71     $view->save();
72
73     $this->drupalGet('test-table');
74     $result = $this->xpath('//caption/details/child::text()');
75     $this->assertFalse(count($result), 'Ensure that the description disappears.');
76   }
77
78   /**
79    * Test table fields in columns.
80    */
81   public function testFieldInColumns() {
82     $this->drupalGet('test-table');
83
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');
92
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'] = ', ';
98     $view->save();
99
100     // Ensure that both columns are properly combined.
101     $this->drupalGet('test-table');
102
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');
105
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');
108   }
109
110   /**
111    * Test that a number with the value of "0" is displayed in the table.
112    */
113   public function testNumericFieldVisible() {
114     // Adds a new datapoint in the views_test_data table to have a person with
115     // an age of zero.
116     $data_set = $this->dataSet();
117     $query = db_insert('views_test_data')
118       ->fields(array_keys($data_set[0]));
119     $query->values([
120       'name' => 'James McCartney',
121       'age' => 0,
122       'job' => 'Baby',
123       'created' => gmmktime(6, 30, 10, 1, 1, 2000),
124       'status' => 1,
125     ]);
126     $query->execute();
127
128     $this->drupalGet('test-table');
129
130     $result = $this->xpath('//tbody/tr/td[contains(., "Baby")]');
131     $this->assertTrue(count($result), 'Ensure that the baby is found.');
132
133     $result = $this->xpath('//tbody/tr/td[text()=0]');
134     $this->assertTrue(count($result), 'Ensure that the baby\'s age is shown');
135   }
136
137   /**
138    * Test that empty columns are hidden when empty_column is set.
139    */
140   public function testEmptyColumn() {
141     // Empty the 'job' data.
142     \Drupal::database()->update('views_test_data')
143       ->fields(['job' => ''])
144       ->execute();
145
146     $this->drupalGet('test-table');
147
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.');
151
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.');
154   }
155
156   /**
157    * Tests grouping by a field.
158    */
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] = [
167       'field' => 'job',
168       'rendered' => TRUE,
169       'rendered_strip' => FALSE,
170     ];
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'] = '';
175     $view->save();
176
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',
181       'age' => 42,
182       'job' => $unsafe_markup,
183       'created' => gmmktime(0, 0, 0, 2, 15, 2001),
184       'status' => 1,
185     ];
186     $database = $this->container->get('database');
187     $database->insert('views_test_data')
188       ->fields(array_keys($unsafe_markup_data))
189       ->values($unsafe_markup_data)
190       ->execute();
191
192     $this->drupalGet('test-table');
193     $expected_captions = [
194       'Job: Speaker',
195       'Job: Songwriter',
196       'Job: Drummer',
197       'Job: Singer',
198       'Job: ' . $unsafe_markup,
199     ];
200
201     // Ensure that we don't find the caption containing unsafe markup.
202     $this->assertNoRaw($unsafe_markup, "Didn't find caption containing unsafe markup.");
203
204     // Ensure that all expected captions are found.
205     foreach ($expected_captions as $raw_caption) {
206       $this->assertEscaped($raw_caption);
207     }
208
209     $display = &$view->getDisplay('default');
210     // Remove the label from the grouping field.
211     $display['display_options']['fields']['job']['label'] = '';
212     $view->save();
213
214     $this->drupalGet('test-table');
215     $expected_captions = [
216       'Speaker',
217       'Songwriter',
218       'Drummer',
219       'Singer',
220       $unsafe_markup,
221     ];
222
223     // Ensure that we don't find the caption containing unsafe markup.
224     $this->assertNoRaw($unsafe_markup, "Didn't find caption containing unsafe markup.");
225
226     // Ensure that all expected captions are found.
227     foreach ($expected_captions as $raw_caption) {
228       $this->assertEscaped($raw_caption);
229     }
230   }
231
232 }