e64177a8c2123afe608cf17d16b65d5336405760
[yaffs-website] / web / core / modules / system / src / Tests / Menu / AssertMenuActiveTrailTrait.php
1 <?php
2
3 namespace Drupal\system\Tests\Menu;
4
5 @trigger_error(__NAMESPACE__ . '\AssertMenuActiveTrailTrait is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Instead, use \Drupal\Tests\system\Functional\Menu\AssertMenuActiveTrailTrait', E_USER_DEPRECATED);
6
7 use Drupal\Core\Url;
8
9 /**
10  * Provides test assertions for verifying the active menu trail.
11  *
12  * @deprecated Scheduled for removal in Drupal 9.0.0.
13  *   Use \Drupal\Tests\system\Functional\Menu\AssertMenuActiveTrailTrait instead.
14  */
15 trait AssertMenuActiveTrailTrait {
16
17   /**
18    * Assert that active trail exists in a menu tree output.
19    *
20    * @param array $tree
21    *   An associative array whose keys are link paths and whose
22    *   values are link titles (not sanitized) of an expected active trail in a
23    *   menu tree output on the page.
24    * @param bool $last_active
25    *   Whether the last link in $tree is expected to be active (TRUE)
26    *   or just to be in the active trail (FALSE).
27    */
28   protected function assertMenuActiveTrail($tree, $last_active) {
29     end($tree);
30     $active_link_path = key($tree);
31     $active_link_title = array_pop($tree);
32     $xpath = '';
33     if ($tree) {
34       $i = 0;
35       foreach ($tree as $link_path => $link_title) {
36         $part_xpath = (!$i ? '//' : '/following-sibling::ul/descendant::');
37         $part_xpath .= 'li[contains(@class, :class)]/a[contains(@href, :href) and contains(text(), :title)]';
38         $part_args = [
39           ':class' => 'menu-item--active-trail',
40           ':href' => Url::fromUri('base:' . $link_path)->toString(),
41           ':title' => $link_title,
42         ];
43         $xpath .= $this->buildXPathQuery($part_xpath, $part_args);
44         $i++;
45       }
46       $elements = $this->xpath($xpath);
47       $this->assertTrue(!empty($elements), 'Active trail to current page was found in menu tree.');
48
49       // Append prefix for active link asserted below.
50       $xpath .= '/following-sibling::ul/descendant::';
51     }
52     else {
53       $xpath .= '//';
54     }
55     $xpath_last_active = ($last_active ? 'and contains(@class, :class-active)' : '');
56     $xpath .= 'li[contains(@class, :class-trail)]/a[contains(@href, :href) ' . $xpath_last_active . 'and contains(text(), :title)]';
57     $args = [
58       ':class-trail' => 'menu-item--active-trail',
59       ':class-active' => 'is-active',
60       ':href' => Url::fromUri('base:' . $active_link_path)->toString(),
61       ':title' => $active_link_title,
62     ];
63     $elements = $this->xpath($xpath, $args);
64     $this->assertTrue(!empty($elements), format_string('Active link %title was found in menu tree, including active trail links %tree.', [
65       '%title' => $active_link_title,
66       '%tree' => implode(' ยป ', $tree),
67     ]));
68   }
69
70 }