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