Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / views / tests / src / Functional / Plugin / StyleTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Functional\Plugin;
4
5 use Drupal\Tests\views\Functional\ViewTestBase;
6 use Drupal\views\Views;
7 use Drupal\views_test_data\Plugin\views\row\RowTest;
8 use Drupal\views\Plugin\views\row\Fields;
9 use Drupal\views\ResultRow;
10 use Drupal\views_test_data\Plugin\views\style\StyleTest as StyleTestPlugin;
11
12 /**
13  * Tests general style functionality.
14  *
15  * @group views
16  * @see \Drupal\views_test_data\Plugin\views\style\StyleTest.
17  */
18 class StyleTest extends ViewTestBase {
19
20   /**
21    * Views used by this test.
22    *
23    * @var array
24    */
25   public static $testViews = ['test_view'];
26
27   /**
28    * Stores the SimpleXML representation of the output.
29    *
30    * @var \SimpleXMLElement
31    */
32   protected $elements;
33
34   protected function setUp($import_test_views = TRUE) {
35     parent::setUp($import_test_views);
36
37     $this->enableViewsTestModule();
38   }
39
40   /**
41    * Tests the general rendering of styles.
42    */
43   public function testStyle() {
44     /** @var \Drupal\Core\Render\RendererInterface $renderer */
45     $renderer = $this->container->get('renderer');
46
47     // This run use the test row plugin and render with it.
48     $view = Views::getView('test_view');
49     $view->setDisplay();
50     $style = $view->display_handler->getOption('style');
51     $style['type'] = 'test_style';
52     $view->display_handler->setOption('style', $style);
53     $row = $view->display_handler->getOption('row');
54     $row['type'] = 'test_row';
55     $view->display_handler->setOption('row', $row);
56     $view->initDisplay();
57     $view->initStyle();
58     // Reinitialize the style as it supports row plugins now.
59     $view->style_plugin->init($view, $view->display_handler);
60     $this->assertTrue($view->rowPlugin instanceof RowTest, 'Make sure the right row plugin class is loaded.');
61
62     $random_text = $this->randomMachineName();
63     $view->rowPlugin->setOutput($random_text);
64
65     $output = $view->preview();
66     $output = $renderer->renderRoot($output);
67     $this->assertTrue(strpos($output, $random_text) !== FALSE, 'Make sure that the rendering of the row plugin appears in the output of the view.');
68
69     // Test without row plugin support.
70     $view = Views::getView('test_view');
71     $view->setDisplay();
72     $style = $view->display_handler->getOption('style');
73     $style['type'] = 'test_style';
74     $view->display_handler->setOption('style', $style);
75     $view->initDisplay();
76     $view->initStyle();
77     $view->style_plugin->setUsesRowPlugin(FALSE);
78     $this->assertTrue($view->style_plugin instanceof StyleTestPlugin, 'Make sure the right style plugin class is loaded.');
79     $this->assertTrue($view->rowPlugin instanceof Fields, 'Make sure that rowPlugin is now a fields instance.');
80
81     $random_text = $this->randomMachineName();
82     // Set some custom text to the output and make sure that this value is
83     // rendered.
84     $view->style_plugin->setOutput($random_text);
85     $output = $view->preview();
86     $output = $renderer->renderRoot($output);
87     $this->assertTrue(strpos($output, $random_text) !== FALSE, 'Make sure that the rendering of the style plugin appears in the output of the view.');
88   }
89
90   public function testGrouping() {
91     $this->_testGrouping(FALSE);
92     $this->_testGrouping(TRUE);
93   }
94
95   /**
96    * Tests the grouping features of styles.
97    */
98   public function _testGrouping($stripped = FALSE) {
99     $view = Views::getView('test_view');
100     $view->setDisplay();
101     // Setup grouping by the job and the age field.
102     $view->initStyle();
103     $view->style_plugin->options['grouping'] = [
104       ['field' => 'job'],
105       ['field' => 'age'],
106     ];
107
108     // Reduce the amount of items to make the test a bit easier.
109     // Set up the pager.
110     $view->displayHandlers->get('default')->overrideOption('pager', [
111       'type' => 'some',
112       'options' => ['items_per_page' => 3],
113     ]);
114
115     // Add the job and age field.
116     $fields = [
117       'name' => [
118         'id' => 'name',
119         'table' => 'views_test_data',
120         'field' => 'name',
121         'relationship' => 'none',
122         'label' => 'Name',
123       ],
124       'job' => [
125         'id' => 'job',
126         'table' => 'views_test_data',
127         'field' => 'job',
128         'relationship' => 'none',
129         'label' => 'Job',
130       ],
131       'age' => [
132         'id' => 'age',
133         'table' => 'views_test_data',
134         'field' => 'age',
135         'relationship' => 'none',
136         'label' => 'Age',
137       ],
138     ];
139     $view->displayHandlers->get('default')->overrideOption('fields', $fields);
140
141     // Now run the query and groupby the result.
142     $this->executeView($view);
143
144     $expected = [];
145     $expected['Job: Singer'] = [];
146     $expected['Job: Singer']['group'] = 'Job: Singer';
147     $expected['Job: Singer']['level'] = 0;
148     $expected['Job: Singer']['rows']['Age: 25'] = [];
149     $expected['Job: Singer']['rows']['Age: 25']['group'] = 'Age: 25';
150     $expected['Job: Singer']['rows']['Age: 25']['level'] = 1;
151     $expected['Job: Singer']['rows']['Age: 25']['rows'][0] = new ResultRow(['index' => 0]);
152     $expected['Job: Singer']['rows']['Age: 25']['rows'][0]->views_test_data_name = 'John';
153     $expected['Job: Singer']['rows']['Age: 25']['rows'][0]->views_test_data_job = 'Singer';
154     $expected['Job: Singer']['rows']['Age: 25']['rows'][0]->views_test_data_age = '25';
155     $expected['Job: Singer']['rows']['Age: 25']['rows'][0]->views_test_data_id = '1';
156     $expected['Job: Singer']['rows']['Age: 27'] = [];
157     $expected['Job: Singer']['rows']['Age: 27']['group'] = 'Age: 27';
158     $expected['Job: Singer']['rows']['Age: 27']['level'] = 1;
159     $expected['Job: Singer']['rows']['Age: 27']['rows'][1] = new ResultRow(['index' => 1]);
160     $expected['Job: Singer']['rows']['Age: 27']['rows'][1]->views_test_data_name = 'George';
161     $expected['Job: Singer']['rows']['Age: 27']['rows'][1]->views_test_data_job = 'Singer';
162     $expected['Job: Singer']['rows']['Age: 27']['rows'][1]->views_test_data_age = '27';
163     $expected['Job: Singer']['rows']['Age: 27']['rows'][1]->views_test_data_id = '2';
164     $expected['Job: Drummer'] = [];
165     $expected['Job: Drummer']['group'] = 'Job: Drummer';
166     $expected['Job: Drummer']['level'] = 0;
167     $expected['Job: Drummer']['rows']['Age: 28'] = [];
168     $expected['Job: Drummer']['rows']['Age: 28']['group'] = 'Age: 28';
169     $expected['Job: Drummer']['rows']['Age: 28']['level'] = 1;
170     $expected['Job: Drummer']['rows']['Age: 28']['rows'][2] = new ResultRow(['index' => 2]);
171     $expected['Job: Drummer']['rows']['Age: 28']['rows'][2]->views_test_data_name = 'Ringo';
172     $expected['Job: Drummer']['rows']['Age: 28']['rows'][2]->views_test_data_job = 'Drummer';
173     $expected['Job: Drummer']['rows']['Age: 28']['rows'][2]->views_test_data_age = '28';
174     $expected['Job: Drummer']['rows']['Age: 28']['rows'][2]->views_test_data_id = '3';
175
176     // Alter the results to support the stripped case.
177     if ($stripped) {
178
179       // Add some html to the result and expected value.
180       $rand1 = '<a data="' . $this->randomMachineName() . '" />';
181       $view->result[0]->views_test_data_job .= $rand1;
182       $expected['Job: Singer']['rows']['Age: 25']['rows'][0]->views_test_data_job = 'Singer' . $rand1;
183       $expected['Job: Singer']['group'] = 'Job: Singer';
184       $rand2 = '<a data="' . $this->randomMachineName() . '" />';
185       $view->result[1]->views_test_data_job .= $rand2;
186       $expected['Job: Singer']['rows']['Age: 27']['rows'][1]->views_test_data_job = 'Singer' . $rand2;
187       $rand3 = '<a data="' . $this->randomMachineName() . '" />';
188       $view->result[2]->views_test_data_job .= $rand3;
189       $expected['Job: Drummer']['rows']['Age: 28']['rows'][2]->views_test_data_job = 'Drummer' . $rand3;
190       $expected['Job: Drummer']['group'] = 'Job: Drummer';
191
192       $view->style_plugin->options['grouping'][0] = ['field' => 'job', 'rendered' => TRUE, 'rendered_strip' => TRUE];
193       $view->style_plugin->options['grouping'][1] = ['field' => 'age', 'rendered' => TRUE, 'rendered_strip' => TRUE];
194     }
195
196     // The newer api passes the value of the grouping as well.
197     $sets_new_rendered = $view->style_plugin->renderGrouping($view->result, $view->style_plugin->options['grouping'], TRUE);
198
199     $this->assertEqual($sets_new_rendered, $expected, 'The style plugins should properly group the results with grouping by the rendered output.');
200
201     // Don't test stripped case, because the actual value is not stripped.
202     if (!$stripped) {
203       $sets_new_value = $view->style_plugin->renderGrouping($view->result, $view->style_plugin->options['grouping'], FALSE);
204
205       // Reorder the group structure to grouping by value.
206       $new_expected = $expected;
207       $new_expected['Singer'] = $expected['Job: Singer'];
208       $new_expected['Singer']['rows']['25'] = $expected['Job: Singer']['rows']['Age: 25'];
209       $new_expected['Singer']['rows']['27'] = $expected['Job: Singer']['rows']['Age: 27'];
210       $new_expected['Drummer'] = $expected['Job: Drummer'];
211       $new_expected['Drummer']['rows']['28'] = $expected['Job: Drummer']['rows']['Age: 28'];
212       unset($new_expected['Job: Singer']);
213       unset($new_expected['Singer']['rows']['Age: 25']);
214       unset($new_expected['Singer']['rows']['Age: 27']);
215       unset($new_expected['Job: Drummer']);
216       unset($new_expected['Drummer']['rows']['Age: 28']);
217
218       $this->assertEqual($sets_new_value, $new_expected, 'The style plugins should proper group the results with grouping by the value.');
219     }
220
221     // Test that grouping works on fields having no label.
222     $fields['job']['label'] = '';
223     $view->destroy();
224     $view->setDisplay();
225     $view->initStyle();
226     $view->displayHandlers->get('default')->overrideOption('fields', $fields);
227     $view->style_plugin->options['grouping'] = [
228       ['field' => 'job'],
229       ['field' => 'age'],
230     ];
231
232     $this->executeView($view);
233
234     if ($stripped) {
235       $view->result[0]->views_test_data_job .= $rand1;
236       $view->result[1]->views_test_data_job .= $rand2;
237       $view->result[2]->views_test_data_job .= $rand3;
238       $view->style_plugin->options['grouping'][0] = ['field' => 'job', 'rendered' => TRUE, 'rendered_strip' => TRUE];
239       $view->style_plugin->options['grouping'][1] = ['field' => 'age', 'rendered' => TRUE, 'rendered_strip' => TRUE];
240     }
241
242     $sets_new_rendered = $view->style_plugin->renderGrouping($view->result, $view->style_plugin->options['grouping'], TRUE);
243
244     // Remove labels from expected results.
245     foreach ($expected as $job => $data) {
246       unset($expected[$job]);
247       $job = str_replace('Job: ', '', $job);
248       $data['group'] = $job;
249       $expected[$job] = $data;
250     }
251     $this->assertEqual($expected, $sets_new_rendered);
252   }
253
254   /**
255    * Tests custom css classes.
256    */
257   public function testCustomRowClasses() {
258     $view = Views::getView('test_view');
259     $view->setDisplay();
260
261     // Setup some random css class.
262     $view->initStyle();
263     $random_name = $this->randomMachineName();
264     $view->style_plugin->options['row_class'] = $random_name . " test-token-{{ name }}";
265
266     $output = $view->preview();
267     $this->storeViewPreview(\Drupal::service('renderer')->renderRoot($output));
268
269     $rows = $this->elements->body->div->div->div;
270     $count = 0;
271     foreach ($rows as $row) {
272       $attributes = $row->attributes();
273       $class = (string) $attributes['class'][0];
274       $this->assertTrue(strpos($class, $random_name) !== FALSE, 'Make sure that a custom css class is added to the output.');
275
276       // Check token replacement.
277       $name = $view->field['name']->getValue($view->result[$count]);
278       $this->assertTrue(strpos($class, "test-token-$name") !== FALSE, 'Make sure that a token in custom css class is replaced.');
279
280       $count++;
281     }
282   }
283
284   /**
285    * Stores a view output in the elements.
286    */
287   protected function storeViewPreview($output) {
288     $htmlDom = new \DOMDocument();
289     @$htmlDom->loadHTML($output);
290     if ($htmlDom) {
291       // It's much easier to work with simplexml than DOM, luckily enough
292       // we can just simply import our DOM tree.
293       $this->elements = simplexml_import_dom($htmlDom);
294     }
295   }
296
297 }