You are here

function UrlTest::testLinkAttributes in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Common/UrlTest.php \Drupal\system\Tests\Common\UrlTest::testLinkAttributes()

Tests that default and custom attributes are handled correctly on links.

File

core/modules/system/src/Tests/Common/UrlTest.php, line 79
Contains \Drupal\system\Tests\Common\UrlTest.

Class

UrlTest
Confirm that \Drupal\Core\Url, \Drupal\Component\Utility\UrlHelper::filterQueryParameters(), \Drupal\Component\Utility\UrlHelper::buildQuery(), and \Drupal\Core\Utility\LinkGeneratorInterface::generate() work correctly with various input.

Namespace

Drupal\system\Tests\Common

Code

function testLinkAttributes() {

  /** @var \Drupal\Core\Render\RendererInterface $renderer */
  $renderer = $this->container
    ->get('renderer');

  // Test that hreflang is added when a link has a known language.
  $language = new Language(array(
    'id' => 'fr',
    'name' => 'French',
  ));
  $hreflang_link = array(
    '#type' => 'link',
    '#options' => array(
      'language' => $language,
    ),
    '#url' => Url::fromUri('https://www.drupal.org'),
    '#title' => 'bar',
  );
  $langcode = $language
    ->getId();

  // Test that the default hreflang handling for links does not override a
  // hreflang attribute explicitly set in the render array.
  $hreflang_override_link = $hreflang_link;
  $hreflang_override_link['#options']['attributes']['hreflang'] = 'foo';
  $rendered = $renderer
    ->renderRoot($hreflang_link);
  $this
    ->assertTrue($this
    ->hasAttribute('hreflang', $rendered, $langcode), format_string('hreflang attribute with value @langcode is present on a rendered link when langcode is provided in the render array.', array(
    '@langcode' => $langcode,
  )));
  $rendered = $renderer
    ->renderRoot($hreflang_override_link);
  $this
    ->assertTrue($this
    ->hasAttribute('hreflang', $rendered, 'foo'), format_string('hreflang attribute with value @hreflang is present on a rendered link when @hreflang is provided in the render array.', array(
    '@hreflang' => 'foo',
  )));

  // Test the active class in links produced by
  // \Drupal\Core\Utility\LinkGeneratorInterface::generate() and #type 'link'.
  $options_no_query = array();
  $options_query = array(
    'query' => array(
      'foo' => 'bar',
      'one' => 'two',
    ),
  );
  $options_query_reverse = array(
    'query' => array(
      'one' => 'two',
      'foo' => 'bar',
    ),
  );

  // Test #type link.
  $path = 'common-test/type-link-active-class';
  $this
    ->drupalGet($path, $options_no_query);
  $links = $this
    ->xpath('//a[@href = :href and contains(@class, :class)]', array(
    ':href' => Url::fromRoute('common_test.l_active_class', [], $options_no_query)
      ->toString(),
    ':class' => 'is-active',
  ));
  $this
    ->assertTrue(isset($links[0]), 'A link generated by the link generator to the current page is marked active.');
  $links = $this
    ->xpath('//a[@href = :href and not(contains(@class, :class))]', array(
    ':href' => Url::fromRoute('common_test.l_active_class', [], $options_query)
      ->toString(),
    ':class' => 'is-active',
  ));
  $this
    ->assertTrue(isset($links[0]), 'A link generated by the link generator 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::fromRoute('common_test.l_active_class', [], $options_query)
      ->toString(),
    ':class' => 'is-active',
  ));
  $this
    ->assertTrue(isset($links[0]), 'A link generated by the link generator 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::fromRoute('common_test.l_active_class', [], $options_query_reverse)
      ->toString(),
    ':class' => 'is-active',
  ));
  $this
    ->assertTrue(isset($links[0]), 'A link generated by the link generator 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::fromRoute('common_test.l_active_class', [], $options_no_query)
      ->toString(),
    ':class' => 'is-active',
  ));
  $this
    ->assertTrue(isset($links[0]), 'A link generated by the link generator to the current page without a query string when the current page has a query string is not marked active.');

  // Test adding a custom class in links produced by
  // \Drupal\Core\Utility\LinkGeneratorInterface::generate() and #type 'link'.
  // Test the link generator.
  $class_l = $this
    ->randomMachineName();
  $link_l = \Drupal::l($this
    ->randomMachineName(), new Url('<current>', [], [
    'attributes' => [
      'class' => [
        $class_l,
      ],
    ],
  ]));
  $this
    ->assertTrue($this
    ->hasAttribute('class', $link_l, $class_l), format_string('Custom class @class is present on link when requested by l()', array(
    '@class' => $class_l,
  )));

  // Test #type.
  $class_theme = $this
    ->randomMachineName();
  $type_link = array(
    '#type' => 'link',
    '#title' => $this
      ->randomMachineName(),
    '#url' => Url::fromRoute('<current>'),
    '#options' => array(
      'attributes' => array(
        'class' => array(
          $class_theme,
        ),
      ),
    ),
  );
  $link_theme = $renderer
    ->renderRoot($type_link);
  $this
    ->assertTrue($this
    ->hasAttribute('class', $link_theme, $class_theme), format_string('Custom class @class is present on link when requested by #type', array(
    '@class' => $class_theme,
  )));
}