diff --git a/core/modules/system/lib/Drupal/system/Tests/Common/UrlTest.php b/core/modules/system/lib/Drupal/system/Tests/Common/UrlTest.php index eeb49cc..72f8af4 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Common/UrlTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Common/UrlTest.php @@ -41,25 +41,50 @@ function testLXSS() { /** * Tests for active class in l() function. */ - function testLActiveClass() { + 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'; - $options = array(); - $this->drupalGet($path, $options); - $links = $this->xpath('//a[@href = :href and contains(@class, :class)]', array(':href' => url($path, $options), ':class' => 'active')); - $this->assertTrue(isset($links[0]), 'A link to the current page is marked active.'); + $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('link'). + $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(\'link\') 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(\'link\') to the current page with a query string when the current page has no query string is not marked active.'); - $options = array('query' => array('foo' => 'bar')); - $links = $this->xpath('//a[@href = :href and not(contains(@class, :class))]', array(':href' => url($path, $options), ':class' => 'active')); - $this->assertTrue(isset($links[0]), 'A link 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(\'link\') to the current page with a query string that matches the current query string is marked active.'); - $this->drupalGet($path, $options); - $links = $this->xpath('//a[@href = :href and contains(@class, :class)]', array(':href' => url($path, $options), ':class' => 'active')); - $this->assertTrue(isset($links[0]), 'A link 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(\'link\') 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.'); - $options = array(); - $links = $this->xpath('//a[@href = :href and not(contains(@class, :class))]', array(':href' => url($path, $options), ':class' => 'active')); - $this->assertTrue(isset($links[0]), 'A link to the current page without a query string when the current page has a query string is not 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(\'link\') to the current page without a query string when the current page has a query string is not marked active.'); } /** diff --git a/core/modules/system/tests/modules/common_test/common_test.module b/core/modules/system/tests/modules/common_test/common_test.module index f932f0f..829bd56 100644 --- a/core/modules/system/tests/modules/common_test/common_test.module +++ b/core/modules/system/tests/modules/common_test/common_test.module @@ -70,6 +70,12 @@ function common_test_menu() { 'access arguments' => array('access content'), 'type' => MENU_CALLBACK, ); + $items['common-test/theme-link-active-class'] = array( + 'title' => 'Test theme_link() adding of active class', + 'page callback' => 'common_test_theme_link_active_class', + 'access arguments' => array('access content'), + 'type' => MENU_CALLBACK, + ); return $items; } @@ -320,6 +326,8 @@ function common_test_cron() { /** * Page callback: Displays links to the current page, one with a query string. + * + * Using #type causes these links to be rendered with l(). */ function common_test_l_active_class() { return array( @@ -335,6 +343,55 @@ function common_test_l_active_class() { '#options' => array( 'query' => array( 'foo' => 'bar', + 'one' => 'two', + ), + ), + ), + 'with_query_reversed' => array( + '#type' => 'link', + '#title' => t('Link with the same query string in reverse order'), + '#href' => current_path(), + '#options' => array( + 'query' => array( + 'one' => 'two', + 'foo' => 'bar', + ), + ), + ), + ); +} + +/** + * Page callback: Displays links to the current page, one with a query string. + * + * Using #theme causes these links to be rendered with theme_link(). + */ +function common_test_theme_link_active_class() { + return array( + 'no_query' => array( + '#theme' => 'link', + '#text' => t('Link with no query string'), + '#path' => current_path(), + ), + 'with_query' => array( + '#theme' => 'link', + '#text' => t('Link with a query string'), + '#path' => current_path(), + '#options' => array( + 'query' => array( + 'foo' => 'bar', + 'one' => 'two', + ), + ), + ), + 'with_query_reversed' => array( + '#theme' => 'link', + '#text' => t('Link with the same query string in reverse order'), + '#path' => current_path(), + '#options' => array( + 'query' => array( + 'one' => 'two', + 'foo' => 'bar', ), ), ),