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