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