3 namespace Drupal\Tests\shortcut\Functional;
5 use Drupal\block_content\Entity\BlockContentType;
6 use Drupal\Component\Utility\SafeMarkup;
7 use Drupal\Component\Render\FormattableMarkup;
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;
15 * Create, view, edit, delete, and change shortcut links.
19 class ShortcutLinksTest extends ShortcutTestBase {
21 use AssertBlockAppearsTrait;
28 public static $modules = ['router_test', 'views', 'block'];
33 protected function setUp() {
36 $this->drupalPlaceBlock('page_title_block');
40 * Tests that creating a shortcut works properly.
42 public function testShortcutLinkAdd() {
45 // Create an alias for the node so we can test aliases.
47 'source' => '/node/' . $this->node->id(),
48 'alias' => '/' . $this->randomMachineName(8),
50 $this->container->get('path.alias_storage')->save($path['source'], $path['alias']);
52 // Create some paths to test.
56 '/admin/config/system/site-information',
57 '/node/' . $this->node->id() . '/edit',
60 '/router_test/test3/value',
63 $test_cases_non_access = [
65 '/admin/config/system/site-information',
68 // Test the add shortcut form UI. Test that the base field description is
70 $this->drupalGet('admin/config/user-interface/shortcut/manage/' . $set->id() . '/add-link');
71 $this->assertRaw('The location this shortcut points to.');
73 // Check that each new shortcut links where it should.
74 foreach ($test_cases as $test_path) {
75 $title = $this->randomMachineName();
77 'title[0][value]' => $title,
78 'link[0][uri]' => $test_path,
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);
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]));
91 $this->assertLink($title, 0, SafeMarkup::format('Shortcut link %url found on the page.', ['%url' => $test_path]));
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.
100 $loaded = Shortcut::load($entity->id());
101 $this->assertEqual($entity->link->uri, $loaded->link->uri);
102 $this->assertEqual($entity->link->options, $loaded->link->options);
105 // Log in as non admin user, to check that access is checked when creating
107 $this->drupalLogin($this->shortcutUser);
108 $title = $this->randomMachineName();
110 'title[0][value]' => $title,
111 'link[0][uri]' => '/admin',
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']));
118 'title[0][value]' => $title,
119 'link[0][uri]' => '/node',
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.');
124 // Create a new shortcut set and add a link to it.
125 $this->drupalLogin($this->adminUser);
127 'label' => $this->randomMachineName(),
128 'id' => strtolower($this->randomMachineName()),
130 $this->drupalPostForm('admin/config/user-interface/shortcut/add-set', $edit, t('Save'));
131 $title = $this->randomMachineName();
133 'title[0][value]' => $title,
134 'link[0][uri]' => '/admin',
136 $this->drupalPostForm('admin/config/user-interface/shortcut/manage/' . $edit['id'] . '/add-link', $form_data, t('Save'));
137 $this->assertResponse(200);
141 * Tests that the "add to shortcut" and "remove from shortcut" links work.
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();
149 $this->drupalLogin($this->rootUser);
150 $this->drupalGet('admin/config/system/cron');
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');
157 $this->drupalGet('admin/structure');
158 $this->assertLink('Cron', 0, 'Shortcut link found on different page');
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');
166 $this->drupalGet('admin/structure');
167 $this->assertNoLink('Cron', 'Shortcut link removed from different page');
169 $this->drupalGet('admin/people');
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');
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');
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.');
194 $this->config('system.theme')->set('default', 'seven')->save();
195 $this->drupalGet('node/' . $this->node->id());
196 $title = $this->node->getTitle();
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');
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');
208 \Drupal::service('module_installer')->install(['block_content']);
209 BlockContentType::create([
211 'label' => 'Basic block',
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)),
227 * Tests that shortcut links can be renamed.
229 public function testShortcutLinkRename() {
232 // Attempt to rename shortcut link.
233 $new_link_name = $this->randomMachineName();
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]));
246 * Tests that changing the path of a shortcut link works.
248 public function testShortcutLinkChangePath() {
251 // Tests changing a shortcut path.
252 $new_link_path = '/admin/config';
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()]));
265 * Tests that changing the route of a shortcut link works.
267 public function testShortcutLinkChangeRoute() {
268 $this->drupalLogin($this->rootUser);
269 $this->drupalGet('admin/content');
270 $this->assertResponse(200);
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);
281 * Tests deleting a shortcut link.
283 public function testShortcutLinkDelete() {
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.');
293 // Delete all the remaining shortcut links.
294 entity_delete_multiple('shortcut', array_filter($ids));
296 // Get the front page to check that no exceptions occur.
297 $this->drupalGet('');
301 * Tests that the add shortcut link is not displayed for 404/403 errors.
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.
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')
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.');
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.');
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.');
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.');
335 * Tests that the 'access shortcuts' permissions works properly.
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')
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');
349 // Verify that users without the 'access shortcuts' permission can't see the
351 $this->drupalLogin($this->drupalCreateUser(['access toolbar']));
352 $this->assertNoLink('Shortcuts', 'Shortcut link not found on page.');
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.');
360 // Verify that users with the 'access shortcuts' permission can see the
362 $this->drupalLogin($this->drupalCreateUser([
363 'access toolbar', 'access shortcuts', 'administer site configuration',
365 $this->clickLink('Shortcuts', 0, 'Shortcut link found on page.');
366 $this->assertLink('Cron', 0, 'Cron shortcut link found on page.');
368 $this->verifyAccessShortcutsPermissionForEditPages();
372 * Tests the shortcuts are correctly ordered by weight in the toolbar.
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();
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');
391 * Tests that the 'access shortcuts' permission is required for shortcut set
392 * administration page access.
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',
401 $noaccess_user = $this->drupalCreateUser($test_permissions);
402 $this->drupalLogin($noaccess_user);
404 // Verify that set administration pages are inaccessible without the
405 // 'access shortcuts' permission.
407 'admin/config/user-interface/shortcut/manage/default/customize',
408 'admin/config/user-interface/shortcut/manage/default',
409 'user/' . $noaccess_user->id() . '/shortcuts',
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);
420 * Tests that the 'access shortcuts' permission is required to access the
423 public function testShortcutBlockAccess() {
424 // Creates a block instance and place in a region through api.
425 $block = $this->drupalPlaceBlock('shortcuts');
427 // Verify that users with the 'access shortcuts' permission can see the
429 $this->drupalLogin($this->shortcutUser);
430 $this->drupalGet('');
431 $this->assertBlockAppears($block);
433 $this->drupalLogout();
435 // Verify that users without the 'access shortcuts' permission can see the
437 $this->drupalLogin($this->drupalCreateUser([]));
438 $this->drupalGet('');
439 $this->assertNoBlockAppears($block);
443 * Passes if a shortcut quick link with the specified label is found.
445 * An optional link index may be passed.
447 * @param string $label
448 * Text between the anchor tags.
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
462 * TRUE if the assertion succeeded, FALSE otherwise.
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);