function UrlTest::testLinkBubbleableMetadata

Same name and namespace in other branches
  1. 8.9.x core/modules/system/tests/src/Functional/Common/UrlTest.php \Drupal\Tests\system\Functional\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/Kernel/Common/UrlTest.php, line 48

Class

UrlTest
Tests the Url object.

Namespace

Drupal\Tests\system\Kernel\Common

Code

public function testLinkBubbleableMetadata() {
    \Drupal::service('module_installer')->install([
        'user',
    ]);
    $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) {
        [
            $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->assertEqualsCanonicalizing($expected_cacheability, $link['#cache']);
        $this->assertEquals($expected_attachments, $link['#attached']);
    }
}

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