Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / views_ui / tests / src / Functional / DisplayCRUDTest.php
1 <?php
2
3 namespace Drupal\Tests\views_ui\Functional;
4
5 use Drupal\views\Views;
6
7 /**
8  * Tests creation, retrieval, updating, and deletion of displays in the Web UI.
9  *
10  * @group views_ui
11  */
12 class DisplayCRUDTest extends UITestBase {
13
14   /**
15    * Views used by this test.
16    *
17    * @var array
18    */
19   public static $testViews = ['test_display'];
20
21   /**
22    * Modules to enable
23    *
24    * @var array
25    */
26   public static $modules = ['contextual'];
27
28   /**
29    * Tests adding a display.
30    */
31   public function testAddDisplay() {
32     // Show the master display.
33     $this->config('views.settings')->set('ui.show.master_display', TRUE)->save();
34
35     $settings['page[create]'] = FALSE;
36     $view = $this->randomView($settings);
37
38     $path_prefix = 'admin/structure/views/view/' . $view['id'] . '/edit';
39     $this->drupalGet($path_prefix);
40
41     // Add a new display.
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');
44
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.');
47
48     $this->drupalPostForm("admin/structure/views/nojs/display/{$view['id']}/page_1/path", ['path' => 'test/path'], t('Apply'));
49     $this->drupalPostForm(NULL, [], t('Save'));
50   }
51
52   /**
53    * Tests removing a display.
54    */
55   public function testRemoveDisplay() {
56     $view = $this->randomView();
57     $path_prefix = 'admin/structure/views/view/' . $view['id'] . '/edit';
58
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.');
61
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.');
64
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.');
70
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.');
73
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.');
78
79     // Now delete again and save the view.
80     $this->drupalPostForm($path_prefix . '/page_1', [], 'Delete Page');
81     $this->drupalPostForm(NULL, [], t('Save'));
82
83     $this->assertNoLinkByHref($path_prefix . '/page_1', 'Make sure there is no display tab for the deleted display.');
84
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.');
94   }
95
96   /**
97    * Tests that the correct display is loaded by default.
98    */
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.');
103   }
104
105   /**
106    * Tests the duplicating of a display.
107    */
108   public function testDuplicateDisplay() {
109     $view = $this->randomView();
110     $path_prefix = 'admin/structure/views/view/' . $view['id'] . '/edit';
111     $path = $view['page[path]'];
112
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.');
117
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'));
123
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'));
130
131     $this->drupalPostForm(NULL, [], t('Save'));
132     $view = Views::getView($view['id']);
133     $view->initDisplay();
134
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');
146   }
147
148 }