You are here

public function UrlTest::testLinkBubbleableMetadata in Drupal 9

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

File

core/modules/system/tests/src/Kernel/Common/UrlTest.php, line 49

Class

UrlTest
Confirm that \Drupal\Core\Url, \Drupal\Component\Utility\UrlHelper::filterQueryParameters(), \Drupal\Component\Utility\UrlHelper::buildQuery(), and \Drupal\Core\Utility\LinkGeneratorInterface::generate() work correctly with various input.

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) {
    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
      ->assertEqualsCanonicalizing($expected_cacheability, $link['#cache']);
    $this
      ->assertEquals($expected_attachments, $link['#attached']);
  }
}