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