You are here

function UrlTest::testLinkBubbleableMetadata in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/system/src/Tests/Common/UrlTest.php \Drupal\system\Tests\Common\UrlTest::testLinkBubbleableMetadata()

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

File

core/modules/system/src/Tests/Common/UrlTest.php, line 50
Contains \Drupal\system\Tests\Common\UrlTest.

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\system\Tests\Common

Code

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