fd0dc7cf5657060a14d131f34e3a41b000240e4c
[yaffs-website] / web / core / modules / system / tests / src / Functional / Menu / AssertBreadcrumbTrait.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\Menu;
4
5 use Drupal\Component\Utility\Html;
6 use Drupal\Core\Url;
7
8 /**
9  * Provides test assertions for verifying breadcrumbs.
10  */
11 trait AssertBreadcrumbTrait {
12
13   use AssertMenuActiveTrailTrait;
14
15   /**
16    * Assert that a given path shows certain breadcrumb links.
17    *
18    * @param \Drupal\Core\Url|string $goto
19    *   (optional) A path or URL to pass to
20    *   Drupal\simpletest\WebTestBase::drupalGet().
21    * @param array $trail
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.
27    * @param array $tree
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.
31    * @param $last_active
32    *   (optional) Whether the last link in $tree is expected to be active (TRUE)
33    *   or just to be in the active trail (FALSE).
34    */
35   protected function assertBreadcrumb($goto, array $trail, $page_title = NULL, array $tree = [], $last_active = TRUE) {
36     if (isset($goto)) {
37       $this->drupalGet($goto);
38     }
39     $this->assertBreadcrumbParts($trail);
40
41     // Additionally assert page title, if given.
42     if (isset($page_title)) {
43       $this->assertTitle(strtr('@title | Drupal', ['@title' => $page_title]));
44     }
45
46     // Additionally assert active trail in a menu tree output, if given.
47     if ($tree) {
48       $this->assertMenuActiveTrail($tree, $last_active);
49     }
50   }
51
52   /**
53    * Assert that a trail exists in the internal browser.
54    *
55    * @param array $trail
56    *   An associative array whose keys are expected breadcrumb link paths and
57    *   whose values are expected breadcrumb link texts (not sanitized).
58    */
59   protected function assertBreadcrumbParts($trail) {
60     // Compare paths with actual breadcrumb.
61     $parts = $this->getBreadcrumbParts();
62     $pass = TRUE;
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
70         // prepended.
71         if ($path == '') {
72           $url = Url::fromRoute('<front>')->toString();
73         }
74         elseif ($path[0] != '/') {
75           $url = Url::fromUri('base:' . $path)->toString();
76         }
77         else {
78           $url = $path;
79         }
80         $part = array_shift($parts);
81         $pass = ($pass && $part['href'] === $url && $part['text'] === Html::escape($title));
82       }
83     }
84     // No parts must be left, or an expected "Home" will always pass.
85     $pass = ($pass && empty($parts));
86
87     $this->assertTrue($pass, format_string('Breadcrumb %parts found on @path.', [
88       '%parts' => implode(' ยป ', $trail),
89       '@path' => $this->getUrl(),
90     ]));
91   }
92
93   /**
94    * Returns the breadcrumb contents of the current page in the internal browser.
95    */
96   protected function getBreadcrumbParts() {
97     $parts = [];
98     $elements = $this->xpath('//nav[@class="breadcrumb"]/ol/li/a');
99     if (!empty($elements)) {
100       foreach ($elements as $element) {
101         $parts[] = [
102           'text' => $element->getText(),
103           'href' => $element->getAttribute('href'),
104           'title' => $element->getAttribute('title'),
105         ];
106       }
107     }
108     return $parts;
109   }
110
111 }