e39f652e55e0d4ea3b28df9628067395c1bb0bbb
[yaffs-website] / web / core / modules / shortcut / tests / src / Functional / ShortcutLinksTest.php
1 <?php
2
3 namespace Drupal\Tests\shortcut\Functional;
4
5 use Drupal\block_content\Entity\BlockContentType;
6 use Drupal\Component\Utility\SafeMarkup;
7 use Drupal\Component\Render\FormattableMarkup;
8 use Drupal\Core\Url;
9 use Drupal\shortcut\Entity\Shortcut;
10 use Drupal\shortcut\Entity\ShortcutSet;
11 use Drupal\Tests\block\Functional\AssertBlockAppearsTrait;
12 use Drupal\views\Entity\View;
13
14 /**
15  * Create, view, edit, delete, and change shortcut links.
16  *
17  * @group shortcut
18  */
19 class ShortcutLinksTest extends ShortcutTestBase {
20
21   use AssertBlockAppearsTrait;
22
23   /**
24    * Modules to enable.
25    *
26    * @var array
27    */
28   public static $modules = ['router_test', 'views', 'block'];
29
30   /**
31    * {@inheritdoc}
32    */
33   protected function setUp() {
34     parent::setUp();
35
36     $this->drupalPlaceBlock('page_title_block');
37   }
38
39   /**
40    * Tests that creating a shortcut works properly.
41    */
42   public function testShortcutLinkAdd() {
43     $set = $this->set;
44
45     // Create an alias for the node so we can test aliases.
46     $path = [
47       'source' => '/node/' . $this->node->id(),
48       'alias' => '/' . $this->randomMachineName(8),
49     ];
50     $this->container->get('path.alias_storage')->save($path['source'], $path['alias']);
51
52     // Create some paths to test.
53     $test_cases = [
54       '/',
55       '/admin',
56       '/admin/config/system/site-information',
57       '/node/' . $this->node->id() . '/edit',
58       $path['alias'],
59       '/router_test/test2',
60       '/router_test/test3/value',
61     ];
62
63     $test_cases_non_access = [
64       '/admin',
65       '/admin/config/system/site-information',
66     ];
67
68     // Check that each new shortcut links where it should.
69     foreach ($test_cases as $test_path) {
70       $title = $this->randomMachineName();
71       $form_data = [
72         'title[0][value]' => $title,
73         'link[0][uri]' => $test_path,
74       ];
75       $this->drupalPostForm('admin/config/user-interface/shortcut/manage/' . $set->id() . '/add-link', $form_data, t('Save'));
76       $this->assertResponse(200);
77       $this->assertText(t('Added a shortcut for @title.', ['@title' => $title]));
78       $saved_set = ShortcutSet::load($set->id());
79       $paths = $this->getShortcutInformation($saved_set, 'link');
80       $this->assertTrue(in_array('internal:' . $test_path, $paths), 'Shortcut created: ' . $test_path);
81
82       if (in_array($test_path, $test_cases_non_access)) {
83         $this->assertNoLink($title, SafeMarkup::format('Shortcut link %url not accessible on the page.', ['%url' => $test_path]));
84       }
85       else {
86         $this->assertLink($title, 0, SafeMarkup::format('Shortcut link %url found on the page.', ['%url' => $test_path]));
87       }
88     }
89     $saved_set = ShortcutSet::load($set->id());
90     // Test that saving and re-loading a shortcut preserves its values.
91     $shortcuts = $saved_set->getShortcuts();
92     foreach ($shortcuts as $entity) {
93       // Test the node routes with parameters.
94       $entity->save();
95       $loaded = Shortcut::load($entity->id());
96       $this->assertEqual($entity->link->uri, $loaded->link->uri);
97       $this->assertEqual($entity->link->options, $loaded->link->options);
98     }
99
100     // Log in as non admin user, to check that access is checked when creating
101     // shortcuts.
102     $this->drupalLogin($this->shortcutUser);
103     $title = $this->randomMachineName();
104     $form_data = [
105       'title[0][value]' => $title,
106       'link[0][uri]' => '/admin',
107     ];
108     $this->drupalPostForm('admin/config/user-interface/shortcut/manage/' . $set->id() . '/add-link', $form_data, t('Save'));
109     $this->assertResponse(200);
110     $this->assertRaw(t("The path '@link_path' is inaccessible.", ['@link_path' => '/admin']));
111
112     $form_data = [
113       'title[0][value]' => $title,
114       'link[0][uri]' => '/node',
115     ];
116     $this->drupalPostForm('admin/config/user-interface/shortcut/manage/' . $set->id() . '/add-link', $form_data, t('Save'));
117     $this->assertLink($title, 0, 'Shortcut link found on the page.');
118
119     // Create a new shortcut set and add a link to it.
120     $this->drupalLogin($this->adminUser);
121     $edit = [
122       'label' => $this->randomMachineName(),
123       'id' => strtolower($this->randomMachineName()),
124     ];
125     $this->drupalPostForm('admin/config/user-interface/shortcut/add-set', $edit, t('Save'));
126     $title = $this->randomMachineName();
127     $form_data = [
128       'title[0][value]' => $title,
129       'link[0][uri]' => '/admin',
130     ];
131     $this->drupalPostForm('admin/config/user-interface/shortcut/manage/' . $edit['id'] . '/add-link', $form_data, t('Save'));
132     $this->assertResponse(200);
133   }
134
135   /**
136    * Tests that the "add to shortcut" and "remove from shortcut" links work.
137    */
138   public function testShortcutQuickLink() {
139     \Drupal::service('theme_handler')->install(['seven']);
140     $this->config('system.theme')->set('admin', 'seven')->save();
141     $this->config('node.settings')->set('use_admin_theme', '1')->save();
142     $this->container->get('router.builder')->rebuild();
143
144     $this->drupalLogin($this->rootUser);
145     $this->drupalGet('admin/config/system/cron');
146
147     // Test the "Add to shortcuts" link.
148     $this->clickLink('Add to Default shortcuts');
149     $this->assertText('Added a shortcut for Cron.');
150     $this->assertLink('Cron', 0, 'Shortcut link found on page');
151
152     $this->drupalGet('admin/structure');
153     $this->assertLink('Cron', 0, 'Shortcut link found on different page');
154
155     // Test the "Remove from shortcuts" link.
156     $this->clickLink('Cron');
157     $this->clickLink('Remove from Default shortcuts');
158     $this->assertText('The shortcut Cron has been deleted.');
159     $this->assertNoLink('Cron', 'Shortcut link removed from page');
160
161     $this->drupalGet('admin/structure');
162     $this->assertNoLink('Cron', 'Shortcut link removed from different page');
163
164     $this->drupalGet('admin/people');
165
166     // Test the "Add to shortcuts" link for a page generated by views.
167     $this->clickLink('Add to Default shortcuts');
168     $this->assertText('Added a shortcut for People.');
169     $this->assertShortcutQuickLink('Remove from Default shortcuts');
170
171     // Test the "Remove from  shortcuts" link for a page generated by views.
172     $this->clickLink('Remove from Default shortcuts');
173     $this->assertText('The shortcut People has been deleted.');
174     $this->assertShortcutQuickLink('Add to Default shortcuts');
175
176     // Test two pages which use same route name but different route parameters.
177     $this->drupalGet('node/add/page');
178     // Add Shortcut for Basic Page.
179     $this->clickLink('Add to Default shortcuts');
180     $this->assertText('Added a shortcut for Create Basic page.');
181     // Assure that Article does not have its shortcut indicated as set.
182     $this->drupalGet('node/add/article');
183     $link = $this->xpath('//a[normalize-space()=:label]', [':label' => 'Remove from Default shortcuts']);
184     $this->assertTrue(empty($link), 'Link Remove to Default shortcuts not found for Create Article page.');
185     // Add Shortcut for Article.
186     $this->clickLink('Add to Default shortcuts');
187     $this->assertText('Added a shortcut for Create Article.');
188
189     $this->config('system.theme')->set('default', 'seven')->save();
190     $this->drupalGet('node/' . $this->node->id());
191     $title = $this->node->getTitle();
192
193     // Test the "Add to shortcuts" link for node view route.
194     $this->clickLink('Add to Default shortcuts');
195     $this->assertText(new FormattableMarkup('Added a shortcut for @title.', ['@title' => $title]));
196     $this->assertShortcutQuickLink('Remove from Default shortcuts');
197
198     // Test the "Remove from shortcuts" link for node view route.
199     $this->clickLink('Remove from Default shortcuts');
200     $this->assertText(new FormattableMarkup('The shortcut @title has been deleted.', ['@title' => $title]));
201     $this->assertShortcutQuickLink('Add to Default shortcuts');
202
203     \Drupal::service('module_installer')->install(['block_content']);
204     BlockContentType::create([
205       'id' => 'basic',
206       'label' => 'Basic block',
207       'revision' => FALSE,
208     ])->save();
209     // Test page with HTML tags in title.
210     $this->drupalGet('admin/structure/block/block-content/manage/basic');
211     $page_title = new FormattableMarkup('Edit %label custom block type', ['%label' => 'Basic block']);
212     $this->assertRaw($page_title);
213     // Add shortcut to this page.
214     $this->clickLink('Add to Default shortcuts');
215     $this->assertRaw(new FormattableMarkup('Added a shortcut for %title.', [
216       '%title' => trim(strip_tags($page_title)),
217     ]));
218
219   }
220
221   /**
222    * Tests that shortcut links can be renamed.
223    */
224   public function testShortcutLinkRename() {
225     $set = $this->set;
226
227     // Attempt to rename shortcut link.
228     $new_link_name = $this->randomMachineName();
229
230     $shortcuts = $set->getShortcuts();
231     $shortcut = reset($shortcuts);
232     $this->drupalPostForm('admin/config/user-interface/shortcut/link/' . $shortcut->id(), ['title[0][value]' => $new_link_name], t('Save'));
233     $saved_set = ShortcutSet::load($set->id());
234     $titles = $this->getShortcutInformation($saved_set, 'title');
235     $this->assertTrue(in_array($new_link_name, $titles), 'Shortcut renamed: ' . $new_link_name);
236     $this->assertLink($new_link_name, 0, 'Renamed shortcut link appears on the page.');
237     $this->assertText(t('The shortcut @link has been updated.', ['@link' => $new_link_name]));
238   }
239
240   /**
241    * Tests that changing the path of a shortcut link works.
242    */
243   public function testShortcutLinkChangePath() {
244     $set = $this->set;
245
246     // Tests changing a shortcut path.
247     $new_link_path = '/admin/config';
248
249     $shortcuts = $set->getShortcuts();
250     $shortcut = reset($shortcuts);
251     $this->drupalPostForm('admin/config/user-interface/shortcut/link/' . $shortcut->id(), ['title[0][value]' => $shortcut->getTitle(), 'link[0][uri]' => $new_link_path], t('Save'));
252     $saved_set = ShortcutSet::load($set->id());
253     $paths = $this->getShortcutInformation($saved_set, 'link');
254     $this->assertTrue(in_array('internal:' . $new_link_path, $paths), 'Shortcut path changed: ' . $new_link_path);
255     $this->assertLinkByHref($new_link_path, 0, 'Shortcut with new path appears on the page.');
256     $this->assertText(t('The shortcut @link has been updated.', ['@link' => $shortcut->getTitle()]));
257   }
258
259   /**
260    * Tests that changing the route of a shortcut link works.
261    */
262   public function testShortcutLinkChangeRoute() {
263     $this->drupalLogin($this->rootUser);
264     $this->drupalGet('admin/content');
265     $this->assertResponse(200);
266     // Disable the view.
267     View::load('content')->disable()->save();
268     /** @var \Drupal\Core\Routing\RouteBuilderInterface $router_builder */
269     $router_builder = \Drupal::service('router.builder');
270     $router_builder->rebuildIfNeeded();
271     $this->drupalGet('admin/content');
272     $this->assertResponse(200);
273   }
274
275   /**
276    * Tests deleting a shortcut link.
277    */
278   public function testShortcutLinkDelete() {
279     $set = $this->set;
280
281     $shortcuts = $set->getShortcuts();
282     $shortcut = reset($shortcuts);
283     $this->drupalPostForm('admin/config/user-interface/shortcut/link/' . $shortcut->id() . '/delete', [], 'Delete');
284     $saved_set = ShortcutSet::load($set->id());
285     $ids = $this->getShortcutInformation($saved_set, 'id');
286     $this->assertFalse(in_array($shortcut->id(), $ids), 'Successfully deleted a shortcut.');
287
288     // Delete all the remaining shortcut links.
289     entity_delete_multiple('shortcut', array_filter($ids));
290
291     // Get the front page to check that no exceptions occur.
292     $this->drupalGet('');
293   }
294
295   /**
296    * Tests that the add shortcut link is not displayed for 404/403 errors.
297    *
298    * Tests that the "Add to shortcuts" link is not displayed on a page not
299    * found or a page the user does not have access to.
300    */
301   public function testNoShortcutLink() {
302     // Change to a theme that displays shortcuts.
303     \Drupal::service('theme_handler')->install(['seven']);
304     $this->config('system.theme')
305       ->set('default', 'seven')
306       ->save();
307
308     $this->drupalGet('page-that-does-not-exist');
309     $result = $this->xpath('//a[contains(@class, "shortcut-action--add")]');
310     $this->assertTrue(empty($result), 'Add to shortcuts link was not shown on a page not found.');
311
312     // The user does not have access to this path.
313     $this->drupalGet('admin/modules');
314     $result = $this->xpath('//a[contains(@class, "shortcut-action--add")]');
315     $this->assertTrue(empty($result), 'Add to shortcuts link was not shown on a page the user does not have access to.');
316
317     // Verify that the testing mechanism works by verifying the shortcut link
318     // appears on admin/content.
319     $this->drupalGet('admin/content');
320     $result = $this->xpath('//a[contains(@class, "shortcut-action--remove")]');
321     $this->assertTrue(!empty($result), 'Remove from shortcuts link was shown on a page the user does have access to.');
322
323     // Verify that the shortcut link appears on routing only pages.
324     $this->drupalGet('router_test/test2');
325     $result = $this->xpath('//a[contains(@class, "shortcut-action--add")]');
326     $this->assertTrue(!empty($result), 'Add to shortcuts link was shown on a page the user does have access to.');
327   }
328
329   /**
330    * Tests that the 'access shortcuts' permissions works properly.
331    */
332   public function testAccessShortcutsPermission() {
333     // Change to a theme that displays shortcuts.
334     \Drupal::service('theme_handler')->install(['seven']);
335     $this->config('system.theme')
336       ->set('default', 'seven')
337       ->save();
338
339     // Add cron to the default shortcut set.
340     $this->drupalLogin($this->rootUser);
341     $this->drupalGet('admin/config/system/cron');
342     $this->clickLink('Add to Default shortcuts');
343
344     // Verify that users without the 'access shortcuts' permission can't see the
345     // shortcuts.
346     $this->drupalLogin($this->drupalCreateUser(['access toolbar']));
347     $this->assertNoLink('Shortcuts', 'Shortcut link not found on page.');
348
349     // Verify that users without the 'administer site configuration' permission
350     // can't see the cron shortcuts.
351     $this->drupalLogin($this->drupalCreateUser(['access toolbar', 'access shortcuts']));
352     $this->assertNoLink('Shortcuts', 'Shortcut link not found on page.');
353     $this->assertNoLink('Cron', 'Cron shortcut link not found on page.');
354
355     // Verify that users with the 'access shortcuts' permission can see the
356     // shortcuts.
357     $this->drupalLogin($this->drupalCreateUser([
358       'access toolbar', 'access shortcuts', 'administer site configuration',
359     ]));
360     $this->clickLink('Shortcuts', 0, 'Shortcut link found on page.');
361     $this->assertLink('Cron', 0, 'Cron shortcut link found on page.');
362
363     $this->verifyAccessShortcutsPermissionForEditPages();
364   }
365
366   /**
367    * Tests the shortcuts are correctly ordered by weight in the toolbar.
368    */
369   public function testShortcutLinkOrder() {
370     // Ensure to give permissions to access the shortcuts.
371     $this->drupalLogin($this->drupalCreateUser(['access toolbar', 'access shortcuts', 'access content overview', 'administer content types']));
372     $this->drupalGet(Url::fromRoute('<front>'));
373     $shortcuts = $this->cssSelect('#toolbar-item-shortcuts-tray .toolbar-menu a');
374     $this->assertEqual($shortcuts[0]->getText(), 'Add content');
375     $this->assertEqual($shortcuts[1]->getText(), 'All content');
376     foreach ($this->set->getShortcuts() as $shortcut) {
377       $shortcut->setWeight($shortcut->getWeight() * -1)->save();
378     }
379     $this->drupalGet(Url::fromRoute('<front>'));
380     $shortcuts = $this->cssSelect('#toolbar-item-shortcuts-tray .toolbar-menu a');
381     $this->assertEqual($shortcuts[0]->getText(), 'All content');
382     $this->assertEqual($shortcuts[1]->getText(), 'Add content');
383   }
384
385   /**
386    * Tests that the 'access shortcuts' permission is required for shortcut set
387    * administration page access.
388    */
389   private function verifyAccessShortcutsPermissionForEditPages() {
390     // Create a user with customize links and switch sets permissions  but
391     // without the 'access shortcuts' permission.
392     $test_permissions = [
393       'customize shortcut links',
394       'switch shortcut sets',
395     ];
396     $noaccess_user = $this->drupalCreateUser($test_permissions);
397     $this->drupalLogin($noaccess_user);
398
399     // Verify that set administration pages are inaccessible without the
400     // 'access shortcuts' permission.
401     $edit_paths = [
402       'admin/config/user-interface/shortcut/manage/default/customize',
403       'admin/config/user-interface/shortcut/manage/default',
404       'user/' . $noaccess_user->id() . '/shortcuts',
405     ];
406
407     foreach ($edit_paths as $path) {
408       $this->drupalGet($path);
409       $message = format_string('Access is denied on %s', ['%s' => $path]);
410       $this->assertResponse(403, $message);
411     }
412   }
413
414   /**
415    * Tests that the 'access shortcuts' permission is required to access the
416    * shortcut block.
417    */
418   public function testShortcutBlockAccess() {
419     // Creates a block instance and place in a region through api.
420     $block = $this->drupalPlaceBlock('shortcuts');
421
422     // Verify that users with the 'access shortcuts' permission can see the
423     // shortcut block.
424     $this->drupalLogin($this->shortcutUser);
425     $this->drupalGet('');
426     $this->assertBlockAppears($block);
427
428     $this->drupalLogout();
429
430     // Verify that users without the 'access shortcuts' permission can see the
431     // shortcut block.
432     $this->drupalLogin($this->drupalCreateUser([]));
433     $this->drupalGet('');
434     $this->assertNoBlockAppears($block);
435   }
436
437   /**
438    * Passes if a shortcut quick link with the specified label is found.
439    *
440    * An optional link index may be passed.
441    *
442    * @param string $label
443    *   Text between the anchor tags.
444    * @param int $index
445    *   Link position counting from zero.
446    * @param string $message
447    *   (optional) A message to display with the assertion. Do not translate
448    *   messages: use format_string() to embed variables in the message text, not
449    *   t(). If left blank, a default message will be displayed.
450    * @param string $group
451    *   (optional) The group this message is in, which is displayed in a column
452    *   in test output. Use 'Debug' to indicate this is debugging output. Do not
453    *   translate this string. Defaults to 'Other'; most tests do not override
454    *   this default.
455    *
456    * @return bool
457    *   TRUE if the assertion succeeded, FALSE otherwise.
458    */
459   protected function assertShortcutQuickLink($label, $index = 0, $message = '', $group = 'Other') {
460     $links = $this->xpath('//a[normalize-space()=:label]', [':label' => $label]);
461     $message = ($message ? $message : SafeMarkup::format('Shortcut quick link with label %label found.', ['%label' => $label]));
462     return $this->assert(isset($links[$index]), $message, $group);
463   }
464
465 }