3 namespace Drupal\Tests\system\Functional\Menu;
5 use Drupal\Component\Utility\Html;
9 * Provides test assertions for verifying breadcrumbs.
11 trait AssertBreadcrumbTrait {
13 use AssertMenuActiveTrailTrait;
16 * Assert that a given path shows certain breadcrumb links.
18 * @param \Drupal\Core\Url|string $goto
19 * (optional) A path or URL to pass to
20 * Drupal\simpletest\WebTestBase::drupalGet().
22 * An associative array whose keys are expected breadcrumb link paths and
23 * whose values are expected breadcrumb link texts (not sanitized).
24 * @param string $page_title
25 * (optional) A page title to additionally assert via
26 * Drupal\simpletest\WebTestBase::assertTitle(). Without site name suffix.
28 * (optional) An associative array whose keys are link paths and whose
29 * values are link titles (not sanitized) of an expected active trail in a
30 * menu tree output on the page.
32 * (optional) Whether the last link in $tree is expected to be active (TRUE)
33 * or just to be in the active trail (FALSE).
35 protected function assertBreadcrumb($goto, array $trail, $page_title = NULL, array $tree = [], $last_active = TRUE) {
37 $this->drupalGet($goto);
39 $this->assertBreadcrumbParts($trail);
41 // Additionally assert page title, if given.
42 if (isset($page_title)) {
43 $this->assertTitle(strtr('@title | Drupal', ['@title' => $page_title]));
46 // Additionally assert active trail in a menu tree output, if given.
48 $this->assertMenuActiveTrail($tree, $last_active);
53 * Assert that a trail exists in the internal browser.
56 * An associative array whose keys are expected breadcrumb link paths and
57 * whose values are expected breadcrumb link texts (not sanitized).
59 protected function assertBreadcrumbParts($trail) {
60 // Compare paths with actual breadcrumb.
61 $parts = $this->getBreadcrumbParts();
63 // There may be more than one breadcrumb on the page. If $trail is empty
64 // this test would go into an infinite loop, so we need to check that too.
65 while ($trail && !empty($parts)) {
66 foreach ($trail as $path => $title) {
67 // If the path is empty, generate the path from the <front> route. If
68 // the path does not start with a leading slash, then run it through
69 // Url::fromUri('base:')->toString() to get the correct base
72 $url = Url::fromRoute('<front>')->toString();
74 elseif ($path[0] != '/') {
75 $url = Url::fromUri('base:' . $path)->toString();
80 $part = array_shift($parts);
81 $pass = ($pass && $part['href'] === $url && $part['text'] === Html::escape($title));
84 // No parts must be left, or an expected "Home" will always pass.
85 $pass = ($pass && empty($parts));
87 $this->assertTrue($pass, format_string('Breadcrumb %parts found on @path.', [
88 '%parts' => implode(' ยป ', $trail),
89 '@path' => $this->getUrl(),
94 * Returns the breadcrumb contents of the current page in the internal browser.
96 protected function getBreadcrumbParts() {
98 $elements = $this->xpath('//nav[@class="breadcrumb"]/ol/li/a');
99 if (!empty($elements)) {
100 foreach ($elements as $element) {
102 'text' => $element->getText(),
103 'href' => $element->getAttribute('href'),
104 'title' => $element->getAttribute('title'),