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