annotate core/modules/system/src/Tests/Menu/AssertBreadcrumbTrait.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\system\Tests\Menu;
Chris@0 4
Chris@0 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);
Chris@0 6
Chris@0 7 use Drupal\Component\Utility\Html;
Chris@0 8 use Drupal\Core\Url;
Chris@0 9
Chris@0 10 /**
Chris@0 11 * Provides test assertions for verifying breadcrumbs.
Chris@0 12 *
Chris@0 13 * @deprecated Scheduled for removal in Drupal 9.0.0.
Chris@0 14 * Use \Drupal\Tests\system\Functional\Menu\AssertBreadcrumbTrait instead.
Chris@0 15 */
Chris@0 16 trait AssertBreadcrumbTrait {
Chris@0 17
Chris@0 18 use AssertMenuActiveTrailTrait;
Chris@0 19
Chris@0 20 /**
Chris@0 21 * Assert that a given path shows certain breadcrumb links.
Chris@0 22 *
Chris@0 23 * @param \Drupal\Core\Url|string $goto
Chris@0 24 * (optional) A path or URL to pass to
Chris@0 25 * Drupal\simpletest\WebTestBase::drupalGet().
Chris@0 26 * @param array $trail
Chris@0 27 * An associative array whose keys are expected breadcrumb link paths and
Chris@0 28 * whose values are expected breadcrumb link texts (not sanitized).
Chris@0 29 * @param string $page_title
Chris@0 30 * (optional) A page title to additionally assert via
Chris@0 31 * Drupal\simpletest\WebTestBase::assertTitle(). Without site name suffix.
Chris@0 32 * @param array $tree
Chris@0 33 * (optional) An associative array whose keys are link paths and whose
Chris@0 34 * values are link titles (not sanitized) of an expected active trail in a
Chris@0 35 * menu tree output on the page.
Chris@0 36 * @param $last_active
Chris@0 37 * (optional) Whether the last link in $tree is expected to be active (TRUE)
Chris@0 38 * or just to be in the active trail (FALSE).
Chris@0 39 */
Chris@0 40 protected function assertBreadcrumb($goto, array $trail, $page_title = NULL, array $tree = [], $last_active = TRUE) {
Chris@0 41 if (isset($goto)) {
Chris@0 42 $this->drupalGet($goto);
Chris@0 43 }
Chris@0 44 $this->assertBreadcrumbParts($trail);
Chris@0 45
Chris@0 46 // Additionally assert page title, if given.
Chris@0 47 if (isset($page_title)) {
Chris@0 48 $this->assertTitle(strtr('@title | Drupal', ['@title' => $page_title]));
Chris@0 49 }
Chris@0 50
Chris@0 51 // Additionally assert active trail in a menu tree output, if given.
Chris@0 52 if ($tree) {
Chris@0 53 $this->assertMenuActiveTrail($tree, $last_active);
Chris@0 54 }
Chris@0 55 }
Chris@0 56
Chris@0 57 /**
Chris@0 58 * Assert that a trail exists in the internal browser.
Chris@0 59 *
Chris@0 60 * @param array $trail
Chris@0 61 * An associative array whose keys are expected breadcrumb link paths and
Chris@0 62 * whose values are expected breadcrumb link texts (not sanitized).
Chris@0 63 */
Chris@0 64 protected function assertBreadcrumbParts($trail) {
Chris@0 65 // Compare paths with actual breadcrumb.
Chris@0 66 $parts = $this->getBreadcrumbParts();
Chris@0 67 $pass = TRUE;
Chris@0 68 // There may be more than one breadcrumb on the page. If $trail is empty
Chris@0 69 // this test would go into an infinite loop, so we need to check that too.
Chris@0 70 while ($trail && !empty($parts)) {
Chris@0 71 foreach ($trail as $path => $title) {
Chris@0 72 // If the path is empty, generate the path from the <front> route. If
Chris@0 73 // the path does not start with a leading slash, then run it through
Chris@0 74 // Url::fromUri('base:')->toString() to get the correct base
Chris@0 75 // prepended.
Chris@0 76 if ($path == '') {
Chris@0 77 $url = Url::fromRoute('<front>')->toString();
Chris@0 78 }
Chris@0 79 elseif ($path[0] != '/') {
Chris@0 80 $url = Url::fromUri('base:' . $path)->toString();
Chris@0 81 }
Chris@0 82 else {
Chris@0 83 $url = $path;
Chris@0 84 }
Chris@0 85 $part = array_shift($parts);
Chris@0 86 $pass = ($pass && $part['href'] === $url && $part['text'] === Html::escape($title));
Chris@0 87 }
Chris@0 88 }
Chris@0 89 // No parts must be left, or an expected "Home" will always pass.
Chris@0 90 $pass = ($pass && empty($parts));
Chris@0 91
Chris@0 92 $this->assertTrue($pass, format_string('Breadcrumb %parts found on @path.', [
Chris@0 93 '%parts' => implode(' ยป ', $trail),
Chris@0 94 '@path' => $this->getUrl(),
Chris@0 95 ]));
Chris@0 96 }
Chris@0 97
Chris@0 98 /**
Chris@0 99 * Returns the breadcrumb contents of the current page in the internal browser.
Chris@0 100 */
Chris@0 101 protected function getBreadcrumbParts() {
Chris@0 102 $parts = [];
Chris@0 103 $elements = $this->xpath('//nav[@class="breadcrumb"]/ol/li/a');
Chris@0 104 if (!empty($elements)) {
Chris@0 105 foreach ($elements as $element) {
Chris@0 106 $parts[] = [
Chris@0 107 'text' => (string) $element,
Chris@0 108 'href' => (string) $element['href'],
Chris@0 109 'title' => (string) $element['title'],
Chris@0 110 ];
Chris@0 111 }
Chris@0 112 }
Chris@0 113 return $parts;
Chris@0 114 }
Chris@0 115
Chris@0 116 }