3 namespace Drupal\Tests\views_ui\Functional;
5 use Drupal\views\Views;
8 * Tests creation, retrieval, updating, and deletion of displays in the Web UI.
12 class DisplayCRUDTest extends UITestBase {
15 * Views used by this test.
19 public static $testViews = ['test_display'];
26 public static $modules = ['contextual'];
29 * Tests adding a display.
31 public function testAddDisplay() {
32 // Show the master display.
33 $this->config('views.settings')->set('ui.show.master_display', TRUE)->save();
35 $settings['page[create]'] = FALSE;
36 $view = $this->randomView($settings);
38 $path_prefix = 'admin/structure/views/view/' . $view['id'] . '/edit';
39 $this->drupalGet($path_prefix);
42 $this->drupalPostForm(NULL, [], 'Add Page');
43 $this->assertLinkByHref($path_prefix . '/page_1', 0, 'Make sure after adding a display the new display appears in the UI');
45 $this->assertNoLink('Master*', 'Make sure the master display is not marked as changed.');
46 $this->assertLink('Page*', 0, 'Make sure the added display is marked as changed.');
48 $this->drupalPostForm("admin/structure/views/nojs/display/{$view['id']}/page_1/path", ['path' => 'test/path'], t('Apply'));
49 $this->drupalPostForm(NULL, [], t('Save'));
53 * Tests removing a display.
55 public function testRemoveDisplay() {
56 $view = $this->randomView();
57 $path_prefix = 'admin/structure/views/view/' . $view['id'] . '/edit';
59 $this->drupalGet($path_prefix . '/default');
60 $this->assertNoFieldById('edit-displays-settings-settings-content-tab-content-details-top-actions-delete', 'Delete Page', 'Make sure there is no delete button on the default display.');
62 $this->drupalGet($path_prefix . '/page_1');
63 $this->assertFieldById('edit-displays-settings-settings-content-tab-content-details-top-actions-delete', 'Delete Page', 'Make sure there is a delete button on the page display.');
65 // Delete the page, so we can test the undo process.
66 $this->drupalPostForm($path_prefix . '/page_1', [], 'Delete Page');
67 $this->assertFieldById('edit-displays-settings-settings-content-tab-content-details-top-actions-undo-delete', 'Undo delete of Page', 'Make sure there a undo button on the page display after deleting.');
68 $element = $this->xpath('//a[contains(@href, :href) and contains(@class, :class)]', [':href' => $path_prefix . '/page_1', ':class' => 'views-display-deleted-link']);
69 $this->assertTrue(!empty($element), 'Make sure the display link is marked as to be deleted.');
71 $element = $this->xpath('//a[contains(@href, :href) and contains(@class, :class)]', [':href' => $path_prefix . '/page_1', ':class' => 'views-display-deleted-link']);
72 $this->assertTrue(!empty($element), 'Make sure the display link is marked as to be deleted.');
74 // Undo the deleting of the display.
75 $this->drupalPostForm($path_prefix . '/page_1', [], 'Undo delete of Page');
76 $this->assertNoFieldById('edit-displays-settings-settings-content-tab-content-details-top-actions-undo-delete', 'Undo delete of Page', 'Make sure there is no undo button on the page display after reverting.');
77 $this->assertFieldById('edit-displays-settings-settings-content-tab-content-details-top-actions-delete', 'Delete Page', 'Make sure there is a delete button on the page display after the reverting.');
79 // Now delete again and save the view.
80 $this->drupalPostForm($path_prefix . '/page_1', [], 'Delete Page');
81 $this->drupalPostForm(NULL, [], t('Save'));
83 $this->assertNoLinkByHref($path_prefix . '/page_1', 'Make sure there is no display tab for the deleted display.');
85 // Test deleting a display that has a modified machine name.
86 $view = $this->randomView();
87 $machine_name = 'new_machine_name';
88 $path_prefix = 'admin/structure/views/view/' . $view['id'] . '/edit';
89 $this->drupalPostForm("admin/structure/views/nojs/display/{$view['id']}/page_1/display_id", ['display_id' => $machine_name], 'Apply');
90 $this->drupalPostForm(NULL, [], 'Delete Page');
91 $this->drupalPostForm(NULL, [], t('Save'));
92 $this->assertResponse(200);
93 $this->assertNoLinkByHref($path_prefix . '/new_machine_name', 'Make sure there is no display tab for the deleted display.');
97 * Tests that the correct display is loaded by default.
99 public function testDefaultDisplay() {
100 $this->drupalGet('admin/structure/views/view/test_display');
101 $elements = $this->xpath('//*[@id="views-page-1-display-title"]');
102 $this->assertEqual(count($elements), 1, 'The page display is loaded as the default display.');
106 * Tests the duplicating of a display.
108 public function testDuplicateDisplay() {
109 $view = $this->randomView();
110 $path_prefix = 'admin/structure/views/view/' . $view['id'] . '/edit';
111 $path = $view['page[path]'];
113 $this->drupalGet($path_prefix);
114 $this->drupalPostForm(NULL, [], 'Duplicate Page');
115 $this->assertLinkByHref($path_prefix . '/page_2', 0, 'Make sure after duplicating the new display appears in the UI');
116 $this->assertUrl($path_prefix . '/page_2', [], 'The user got redirected to the new display.');
118 // Set the title and override the css classes.
119 $random_title = $this->randomMachineName();
120 $random_css = $this->randomMachineName();
121 $this->drupalPostForm("admin/structure/views/nojs/display/{$view['id']}/page_2/title", ['title' => $random_title], t('Apply'));
122 $this->drupalPostForm("admin/structure/views/nojs/display/{$view['id']}/page_2/css_class", ['override[dropdown]' => 'page_2', 'css_class' => $random_css], t('Apply'));
124 // Duplicate as a different display type.
125 $this->drupalPostForm(NULL, [], 'Duplicate as Block');
126 $this->assertLinkByHref($path_prefix . '/block_1', 0, 'Make sure after duplicating the new display appears in the UI');
127 $this->assertUrl($path_prefix . '/block_1', [], 'The user got redirected to the new display.');
128 $this->assertText(t('Block settings'));
129 $this->assertNoText(t('Page settings'));
131 $this->drupalPostForm(NULL, [], t('Save'));
132 $view = Views::getView($view['id']);
133 $view->initDisplay();
135 $page_2 = $view->displayHandlers->get('page_2');
136 $this->assertTrue($page_2, 'The new page display got saved.');
137 $this->assertEqual($page_2->display['display_title'], 'Page');
138 $this->assertEqual($page_2->display['display_options']['path'], $path);
139 $block_1 = $view->displayHandlers->get('block_1');
140 $this->assertTrue($block_1, 'The new block display got saved.');
141 $this->assertEqual($block_1->display['display_plugin'], 'block');
142 $this->assertEqual($block_1->display['display_title'], 'Block', 'The new display title got generated as expected.');
143 $this->assertFalse(isset($block_1->display['display_options']['path']));
144 $this->assertEqual($block_1->getOption('title'), $random_title, 'The overridden title option from the display got copied into the duplicate');
145 $this->assertEqual($block_1->getOption('css_class'), $random_css, 'The overridden css_class option from the display got copied into the duplicate');
147 // Test duplicating a display after changing the machine name.
148 $view_id = $view->id();
149 $this->drupalPostForm("admin/structure/views/nojs/display/$view_id/page_2/display_id", ['display_id' => 'page_new'], 'Apply');
150 $this->drupalPostForm(NULL, [], 'Duplicate as Block');
151 $this->drupalPostForm(NULL, [], t('Save'));
152 $view = Views::getView($view_id);
153 $view->initDisplay();
154 $this->assertNotNull($view->displayHandlers->get('page_new'), 'The original display is saved with a changed id');
155 $this->assertNotNull($view->displayHandlers->get('block_2'), 'The duplicate display is saved with new id');