3 namespace Drupal\system\Tests\Menu;
5 @trigger_error(__NAMESPACE__ . '\AssertBreadcrumbTrait is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Instead, use \Drupal\Tests\system\Functional\Menu\AssertBreadcrumbTrait', E_USER_DEPRECATED);
7 use Drupal\Component\Utility\Html;
11 * Provides test assertions for verifying breadcrumbs.
13 * @deprecated Scheduled for removal in Drupal 9.0.0.
14 * Use \Drupal\Tests\system\Functional\Menu\AssertBreadcrumbTrait instead.
16 trait AssertBreadcrumbTrait {
18 use AssertMenuActiveTrailTrait;
21 * Assert that a given path shows certain breadcrumb links.
23 * @param \Drupal\Core\Url|string $goto
24 * (optional) A path or URL to pass to
25 * Drupal\simpletest\WebTestBase::drupalGet().
27 * An associative array whose keys are expected breadcrumb link paths and
28 * whose values are expected breadcrumb link texts (not sanitized).
29 * @param string $page_title
30 * (optional) A page title to additionally assert via
31 * Drupal\simpletest\WebTestBase::assertTitle(). Without site name suffix.
33 * (optional) An associative array whose keys are link paths and whose
34 * values are link titles (not sanitized) of an expected active trail in a
35 * menu tree output on the page.
37 * (optional) Whether the last link in $tree is expected to be active (TRUE)
38 * or just to be in the active trail (FALSE).
40 protected function assertBreadcrumb($goto, array $trail, $page_title = NULL, array $tree = [], $last_active = TRUE) {
42 $this->drupalGet($goto);
44 $this->assertBreadcrumbParts($trail);
46 // Additionally assert page title, if given.
47 if (isset($page_title)) {
48 $this->assertTitle(strtr('@title | Drupal', ['@title' => $page_title]));
51 // Additionally assert active trail in a menu tree output, if given.
53 $this->assertMenuActiveTrail($tree, $last_active);
58 * Assert that a trail exists in the internal browser.
61 * An associative array whose keys are expected breadcrumb link paths and
62 * whose values are expected breadcrumb link texts (not sanitized).
64 protected function assertBreadcrumbParts($trail) {
65 // Compare paths with actual breadcrumb.
66 $parts = $this->getBreadcrumbParts();
68 // There may be more than one breadcrumb on the page. If $trail is empty
69 // this test would go into an infinite loop, so we need to check that too.
70 while ($trail && !empty($parts)) {
71 foreach ($trail as $path => $title) {
72 // If the path is empty, generate the path from the <front> route. If
73 // the path does not start with a leading slash, then run it through
74 // Url::fromUri('base:')->toString() to get the correct base
77 $url = Url::fromRoute('<front>')->toString();
79 elseif ($path[0] != '/') {
80 $url = Url::fromUri('base:' . $path)->toString();
85 $part = array_shift($parts);
86 $pass = ($pass && $part['href'] === $url && $part['text'] === Html::escape($title));
89 // No parts must be left, or an expected "Home" will always pass.
90 $pass = ($pass && empty($parts));
92 $this->assertTrue($pass, format_string('Breadcrumb %parts found on @path.', [
93 '%parts' => implode(' ยป ', $trail),
94 '@path' => $this->getUrl(),
99 * Returns the breadcrumb contents of the current page in the internal browser.
101 protected function getBreadcrumbParts() {
103 $elements = $this->xpath('//nav[@class="breadcrumb"]/ol/li/a');
104 if (!empty($elements)) {
105 foreach ($elements as $element) {
107 'text' => (string) $element,
108 'href' => (string) $element['href'],
109 'title' => (string) $element['title'],