AssertMenuActiveTrailTrait.php

Same filename and directory in other branches
  1. 9 core/modules/system/tests/src/Functional/Menu/AssertMenuActiveTrailTrait.php
  2. 8.9.x core/modules/system/src/Tests/Menu/AssertMenuActiveTrailTrait.php
  3. 8.9.x core/modules/system/tests/src/Functional/Menu/AssertMenuActiveTrailTrait.php
  4. 10 core/modules/system/tests/src/Functional/Menu/AssertMenuActiveTrailTrait.php

Namespace

Drupal\Tests\system\Functional\Menu

File

core/modules/system/tests/src/Functional/Menu/AssertMenuActiveTrailTrait.php

View source
<?php

declare (strict_types=1);
namespace Drupal\Tests\system\Functional\Menu;

use Drupal\Core\Url;

/**
 * Provides test assertions for verifying the active menu trail.
 */
trait AssertMenuActiveTrailTrait {
    
    /**
     * Assert that active trail exists in a menu tree output.
     *
     * @param array $tree
     *   An associative array whose keys are link paths and whose
     *   values are link titles (not sanitized) of an expected active trail in a
     *   menu tree output on the page.
     * @param bool $last_active
     *   Whether the last link in $tree is expected to be active (TRUE)
     *   or just to be in the active trail (FALSE).
     * @param string $active_trail_class
     *   (optional) The class of the active trail. Defaults to
     *   'menu-item--active-trail'.
     * @param string $active_class
     *   (optional) The class of the active element. Defaults to 'is-active'.
     */
    protected function assertMenuActiveTrail($tree, $last_active, $active_trail_class = 'menu-item--active-trail', $active_class = 'is-active') {
        $active_link_path = array_key_last($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++;
            }
            $this->assertSession()
                ->elementExists('xpath', $xpath);
            // Append prefix for active link asserted below.
            $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,
        ];
        $this->assertSession()
            ->elementExists('xpath', $this->assertSession()
            ->buildXPathQuery($xpath, $args));
    }

}

Traits

Title Deprecated Summary
AssertMenuActiveTrailTrait Provides test assertions for verifying the active menu trail.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.