function EngineTwigTest::testTwigLinkGenerator

Same name in other branches
  1. 8.9.x core/modules/system/tests/src/Functional/Theme/EngineTwigTest.php \Drupal\Tests\system\Functional\Theme\EngineTwigTest::testTwigLinkGenerator()
  2. 10 core/modules/system/tests/src/Functional/Theme/EngineTwigTest.php \Drupal\Tests\system\Functional\Theme\EngineTwigTest::testTwigLinkGenerator()
  3. 11.x core/modules/system/tests/src/Functional/Theme/EngineTwigTest.php \Drupal\Tests\system\Functional\Theme\EngineTwigTest::testTwigLinkGenerator()

Tests the link_generator Twig functions.

File

core/modules/system/tests/src/Functional/Theme/EngineTwigTest.php, line 84

Class

EngineTwigTest
Tests Twig-specific theme functionality.

Namespace

Drupal\Tests\system\Functional\Theme

Code

public function testTwigLinkGenerator() {
    $this->drupalGet('twig-theme-test/link-generator');
    
    /** @var \Drupal\Core\Utility\LinkGenerator $link_generator */
    $link_generator = $this->container
        ->get('link_generator');
    $generated_url = Url::fromRoute('user.register', [], [
        'absolute' => TRUE,
    ])->toString(TRUE)
        ->getGeneratedUrl();
    $expected = [
        'link via the linkgenerator: ' . $link_generator->generate('register', new Url('user.register', [], [
            'absolute' => TRUE,
        ])),
        'link via the linkgenerator: ' . $link_generator->generate('register', new Url('user.register', [], [
            'absolute' => TRUE,
            'attributes' => [
                'foo' => 'bar',
            ],
        ])),
        'link via the linkgenerator: ' . $link_generator->generate('register', new Url('user.register', [], [
            'attributes' => [
                'foo' => 'bar',
                'id' => 'kitten',
            ],
        ])),
        'link via the linkgenerator: ' . $link_generator->generate('register', new Url('user.register', [], [
            'attributes' => [
                'id' => 'kitten',
            ],
        ])),
        'link via the linkgenerator: ' . $link_generator->generate('register', new Url('user.register', [], [
            'attributes' => [
                'class' => [
                    'llama',
                    'kitten',
                    'panda',
                ],
            ],
        ])),
        'link via the linkgenerator: ' . $link_generator->generate(Markup::create('<span>register</span>'), new Url('user.register', [], [
            'absolute' => TRUE,
        ])),
        'link via the linkgenerator: <a href="' . $generated_url . '"><span>register</span><svg></svg></a>',
        'link via the linkgenerator: ' . $link_generator->generate('register', new Url('user.register', [], [
            'attributes' => [
                'foo' => 'bar',
            ],
        ])) . ' ' . $link_generator->generate('register', new Url('user.register', [], [
            'attributes' => [
                'foo' => 'bar',
            ],
        ])),
    ];
    // Verify that link() has the ability to bubble cacheability metadata:
    // absolute URLs should bubble the 'url.site' cache context. (This only
    // needs to test that cacheability metadata is bubbled *at all*; detailed
    // tests for *which* cacheability metadata is bubbled live elsewhere.)
    $this->assertCacheContext('url.site');
    $content = $this->getSession()
        ->getPage()
        ->getContent();
    $this->assertNotEmpty($content, 'Page content is not empty');
    foreach ($expected as $string) {
        $this->assertSession()
            ->responseContains('<div>' . $string . '</div>');
    }
}

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