function LinkGeneratorTest::testGenerateWithAlterHook

Same name in other branches
  1. 8.9.x core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php \Drupal\Tests\Core\Utility\LinkGeneratorTest::testGenerateWithAlterHook()
  2. 10 core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php \Drupal\Tests\Core\Utility\LinkGeneratorTest::testGenerateWithAlterHook()
  3. 11.x core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php \Drupal\Tests\Core\Utility\LinkGeneratorTest::testGenerateWithAlterHook()

Tests altering the URL object using hook_link_alter().

@covers ::generate

File

core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php, line 589

Class

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

Namespace

Drupal\Tests\Core\Utility

Code

public function testGenerateWithAlterHook() {
    $options = [
        'query' => [],
        'language' => NULL,
        'set_active_class' => FALSE,
        'absolute' => FALSE,
    ];
    $this->urlGenerator
        ->expects($this->any())
        ->method('generateFromRoute')
        ->willReturnMap([
        [
            '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());
}

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