3 namespace Drupal\shortcut\Tests;
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\views\Entity\View;
14 * Create, view, edit, delete, and change shortcut links.
18 class ShortcutLinksTest extends ShortcutTestBase {
25 public static $modules = ['router_test', 'views', 'block'];
30 protected function setUp() {
33 $this->drupalPlaceBlock('page_title_block');
37 * Tests that creating a shortcut works properly.
39 public function testShortcutLinkAdd() {
42 // Create an alias for the node so we can test aliases.
44 'source' => '/node/' . $this->node->id(),
45 'alias' => '/' . $this->randomMachineName(8),
47 $this->container->get('path.alias_storage')->save($path['source'], $path['alias']);
49 // Create some paths to test.
53 '/admin/config/system/site-information',
54 '/node/' . $this->node->id() . '/edit',
57 '/router_test/test3/value',
60 $test_cases_non_access = [
62 '/admin/config/system/site-information',
65 // Check that each new shortcut links where it should.
66 foreach ($test_cases as $test_path) {
67 $title = $this->randomMachineName();
69 'title[0][value]' => $title,
70 'link[0][uri]' => $test_path,
72 $this->drupalPostForm('admin/config/user-interface/shortcut/manage/' . $set->id() . '/add-link', $form_data, t('Save'));
73 $this->assertResponse(200);
74 $this->assertText(t('Added a shortcut for @title.', ['@title' => $title]));
75 $saved_set = ShortcutSet::load($set->id());
76 $paths = $this->getShortcutInformation($saved_set, 'link');
77 $this->assertTrue(in_array('internal:' . $test_path, $paths), 'Shortcut created: ' . $test_path);
79 if (in_array($test_path, $test_cases_non_access)) {
80 $this->assertNoLink($title, SafeMarkup::format('Shortcut link %url not accessible on the page.', ['%url' => $test_path]));
83 $this->assertLink($title, 0, SafeMarkup::format('Shortcut link %url found on the page.', ['%url' => $test_path]));
86 $saved_set = ShortcutSet::load($set->id());
87 // Test that saving and re-loading a shortcut preserves its values.
88 $shortcuts = $saved_set->getShortcuts();
89 foreach ($shortcuts as $entity) {
90 // Test the node routes with parameters.
92 $loaded = Shortcut::load($entity->id());
93 $this->assertEqual($entity->link->uri, $loaded->link->uri);
94 $this->assertEqual($entity->link->options, $loaded->link->options);
97 // Log in as non admin user, to check that access is checked when creating
99 $this->drupalLogin($this->shortcutUser);
100 $title = $this->randomMachineName();
102 'title[0][value]' => $title,
103 'link[0][uri]' => '/admin',
105 $this->drupalPostForm('admin/config/user-interface/shortcut/manage/' . $set->id() . '/add-link', $form_data, t('Save'));
106 $this->assertResponse(200);
107 $this->assertRaw(t("The path '@link_path' is inaccessible.", ['@link_path' => '/admin']));
110 'title[0][value]' => $title,
111 'link[0][uri]' => '/node',
113 $this->drupalPostForm('admin/config/user-interface/shortcut/manage/' . $set->id() . '/add-link', $form_data, t('Save'));
114 $this->assertLink($title, 0, 'Shortcut link found on the page.');
116 // Create a new shortcut set and add a link to it.
117 $this->drupalLogin($this->adminUser);
119 'label' => $this->randomMachineName(),
120 'id' => strtolower($this->randomMachineName()),
122 $this->drupalPostForm('admin/config/user-interface/shortcut/add-set', $edit, t('Save'));
123 $title = $this->randomMachineName();
125 'title[0][value]' => $title,
126 'link[0][uri]' => '/admin',
128 $this->drupalPostForm('admin/config/user-interface/shortcut/manage/' . $edit['id'] . '/add-link', $form_data, t('Save'));
129 $this->assertResponse(200);
133 * Tests that the "add to shortcut" and "remove from shortcut" links work.
135 public function testShortcutQuickLink() {
136 \Drupal::service('theme_handler')->install(['seven']);
137 $this->config('system.theme')->set('admin', 'seven')->save();
138 $this->config('node.settings')->set('use_admin_theme', '1')->save();
139 $this->container->get('router.builder')->rebuild();
141 $this->drupalLogin($this->rootUser);
142 $this->drupalGet('admin/config/system/cron');
144 // Test the "Add to shortcuts" link.
145 $this->clickLink('Add to Default shortcuts');
146 $this->assertText('Added a shortcut for Cron.');
147 $this->assertLink('Cron', 0, 'Shortcut link found on page');
149 $this->drupalGet('admin/structure');
150 $this->assertLink('Cron', 0, 'Shortcut link found on different page');
152 // Test the "Remove from shortcuts" link.
153 $this->clickLink('Cron');
154 $this->clickLink('Remove from Default shortcuts');
155 $this->assertText('The shortcut Cron has been deleted.');
156 $this->assertNoLink('Cron', 'Shortcut link removed from page');
158 $this->drupalGet('admin/structure');
159 $this->assertNoLink('Cron', 'Shortcut link removed from different page');
161 $this->drupalGet('admin/people');
163 // Test the "Add to shortcuts" link for a page generated by views.
164 $this->clickLink('Add to Default shortcuts');
165 $this->assertText('Added a shortcut for People.');
166 $this->assertShortcutQuickLink('Remove from Default shortcuts');
168 // Test the "Remove from shortcuts" link for a page generated by views.
169 $this->clickLink('Remove from Default shortcuts');
170 $this->assertText('The shortcut People has been deleted.');
171 $this->assertShortcutQuickLink('Add to Default shortcuts');
173 // Test two pages which use same route name but different route parameters.
174 $this->drupalGet('node/add/page');
175 // Add Shortcut for Basic Page.
176 $this->clickLink('Add to Default shortcuts');
177 $this->assertText('Added a shortcut for Create Basic page.');
178 // Assure that Article does not have its shortcut indicated as set.
179 $this->drupalGet('node/add/article');
180 $link = $this->xpath('//a[normalize-space()=:label]', [':label' => 'Remove from Default shortcuts']);
181 $this->assertTrue(empty($link), 'Link Remove to Default shortcuts not found for Create Article page.');
182 // Add Shortcut for Article.
183 $this->clickLink('Add to Default shortcuts');
184 $this->assertText('Added a shortcut for Create Article.');
186 $this->config('system.theme')->set('default', 'seven')->save();
187 $this->drupalGet('node/' . $this->node->id());
188 $title = $this->node->getTitle();
190 // Test the "Add to shortcuts" link for node view route.
191 $this->clickLink('Add to Default shortcuts');
192 $this->assertText(new FormattableMarkup('Added a shortcut for @title.', ['@title' => $title]));
193 $this->assertShortcutQuickLink('Remove from Default shortcuts');
195 // Test the "Remove from shortcuts" link for node view route.
196 $this->clickLink('Remove from Default shortcuts');
197 $this->assertText(new FormattableMarkup('The shortcut @title has been deleted.', ['@title' => $title]));
198 $this->assertShortcutQuickLink('Add to Default shortcuts');
200 \Drupal::service('module_installer')->install(['block_content']);
201 BlockContentType::create([
203 'label' => 'Basic block',
206 // Test page with HTML tags in title.
207 $this->drupalGet('admin/structure/block/block-content/manage/basic');
208 $page_title = new FormattableMarkup('Edit %label custom block type', ['%label' => 'Basic block']);
209 $this->assertRaw($page_title);
210 // Add shortcut to this page.
211 $this->clickLink('Add to Default shortcuts');
212 $this->assertRaw(new FormattableMarkup('Added a shortcut for %title.', [
213 '%title' => trim(strip_tags($page_title)),
219 * Tests that shortcut links can be renamed.
221 public function testShortcutLinkRename() {
224 // Attempt to rename shortcut link.
225 $new_link_name = $this->randomMachineName();
227 $shortcuts = $set->getShortcuts();
228 $shortcut = reset($shortcuts);
229 $this->drupalPostForm('admin/config/user-interface/shortcut/link/' . $shortcut->id(), ['title[0][value]' => $new_link_name], t('Save'));
230 $saved_set = ShortcutSet::load($set->id());
231 $titles = $this->getShortcutInformation($saved_set, 'title');
232 $this->assertTrue(in_array($new_link_name, $titles), 'Shortcut renamed: ' . $new_link_name);
233 $this->assertLink($new_link_name, 0, 'Renamed shortcut link appears on the page.');
234 $this->assertText(t('The shortcut @link has been updated.', ['@link' => $new_link_name]));
238 * Tests that changing the path of a shortcut link works.
240 public function testShortcutLinkChangePath() {
243 // Tests changing a shortcut path.
244 $new_link_path = '/admin/config';
246 $shortcuts = $set->getShortcuts();
247 $shortcut = reset($shortcuts);
248 $this->drupalPostForm('admin/config/user-interface/shortcut/link/' . $shortcut->id(), ['title[0][value]' => $shortcut->getTitle(), 'link[0][uri]' => $new_link_path], t('Save'));
249 $saved_set = ShortcutSet::load($set->id());
250 $paths = $this->getShortcutInformation($saved_set, 'link');
251 $this->assertTrue(in_array('internal:' . $new_link_path, $paths), 'Shortcut path changed: ' . $new_link_path);
252 $this->assertLinkByHref($new_link_path, 0, 'Shortcut with new path appears on the page.');
253 $this->assertText(t('The shortcut @link has been updated.', ['@link' => $shortcut->getTitle()]));
257 * Tests that changing the route of a shortcut link works.
259 public function testShortcutLinkChangeRoute() {
260 $this->drupalLogin($this->rootUser);
261 $this->drupalGet('admin/content');
262 $this->assertResponse(200);
264 View::load('content')->disable()->save();
265 /** @var \Drupal\Core\Routing\RouteBuilderInterface $router_builder */
266 $router_builder = \Drupal::service('router.builder');
267 $router_builder->rebuildIfNeeded();
268 $this->drupalGet('admin/content');
269 $this->assertResponse(200);
273 * Tests deleting a shortcut link.
275 public function testShortcutLinkDelete() {
278 $shortcuts = $set->getShortcuts();
279 $shortcut = reset($shortcuts);
280 $this->drupalPostForm('admin/config/user-interface/shortcut/link/' . $shortcut->id() . '/delete', [], 'Delete');
281 $saved_set = ShortcutSet::load($set->id());
282 $ids = $this->getShortcutInformation($saved_set, 'id');
283 $this->assertFalse(in_array($shortcut->id(), $ids), 'Successfully deleted a shortcut.');
285 // Delete all the remaining shortcut links.
286 entity_delete_multiple('shortcut', array_filter($ids));
288 // Get the front page to check that no exceptions occur.
289 $this->drupalGet('');
293 * Tests that the add shortcut link is not displayed for 404/403 errors.
295 * Tests that the "Add to shortcuts" link is not displayed on a page not
296 * found or a page the user does not have access to.
298 public function testNoShortcutLink() {
299 // Change to a theme that displays shortcuts.
300 \Drupal::service('theme_handler')->install(['seven']);
301 $this->config('system.theme')
302 ->set('default', 'seven')
305 $this->drupalGet('page-that-does-not-exist');
306 $result = $this->xpath('//a[contains(@class, "shortcut-action--add")]');
307 $this->assertTrue(empty($result), 'Add to shortcuts link was not shown on a page not found.');
309 // The user does not have access to this path.
310 $this->drupalGet('admin/modules');
311 $result = $this->xpath('//a[contains(@class, "shortcut-action--add")]');
312 $this->assertTrue(empty($result), 'Add to shortcuts link was not shown on a page the user does not have access to.');
314 // Verify that the testing mechanism works by verifying the shortcut link
315 // appears on admin/content.
316 $this->drupalGet('admin/content');
317 $result = $this->xpath('//a[contains(@class, "shortcut-action--remove")]');
318 $this->assertTrue(!empty($result), 'Remove from shortcuts link was shown on a page the user does have access to.');
320 // Verify that the shortcut link appears on routing only pages.
321 $this->drupalGet('router_test/test2');
322 $result = $this->xpath('//a[contains(@class, "shortcut-action--add")]');
323 $this->assertTrue(!empty($result), 'Add to shortcuts link was shown on a page the user does have access to.');
327 * Tests that the 'access shortcuts' permissions works properly.
329 public function testAccessShortcutsPermission() {
330 // Change to a theme that displays shortcuts.
331 \Drupal::service('theme_handler')->install(['seven']);
332 $this->config('system.theme')
333 ->set('default', 'seven')
336 // Add cron to the default shortcut set.
337 $this->drupalLogin($this->rootUser);
338 $this->drupalGet('admin/config/system/cron');
339 $this->clickLink('Add to Default shortcuts');
341 // Verify that users without the 'access shortcuts' permission can't see the
343 $this->drupalLogin($this->drupalCreateUser(['access toolbar']));
344 $this->assertNoLink('Shortcuts', 'Shortcut link not found on page.');
346 // Verify that users without the 'administer site configuration' permission
347 // can't see the cron shortcuts.
348 $this->drupalLogin($this->drupalCreateUser(['access toolbar', 'access shortcuts']));
349 $this->assertNoLink('Shortcuts', 'Shortcut link not found on page.');
350 $this->assertNoLink('Cron', 'Cron shortcut link not found on page.');
352 // Verify that users with the 'access shortcuts' permission can see the
354 $this->drupalLogin($this->drupalCreateUser([
355 'access toolbar', 'access shortcuts', 'administer site configuration',
357 $this->clickLink('Shortcuts', 0, 'Shortcut link found on page.');
358 $this->assertLink('Cron', 0, 'Cron shortcut link found on page.');
360 $this->verifyAccessShortcutsPermissionForEditPages();
364 * Tests the shortcuts are correctly ordered by weight in the toolbar.
366 public function testShortcutLinkOrder() {
367 // Ensure to give permissions to access the shortcuts.
368 $this->drupalLogin($this->drupalCreateUser(['access toolbar', 'access shortcuts', 'access content overview', 'administer content types']));
369 $this->drupalGet(Url::fromRoute('<front>'));
370 $shortcuts = $this->cssSelect('#toolbar-item-shortcuts-tray .toolbar-menu a');
371 $this->assertEqual((string) $shortcuts[0], 'Add content');
372 $this->assertEqual((string) $shortcuts[1], 'All content');
373 foreach ($this->set->getShortcuts() as $shortcut) {
374 $shortcut->setWeight($shortcut->getWeight() * -1)->save();
376 $this->drupalGet(Url::fromRoute('<front>'));
377 $shortcuts = $this->cssSelect('#toolbar-item-shortcuts-tray .toolbar-menu a');
378 $this->assertEqual((string) $shortcuts[0], 'All content');
379 $this->assertEqual((string) $shortcuts[1], 'Add content');
383 * Tests that the 'access shortcuts' permission is required for shortcut set
384 * administration page access.
386 private function verifyAccessShortcutsPermissionForEditPages() {
387 // Create a user with customize links and switch sets permissions but
388 // without the 'access shortcuts' permission.
389 $test_permissions = [
390 'customize shortcut links',
391 'switch shortcut sets',
393 $noaccess_user = $this->drupalCreateUser($test_permissions);
394 $this->drupalLogin($noaccess_user);
396 // Verify that set administration pages are inaccessible without the
397 // 'access shortcuts' permission.
399 'admin/config/user-interface/shortcut/manage/default/customize',
400 'admin/config/user-interface/shortcut/manage/default',
401 'user/' . $noaccess_user->id() . '/shortcuts',
404 foreach ($edit_paths as $path) {
405 $this->drupalGet($path);
406 $message = format_string('Access is denied on %s', ['%s' => $path]);
407 $this->assertResponse(403, $message);
412 * Tests that the 'access shortcuts' permission is required to access the
415 public function testShortcutBlockAccess() {
416 // Creates a block instance and place in a region through api.
417 $block = $this->drupalPlaceBlock('shortcuts');
419 // Verify that users with the 'access shortcuts' permission can see the
421 $this->drupalLogin($this->shortcutUser);
422 $this->drupalGet('');
423 $this->assertBlockAppears($block);
425 $this->drupalLogout();
427 // Verify that users without the 'access shortcuts' permission can see the
429 $this->drupalLogin($this->drupalCreateUser([]));
430 $this->drupalGet('');
431 $this->assertNoBlockAppears($block);
435 * Passes if a shortcut quick link with the specified label is found.
437 * An optional link index may be passed.
439 * @param string $label
440 * Text between the anchor tags.
442 * Link position counting from zero.
443 * @param string $message
444 * (optional) A message to display with the assertion. Do not translate
445 * messages: use format_string() to embed variables in the message text, not
446 * t(). If left blank, a default message will be displayed.
447 * @param string $group
448 * (optional) The group this message is in, which is displayed in a column
449 * in test output. Use 'Debug' to indicate this is debugging output. Do not
450 * translate this string. Defaults to 'Other'; most tests do not override
454 * TRUE if the assertion succeeded, FALSE otherwise.
456 protected function assertShortcutQuickLink($label, $index = 0, $message = '', $group = 'Other') {
457 $links = $this->xpath('//a[normalize-space()=:label]', [':label' => $label]);
458 $message = ($message ? $message : SafeMarkup::format('Shortcut quick link with label %label found.', ['%label' => $label]));
459 return $this->assert(isset($links[$index]), $message, $group);