a709d1f41382dee4bbd62389a7bc8ea3457e3865
[yaffs-website] / web / core / modules / views_ui / src / Tests / DefaultViewsTest.php
1 <?php
2
3 namespace Drupal\views_ui\Tests;
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
24   protected function setUp() {
25     parent::setUp();
26
27     $this->drupalPlaceBlock('page_title_block');
28   }
29
30   /**
31    * Tests default views.
32    */
33   public function testDefaultViews() {
34     // Make sure the view starts off as disabled (does not appear on the listing
35     // page).
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);
41
42     // Enable the view, and make sure it is now visible on the main listing
43     // page.
44     $this->drupalGet('admin/structure/views');
45     $this->clickViewsOperationLink(t('Enable'), '/glossary/');
46     $this->assertUrl('admin/structure/views');
47     $this->assertLinkByHref($edit_href);
48
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);
54
55     // Edit the view and change the title. Make sure that the new title is
56     // displayed.
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);
64
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'));
68
69     // Check there is an enable link. i.e. The view has not been enabled after
70     // editing.
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/');
75
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);
84
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');
88     $edit = [
89       'id' => 'duplicate_of_glossary',
90     ];
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.');
94
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.');
101
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
104     // listing page.
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);
116
117     // Clear permissions for anonymous users to check access for default views.
118     Role::load(RoleInterface::ANONYMOUS_ID)->revokePermission('access content')->save();
119
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);
126
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');
140
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'));
146
147     $this->drupalGet('glossary');
148     $this->assertResponse(200);
149
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');
157   }
158
159   /**
160    * Tests that enabling views moves them to the correct table.
161    */
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)]';
165
166     $arguments = [
167       ':status' => 'views-list-section enabled',
168       ':title' => 'test_view_status',
169     ];
170
171     $this->drupalGet('admin/structure/views');
172
173     $elements = $this->xpath($xpath, $arguments);
174     $this->assertIdentical(count($elements), 0, 'A disabled view is not found in the enabled views table.');
175
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.');
179
180     // Enable the view.
181     $this->clickViewsOperationLink(t('Enable'), '/test_view_status/');
182
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.');
185
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.');
189
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);
193   }
194
195   /**
196    * Tests that page displays show the correct path.
197    */
198   public function testPathDestination() {
199     $this->drupalGet('admin/structure/views');
200
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');
205
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());
209   }
210
211   /**
212    * Click a link to perform an operation on a view.
213    *
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.
217    *
218    * @param $label
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.
225    *
226    * @return
227    *   The page content that results from clicking on the link, or FALSE on
228    *   failure. Failure also results in a failed assertion.
229    */
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;
236         break;
237       }
238     }
239     $this->assertTrue(isset($index), format_string('Link to "@label" containing @part found.', ['@label' => $label, '@part' => $unique_href_part]));
240     if (isset($index)) {
241       return $this->clickLink($label, $index);
242     }
243     else {
244       return FALSE;
245     }
246   }
247
248 }