View source
<?php
namespace Drupal\Tests\system\Functional\Menu;
use Drupal\Core\Url;
trait AssertMenuActiveTrailTrait {
protected function assertMenuActiveTrail($tree, $last_active, $active_trail_class = 'menu-item--active-trail', $active_class = 'is-active') {
end($tree);
$active_link_path = key($tree);
$active_link_title = array_pop($tree);
$xpath = '';
if ($tree) {
$i = 0;
foreach ($tree as $link_path => $link_title) {
$part_xpath = !$i ? '//' : '/following-sibling::ul/descendant::';
$part_xpath .= 'li[contains(@class, :class-trail)]/a[contains(@href, :href) and contains(text(), :title)]';
$part_args = [
':class-trail' => $active_trail_class,
':href' => Url::fromUri('base:' . $link_path)
->toString(),
':title' => $link_title,
];
$xpath .= $this
->assertSession()
->buildXPathQuery($part_xpath, $part_args);
$i++;
}
$elements = $this
->xpath($xpath);
$this
->assertNotEmpty($elements, 'Active trail to current page should be visible in menu tree.');
$xpath .= '/following-sibling::ul/descendant::';
}
else {
$xpath .= '//';
}
$xpath_last_active = $last_active ? 'and contains(@class, :class-active)' : '';
$xpath .= 'li[contains(@class, :class-trail)]/a[contains(@href, :href) ' . $xpath_last_active . 'and contains(text(), :title)]';
$args = [
':class-trail' => $active_trail_class,
':class-active' => $active_class,
':href' => Url::fromUri('base:' . $active_link_path)
->toString(),
':title' => $active_link_title,
];
$elements = $this
->xpath($xpath, $args);
$this
->assertNotEmpty($elements, sprintf('Active link %s should be visible in menu tree, including active trail links %s.', $active_link_title, implode(' » ', $tree)));
}
}