3 namespace Drupal\system\Tests\Menu;
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);
10 * Provides test assertions for verifying the active menu trail.
12 * @deprecated Scheduled for removal in Drupal 9.0.0.
13 * Use \Drupal\Tests\system\Functional\Menu\AssertMenuActiveTrailTrait instead.
15 trait AssertMenuActiveTrailTrait {
18 * Assert that active trail exists in a menu tree output.
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).
28 protected function assertMenuActiveTrail($tree, $last_active) {
30 $active_link_path = key($tree);
31 $active_link_title = array_pop($tree);
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)]';
39 ':class' => 'menu-item--active-trail',
40 ':href' => Url::fromUri('base:' . $link_path)->toString(),
41 ':title' => $link_title,
43 $xpath .= $this->buildXPathQuery($part_xpath, $part_args);
46 $elements = $this->xpath($xpath);
47 $this->assertTrue(!empty($elements), 'Active trail to current page was found in menu tree.');
49 // Append prefix for active link asserted below.
50 $xpath .= '/following-sibling::ul/descendant::';
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)]';
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,
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),