You are here

public function LinkGeneratorTest::testGenerateWithAlterHook in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php \Drupal\Tests\Core\Utility\LinkGeneratorTest::testGenerateWithAlterHook()
  2. 9 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());
}