function UrlTest::testLinkActiveClass

Tests for active class in links produced by l() and theme_link() functions.

File

drupal/core/modules/system/lib/Drupal/system/Tests/Common/UrlTest.php, line 56
Definition of Drupal\system\Tests\Common\UrlTest.

Class

UrlTest
Tests for URL generation functions.

Namespace

Drupal\system\Tests\Common

Code

function testLinkActiveClass() {
  $options_no_query = array();
  $options_query = array(
    'query' => array(
      'foo' => 'bar',
      'one' => 'two',
    ),
  );
  $options_query_reverse = array(
    'query' => array(
      'one' => 'two',
      'foo' => 'bar',
    ),
  );

  // Test l().
  $path = 'common-test/l-active-class';
  $this
    ->drupalGet($path, $options_no_query);
  $links = $this
    ->xpath('//a[@href = :href and contains(@class, :class)]', array(
    ':href' => url($path, $options_no_query),
    ':class' => 'active',
  ));
  $this
    ->assertTrue(isset($links[0]), 'A link generated by l() to the current page is marked active.');
  $links = $this
    ->xpath('//a[@href = :href and not(contains(@class, :class))]', array(
    ':href' => url($path, $options_query),
    ':class' => 'active',
  ));
  $this
    ->assertTrue(isset($links[0]), 'A link generated by l() to the current page with a query string when the current page has no query string is not marked active.');
  $this
    ->drupalGet($path, $options_query);
  $links = $this
    ->xpath('//a[@href = :href and contains(@class, :class)]', array(
    ':href' => url($path, $options_query),
    ':class' => 'active',
  ));
  $this
    ->assertTrue(isset($links[0]), 'A link generated by l() to the current page with a query string that matches the current query string is marked active.');
  $links = $this
    ->xpath('//a[@href = :href and contains(@class, :class)]', array(
    ':href' => url($path, $options_query_reverse),
    ':class' => 'active',
  ));
  $this
    ->assertTrue(isset($links[0]), 'A link generated by l() to the current page with a query string that has matching parameters to the current query string but in a different order is marked active.');
  $links = $this
    ->xpath('//a[@href = :href and not(contains(@class, :class))]', array(
    ':href' => url($path, $options_no_query),
    ':class' => 'active',
  ));
  $this
    ->assertTrue(isset($links[0]), 'A link generated by l() to the current page without a query string when the current page has a query string is not marked active.');

  // Test #theme.
  $path = 'common-test/theme-link-active-class';
  $this
    ->drupalGet($path, $options_no_query);
  $links = $this
    ->xpath('//a[@href = :href and contains(@class, :class)]', array(
    ':href' => url($path, $options_no_query),
    ':class' => 'active',
  ));
  $this
    ->assertTrue(isset($links[0]), 'A link generated by #theme to the current page is marked active.');
  $links = $this
    ->xpath('//a[@href = :href and not(contains(@class, :class))]', array(
    ':href' => url($path, $options_query),
    ':class' => 'active',
  ));
  $this
    ->assertTrue(isset($links[0]), 'A link generated by #theme to the current page with a query string when the current page has no query string is not marked active.');
  $this
    ->drupalGet($path, $options_query);
  $links = $this
    ->xpath('//a[@href = :href and contains(@class, :class)]', array(
    ':href' => url($path, $options_query),
    ':class' => 'active',
  ));
  $this
    ->assertTrue(isset($links[0]), 'A link generated by #theme to the current page with a query string that matches the current query string is marked active.');
  $links = $this
    ->xpath('//a[@href = :href and contains(@class, :class)]', array(
    ':href' => url($path, $options_query_reverse),
    ':class' => 'active',
  ));
  $this
    ->assertTrue(isset($links[0]), 'A link generated by #theme to the current page with a query string that has matching parameters to the current query string but in a different order is marked active.');
  $links = $this
    ->xpath('//a[@href = :href and not(contains(@class, :class))]', array(
    ':href' => url($path, $options_no_query),
    ':class' => 'active',
  ));
  $this
    ->assertTrue(isset($links[0]), 'A link generated by #theme to the current page without a query string when the current page has a query string is not marked active.');
}