Mercurial > hg > isophonics-drupal-site
comparison core/modules/system/src/Tests/Menu/AssertBreadcrumbTrait.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\system\Tests\Menu; | |
4 | |
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); | |
6 | |
7 use Drupal\Component\Utility\Html; | |
8 use Drupal\Core\Url; | |
9 | |
10 /** | |
11 * Provides test assertions for verifying breadcrumbs. | |
12 * | |
13 * @deprecated Scheduled for removal in Drupal 9.0.0. | |
14 * Use \Drupal\Tests\system\Functional\Menu\AssertBreadcrumbTrait instead. | |
15 */ | |
16 trait AssertBreadcrumbTrait { | |
17 | |
18 use AssertMenuActiveTrailTrait; | |
19 | |
20 /** | |
21 * Assert that a given path shows certain breadcrumb links. | |
22 * | |
23 * @param \Drupal\Core\Url|string $goto | |
24 * (optional) A path or URL to pass to | |
25 * Drupal\simpletest\WebTestBase::drupalGet(). | |
26 * @param array $trail | |
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. | |
32 * @param array $tree | |
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. | |
36 * @param $last_active | |
37 * (optional) Whether the last link in $tree is expected to be active (TRUE) | |
38 * or just to be in the active trail (FALSE). | |
39 */ | |
40 protected function assertBreadcrumb($goto, array $trail, $page_title = NULL, array $tree = [], $last_active = TRUE) { | |
41 if (isset($goto)) { | |
42 $this->drupalGet($goto); | |
43 } | |
44 $this->assertBreadcrumbParts($trail); | |
45 | |
46 // Additionally assert page title, if given. | |
47 if (isset($page_title)) { | |
48 $this->assertTitle(strtr('@title | Drupal', ['@title' => $page_title])); | |
49 } | |
50 | |
51 // Additionally assert active trail in a menu tree output, if given. | |
52 if ($tree) { | |
53 $this->assertMenuActiveTrail($tree, $last_active); | |
54 } | |
55 } | |
56 | |
57 /** | |
58 * Assert that a trail exists in the internal browser. | |
59 * | |
60 * @param array $trail | |
61 * An associative array whose keys are expected breadcrumb link paths and | |
62 * whose values are expected breadcrumb link texts (not sanitized). | |
63 */ | |
64 protected function assertBreadcrumbParts($trail) { | |
65 // Compare paths with actual breadcrumb. | |
66 $parts = $this->getBreadcrumbParts(); | |
67 $pass = TRUE; | |
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 | |
75 // prepended. | |
76 if ($path == '') { | |
77 $url = Url::fromRoute('<front>')->toString(); | |
78 } | |
79 elseif ($path[0] != '/') { | |
80 $url = Url::fromUri('base:' . $path)->toString(); | |
81 } | |
82 else { | |
83 $url = $path; | |
84 } | |
85 $part = array_shift($parts); | |
86 $pass = ($pass && $part['href'] === $url && $part['text'] === Html::escape($title)); | |
87 } | |
88 } | |
89 // No parts must be left, or an expected "Home" will always pass. | |
90 $pass = ($pass && empty($parts)); | |
91 | |
92 $this->assertTrue($pass, format_string('Breadcrumb %parts found on @path.', [ | |
93 '%parts' => implode(' ยป ', $trail), | |
94 '@path' => $this->getUrl(), | |
95 ])); | |
96 } | |
97 | |
98 /** | |
99 * Returns the breadcrumb contents of the current page in the internal browser. | |
100 */ | |
101 protected function getBreadcrumbParts() { | |
102 $parts = []; | |
103 $elements = $this->xpath('//nav[@class="breadcrumb"]/ol/li/a'); | |
104 if (!empty($elements)) { | |
105 foreach ($elements as $element) { | |
106 $parts[] = [ | |
107 'text' => (string) $element, | |
108 'href' => (string) $element['href'], | |
109 'title' => (string) $element['title'], | |
110 ]; | |
111 } | |
112 } | |
113 return $parts; | |
114 } | |
115 | |
116 } |