function LinkGeneratorTest::testGenerateExternal

Same name and namespace in other branches
  1. 8.9.x core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php \Drupal\Tests\Core\Utility\LinkGeneratorTest::testGenerateExternal()
  2. 10 core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php \Drupal\Tests\Core\Utility\LinkGeneratorTest::testGenerateExternal()
  3. 11.x 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 <a href="/api/drupal/core%21lib%21Drupal%21Core%21Utility%21LinkGenerator.php/class/LinkGenerator/9" title="Provides a class which generates a link with route names and parameters." class="local">\Drupal\Core\Utility\LinkGenerator</a> @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);
}

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