You are here

public function LinkGeneratorTest::testGenerateWithHtml in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php \Drupal\Tests\Core\Utility\LinkGeneratorTest::testGenerateWithHtml()

Tests the link method with html.

See also

\Drupal\Core\Utility\LinkGenerator::generate()

File

core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php, line 336
Contains \Drupal\Tests\Core\Utility\LinkGeneratorTest.

Class

LinkGeneratorTest
@coversDefaultClass \Drupal\Core\Utility\LinkGenerator @group Utility

Namespace

Drupal\Tests\Core\Utility

Code

public function testGenerateWithHtml() {
  $this->urlGenerator
    ->expects($this
    ->at(0))
    ->method('generateFromRoute')
    ->with('test_route_5', array(), $this->defaultOptions)
    ->willReturn((new GeneratedUrl())
    ->setGeneratedUrl('/test-route-5'));
  $this->urlGenerator
    ->expects($this
    ->at(1))
    ->method('generateFromRoute')
    ->with('test_route_5', array(), $this->defaultOptions)
    ->willReturn((new GeneratedUrl())
    ->setGeneratedUrl('/test-route-5'));

  // Test that HTML tags are stripped from the 'title' attribute.
  $url = new Url('test_route_5', array(), array(
    'attributes' => array(
      'title' => '<em>HTML Tooltip</em>',
    ),
  ));
  $url
    ->setUrlGenerator($this->urlGenerator);
  $result = $this->linkGenerator
    ->generate('Test', $url);
  $this
    ->assertLink(array(
    'attributes' => array(
      'href' => '/test-route-5',
      'title' => 'HTML Tooltip',
    ),
  ), $result);

  // Test that safe HTML is output inside the anchor tag unescaped. The
  // Markup::create() call is an intentional unit test for the interaction
  // between MarkupInterface and the LinkGenerator.
  $url = new Url('test_route_5', array());
  $url
    ->setUrlGenerator($this->urlGenerator);
  $result = $this->linkGenerator
    ->generate(Markup::create('<em>HTML output</em>'), $url);
  $this
    ->assertLink(array(
    'attributes' => array(
      'href' => '/test-route-5',
    ),
    'child' => array(
      'tag' => 'em',
    ),
  ), $result);
  $this
    ->assertTrue(strpos($result, '<em>HTML output</em>') !== FALSE);
}