3 namespace Drupal\Tests\shortcut\Functional;
5 use Drupal\block_content\Entity\BlockContentType;
6 use Drupal\Component\Render\FormattableMarkup;
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;
14 * Create, view, edit, delete, and change shortcut links.
18 class ShortcutLinksTest extends ShortcutTestBase {
20 use AssertBlockAppearsTrait;
27 public static $modules = ['router_test', 'views', 'block'];
32 protected function setUp() {
35 $this->drupalPlaceBlock('page_title_block');
39 * Tests that creating a shortcut works properly.
41 public function testShortcutLinkAdd() {
44 // Create an alias for the node so we can test aliases.
46 'source' => '/node/' . $this->node->id(),
47 'alias' => '/' . $this->randomMachineName(8),
49 $this->container->get('path.alias_storage')->save($path['source'], $path['alias']);
51 // Create some paths to test.
55 '/admin/config/system/site-information',
56 '/node/' . $this->node->id() . '/edit',
59 '/router_test/test3/value',
62 $test_cases_non_access = [
64 '/admin/config/system/site-information',
67 // Test the add shortcut form UI. Test that the base field description is
69 $this->drupalGet('admin/config/user-interface/shortcut/manage/' . $set->id() . '/add-link');
70 $this->assertRaw('The location this shortcut points to.');
72 // Check that each new shortcut links where it should.
73 foreach ($test_cases as $test_path) {
74 $title = $this->randomMachineName();
76 'title[0][value]' => $title,
77 'link[0][uri]' => $test_path,
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);
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]));
90 $this->assertLink($title, 0, new FormattableMarkup('Shortcut link %url found on the page.', ['%url' => $test_path]));
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.
99 $loaded = Shortcut::load($entity->id());
100 $this->assertEqual($entity->link->uri, $loaded->link->uri);
101 $this->assertEqual($entity->link->options, $loaded->link->options);
104 // Log in as non admin user, to check that access is checked when creating
106 $this->drupalLogin($this->shortcutUser);
107 $title = $this->randomMachineName();
109 'title[0][value]' => $title,
110 'link[0][uri]' => '/admin',
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']));
117 'title[0][value]' => $title,
118 'link[0][uri]' => '/node',
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.');
123 // Create a new shortcut set and add a link to it.
124 $this->drupalLogin($this->adminUser);
126 'label' => $this->randomMachineName(),
127 'id' => strtolower($this->randomMachineName()),
129 $this->drupalPostForm('admin/config/user-interface/shortcut/add-set', $edit, t('Save'));
130 $title = $this->randomMachineName();
132 'title[0][value]' => $title,
133 'link[0][uri]' => '/admin',
135 $this->drupalPostForm('admin/config/user-interface/shortcut/manage/' . $edit['id'] . '/add-link', $form_data, t('Save'));
136 $this->assertResponse(200);
140 * Tests that the "add to shortcut" and "remove from shortcut" links work.
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();
148 $this->drupalLogin($this->rootUser);
149 $this->drupalGet('admin/config/system/cron');
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');
156 $this->drupalGet('admin/structure');
157 $this->assertLink('Cron', 0, 'Shortcut link found on different page');
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');
165 $this->drupalGet('admin/structure');
166 $this->assertNoLink('Cron', 'Shortcut link removed from different page');
168 $this->drupalGet('admin/people');
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');
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');
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.');
193 $this->config('system.theme')->set('default', 'seven')->save();
194 $this->drupalGet('node/' . $this->node->id());
195 $title = $this->node->getTitle();
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');
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');
207 \Drupal::service('module_installer')->install(['block_content']);
208 BlockContentType::create([
210 'label' => 'Basic block',
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)),
226 * Tests that shortcut links can be renamed.
228 public function testShortcutLinkRename() {
231 // Attempt to rename shortcut link.
232 $new_link_name = $this->randomMachineName();
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]));
245 * Tests that changing the path of a shortcut link works.
247 public function testShortcutLinkChangePath() {
250 // Tests changing a shortcut path.
251 $new_link_path = '/admin/config';
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()]));
264 * Tests that changing the route of a shortcut link works.
266 public function testShortcutLinkChangeRoute() {
267 $this->drupalLogin($this->rootUser);
268 $this->drupalGet('admin/content');
269 $this->assertResponse(200);
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);
280 * Tests deleting a shortcut link.
282 public function testShortcutLinkDelete() {
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.');
292 // Delete all the remaining shortcut links.
293 entity_delete_multiple('shortcut', array_filter($ids));
295 // Get the front page to check that no exceptions occur.
296 $this->drupalGet('');
300 * Tests that the add shortcut link is not displayed for 404/403 errors.
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.
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')
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.');
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.');
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.');
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.');
334 * Tests that the 'access shortcuts' permissions works properly.
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')
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');
348 // Verify that users without the 'access shortcuts' permission can't see the
350 $this->drupalLogin($this->drupalCreateUser(['access toolbar']));
351 $this->assertNoLink('Shortcuts', 'Shortcut link not found on page.');
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.');
359 // Verify that users with the 'access shortcuts' permission can see the
361 $this->drupalLogin($this->drupalCreateUser([
362 'access toolbar', 'access shortcuts', 'administer site configuration',
364 $this->clickLink('Shortcuts', 0, 'Shortcut link found on page.');
365 $this->assertLink('Cron', 0, 'Cron shortcut link found on page.');
367 $this->verifyAccessShortcutsPermissionForEditPages();
371 * Tests the shortcuts are correctly ordered by weight in the toolbar.
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();
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');
390 * Tests that the 'access shortcuts' permission is required for shortcut set
391 * administration page access.
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',
400 $noaccess_user = $this->drupalCreateUser($test_permissions);
401 $this->drupalLogin($noaccess_user);
403 // Verify that set administration pages are inaccessible without the
404 // 'access shortcuts' permission.
406 'admin/config/user-interface/shortcut/manage/default/customize',
407 'admin/config/user-interface/shortcut/manage/default',
408 'user/' . $noaccess_user->id() . '/shortcuts',
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);
419 * Tests that the 'access shortcuts' permission is required to access the
422 public function testShortcutBlockAccess() {
423 // Creates a block instance and place in a region through api.
424 $block = $this->drupalPlaceBlock('shortcuts');
426 // Verify that users with the 'access shortcuts' permission can see the
428 $this->drupalLogin($this->shortcutUser);
429 $this->drupalGet('');
430 $this->assertBlockAppears($block);
432 $this->drupalLogout();
434 // Verify that users without the 'access shortcuts' permission can see the
436 $this->drupalLogin($this->drupalCreateUser([]));
437 $this->drupalGet('');
438 $this->assertNoBlockAppears($block);
442 * Passes if a shortcut quick link with the specified label is found.
444 * An optional link index may be passed.
446 * @param string $label
447 * Text between the anchor tags.
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
461 * TRUE if the assertion succeeded, FALSE otherwise.
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);