function LinkGeneratorTest::testGenerateWithHtml

Same name in other branches
  1. 8.9.x core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php \Drupal\Tests\Core\Utility\LinkGeneratorTest::testGenerateWithHtml()
  2. 10 core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php \Drupal\Tests\Core\Utility\LinkGeneratorTest::testGenerateWithHtml()
  3. 11.x 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 421

Class

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

Namespace

Drupal\Tests\Core\Utility

Code

public function testGenerateWithHtml() {
    $this->urlGenerator
        ->expects($this->exactly(2))
        ->method('generateFromRoute')
        ->with('test_route_5', [], $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', [], [
        'attributes' => [
            'title' => '<em>HTML Tooltip</em>',
        ],
    ]);
    $url->setUrlGenerator($this->urlGenerator);
    $result = $this->linkGenerator
        ->generate('Test', $url);
    $this->assertLink([
        'attributes' => [
            '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', []);
    $url->setUrlGenerator($this->urlGenerator);
    $result = $this->linkGenerator
        ->generate(Markup::create('<em>HTML output</em>'), $url);
    $this->assertLink([
        'attributes' => [
            'href' => '/test-route-5',
        ],
        'child' => [
            'tag' => 'em',
        ],
    ], $result);
    $this->assertStringContainsString('<em>HTML output</em>', $result);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.