You are here

public function LinkGeneratorTest::testGenerateBubbleableMetadata in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php \Drupal\Tests\Core\Utility\LinkGeneratorTest::testGenerateBubbleableMetadata()

Tests the LinkGenerator's support for collecting bubbleable metadata.

See also

\Drupal\Core\Utility\LinkGenerator::generate()

\Drupal\Core\Utility\LinkGenerator::generateFromLink()

File

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

Class

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

Namespace

Drupal\Tests\Core\Utility

Code

public function testGenerateBubbleableMetadata() {
  $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'),
    ],
  ]);
  $url = new Url('test_route_1');
  $url
    ->setUrlGenerator($this->urlGenerator);
  $expected_link_markup = '<a href="/test-route-1">Test</a>';

  // Test ::generate().
  $this
    ->assertSame($expected_link_markup, (string) $this->linkGenerator
    ->generate('Test', $url));
  $generated_link = $this->linkGenerator
    ->generate('Test', $url);
  $this
    ->assertSame($expected_link_markup, (string) $generated_link
    ->getGeneratedLink());
  $this
    ->assertInstanceOf('\\Drupal\\Core\\Render\\BubbleableMetadata', $generated_link);

  // Test ::generateFromLink().
  $link = new Link('Test', $url);
  $this
    ->assertSame($expected_link_markup, (string) $this->linkGenerator
    ->generateFromLink($link));
  $generated_link = $this->linkGenerator
    ->generateFromLink($link);
  $this
    ->assertSame($expected_link_markup, (string) $generated_link
    ->getGeneratedLink());
  $this
    ->assertInstanceOf('\\Drupal\\Core\\Render\\BubbleableMetadata', $generated_link);
}