function LinkGeneratorTest::testGenerateActive

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php \Drupal\Tests\Core\Utility\LinkGeneratorTest::testGenerateActive()
  2. 8.9.x core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php \Drupal\Tests\Core\Utility\LinkGeneratorTest::testGenerateActive()
  3. 11.x core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php \Drupal\Tests\Core\Utility\LinkGeneratorTest::testGenerateActive()

Tests the active class on the link method.

See also

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

File

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

Class

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

Namespace

Drupal\Tests\Core\Utility

Code

public function testGenerateActive() : void {
    $this->urlGenerator
        ->expects($this->exactly(5))
        ->method('generateFromRoute')
        ->willReturnCallback(function ($name, $parameters = [], $options = [], $collect_bubbleable_metadata = FALSE) {
        switch ($name) {
            case 'test_route_1':
                return (new GeneratedUrl())->setGeneratedUrl('/test-route-1');
            case 'test_route_3':
                return (new GeneratedUrl())->setGeneratedUrl('/test-route-3');
            case 'test_route_4':
                if ($parameters['object'] == '1') {
                    return (new GeneratedUrl())->setGeneratedUrl('/test-route-4/1');
                }
        }
    });
    $this->urlGenerator
        ->expects($this->exactly(4))
        ->method('getPathFromRoute')
        ->willReturnMap([
        [
            'test_route_1',
            [],
            'test-route-1',
        ],
        [
            'test_route_3',
            [],
            'test-route-3',
        ],
        [
            'test_route_4',
            [
                'object' => '1',
            ],
            'test-route-4/1',
        ],
    ]);
    $this->moduleHandler
        ->expects($this->exactly(5))
        ->method('alter');
    // Render a link.
    $url = new Url('test_route_1', [], [
        'set_active_class' => TRUE,
    ]);
    $url->setUrlGenerator($this->urlGenerator);
    $result = $this->linkGenerator
        ->generate('Test', $url);
    $this->assertLink([
        'attributes' => [
            'data-drupal-link-system-path' => 'test-route-1',
        ],
    ], $result);
    // Render a link with the set_active_class option disabled.
    $url = new Url('test_route_1', [], [
        'set_active_class' => FALSE,
    ]);
    $url->setUrlGenerator($this->urlGenerator);
    $result = $this->linkGenerator
        ->generate('Test', $url);
    $this->assertNoXPathResults('//a[@data-drupal-link-system-path="test-route-1"]', (string) $result);
    // Render a link with an associated language.
    $url = new Url('test_route_1', [], [
        'language' => new Language([
            'id' => 'de',
        ]),
        'set_active_class' => TRUE,
    ]);
    $url->setUrlGenerator($this->urlGenerator);
    $result = $this->linkGenerator
        ->generate('Test', $url);
    $this->assertLink([
        'attributes' => [
            'data-drupal-link-system-path' => 'test-route-1',
            'hreflang' => 'de',
        ],
    ], $result);
    // Render a link with a query parameter.
    $url = new Url('test_route_3', [], [
        'query' => [
            'value' => 'example_1',
        ],
        'set_active_class' => TRUE,
    ]);
    $url->setUrlGenerator($this->urlGenerator);
    $result = $this->linkGenerator
        ->generate('Test', $url);
    $this->assertLink([
        'attributes' => [
            'data-drupal-link-system-path' => 'test-route-3',
            'data-drupal-link-query' => '{"value":"example_1"}',
        ],
    ], $result);
    // Render a link with route parameters and a query parameter.
    $url = new Url('test_route_4', [
        'object' => '1',
    ], [
        'query' => [
            'value' => 'example_1',
        ],
        'set_active_class' => TRUE,
    ]);
    $url->setUrlGenerator($this->urlGenerator);
    $result = $this->linkGenerator
        ->generate('Test', $url);
    $this->assertLink([
        'attributes' => [
            'data-drupal-link-system-path' => 'test-route-4/1',
            'data-drupal-link-query' => '{"value":"example_1"}',
        ],
    ], $result);
}

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