3 namespace Drupal\Tests\system\Functional\Menu;
6 use Drupal\node\Entity\NodeType;
7 use Drupal\Tests\BrowserTestBase;
8 use Drupal\user\RoleInterface;
11 * Tests breadcrumbs functionality.
15 class BreadcrumbTest extends BrowserTestBase {
17 use AssertBreadcrumbTrait;
24 public static $modules = ['menu_test', 'block'];
27 * An administrative user.
29 * @var \Drupal\user\UserInterface
36 * @var \Drupal\user\UserInterface
41 * Test paths in the Standard profile.
45 protected $profile = 'standard';
47 protected function setUp() {
50 $perms = array_keys(\Drupal::service('user.permissions')->getPermissions());
51 $this->adminUser = $this->drupalCreateUser($perms);
52 $this->drupalLogin($this->adminUser);
54 // This test puts menu links in the Tools menu and then tests for their
55 // presence on the page, so we need to ensure that the Tools block will be
56 // displayed in the admin theme.
57 $this->drupalPlaceBlock('system_menu_block:tools', [
58 'region' => 'content',
59 'theme' => $this->config('system.theme')->get('admin'),
64 * Tests breadcrumbs on node and administrative paths.
66 public function testBreadCrumbs() {
67 // Prepare common base breadcrumb elements.
68 $home = ['' => 'Home'];
69 $admin = $home + ['admin' => t('Administration')];
70 $config = $admin + ['admin/config' => t('Configuration')];
73 // Verify Taxonomy administration breadcrumbs.
75 'admin/structure' => t('Structure'),
77 $this->assertBreadcrumb('admin/structure/taxonomy', $trail);
80 'admin/structure/taxonomy' => t('Taxonomy'),
82 $this->assertBreadcrumb('admin/structure/taxonomy/manage/tags', $trail);
84 'admin/structure/taxonomy/manage/tags' => t('Edit Tags'),
86 $this->assertBreadcrumb('admin/structure/taxonomy/manage/tags/overview', $trail);
87 $this->assertBreadcrumb('admin/structure/taxonomy/manage/tags/add', $trail);
89 // Verify Menu administration breadcrumbs.
91 'admin/structure' => t('Structure'),
93 $this->assertBreadcrumb('admin/structure/menu', $trail);
96 'admin/structure/menu' => t('Menus'),
98 $this->assertBreadcrumb('admin/structure/menu/manage/tools', $trail);
101 'admin/structure/menu/manage/tools' => t('Tools'),
103 $this->assertBreadcrumb("admin/structure/menu/link/node.add_page/edit", $trail);
104 $this->assertBreadcrumb('admin/structure/menu/manage/tools/add', $trail);
106 // Verify Node administration breadcrumbs.
108 'admin/structure' => t('Structure'),
109 'admin/structure/types' => t('Content types'),
111 $this->assertBreadcrumb('admin/structure/types/add', $trail);
112 $this->assertBreadcrumb("admin/structure/types/manage/$type", $trail);
114 "admin/structure/types/manage/$type" => t('Article'),
116 $this->assertBreadcrumb("admin/structure/types/manage/$type/fields", $trail);
117 $this->assertBreadcrumb("admin/structure/types/manage/$type/display", $trail);
118 $trail_teaser = $trail + [
119 "admin/structure/types/manage/$type/display" => t('Manage display'),
121 $this->assertBreadcrumb("admin/structure/types/manage/$type/display/teaser", $trail_teaser);
122 $this->assertBreadcrumb("admin/structure/types/manage/$type/delete", $trail);
124 "admin/structure/types/manage/$type/fields" => t('Manage fields'),
126 $this->assertBreadcrumb("admin/structure/types/manage/$type/fields/node.$type.body", $trail);
128 // Verify Filter text format administration breadcrumbs.
129 $filter_formats = filter_formats();
130 $format = reset($filter_formats);
131 $format_id = $format->id();
133 'admin/config/content' => t('Content authoring'),
135 $this->assertBreadcrumb('admin/config/content/formats', $trail);
138 'admin/config/content/formats' => t('Text formats and editors'),
140 $this->assertBreadcrumb('admin/config/content/formats/add', $trail);
141 $this->assertBreadcrumb("admin/config/content/formats/manage/$format_id", $trail);
142 // @todo Remove this part once we have a _title_callback, see
143 // https://www.drupal.org/node/2076085.
145 "admin/config/content/formats/manage/$format_id" => $format->label(),
147 $this->assertBreadcrumb("admin/config/content/formats/manage/$format_id/disable", $trail);
149 // Verify node breadcrumbs (without menu link).
150 $node1 = $this->drupalCreateNode();
151 $nid1 = $node1->id();
153 $this->assertBreadcrumb("node/$nid1", $trail);
154 // Also verify that the node does not appear elsewhere (e.g., menu trees).
155 $this->assertNoLink($node1->getTitle());
156 // Also verify that the node does not appear elsewhere (e.g., menu trees).
157 $this->assertNoLink($node1->getTitle());
160 "node/$nid1" => $node1->getTitle(),
162 $this->assertBreadcrumb("node/$nid1/edit", $trail);
164 // Verify that breadcrumb on node listing page contains "Home" only.
166 $this->assertBreadcrumb('node', $trail);
168 // Verify node breadcrumbs (in menu).
169 // Do this separately for Main menu and Tools menu, since only the
170 // latter is a preferred menu by default.
171 // @todo Also test all themes? Manually testing led to the suspicion that
172 // breadcrumbs may differ, possibly due to theme overrides.
173 $menus = ['main', 'tools'];
174 // Alter node type menu settings.
175 $node_type = NodeType::load($type);
176 $node_type->setThirdPartySetting('menu_ui', 'available_menus', $menus);
177 $node_type->setThirdPartySetting('menu_ui', 'parent', 'tools:');
180 foreach ($menus as $menu) {
181 // Create a parent node in the current menu.
182 $title = $this->randomMachineName();
183 $node2 = $this->drupalCreateNode([
188 'title' => 'Parent ' . $title,
190 'menu_name' => $menu,
195 if ($menu == 'tools') {
200 // Create a Tools menu link for 'node', move the last parent node menu
201 // link below it, and verify a full breadcrumb for the last child node.
204 'title[0][value]' => 'Root',
205 'link[0][uri]' => '/node',
207 $this->drupalPostForm("admin/structure/menu/manage/$menu/add", $edit, t('Save'));
208 $menu_links = entity_load_multiple_by_properties('menu_link_content', ['title' => 'Root']);
209 $link = reset($menu_links);
212 'menu[menu_parent]' => $link->getMenuName() . ':' . $link->getPluginId(),
214 $this->drupalPostForm('node/' . $parent->id() . '/edit', $edit, t('Save'));
216 "node" => $link->getTitle(),
218 $trail = $home + $expected;
219 $tree = $expected + [
220 'node/' . $parent->id() => $parent->menu['title'],
223 'node/' . $parent->id() => $parent->menu['title'],
226 // Add a taxonomy term/tag to last node, and add a link for that term to the
233 'field_tags[target_id]' => implode(',', array_keys($tags)),
235 $this->drupalPostForm('node/' . $parent->id() . '/edit', $edit, t('Save'));
237 // Put both terms into a hierarchy Drupal ยป Breadcrumbs. Required for both
238 // the menu links and the terms itself, since taxonomy_term_page() resets
239 // the breadcrumb based on taxonomy term hierarchy.
241 foreach ($tags as $name => $null) {
242 $terms = entity_load_multiple_by_properties('taxonomy_term', ['name' => $name]);
243 $term = reset($terms);
244 $tags[$name]['term'] = $term;
247 'parent[]' => [$parent_tid],
249 $this->drupalPostForm("taxonomy/term/{$term->id()}/edit", $edit, t('Save'));
251 $parent_tid = $term->id();
254 foreach ($tags as $name => $data) {
255 $term = $data['term'];
257 'title[0][value]' => "$name link",
258 'link[0][uri]' => "/taxonomy/term/{$term->id()}",
259 'menu_parent' => "$menu:{$parent_mlid}",
260 'enabled[value]' => 1,
262 $this->drupalPostForm("admin/structure/menu/manage/$menu/add", $edit, t('Save'));
263 $menu_links = entity_load_multiple_by_properties('menu_link_content', [
264 'title' => $edit['title[0][value]'],
265 'link.uri' => 'internal:/taxonomy/term/' . $term->id(),
267 $tags[$name]['link'] = reset($menu_links);
268 $parent_mlid = $tags[$name]['link']->getPluginId();
271 // Verify expected breadcrumbs for menu links.
274 // Logout the user because we want to check the active class as well, which
275 // is just rendered as anonymous user.
276 $this->drupalLogout();
277 foreach ($tags as $name => $data) {
278 $term = $data['term'];
279 /** @var \Drupal\menu_link_content\MenuLinkContentInterface $link */
280 $link = $data['link'];
282 $link_path = $link->getUrlObject()->getInternalPath();
284 $link_path => $link->getTitle(),
286 $this->assertBreadcrumb($link_path, $trail, $term->getName(), $tree);
287 $this->assertEscaped($parent->getTitle(), 'Tagged node found.');
289 // Additionally make sure that this link appears only once; i.e., the
290 // untranslated menu links automatically generated from menu router items
291 // ('taxonomy/term/%') should never be translated and appear in any menu
292 // other than the breadcrumb trail.
293 $elements = $this->xpath('//nav[@id=:menu]/descendant::a[@href=:href]', [
294 ':menu' => 'block-bartik-tools',
295 ':href' => Url::fromUri('base:' . $link_path)->toString(),
297 $this->assertTrue(count($elements) == 1, "Link to {$link_path} appears only once.");
299 // Next iteration should expect this tag as parent link.
300 // Note: Term name, not link name, due to taxonomy_term_page().
302 $link_path => $term->getName(),
306 // Verify breadcrumbs on user and user/%.
307 // We need to log back in and out below, and cannot simply grant the
308 // 'administer users' permission, since user_page() makes your head explode.
309 user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [
310 'access user profiles',
313 // Verify breadcrumb on front page.
314 $this->assertBreadcrumb('<front>', []);
316 // Verify breadcrumb on user pages (without menu link) for anonymous user.
318 $this->assertBreadcrumb('user', $trail, t('Log in'));
319 $this->assertBreadcrumb('user/' . $this->adminUser->id(), $trail, $this->adminUser->getUsername());
321 // Verify breadcrumb on user pages (without menu link) for registered users.
322 $this->drupalLogin($this->adminUser);
324 $this->assertBreadcrumb('user', $trail, $this->adminUser->getUsername());
325 $this->assertBreadcrumb('user/' . $this->adminUser->id(), $trail, $this->adminUser->getUsername());
327 'user/' . $this->adminUser->id() => $this->adminUser->getUsername(),
329 $this->assertBreadcrumb('user/' . $this->adminUser->id() . '/edit', $trail, $this->adminUser->getUsername());
331 // Create a second user to verify breadcrumb on user pages again.
332 $this->webUser = $this->drupalCreateUser([
334 'access user profiles',
336 $this->drupalLogin($this->webUser);
338 // Verify correct breadcrumb and page title on another user's account pages.
340 $this->assertBreadcrumb('user/' . $this->adminUser->id(), $trail, $this->adminUser->getUsername());
342 'user/' . $this->adminUser->id() => $this->adminUser->getUsername(),
344 $this->assertBreadcrumb('user/' . $this->adminUser->id() . '/edit', $trail, $this->adminUser->getUsername());
346 // Verify correct breadcrumb and page title when viewing own user account.
348 $this->assertBreadcrumb('user/' . $this->webUser->id(), $trail, $this->webUser->getUsername());
350 'user/' . $this->webUser->id() => $this->webUser->getUsername(),
352 $this->assertBreadcrumb('user/' . $this->webUser->id() . '/edit', $trail, $this->webUser->getUsername());
354 // Create an only slightly privileged user being able to access site reports
355 // but not administration pages.
356 $this->webUser = $this->drupalCreateUser([
357 'access site reports',
359 $this->drupalLogin($this->webUser);
361 // Verify that we can access recent log entries, there is a corresponding
362 // page title, and that the breadcrumb is just the Home link (because the
363 // user is not able to access "Administer".
365 $this->assertBreadcrumb('admin', $trail, t('Access denied'));
366 $this->assertSession()->statusCodeEquals(403);
368 // Since the 'admin' path is not accessible, we still expect only the Home
370 $this->assertBreadcrumb('admin/reports', $trail, t('Reports'));
371 $this->assertSession()->statusCodeNotEquals(403);
373 // Since the Reports page is accessible, that will show.
374 $trail += ['admin/reports' => t('Reports')];
375 $this->assertBreadcrumb('admin/reports/dblog', $trail, t('Recent log messages'));
376 $this->assertSession()->statusCodeNotEquals(403);
378 // Ensure that the breadcrumb is safe against XSS.
379 $this->drupalGet('menu-test/breadcrumb1/breadcrumb2/breadcrumb3');
380 $this->assertRaw('<script>alert(12);</script>');
381 $this->assertEscaped('<script>alert(123);</script>');