LinkGenerationTest.php in Drupal 9
File
core/tests/Drupal/KernelTests/Core/Url/LinkGenerationTest.php
View source
<?php
namespace Drupal\KernelTests\Core\Url;
use Drupal\Component\Render\MarkupInterface;
use Drupal\Core\Render\RenderContext;
use Drupal\Core\Url;
use Drupal\KernelTests\KernelTestBase;
class LinkGenerationTest extends KernelTestBase {
protected static $modules = [
'link_generation_test',
];
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);
$this
->assertRaw('<em>link with markup</em>');
\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);
$this
->assertEscaped('<em>link with markup</em> <strong>Test!</strong>');
\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);
$this
->assertRaw('<em>link with markup</em> <strong>Test!</strong>');
$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);
$this
->assertEscaped('<em>link with markup</em>');
$this
->assertRaw('<strong>Test!</strong>');
}
}