X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fsystem%2Fsrc%2FTests%2FMenu%2FAssertBreadcrumbTrait.php;fp=web%2Fcore%2Fmodules%2Fsystem%2Fsrc%2FTests%2FMenu%2FAssertBreadcrumbTrait.php;h=a18f45cd3f4bb3f4a123a26210ad044a5e90343d;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/core/modules/system/src/Tests/Menu/AssertBreadcrumbTrait.php b/web/core/modules/system/src/Tests/Menu/AssertBreadcrumbTrait.php new file mode 100644 index 000000000..a18f45cd3 --- /dev/null +++ b/web/core/modules/system/src/Tests/Menu/AssertBreadcrumbTrait.php @@ -0,0 +1,111 @@ +drupalGet($goto); + } + $this->assertBreadcrumbParts($trail); + + // Additionally assert page title, if given. + if (isset($page_title)) { + $this->assertTitle(strtr('@title | Drupal', ['@title' => $page_title])); + } + + // Additionally assert active trail in a menu tree output, if given. + if ($tree) { + $this->assertMenuActiveTrail($tree, $last_active); + } + } + + /** + * Assert that a trail exists in the internal browser. + * + * @param array $trail + * An associative array whose keys are expected breadcrumb link paths and + * whose values are expected breadcrumb link texts (not sanitized). + */ + protected function assertBreadcrumbParts($trail) { + // Compare paths with actual breadcrumb. + $parts = $this->getBreadcrumbParts(); + $pass = TRUE; + // There may be more than one breadcrumb on the page. If $trail is empty + // this test would go into an infinite loop, so we need to check that too. + while ($trail && !empty($parts)) { + foreach ($trail as $path => $title) { + // If the path is empty, generate the path from the 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('')->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)); + } + } + // No parts must be left, or an expected "Home" will always pass. + $pass = ($pass && empty($parts)); + + $this->assertTrue($pass, format_string('Breadcrumb %parts found on @path.', [ + '%parts' => implode(' » ', $trail), + '@path' => $this->getUrl(), + ])); + } + + /** + * Returns the breadcrumb contents of the current page in the internal browser. + */ + protected function getBreadcrumbParts() { + $parts = []; + $elements = $this->xpath('//nav[@class="breadcrumb"]/ol/li/a'); + if (!empty($elements)) { + foreach ($elements as $element) { + $parts[] = [ + 'text' => (string) $element, + 'href' => (string) $element['href'], + 'title' => (string) $element['title'], + ]; + } + } + return $parts; + } + +}