Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / menu_link_content / tests / src / Kernel / MenuLinkContentCacheabilityBubblingTest.php
1 <?php
2
3 namespace Drupal\Tests\menu_link_content\Kernel;
4
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Core\Language\LanguageInterface;
7 use Drupal\Core\Menu\MenuTreeParameters;
8 use Drupal\Core\Render\BubbleableMetadata;
9 use Drupal\menu_link_content\Entity\MenuLinkContent;
10 use Drupal\KernelTests\KernelTestBase;
11 use Drupal\user\Entity\User;
12 use Symfony\Cmf\Component\Routing\RouteObjectInterface;
13 use Symfony\Component\HttpFoundation\Request;
14 use Symfony\Component\Routing\Route;
15
16 /**
17  * Ensures that rendered menu links bubble the necessary bubbleable metadata
18  * for outbound path/route processing.
19  *
20  * @group menu_link_content
21  */
22 class MenuLinkContentCacheabilityBubblingTest extends KernelTestBase {
23
24   /**
25    * {@inheritdoc}
26    */
27   public static $modules = ['menu_link_content', 'system', 'link', 'outbound_processing_test', 'url_alter_test', 'user'];
28
29   /**
30    * {@inheritdoc}
31    */
32   protected function setUp() {
33     parent::setUp();
34
35     $this->installEntitySchema('menu_link_content');
36     $this->installEntitySchema('user');
37
38     // Ensure that the weight of module_link_content is higher than system.
39     // @see menu_link_content_install()
40     module_set_weight('menu_link_content', 1);
41   }
42
43   /**
44    * Tests bubbleable metadata of menu links' outbound route/path processing.
45    */
46   public function testOutboundPathAndRouteProcessing() {
47     \Drupal::service('router.builder')->rebuild();
48
49     $request_stack = \Drupal::requestStack();
50     /** @var \Symfony\Component\Routing\RequestContext $request_context */
51     $request_context = \Drupal::service('router.request_context');
52
53     $request = Request::create('/');
54     $request->attributes->set(RouteObjectInterface::ROUTE_NAME, '<front>');
55     $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/'));
56     $request_stack->push($request);
57     $request_context->fromRequest($request);
58
59     $menu_tree = \Drupal::menuTree();
60     $renderer = \Drupal::service('renderer');
61
62     $default_menu_cacheability = (new BubbleableMetadata())
63       ->setCacheMaxAge(Cache::PERMANENT)
64       ->setCacheTags(['config:system.menu.tools'])
65       ->setCacheContexts(['languages:' . LanguageInterface::TYPE_INTERFACE, 'theme', 'user.permissions']);
66
67     User::create(['uid' => 1, 'name' => $this->randomString()])->save();
68     User::create(['uid' => 2, 'name' => $this->randomString()])->save();
69
70     // Five test cases, four asserting one outbound path/route processor, and
71     // together covering one of each:
72     // - no cacheability metadata,
73     // - a cache context,
74     // - a cache tag,
75     // - a cache max-age.
76     // Plus an additional test case to verify that multiple links adding
77     // cacheability metadata of the same type is working (two links with cache
78     // tags).
79     $test_cases = [
80       // \Drupal\Core\RouteProcessor\RouteProcessorCurrent: 'route' cache context.
81       [
82         'uri' => 'route:<current>',
83         'cacheability' => (new BubbleableMetadata())->setCacheContexts(['route']),
84       ],
85       // \Drupal\Core\Access\RouteProcessorCsrf: placeholder.
86       [
87         'uri' => 'route:outbound_processing_test.route.csrf',
88         'cacheability' => (new BubbleableMetadata())->setCacheContexts(['session'])->setAttachments(['placeholders' => []]),
89       ],
90       // \Drupal\Core\PathProcessor\PathProcessorFront: permanently cacheable.
91       [
92         'uri' => 'internal:/',
93         'cacheability' => (new BubbleableMetadata()),
94       ],
95       // \Drupal\url_alter_test\PathProcessorTest: user entity's cache tags.
96       [
97         'uri' => 'internal:/user/1',
98         'cacheability' => (new BubbleableMetadata())->setCacheTags(User::load(1)->getCacheTags()),
99       ],
100       [
101         'uri' => 'internal:/user/2',
102         'cacheability' => (new BubbleableMetadata())->setCacheTags(User::load(2)->getCacheTags()),
103       ],
104     ];
105
106     // Test each expectation individually.
107     foreach ($test_cases as $expectation) {
108       $menu_link_content = MenuLinkContent::create([
109         'link' => ['uri' => $expectation['uri']],
110         'menu_name' => 'tools',
111         'title' => 'Link test',
112       ]);
113       $menu_link_content->save();
114       $tree = $menu_tree->load('tools', new MenuTreeParameters());
115       $build = $menu_tree->build($tree);
116       $renderer->renderRoot($build);
117
118       $expected_cacheability = $default_menu_cacheability->merge($expectation['cacheability']);
119       $this->assertEqual($expected_cacheability, BubbleableMetadata::createFromRenderArray($build));
120
121       $menu_link_content->delete();
122     }
123
124     // Now test them all together in one menu: the rendered menu's cacheability
125     // metadata should be the combination of the cacheability of all links, and
126     // thus of all tested outbound path & route processors.
127     $expected_cacheability = new BubbleableMetadata();
128     foreach ($test_cases as $expectation) {
129       $menu_link_content = MenuLinkContent::create([
130         'link' => ['uri' => $expectation['uri']],
131         'menu_name' => 'tools',
132         'title' => 'Link test',
133       ]);
134       $menu_link_content->save();
135       $expected_cacheability = $expected_cacheability->merge($expectation['cacheability']);
136     }
137     $tree = $menu_tree->load('tools', new MenuTreeParameters());
138     $build = $menu_tree->build($tree);
139     $renderer->renderRoot($build);
140     $expected_cacheability = $expected_cacheability->merge($default_menu_cacheability);
141     $this->assertEqual($expected_cacheability, BubbleableMetadata::createFromRenderArray($build));
142   }
143
144 }