class LinkGenerationTest
Same name and namespace in other branches
- 11.x core/tests/Drupal/KernelTests/Core/Url/LinkGenerationTest.php \Drupal\KernelTests\Core\Url\LinkGenerationTest
- 10 core/tests/Drupal/KernelTests/Core/Url/LinkGenerationTest.php \Drupal\KernelTests\Core\Url\LinkGenerationTest
- 8.9.x core/tests/Drupal/KernelTests/Core/Url/LinkGenerationTest.php \Drupal\KernelTests\Core\Url\LinkGenerationTest
Tests link generation with hooks.
@group Utility
Hierarchy
- class \Drupal\KernelTests\KernelTestBase extends \Drupal\Core\DependencyInjection\ServiceProviderInterface uses \Drupal\KernelTests\AssertLegacyTrait, \Drupal\KernelTests\AssertContentTrait, \Drupal\Tests\RandomGeneratorTrait, \Drupal\Tests\ConfigTestTrait, \Drupal\Tests\ExtensionListTestTrait, \Drupal\Tests\TestRequirementsTrait, \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait implements \PHPUnit\Framework\TestCase
- class \Drupal\KernelTests\Core\Url\LinkGenerationTest implements \Drupal\KernelTests\KernelTestBase
Expanded class hierarchy of LinkGenerationTest
File
-
core/
tests/ Drupal/ KernelTests/ Core/ Url/ LinkGenerationTest.php, line 15
Namespace
Drupal\KernelTests\Core\UrlView source
class LinkGenerationTest extends KernelTestBase {
protected static $modules = [
'link_generation_test',
];
/**
* Tests how hook_link_alter() can affect escaping of the link text.
*/
public function testHookLinkAlter() {
$url = Url::fromUri('http://example.com');
$renderer = \Drupal::service('renderer');
$link = $renderer->executeInRenderContext(new RenderContext(), function () use ($url) {
return \Drupal::service('link_generator')->generate([
'#markup' => '<em>link with markup</em>',
], $url);
});
$this->setRawContent($link);
$this->assertInstanceOf(MarkupInterface::class, $link);
// Ensure the content of the link is not escaped.
$this->assertRaw('<em>link with markup</em>');
// Test just adding text to an already safe string.
\Drupal::state()->set('link_generation_test_link_alter', TRUE);
$link = $renderer->executeInRenderContext(new RenderContext(), function () use ($url) {
return \Drupal::service('link_generator')->generate([
'#markup' => '<em>link with markup</em>',
], $url);
});
$this->setRawContent($link);
$this->assertInstanceOf(MarkupInterface::class, $link);
// Ensure the content of the link is escaped.
$this->assertEscaped('<em>link with markup</em> <strong>Test!</strong>');
// Test passing a safe string to t().
\Drupal::state()->set('link_generation_test_link_alter_safe', TRUE);
$link = $renderer->executeInRenderContext(new RenderContext(), function () use ($url) {
return \Drupal::service('link_generator')->generate([
'#markup' => '<em>link with markup</em>',
], $url);
});
$this->setRawContent($link);
$this->assertInstanceOf(MarkupInterface::class, $link);
// Ensure the content of the link is escaped.
$this->assertRaw('<em>link with markup</em> <strong>Test!</strong>');
// Test passing an unsafe string to t().
$link = $renderer->executeInRenderContext(new RenderContext(), function () use ($url) {
return \Drupal::service('link_generator')->generate('<em>link with markup</em>', $url);
});
$this->setRawContent($link);
$this->assertInstanceOf(MarkupInterface::class, $link);
// Ensure the content of the link is escaped.
$this->assertEscaped('<em>link with markup</em>');
$this->assertRaw('<strong>Test!</strong>');
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.