function UrlTest::testLinkBubbleableMetadata

Same name and namespace in other branches
  1. 9 core/modules/system/tests/src/Kernel/Common/UrlTest.php \Drupal\Tests\system\Kernel\Common\UrlTest::testLinkBubbleableMetadata()
  2. 10 core/modules/system/tests/src/Kernel/Common/UrlTest.php \Drupal\Tests\system\Kernel\Common\UrlTest::testLinkBubbleableMetadata()
  3. 11.x core/modules/system/tests/src/Kernel/Common/UrlTest.php \Drupal\Tests\system\Kernel\Common\UrlTest::testLinkBubbleableMetadata()

Tests that #type=link bubbles outbound route/path processors' metadata.

File

core/modules/system/tests/src/Functional/Common/UrlTest.php, line 54

Class

UrlTest
Confirm that <a href="/api/drupal/core%21lib%21Drupal%21Core%21Url.php/class/Url/8.9.x" title="Defines an object that holds information about a URL." class="local">\Drupal\Core\Url</a>, <a href="/api/drupal/core%21lib%21Drupal%21Component%21Utility%21UrlHelper.php/function/UrlHelper%3A%3AfilterQueryParameters/8.9.x" title="Filters a URL query parameter array to remove unwanted elements." class="local">\Drupal\Component\Utility\UrlHelper::filterQueryParameters</a>(), <a href="/api/drupal/core%21lib%21Drupal%21Component%21Utility%21UrlHelper.php/function/UrlHelper%3A%3AbuildQuery/8.9.x" title="Parses an array into a valid, rawurlencoded query string." class="local">\Drupal\Component\Utility\UrlHelper::buildQuery</a>(), and <a href="/api/drupal/core%21lib%21Drupal%21Core%21Utility%21LinkGeneratorInterface.php/function/LinkGeneratorInterface%3A%3Agenerate/8.9.x" title="Renders a link to a URL." class="local">\Drupal\Core\Utility\LinkGeneratorInterface::generate</a>() work correctly with various input.

Namespace

Drupal\Tests\system\Functional\Common

Code

public function testLinkBubbleableMetadata() {
    $cases = [
        [
            'Regular link',
            'internal:/user',
            [],
            [
                'contexts' => [],
                'tags' => [],
                'max-age' => Cache::PERMANENT,
            ],
            [],
        ],
        [
            'Regular link, absolute',
            'internal:/user',
            [
                'absolute' => TRUE,
            ],
            [
                'contexts' => [
                    'url.site',
                ],
                'tags' => [],
                'max-age' => Cache::PERMANENT,
            ],
            [],
        ],
        [
            'Route processor link',
            'route:system.run_cron',
            [],
            [
                'contexts' => [
                    'session',
                ],
                'tags' => [],
                'max-age' => Cache::PERMANENT,
            ],
            [
                'placeholders' => [],
            ],
        ],
        [
            'Route processor link, absolute',
            'route:system.run_cron',
            [
                'absolute' => TRUE,
            ],
            [
                'contexts' => [
                    'url.site',
                    'session',
                ],
                'tags' => [],
                'max-age' => Cache::PERMANENT,
            ],
            [
                'placeholders' => [],
            ],
        ],
        [
            'Path processor link',
            'internal:/user/1',
            [],
            [
                'contexts' => [],
                'tags' => [
                    'user:1',
                ],
                'max-age' => Cache::PERMANENT,
            ],
            [],
        ],
        [
            'Path processor link, absolute',
            'internal:/user/1',
            [
                'absolute' => TRUE,
            ],
            [
                'contexts' => [
                    'url.site',
                ],
                'tags' => [
                    'user:1',
                ],
                'max-age' => Cache::PERMANENT,
            ],
            [],
        ],
    ];
    foreach ($cases as $case) {
        list($title, $uri, $options, $expected_cacheability, $expected_attachments) = $case;
        $expected_cacheability['contexts'] = Cache::mergeContexts($expected_cacheability['contexts'], [
            'languages:language_interface',
            'theme',
            'user.permissions',
        ]);
        $link = [
            '#type' => 'link',
            '#title' => $title,
            '#options' => $options,
            '#url' => Url::fromUri($uri),
        ];
        \Drupal::service('renderer')->renderRoot($link);
        $this->assertEqual($expected_cacheability, $link['#cache']);
        $this->assertEqual($expected_attachments, $link['#attached']);
    }
}

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