3 namespace Drupal\system\Tests\Menu;
6 use Drupal\node\Entity\NodeType;
7 use Drupal\user\RoleInterface;
10 * Tests breadcrumbs functionality.
14 class BreadcrumbTest extends MenuTestBase {
21 public static $modules = ['menu_test', 'block'];
24 * An administrative user.
26 * @var \Drupal\user\UserInterface
33 * @var \Drupal\user\UserInterface
38 * Test paths in the Standard profile.
40 protected $profile = 'standard';
42 protected function setUp() {
45 $perms = array_keys(\Drupal::service('user.permissions')->getPermissions());
46 $this->adminUser = $this->drupalCreateUser($perms);
47 $this->drupalLogin($this->adminUser);
49 // This test puts menu links in the Tools menu and then tests for their
50 // presence on the page, so we need to ensure that the Tools block will be
51 // displayed in the admin theme.
52 $this->drupalPlaceBlock('system_menu_block:tools', [
53 'region' => 'content',
54 'theme' => $this->config('system.theme')->get('admin'),
59 * Tests breadcrumbs on node and administrative paths.
61 public function testBreadCrumbs() {
62 // Prepare common base breadcrumb elements.
63 $home = ['' => 'Home'];
64 $admin = $home + ['admin' => t('Administration')];
65 $config = $admin + ['admin/config' => t('Configuration')];
68 // Verify Taxonomy administration breadcrumbs.
70 'admin/structure' => t('Structure'),
72 $this->assertBreadcrumb('admin/structure/taxonomy', $trail);
75 'admin/structure/taxonomy' => t('Taxonomy'),
77 $this->assertBreadcrumb('admin/structure/taxonomy/manage/tags', $trail);
79 'admin/structure/taxonomy/manage/tags' => t('Tags'),
81 $this->assertBreadcrumb('admin/structure/taxonomy/manage/tags/overview', $trail);
82 $this->assertBreadcrumb('admin/structure/taxonomy/manage/tags/add', $trail);
84 // Verify Menu administration breadcrumbs.
86 'admin/structure' => t('Structure'),
88 $this->assertBreadcrumb('admin/structure/menu', $trail);
91 'admin/structure/menu' => t('Menus'),
93 $this->assertBreadcrumb('admin/structure/menu/manage/tools', $trail);
96 'admin/structure/menu/manage/tools' => t('Tools'),
98 $this->assertBreadcrumb("admin/structure/menu/link/node.add_page/edit", $trail);
99 $this->assertBreadcrumb('admin/structure/menu/manage/tools/add', $trail);
101 // Verify Node administration breadcrumbs.
103 'admin/structure' => t('Structure'),
104 'admin/structure/types' => t('Content types'),
106 $this->assertBreadcrumb('admin/structure/types/add', $trail);
107 $this->assertBreadcrumb("admin/structure/types/manage/$type", $trail);
109 "admin/structure/types/manage/$type" => t('Article'),
111 $this->assertBreadcrumb("admin/structure/types/manage/$type/fields", $trail);
112 $this->assertBreadcrumb("admin/structure/types/manage/$type/display", $trail);
113 $trail_teaser = $trail + [
114 "admin/structure/types/manage/$type/display" => t('Manage display'),
116 $this->assertBreadcrumb("admin/structure/types/manage/$type/display/teaser", $trail_teaser);
117 $this->assertBreadcrumb("admin/structure/types/manage/$type/delete", $trail);
119 "admin/structure/types/manage/$type/fields" => t('Manage fields'),
121 $this->assertBreadcrumb("admin/structure/types/manage/$type/fields/node.$type.body", $trail);
123 // Verify Filter text format administration breadcrumbs.
124 $filter_formats = filter_formats();
125 $format = reset($filter_formats);
126 $format_id = $format->id();
128 'admin/config/content' => t('Content authoring'),
130 $this->assertBreadcrumb('admin/config/content/formats', $trail);
133 'admin/config/content/formats' => t('Text formats and editors'),
135 $this->assertBreadcrumb('admin/config/content/formats/add', $trail);
136 $this->assertBreadcrumb("admin/config/content/formats/manage/$format_id", $trail);
137 // @todo Remove this part once we have a _title_callback, see
138 // https://www.drupal.org/node/2076085.
140 "admin/config/content/formats/manage/$format_id" => $format->label(),
142 $this->assertBreadcrumb("admin/config/content/formats/manage/$format_id/disable", $trail);
144 // Verify node breadcrumbs (without menu link).
145 $node1 = $this->drupalCreateNode();
146 $nid1 = $node1->id();
148 $this->assertBreadcrumb("node/$nid1", $trail);
149 // Also verify that the node does not appear elsewhere (e.g., menu trees).
150 $this->assertNoLink($node1->getTitle());
151 // Also verify that the node does not appear elsewhere (e.g., menu trees).
152 $this->assertNoLink($node1->getTitle());
155 "node/$nid1" => $node1->getTitle(),
157 $this->assertBreadcrumb("node/$nid1/edit", $trail);
159 // Verify that breadcrumb on node listing page contains "Home" only.
161 $this->assertBreadcrumb('node', $trail);
163 // Verify node breadcrumbs (in menu).
164 // Do this separately for Main menu and Tools menu, since only the
165 // latter is a preferred menu by default.
166 // @todo Also test all themes? Manually testing led to the suspicion that
167 // breadcrumbs may differ, possibly due to theme overrides.
168 $menus = ['main', 'tools'];
169 // Alter node type menu settings.
170 $node_type = NodeType::load($type);
171 $node_type->setThirdPartySetting('menu_ui', 'available_menus', $menus);
172 $node_type->setThirdPartySetting('menu_ui', 'parent', 'tools:');
175 foreach ($menus as $menu) {
176 // Create a parent node in the current menu.
177 $title = $this->randomMachineName();
178 $node2 = $this->drupalCreateNode([
183 'title' => 'Parent ' . $title,
185 'menu_name' => $menu,
190 if ($menu == 'tools') {
195 // Create a Tools menu link for 'node', move the last parent node menu
196 // link below it, and verify a full breadcrumb for the last child node.
199 'title[0][value]' => 'Root',
200 'link[0][uri]' => '/node',
202 $this->drupalPostForm("admin/structure/menu/manage/$menu/add", $edit, t('Save'));
203 $menu_links = entity_load_multiple_by_properties('menu_link_content', ['title' => 'Root']);
204 $link = reset($menu_links);
207 'menu[menu_parent]' => $link->getMenuName() . ':' . $link->getPluginId(),
209 $this->drupalPostForm('node/' . $parent->id() . '/edit', $edit, t('Save and keep published'));
211 "node" => $link->getTitle(),
213 $trail = $home + $expected;
214 $tree = $expected + [
215 'node/' . $parent->id() => $parent->menu['title'],
218 'node/' . $parent->id() => $parent->menu['title'],
221 // Add a taxonomy term/tag to last node, and add a link for that term to the
228 'field_tags[target_id]' => implode(',', array_keys($tags)),
230 $this->drupalPostForm('node/' . $parent->id() . '/edit', $edit, t('Save and keep published'));
232 // Put both terms into a hierarchy Drupal ยป Breadcrumbs. Required for both
233 // the menu links and the terms itself, since taxonomy_term_page() resets
234 // the breadcrumb based on taxonomy term hierarchy.
236 foreach ($tags as $name => $null) {
237 $terms = entity_load_multiple_by_properties('taxonomy_term', ['name' => $name]);
238 $term = reset($terms);
239 $tags[$name]['term'] = $term;
242 'parent[]' => [$parent_tid],
244 $this->drupalPostForm("taxonomy/term/{$term->id()}/edit", $edit, t('Save'));
246 $parent_tid = $term->id();
249 foreach ($tags as $name => $data) {
250 $term = $data['term'];
252 'title[0][value]' => "$name link",
253 'link[0][uri]' => "/taxonomy/term/{$term->id()}",
254 'menu_parent' => "$menu:{$parent_mlid}",
255 'enabled[value]' => 1,
257 $this->drupalPostForm("admin/structure/menu/manage/$menu/add", $edit, t('Save'));
258 $menu_links = entity_load_multiple_by_properties('menu_link_content', [
259 'title' => $edit['title[0][value]'],
260 'link.uri' => 'internal:/taxonomy/term/' . $term->id(),
262 $tags[$name]['link'] = reset($menu_links);
263 $parent_mlid = $tags[$name]['link']->getPluginId();
266 // Verify expected breadcrumbs for menu links.
269 // Logout the user because we want to check the active class as well, which
270 // is just rendered as anonymous user.
271 $this->drupalLogout();
272 foreach ($tags as $name => $data) {
273 $term = $data['term'];
274 /** @var \Drupal\menu_link_content\MenuLinkContentInterface $link */
275 $link = $data['link'];
277 $link_path = $link->getUrlObject()->getInternalPath();
279 $link_path => $link->getTitle(),
281 $this->assertBreadcrumb($link_path, $trail, $term->getName(), $tree);
282 $this->assertEscaped($parent->getTitle(), 'Tagged node found.');
284 // Additionally make sure that this link appears only once; i.e., the
285 // untranslated menu links automatically generated from menu router items
286 // ('taxonomy/term/%') should never be translated and appear in any menu
287 // other than the breadcrumb trail.
288 $elements = $this->xpath('//nav[@id=:menu]/descendant::a[@href=:href]', [
289 ':menu' => 'block-bartik-tools',
290 ':href' => Url::fromUri('base:' . $link_path)->toString(),
292 $this->assertTrue(count($elements) == 1, "Link to {$link_path} appears only once.");
294 // Next iteration should expect this tag as parent link.
295 // Note: Term name, not link name, due to taxonomy_term_page().
297 $link_path => $term->getName(),
301 // Verify breadcrumbs on user and user/%.
302 // We need to log back in and out below, and cannot simply grant the
303 // 'administer users' permission, since user_page() makes your head explode.
304 user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [
305 'access user profiles',
308 // Verify breadcrumb on front page.
309 $this->assertBreadcrumb('<front>', []);
311 // Verify breadcrumb on user pages (without menu link) for anonymous user.
313 $this->assertBreadcrumb('user', $trail, t('Log in'));
314 $this->assertBreadcrumb('user/' . $this->adminUser->id(), $trail, $this->adminUser->getUsername());
316 // Verify breadcrumb on user pages (without menu link) for registered users.
317 $this->drupalLogin($this->adminUser);
319 $this->assertBreadcrumb('user', $trail, $this->adminUser->getUsername());
320 $this->assertBreadcrumb('user/' . $this->adminUser->id(), $trail, $this->adminUser->getUsername());
322 'user/' . $this->adminUser->id() => $this->adminUser->getUsername(),
324 $this->assertBreadcrumb('user/' . $this->adminUser->id() . '/edit', $trail, $this->adminUser->getUsername());
326 // Create a second user to verify breadcrumb on user pages again.
327 $this->webUser = $this->drupalCreateUser([
329 'access user profiles',
331 $this->drupalLogin($this->webUser);
333 // Verify correct breadcrumb and page title on another user's account pages.
335 $this->assertBreadcrumb('user/' . $this->adminUser->id(), $trail, $this->adminUser->getUsername());
337 'user/' . $this->adminUser->id() => $this->adminUser->getUsername(),
339 $this->assertBreadcrumb('user/' . $this->adminUser->id() . '/edit', $trail, $this->adminUser->getUsername());
341 // Verify correct breadcrumb and page title when viewing own user account.
343 $this->assertBreadcrumb('user/' . $this->webUser->id(), $trail, $this->webUser->getUsername());
345 'user/' . $this->webUser->id() => $this->webUser->getUsername(),
347 $this->assertBreadcrumb('user/' . $this->webUser->id() . '/edit', $trail, $this->webUser->getUsername());
349 // Create an only slightly privileged user being able to access site reports
350 // but not administration pages.
351 $this->webUser = $this->drupalCreateUser([
352 'access site reports',
354 $this->drupalLogin($this->webUser);
356 // Verify that we can access recent log entries, there is a corresponding
357 // page title, and that the breadcrumb is just the Home link (because the
358 // user is not able to access "Administer".
360 $this->assertBreadcrumb('admin', $trail, t('Access denied'));
361 $this->assertResponse(403);
363 // Since the 'admin' path is not accessible, we still expect only the Home
365 $this->assertBreadcrumb('admin/reports', $trail, t('Reports'));
366 $this->assertNoResponse(403);
368 // Since the Reports page is accessible, that will show.
369 $trail += ['admin/reports' => t('Reports')];
370 $this->assertBreadcrumb('admin/reports/dblog', $trail, t('Recent log messages'));
371 $this->assertNoResponse(403);
373 // Ensure that the breadcrumb is safe against XSS.
374 $this->drupalGet('menu-test/breadcrumb1/breadcrumb2/breadcrumb3');
375 $this->assertRaw('<script>alert(12);</script>');
376 $this->assertEscaped('<script>alert(123);</script>');