3 namespace Drupal\views_ui\Tests;
6 use Drupal\user\Entity\Role;
7 use Drupal\user\RoleInterface;
10 * Tests enabling, disabling, and reverting default views via the listing page.
14 class DefaultViewsTest extends UITestBase {
17 * Views used by this test.
21 public static $testViews = ['test_view_status', 'test_page_display_menu', 'test_page_display_arguments'];
24 protected function setUp() {
27 $this->drupalPlaceBlock('page_title_block');
31 * Tests default views.
33 public function testDefaultViews() {
34 // Make sure the view starts off as disabled (does not appear on the listing
36 $edit_href = 'admin/structure/views/view/glossary';
37 $this->drupalGet('admin/structure/views');
38 // @todo Disabled default views do now appear on the front page. Test this
39 // behavior with templates instead.
40 // $this->assertNoLinkByHref($edit_href);
42 // Enable the view, and make sure it is now visible on the main listing
44 $this->drupalGet('admin/structure/views');
45 $this->clickViewsOperationLink(t('Enable'), '/glossary/');
46 $this->assertUrl('admin/structure/views');
47 $this->assertLinkByHref($edit_href);
49 // It should not be possible to revert the view yet.
50 // @todo Figure out how to handle this with the new configuration system.
51 // $this->assertNoLink(t('Revert'));
52 // $revert_href = 'admin/structure/views/view/glossary/revert';
53 // $this->assertNoLinkByHref($revert_href);
55 // Edit the view and change the title. Make sure that the new title is
57 $new_title = $this->randomMachineName(16);
58 $edit = ['title' => $new_title];
59 $this->drupalPostForm('admin/structure/views/nojs/display/glossary/page_1/title', $edit, t('Apply'));
60 $this->drupalPostForm('admin/structure/views/view/glossary/edit/page_1', [], t('Save'));
61 $this->drupalGet('glossary');
62 $this->assertResponse(200);
63 $this->assertText($new_title);
65 // Save another view in the UI.
66 $this->drupalPostForm('admin/structure/views/nojs/display/archive/page_1/title', [], t('Apply'));
67 $this->drupalPostForm('admin/structure/views/view/archive/edit/page_1', [], t('Save'));
69 // Check there is an enable link. i.e. The view has not been enabled after
71 $this->drupalGet('admin/structure/views');
72 $this->assertLinkByHref('admin/structure/views/view/archive/enable');
73 // Enable it again so it can be tested for access permissions.
74 $this->clickViewsOperationLink(t('Enable'), '/archive/');
76 // It should now be possible to revert the view. Do that, and make sure the
77 // view title we added above no longer is displayed.
78 // $this->drupalGet('admin/structure/views');
79 // $this->assertLink(t('Revert'));
80 // $this->assertLinkByHref($revert_href);
81 // $this->drupalPostForm($revert_href, array(), t('Revert'));
82 // $this->drupalGet('glossary');
83 // $this->assertNoText($new_title);
85 // Duplicate the view and check that the normal schema of duplicated views is used.
86 $this->drupalGet('admin/structure/views');
87 $this->clickViewsOperationLink(t('Duplicate'), '/glossary');
89 'id' => 'duplicate_of_glossary',
91 $this->assertTitle(t('Duplicate of @label | @site-name', ['@label' => 'Glossary', '@site-name' => $this->config('system.site')->get('name')]));
92 $this->drupalPostForm(NULL, $edit, t('Duplicate'));
93 $this->assertUrl('admin/structure/views/view/duplicate_of_glossary', [], 'The normal duplicating name schema is applied.');
95 // Duplicate a view and set a custom name.
96 $this->drupalGet('admin/structure/views');
97 $this->clickViewsOperationLink(t('Duplicate'), '/glossary');
98 $random_name = strtolower($this->randomMachineName());
99 $this->drupalPostForm(NULL, ['id' => $random_name], t('Duplicate'));
100 $this->assertUrl("admin/structure/views/view/$random_name", [], 'The custom view name got saved.');
102 // Now disable the view, and make sure it stops appearing on the main view
103 // listing page but instead goes back to displaying on the disabled views
105 // @todo Test this behavior with templates instead.
106 $this->drupalGet('admin/structure/views');
107 $this->clickViewsOperationLink(t('Disable'), '/glossary/');
108 // $this->assertUrl('admin/structure/views');
109 // $this->assertNoLinkByHref($edit_href);
110 // The easiest way to verify it appears on the disabled views listing page
111 // is to try to click the "enable" link from there again.
112 $this->drupalGet('admin/structure/views');
113 $this->clickViewsOperationLink(t('Enable'), '/glossary/');
114 $this->assertUrl('admin/structure/views');
115 $this->assertLinkByHref($edit_href);
117 // Clear permissions for anonymous users to check access for default views.
118 Role::load(RoleInterface::ANONYMOUS_ID)->revokePermission('access content')->save();
120 // Test the default views disclose no data by default.
121 $this->drupalLogout();
122 $this->drupalGet('glossary');
123 $this->assertResponse(403);
124 $this->drupalGet('archive');
125 $this->assertResponse(403);
127 // Test deleting a view.
128 $this->drupalLogin($this->fullAdminUser);
129 $this->drupalGet('admin/structure/views');
130 $this->clickViewsOperationLink(t('Delete'), '/glossary/');
131 // Submit the confirmation form.
132 $this->drupalPostForm(NULL, [], t('Delete'));
133 // Ensure the view is no longer listed.
134 $this->assertUrl('admin/structure/views');
135 $this->assertNoLinkByHref($edit_href);
136 // Ensure the view is no longer available.
137 $this->drupalGet($edit_href);
138 $this->assertResponse(404);
139 $this->assertText('Page not found');
141 // Delete all duplicated Glossary views.
142 $this->drupalGet('admin/structure/views');
143 $this->clickViewsOperationLink(t('Delete'), 'duplicate_of_glossary');
144 // Submit the confirmation form.
145 $this->drupalPostForm(NULL, [], t('Delete'));
147 $this->drupalGet('glossary');
148 $this->assertResponse(200);
150 $this->drupalGet('admin/structure/views');
151 $this->clickViewsOperationLink(t('Delete'), $random_name);
152 // Submit the confirmation form.
153 $this->drupalPostForm(NULL, [], t('Delete'));
154 $this->drupalGet('glossary');
155 $this->assertResponse(404);
156 $this->assertText('Page not found');
160 * Tests that enabling views moves them to the correct table.
162 public function testSplitListing() {
163 // Build a re-usable xpath query.
164 $xpath = '//div[@id="views-entity-list"]/div[@class = :status]/table//td/text()[contains(., :title)]';
167 ':status' => 'views-list-section enabled',
168 ':title' => 'test_view_status',
171 $this->drupalGet('admin/structure/views');
173 $elements = $this->xpath($xpath, $arguments);
174 $this->assertIdentical(count($elements), 0, 'A disabled view is not found in the enabled views table.');
176 $arguments[':status'] = 'views-list-section disabled';
177 $elements = $this->xpath($xpath, $arguments);
178 $this->assertIdentical(count($elements), 1, 'A disabled view is found in the disabled views table.');
181 $this->clickViewsOperationLink(t('Enable'), '/test_view_status/');
183 $elements = $this->xpath($xpath, $arguments);
184 $this->assertIdentical(count($elements), 0, 'After enabling a view, it is not found in the disabled views table.');
186 $arguments[':status'] = 'views-list-section enabled';
187 $elements = $this->xpath($xpath, $arguments);
188 $this->assertIdentical(count($elements), 1, 'After enabling a view, it is found in the enabled views table.');
190 // Attempt to disable the view by path directly, with no token.
191 $this->drupalGet('admin/structure/views/view/test_view_status/disable');
192 $this->assertResponse(403);
196 * Tests that page displays show the correct path.
198 public function testPathDestination() {
199 $this->drupalGet('admin/structure/views');
201 // Check that links to views on default tabs are rendered correctly.
202 $this->assertLinkByHref('test_page_display_menu');
203 $this->assertNoLinkByHref('test_page_display_menu/default');
204 $this->assertLinkByHref('test_page_display_menu/local');
206 // Check that a dynamic path is shown as text.
207 $this->assertRaw('test_route_with_suffix/%/suffix');
208 $this->assertNoLinkByHref(Url::fromUri('base:test_route_with_suffix/%/suffix')->toString());
212 * Click a link to perform an operation on a view.
214 * In general, we expect lots of links titled "enable" or "disable" on the
215 * various views listing pages, and they might have tokens in them. So we
216 * need special code to find the correct one to click.
219 * Text between the anchor tags of the desired link.
220 * @param $unique_href_part
221 * A unique string that is expected to occur within the href of the desired
222 * link. For example, if the link URL is expected to look like
223 * "admin/structure/views/view/glossary/*", then "/glossary/" could be
224 * passed as the expected unique string.
227 * The page content that results from clicking on the link, or FALSE on
228 * failure. Failure also results in a failed assertion.
230 public function clickViewsOperationLink($label, $unique_href_part) {
231 $links = $this->xpath('//a[normalize-space(text())=:label]', [':label' => $label]);
232 foreach ($links as $link_index => $link) {
233 $position = strpos($link['href'], $unique_href_part);
234 if ($position !== FALSE) {
235 $index = $link_index;
239 $this->assertTrue(isset($index), format_string('Link to "@label" containing @part found.', ['@label' => $label, '@part' => $unique_href_part]));
241 return $this->clickLink($label, $index);