Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / views_ui / tests / src / Functional / DisplayTest.php
1 <?php
2
3 namespace Drupal\Tests\views_ui\Functional;
4
5 use Drupal\Component\Utility\SafeMarkup;
6 use Drupal\views\Entity\View;
7 use Drupal\views\Views;
8
9 /**
10  * Tests the display UI.
11  *
12  * @group views_ui
13  */
14 class DisplayTest extends UITestBase {
15
16   /**
17    * Views used by this test.
18    *
19    * @var array
20    */
21   public static $testViews = ['test_display'];
22
23   /**
24    * Modules to enable
25    *
26    * @var array
27    */
28   public static $modules = ['contextual'];
29
30   /**
31    * Tests adding a display.
32    */
33   public function testAddDisplay() {
34     $view = $this->randomView();
35     $this->assertNoText('Block');
36     $this->assertNoText('Block 2');
37
38     $this->drupalPostForm(NULL, [], t('Add @display', ['@display' => 'Block']));
39     $this->assertText('Block');
40     $this->assertNoText('Block 2');
41   }
42
43   /**
44    * Tests reordering of displays.
45    */
46   public function testReorderDisplay() {
47     $view = [
48       'block[create]' => TRUE
49     ];
50     $view = $this->randomView($view);
51
52     $this->clickLink(t('Reorder displays'));
53     $this->assertTrue($this->xpath('//tr[@id="display-row-default"]'), 'Make sure the default display appears on the reorder listing');
54     $this->assertTrue($this->xpath('//tr[@id="display-row-page_1"]'), 'Make sure the page display appears on the reorder listing');
55     $this->assertTrue($this->xpath('//tr[@id="display-row-block_1"]'), 'Make sure the block display appears on the reorder listing');
56
57     // Ensure the view displays are in the expected order in configuration.
58     $expected_display_order = ['default', 'block_1', 'page_1'];
59     $this->assertEqual(array_keys(Views::getView($view['id'])->storage->get('display')), $expected_display_order, 'The correct display names are present.');
60     // Put the block display in front of the page display.
61     $edit = [
62       'displays[page_1][weight]' => 2,
63       'displays[block_1][weight]' => 1
64     ];
65     $this->drupalPostForm(NULL, $edit, t('Apply'));
66     $this->drupalPostForm(NULL, [], t('Save'));
67
68     $view = Views::getView($view['id']);
69     $displays = $view->storage->get('display');
70     $this->assertEqual($displays['default']['position'], 0, 'Make sure the master display comes first.');
71     $this->assertEqual($displays['block_1']['position'], 1, 'Make sure the block display comes before the page display.');
72     $this->assertEqual($displays['page_1']['position'], 2, 'Make sure the page display comes after the block display.');
73
74     // Ensure the view displays are in the expected order in configuration.
75     $this->assertEqual(array_keys($view->storage->get('display')), $expected_display_order, 'The correct display names are present.');
76   }
77
78   /**
79    * Tests disabling of a display.
80    */
81   public function testDisableDisplay() {
82     $view = $this->randomView();
83     $path_prefix = 'admin/structure/views/view/' . $view['id'] . '/edit';
84
85     $this->drupalGet($path_prefix);
86     $this->assertFalse($this->xpath('//div[contains(@class, :class)]', [':class' => 'views-display-disabled']), 'Make sure the disabled display css class does not appear after initial adding of a view.');
87
88     $this->assertFieldById('edit-displays-settings-settings-content-tab-content-details-top-actions-disable', NULL, 'Make sure the disable button is visible.');
89     $this->assertNoFieldById('edit-displays-settings-settings-content-tab-content-details-top-actions-enable', NULL, 'Make sure the enable button is not visible.');
90     $this->drupalPostForm(NULL, [], 'Disable Page');
91     $this->assertTrue($this->xpath('//div[contains(@class, :class)]', [':class' => 'views-display-disabled']), 'Make sure the disabled display css class appears once the display is marked as such.');
92
93     $this->assertNoFieldById('edit-displays-settings-settings-content-tab-content-details-top-actions-disable', NULL, 'Make sure the disable button is not visible.');
94     $this->assertFieldById('edit-displays-settings-settings-content-tab-content-details-top-actions-enable', NULL, 'Make sure the enable button is visible.');
95     $this->drupalPostForm(NULL, [], 'Enable Page');
96     $this->assertFalse($this->xpath('//div[contains(@class, :class)]', [':class' => 'views-display-disabled']), 'Make sure the disabled display css class does not appears once the display is enabled again.');
97   }
98
99   /**
100    * Tests views_ui_views_plugins_display_alter is altering plugin definitions.
101    */
102   public function testDisplayPluginsAlter() {
103     $definitions = Views::pluginManager('display')->getDefinitions();
104
105     $expected = [
106       'route_name' => 'entity.view.edit_form',
107       'route_parameters_names' => ['view' => 'id'],
108     ];
109
110     // Test the expected views_ui array exists on each definition.
111     foreach ($definitions as $definition) {
112       $this->assertIdentical($definition['contextual links']['entity.view.edit_form'], $expected, 'Expected views_ui array found in plugin definition.');
113     }
114   }
115
116   /**
117    * Tests display areas.
118    */
119   public function testDisplayAreas() {
120     // Show the advanced column.
121     $this->config('views.settings')->set('ui.show.advanced_column', TRUE)->save();
122
123     // Add a new data display to the view.
124     $view = Views::getView('test_display');
125     $view->storage->addDisplay('display_no_area_test');
126     $view->save();
127
128     $this->drupalGet('admin/structure/views/view/test_display/edit/display_no_area_test_1');
129
130     $areas = [
131       'header',
132       'footer',
133       'empty',
134     ];
135
136     // Assert that the expected text is found in each area category.
137     foreach ($areas as $type) {
138       $element = $this->xpath('//div[contains(@class, :class)]/div', [':class' => $type]);
139       $this->assertEqual($element[0]->getHtml(), SafeMarkup::format('The selected display type does not use @type plugins', ['@type' => $type]));
140     }
141   }
142
143   /**
144    * Tests the link-display setting.
145    */
146   public function testLinkDisplay() {
147     // Test setting the link display in the UI form.
148     $path = 'admin/structure/views/view/test_display/edit/block_1';
149     $link_display_path = 'admin/structure/views/nojs/display/test_display/block_1/link_display';
150
151     // Test the link text displays 'None' and not 'Block 1'
152     $this->drupalGet($path);
153     $result = $this->xpath("//a[contains(@href, :path)]", [':path' => $link_display_path]);
154     $this->assertEqual($result[0]->getHtml(), t('None'), 'Make sure that the link option summary shows "None" by default.');
155
156     $this->drupalGet($link_display_path);
157     $this->assertFieldChecked('edit-link-display-0');
158
159     // Test the default radio option on the link display form.
160     $this->drupalPostForm($link_display_path, ['link_display' => 'page_1'], t('Apply'));
161     // The form redirects to the master display.
162     $this->drupalGet($path);
163
164     $result = $this->xpath("//a[contains(@href, :path)]", [':path' => $link_display_path]);
165     $this->assertEqual($result[0]->getHtml(), 'Page', 'Make sure that the link option summary shows the right linked display.');
166
167     $this->drupalPostForm($link_display_path, ['link_display' => 'custom_url', 'link_url' => 'a-custom-url'], t('Apply'));
168     // The form redirects to the master display.
169     $this->drupalGet($path);
170
171     $this->assertLink(t('Custom URL'), 0, 'The link option has custom URL as summary.');
172
173     // Test the default link_url value for new display
174     $this->drupalPostForm(NULL, [], t('Add Block'));
175     $this->assertUrl('admin/structure/views/view/test_display/edit/block_2');
176     $this->clickLink(t('Custom URL'));
177     $this->assertFieldByName('link_url', 'a-custom-url');
178   }
179
180   /**
181    * Tests that the view status is correctly reflected on the edit form.
182    */
183   public function testViewStatus() {
184     $view = $this->randomView();
185     $id = $view['id'];
186
187     // The view should initially have the enabled class on it's form wrapper.
188     $this->drupalGet('admin/structure/views/view/' . $id);
189     $elements = $this->xpath('//div[contains(@class, :edit) and contains(@class, :status)]', [':edit' => 'views-edit-view', ':status' => 'enabled']);
190     $this->assertTrue($elements, 'The enabled class was found on the form wrapper');
191
192     $view = Views::getView($id);
193     $view->storage->disable()->save();
194
195     $this->drupalGet('admin/structure/views/view/' . $id);
196     $elements = $this->xpath('//div[contains(@class, :edit) and contains(@class, :status)]', [':edit' => 'views-edit-view', ':status' => 'disabled']);
197     $this->assertTrue($elements, 'The disabled class was found on the form wrapper.');
198   }
199
200   /**
201    * Ensures that no XSS is possible for buttons.
202    */
203   public function testDisplayTitleInButtonsXss() {
204     $xss_markup = '"><script>alert(123)</script>';
205     $view = $this->randomView();
206     $view = View::load($view['id']);
207     \Drupal::configFactory()->getEditable('views.settings')->set('ui.show.master_display', TRUE)->save();
208
209     foreach ([$xss_markup, '&quot;><script>alert(123)</script>'] as $input) {
210       $display =& $view->getDisplay('page_1');
211       $display['display_title'] = $input;
212       $view->save();
213
214       $this->drupalGet("admin/structure/views/view/{$view->id()}");
215       $escaped = views_ui_truncate($input, 25);
216       $this->assertEscaped($escaped);
217       $this->assertNoRaw($xss_markup);
218
219       $this->drupalGet("admin/structure/views/view/{$view->id()}/edit/page_1");
220       $this->assertEscaped("View $escaped");
221       $this->assertNoRaw("View $xss_markup");
222       $this->assertEscaped("Duplicate $escaped");
223       $this->assertNoRaw("Duplicate $xss_markup");
224       $this->assertEscaped("Delete $escaped");
225       $this->assertNoRaw("Delete $xss_markup");
226     }
227   }
228
229   /**
230    * Tests the action links on the edit display UI.
231    */
232   public function testActionLinks() {
233     // Change the display title of a display so it contains characters that will
234     // be escaped when rendered.
235     $display_title = "'<test>'";
236     $this->drupalGet('admin/structure/views/view/test_display');
237     $display_title_path = 'admin/structure/views/nojs/display/test_display/block_1/display_title';
238     $this->drupalPostForm($display_title_path, ['display_title' => $display_title], t('Apply'));
239
240     // Ensure that the title is escaped as expected.
241     $this->assertEscaped($display_title);
242     $this->assertNoRaw($display_title);
243
244     // Ensure that the dropdown buttons are displayed correctly.
245     $this->assertFieldByXpath('//input[@type="submit"]', 'Duplicate ' . $display_title);
246     $this->assertFieldByXpath('//input[@type="submit"]', 'Delete ' . $display_title);
247     $this->assertFieldByXpath('//input[@type="submit"]', 'Disable ' . $display_title);
248     $this->assertNoFieldByXpath('//input[@type="submit"]', 'Enable ' . $display_title);
249
250     // Disable the display so we can test the rendering of the "Enable" button.
251     $this->drupalPostForm(NULL, NULL, 'Disable ' . $display_title);
252     $this->assertFieldByXpath('//input[@type="submit"]', 'Enable ' . $display_title);
253     $this->assertNoFieldByXpath('//input[@type="submit"]', 'Disable ' . $display_title);
254
255     // Ensure that the title is escaped as expected.
256     $this->assertEscaped($display_title);
257     $this->assertNoRaw($display_title);
258   }
259
260   /**
261    * Tests that the override option is hidden when it's not needed.
262    */
263   public function testHideDisplayOverride() {
264     // Test that the override option appears with two displays.
265     $this->drupalGet('admin/structure/views/nojs/handler/test_display/page_1/field/title');
266     $this->assertText('All displays');
267
268     // Remove a display and test if the override option is hidden.
269     $this->drupalPostForm('admin/structure/views/view/test_display/edit/block_1', [], t('Delete @display', ['@display' => 'Block']));
270     $this->drupalPostForm(NULL, [], t('Save'));
271
272     $this->drupalGet('admin/structure/views/nojs/handler/test_display/page_1/field/title');
273     $this->assertNoText('All displays');
274
275     // Test that the override option is shown when display master is on.
276     \Drupal::configFactory()->getEditable('views.settings')->set('ui.show.master_display', TRUE)->save();
277     $this->drupalGet('admin/structure/views/nojs/handler/test_display/page_1/field/title');
278     $this->assertText('All displays');
279
280     // Test that the override option is shown if the current display is
281     // overridden so that the option to revert is available.
282     $this->drupalPostForm(NULL, ['override[dropdown]' => 'page_1'], t('Apply'));
283     \Drupal::configFactory()->getEditable('views.settings')->set('ui.show.master_display', FALSE)->save();
284     $this->drupalGet('admin/structure/views/nojs/handler/test_display/page_1/field/title');
285     $this->assertText('Revert to default');
286   }
287
288 }