You are here

protected function MenuTrailByPathTestCase::assertMenuActiveTrail in Menu Trail By Path 7.3

Same name and namespace in other branches
  1. 7.2 menu_trail_by_path.test \MenuTrailByPathTestCase::assertMenuActiveTrail()

Based on: public/modules/simpletest/tests/menu.test:61

Assert that active trail exists in a menu tree output.

Parameters

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.

bool $last_active: Whether the last link in $tree is expected to be active (TRUE) or just to be in the active trail (FALSE).

8 calls to MenuTrailByPathTestCase::assertMenuActiveTrail()
MenuTrailByPathTestCase::testUrlHome in ./menu_trail_by_path.test
Test url: Home
MenuTrailByPathTestCase::testUrlNews in ./menu_trail_by_path.test
Test url: News
MenuTrailByPathTestCase::testUrlNewsCategorya in ./menu_trail_by_path.test
Test url: News » Category A
MenuTrailByPathTestCase::testUrlNewsCategoryaItema in ./menu_trail_by_path.test
Test url: News » Category A » Item A
MenuTrailByPathTestCase::testUrlNewsNewsOverview in ./menu_trail_by_path.test
Test url: News » News overview

... See full list

File

./menu_trail_by_path.test, line 258
Tests for menu_trail_by_path module.

Class

MenuTrailByPathTestCase
@file Tests for menu_trail_by_path module.

Code

protected function assertMenuActiveTrail($tree, $last_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)]/a[contains(@href, :href) and contains(text(), :title)]';
      $part_args = array(
        ':class' => 'active-trail',
        ':href' => url($link_path),
        ':title' => $link_title,
      );
      $xpath .= $this
        ->buildXPathQuery($part_xpath, $part_args);
      $i++;
    }
    $elements = $this
      ->xpath($xpath);
    $this
      ->assertTrue(!empty($elements), 'Active trail to current page was found in menu tree.');

    // 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 = array(
    ':class-trail' => 'active-trail',
    ':class-active' => 'active',
    ':href' => url($active_link_path),
    ':title' => $active_link_title,
  );
  $elements = $this
    ->xpath($xpath, $args);
  $this
    ->assertTrue(!empty($elements), format_string('Active link %title was found in menu tree, including active trail links %tree.', array(
    '%title' => $active_link_title,
    '%tree' => implode(' » ', $tree),
  )));
}