function AssertBreadcrumbTrait::assertBreadcrumbParts

Same name in other branches
  1. 9 core/modules/system/tests/src/Functional/Menu/AssertBreadcrumbTrait.php \Drupal\Tests\system\Functional\Menu\AssertBreadcrumbTrait::assertBreadcrumbParts()
  2. 8.9.x core/modules/system/src/Tests/Menu/AssertBreadcrumbTrait.php \Drupal\system\Tests\Menu\AssertBreadcrumbTrait::assertBreadcrumbParts()
  3. 8.9.x core/modules/system/tests/src/Functional/Menu/AssertBreadcrumbTrait.php \Drupal\Tests\system\Functional\Menu\AssertBreadcrumbTrait::assertBreadcrumbParts()
  4. 10 core/modules/system/tests/src/Functional/Menu/AssertBreadcrumbTrait.php \Drupal\Tests\system\Functional\Menu\AssertBreadcrumbTrait::assertBreadcrumbParts()

Assert that a trail exists in the internal browser.

Parameters

array $trail: An associative array whose keys are expected breadcrumb link paths and whose values are expected breadcrumb link texts (not sanitized).

1 call to AssertBreadcrumbTrait::assertBreadcrumbParts()
AssertBreadcrumbTrait::assertBreadcrumb in core/modules/system/tests/src/Functional/Menu/AssertBreadcrumbTrait.php
Assert that a given path shows certain breadcrumb links.

File

core/modules/system/tests/src/Functional/Menu/AssertBreadcrumbTrait.php, line 67

Class

AssertBreadcrumbTrait
Provides test assertions for verifying breadcrumbs.

Namespace

Drupal\Tests\system\Functional\Menu

Code

protected function assertBreadcrumbParts($trail) {
    // Compare paths with actual breadcrumb.
    $parts = $this->getBreadcrumbParts();
    $found = $parts;
    $pass = TRUE;
    if (!empty($trail) && !empty($parts)) {
        foreach ($trail as $path => $title) {
            // If the path is empty, generate the path from the <front> route.  If
            // the path does not start with a leading slash, then run it through
            // Url::fromUri('base:')->toString() to get the correct base
            // prepended.
            if ($path == '') {
                $url = Url::fromRoute('<front>')->toString();
            }
            elseif ($path[0] != '/') {
                $url = Url::fromUri('base:' . $path)->toString();
            }
            else {
                $url = $path;
            }
            $part = array_shift($parts);
            $pass = $pass && $part['href'] === $url && $part['text'] === Html::escape($title);
        }
    }
    elseif (!empty($trail) && empty($parts) || empty($trail) && !empty($parts)) {
        // Fail if there is no breadcrumb and we have a trail or breadcrumb
        // exists but trail is empty.
        $pass = FALSE;
    }
    // No parts must be left, or an expected "Home" will always pass.
    $pass = $pass && empty($parts);
    $this->assertTrue($pass, sprintf('Expected breadcrumb %s on %s but found %s.', implode(' » ', $trail), $this->getUrl(), implode(' » ', array_map(function (array $item) {
        return $item['text'];
    }, $found))));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.