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