function EngineTwigTest::testTwigUrlGenerator

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

Tests the url and url_generate Twig functions.

File

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

Class

EngineTwigTest
Tests Twig-specific theme functionality.

Namespace

Drupal\Tests\system\Functional\Theme

Code

public function testTwigUrlGenerator() : void {
    $this->drupalGet('twig-theme-test/url-generator');
    // Find the absolute URL of the current site.
    $url_generator = $this->container
        ->get('url_generator');
    $expected = [
        'path (as route) not absolute: ' . $url_generator->generateFromRoute('user.register'),
        'url (as route) absolute: ' . $url_generator->generateFromRoute('user.register', [], [
            'absolute' => TRUE,
        ]),
        'path (as route) not absolute with fragment: ' . $url_generator->generateFromRoute('user.register', [], [
            'fragment' => 'bottom',
        ]),
        'url (as route) absolute despite option: ' . $url_generator->generateFromRoute('user.register', [], [
            'absolute' => TRUE,
        ]),
        'url (as route) absolute with fragment: ' . $url_generator->generateFromRoute('user.register', [], [
            'absolute' => TRUE,
            'fragment' => 'bottom',
        ]),
    ];
    // Verify that url() 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');
    // Make sure we got something.
    $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.