function LinkGeneratorTest::testGenerateExternal

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php \Drupal\Tests\Core\Utility\LinkGeneratorTest::testGenerateExternal()
  2. 8.9.x core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php \Drupal\Tests\Core\Utility\LinkGeneratorTest::testGenerateExternal()
  3. 10 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 250

Class

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

Namespace

Drupal\Tests\Core\Utility

Code

public function testGenerateExternal() : void {
    $this->urlAssembler
        ->expects($this->once())
        ->method('assemble')
        ->with('https://www.drupal.org', [
        'set_active_class' => TRUE,
        'external' => TRUE,
    ] + $this->defaultOptions)
        ->willReturnArgument(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);
}

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