You are here

public function LinkGeneratorTest::testGenerateExternal 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::testGenerateExternal()

Tests the generate() method with an external URL.

The set_active_class option is set to TRUE to ensure this does not cause an error together with an external URL.

@covers ::generate

File

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

Class

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

Namespace

Drupal\Tests\Core\Utility

Code

public function testGenerateExternal() {
  $this->urlAssembler
    ->expects($this
    ->once())
    ->method('assemble')
    ->with('https://www.drupal.org', [
    'set_active_class' => TRUE,
    'external' => TRUE,
  ] + $this->defaultOptions)
    ->will($this
    ->returnArgument(0));
  $this->moduleHandler
    ->expects($this
    ->once())
    ->method('alter')
    ->with('link', $this
    ->isType('array'));
  $this->urlAssembler
    ->expects($this
    ->once())
    ->method('assemble')
    ->with('https://www.drupal.org', [
    'set_active_class' => TRUE,
    'external' => TRUE,
  ] + $this->defaultOptions)
    ->willReturnArgument(0);
  $url = Url::fromUri('https://www.drupal.org');
  $url
    ->setUrlGenerator($this->urlGenerator);
  $url
    ->setUnroutedUrlAssembler($this->urlAssembler);
  $url
    ->setOption('set_active_class', TRUE);
  $result = $this->linkGenerator
    ->generate('Drupal', $url);
  $this
    ->assertLink([
    'attributes' => [
      'href' => 'https://www.drupal.org',
    ],
    'content' => 'Drupal',
  ], $result);
}