3 namespace Drupal\Tests\views_ui\Functional;
5 use Drupal\Component\Utility\SafeMarkup;
6 use Drupal\views\Entity\View;
7 use Drupal\views\Views;
10 * Tests the display UI.
14 class DisplayTest extends UITestBase {
17 * Views used by this test.
21 public static $testViews = ['test_display'];
28 public static $modules = ['contextual'];
31 * Tests adding a display.
33 public function testAddDisplay() {
34 $view = $this->randomView();
35 $this->assertNoText('Block');
36 $this->assertNoText('Block 2');
38 $this->drupalPostForm(NULL, [], t('Add @display', ['@display' => 'Block']));
39 $this->assertText('Block');
40 $this->assertNoText('Block 2');
44 * Tests reordering of displays.
46 public function testReorderDisplay() {
48 'block[create]' => TRUE
50 $view = $this->randomView($view);
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');
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.
62 'displays[page_1][weight]' => 2,
63 'displays[block_1][weight]' => 1
65 $this->drupalPostForm(NULL, $edit, t('Apply'));
66 $this->drupalPostForm(NULL, [], t('Save'));
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.');
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.');
79 * Tests disabling of a display.
81 public function testDisableDisplay() {
82 $view = $this->randomView();
83 $path_prefix = 'admin/structure/views/view/' . $view['id'] . '/edit';
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.');
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.');
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.');
100 * Tests views_ui_views_plugins_display_alter is altering plugin definitions.
102 public function testDisplayPluginsAlter() {
103 $definitions = Views::pluginManager('display')->getDefinitions();
106 'route_name' => 'entity.view.edit_form',
107 'route_parameters_names' => ['view' => 'id'],
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.');
117 * Tests display areas.
119 public function testDisplayAreas() {
120 // Show the advanced column.
121 $this->config('views.settings')->set('ui.show.advanced_column', TRUE)->save();
123 // Add a new data display to the view.
124 $view = Views::getView('test_display');
125 $view->storage->addDisplay('display_no_area_test');
128 $this->drupalGet('admin/structure/views/view/test_display/edit/display_no_area_test_1');
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]));
144 * Tests the link-display setting.
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';
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.');
156 $this->drupalGet($link_display_path);
157 $this->assertFieldChecked('edit-link-display-0');
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);
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.');
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);
171 $this->assertLink(t('Custom URL'), 0, 'The link option has custom URL as summary.');
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');
181 * Tests that the view status is correctly reflected on the edit form.
183 public function testViewStatus() {
184 $view = $this->randomView();
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');
192 $view = Views::getView($id);
193 $view->storage->disable()->save();
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.');
201 * Ensures that no XSS is possible for buttons.
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();
209 foreach ([$xss_markup, '"><script>alert(123)</script>'] as $input) {
210 $display =& $view->getDisplay('page_1');
211 $display['display_title'] = $input;
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);
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");
230 * Tests the action links on the edit display UI.
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'));
240 // Ensure that the title is escaped as expected.
241 $this->assertEscaped($display_title);
242 $this->assertNoRaw($display_title);
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);
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);
255 // Ensure that the title is escaped as expected.
256 $this->assertEscaped($display_title);
257 $this->assertNoRaw($display_title);
261 * Tests that the override option is hidden when it's not needed.
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');
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'));
272 $this->drupalGet('admin/structure/views/nojs/handler/test_display/page_1/field/title');
273 $this->assertNoText('All displays');
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');
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');