View source
<?php
namespace Drupal\menu_link_content\Tests;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Menu\MenuTreeParameters;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\simpletest\KernelTestBase;
use Drupal\user\Entity\User;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Route;
class MenuLinkContentCacheabilityBubblingTest extends KernelTestBase {
public static $modules = [
'menu_link_content',
'system',
'link',
'outbound_processing_test',
'url_alter_test',
'user',
];
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('menu_link_content');
$this
->installEntitySchema('user');
$this
->installSchema('system', [
'url_alias',
'router',
]);
module_set_weight('menu_link_content', 1);
}
public function testOutboundPathAndRouteProcessing() {
\Drupal::service('router.builder')
->rebuild();
$request_stack = \Drupal::requestStack();
$request_context = \Drupal::service('router.request_context');
$request = Request::create('/');
$request->attributes
->set(RouteObjectInterface::ROUTE_NAME, '<front>');
$request->attributes
->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/'));
$request_stack
->push($request);
$request_context
->fromRequest($request);
$menu_tree = \Drupal::menuTree();
$renderer = \Drupal::service('renderer');
$default_menu_cacheability = (new BubbleableMetadata())
->setCacheMaxAge(Cache::PERMANENT)
->setCacheTags([
'config:system.menu.tools',
])
->setCacheContexts([
'languages:' . LanguageInterface::TYPE_INTERFACE,
'theme',
'user.permissions',
]);
User::create([
'uid' => 1,
'name' => $this
->randomString(),
])
->save();
User::create([
'uid' => 2,
'name' => $this
->randomString(),
])
->save();
$test_cases = [
[
'uri' => 'route:<current>',
'cacheability' => (new BubbleableMetadata())
->setCacheContexts([
'route',
]),
],
[
'uri' => 'route:outbound_processing_test.route.csrf',
'cacheability' => (new BubbleableMetadata())
->setCacheContexts([
'session',
])
->setAttachments([
'placeholders' => [],
]),
],
[
'uri' => 'internal:/',
'cacheability' => new BubbleableMetadata(),
],
[
'uri' => 'internal:/user/1',
'cacheability' => (new BubbleableMetadata())
->setCacheTags(User::load(1)
->getCacheTags()),
],
[
'uri' => 'internal:/user/2',
'cacheability' => (new BubbleableMetadata())
->setCacheTags(User::load(2)
->getCacheTags()),
],
];
foreach ($test_cases as $expectation) {
$menu_link_content = MenuLinkContent::create([
'link' => [
'uri' => $expectation['uri'],
],
'menu_name' => 'tools',
]);
$menu_link_content
->save();
$tree = $menu_tree
->load('tools', new MenuTreeParameters());
$build = $menu_tree
->build($tree);
$renderer
->renderRoot($build);
$expected_cacheability = $default_menu_cacheability
->merge($expectation['cacheability']);
$this
->assertEqual($expected_cacheability, BubbleableMetadata::createFromRenderArray($build));
$menu_link_content
->delete();
}
$expected_cacheability = new BubbleableMetadata();
foreach ($test_cases as $expectation) {
$menu_link_content = MenuLinkContent::create([
'link' => [
'uri' => $expectation['uri'],
],
'menu_name' => 'tools',
]);
$menu_link_content
->save();
$expected_cacheability = $expected_cacheability
->merge($expectation['cacheability']);
}
$tree = $menu_tree
->load('tools', new MenuTreeParameters());
$build = $menu_tree
->build($tree);
$renderer
->renderRoot($build);
$expected_cacheability = $expected_cacheability
->merge($default_menu_cacheability);
$this
->assertEqual($expected_cacheability, BubbleableMetadata::createFromRenderArray($build));
}
}