Commit 260c6ee9 authored by catch's avatar catch

Issue #2675648 by Bès, dawehner, Haza, chx: Code after hook_link_alter()...

Issue #2675648 by Bès, dawehner, Haza, chx: Code after hook_link_alter() invocation does not use the altered data
parent 6309f944
......@@ -141,6 +141,7 @@ public function generate($text, Url $url) {
// Allow other modules to modify the structure of the link.
$this->moduleHandler->alter('link', $variables);
$url = $variables['url'];
// Move attributes out of options since generateFromRoute() doesn't need
// them. Make sure the "href" comes first for testing purposes.
......
......@@ -70,7 +70,9 @@ class LinkGeneratorTest extends UnitTestCase {
protected function setUp() {
parent::setUp();
$this->urlGenerator = $this->getMock('\Drupal\Core\Routing\UrlGenerator', array(), array(), '', FALSE);
$this->urlGenerator = $this->getMockBuilder('\Drupal\Core\Routing\UrlGenerator')
->disableOriginalConstructor()
->getMock();
$this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
$this->renderer = $this->getMock('\Drupal\Core\Render\RendererInterface');
$this->linkGenerator = new LinkGenerator($this->urlGenerator, $this->moduleHandler, $this->renderer);
......@@ -508,6 +510,33 @@ public function testGenerateBubbleableMetadata() {
$this->assertInstanceOf('\Drupal\Core\Render\BubbleableMetadata', $generated_link);
}
/**
* Tests altering the URL object using hook_link_alter().
*
* @covers ::generate
*/
public function testGenerateWithAlterHook() {
$options = ['query' => [], 'language' => NULL, 'set_active_class' => FALSE, 'absolute' => FALSE];
$this->urlGenerator->expects($this->any())
->method('generateFromRoute')
->will($this->returnValueMap([
['test_route_1', [], $options, TRUE, (new GeneratedUrl())->setGeneratedUrl('/test-route-1')],
['test_route_2', [], $options, TRUE, (new GeneratedUrl())->setGeneratedUrl('/test-route-2')],
]));
$url = new Url('test_route_2');
$url->setUrlGenerator($this->urlGenerator);
$this->moduleHandler->expects($this->atLeastOnce())
->method('alter')
->willReturnCallback(function ($hook, &$options) {
$options['url'] = (new Url('test_route_1'))->setUrlGenerator($this->urlGenerator);
});
$expected_link_markup = '<a href="/test-route-1">Test</a>';
$this->assertEquals($expected_link_markup, (string) $this->linkGenerator->generate('Test', $url)->getGeneratedLink());
}
/**
* Checks that a link with certain properties exists in a given HTML snippet.
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment