Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / system / tests / src / Unit / Menu / MenuLinkTreeTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Unit\Menu;
4
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Cache\Cache;
7 use Drupal\Core\DependencyInjection\ContainerBuilder;
8 use Drupal\Core\Menu\MenuLinkTree;
9 use Drupal\Core\Menu\MenuLinkTreeElement;
10 use Drupal\Core\Template\Attribute;
11 use Drupal\Core\Url;
12 use Drupal\Tests\Core\Menu\MenuLinkMock;
13 use Drupal\Tests\UnitTestCase;
14
15 /**
16  * @coversDefaultClass \Drupal\Core\Menu\MenuLinkTree
17  * @group Menu
18  */
19 class MenuLinkTreeTest extends UnitTestCase {
20
21   /**
22    * The tested menu link tree service.
23    *
24    * @var \Drupal\Core\Menu\MenuLinkTree
25    */
26   protected $menuLinkTree;
27
28   /**
29    * {@inheritdoc}
30    */
31   protected function setUp() {
32     parent::setUp();
33
34     $this->menuLinkTree = new MenuLinkTree(
35       $this->getMock('\Drupal\Core\Menu\MenuTreeStorageInterface'),
36       $this->getMock('\Drupal\Core\Menu\MenuLinkManagerInterface'),
37       $this->getMock('\Drupal\Core\Routing\RouteProviderInterface'),
38       $this->getMock('\Drupal\Core\Menu\MenuActiveTrailInterface'),
39       $this->getMock('\Drupal\Core\Controller\ControllerResolverInterface')
40     );
41
42     $cache_contexts_manager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager')
43       ->disableOriginalConstructor()
44       ->getMock();
45     $cache_contexts_manager->method('assertValidTokens')->willReturn(TRUE);
46     $container = new ContainerBuilder();
47     $container->set('cache_contexts_manager', $cache_contexts_manager);
48     \Drupal::setContainer($container);
49   }
50
51   /**
52    * @covers ::build
53    *
54    * MenuLinkTree::build() gathers both:
55    * 1. the tree's access cacheability: the cacheability of the access result
56    *    of checking a link in a menu tree's access. Callers can opt out of
57    *    this by MenuLinkTreeElement::access to NULL (the default) value, in
58    *    which case the menu link is always visible. Only when an
59    *    AccessResultInterface object is specified, we gather this cacheability
60    *    metadata.
61    *    This means there are three cases:
62    *    a. no access result (NULL): menu link is visible
63    *    b. AccessResultInterface object that is allowed: menu link is visible
64    *    c. AccessResultInterface object that is not allowed: menu link is
65    *       invisible, but cacheability metadata is still applicable
66    * 2. the tree's menu links' cacheability: the cacheability of a menu link
67    *    itself, because it may be dynamic. For this reason, MenuLinkInterface
68    *    extends CacheableDependencyInterface. It allows any menu link plugin to
69    *    mark itself as uncacheable (max-age=0) or dynamic (by specifying cache
70    *    tags and/or contexts), to indicate the extent of dynamism.
71    *    This means there are two cases:
72    *    a. permanently cacheable, no cache tags, no cache contexts
73    *    b. anything else: non-permanently cacheable, and/or cache tags, and/or
74    *       cache contexts.
75    *
76    * Finally, there are four important shapes of trees, all of which we want to
77    * test:
78    * 1. the empty tree
79    * 2. a single-element tree
80    * 3. a single-level tree (>1 element; just 1 element is case 2)
81    * 4. a multi-level tree
82    *
83    * The associated data provider aims to test the handling of both of these
84    * types of cacheability, and for all four tree shapes, for each of the types
85    * of values for the two types of cacheability.
86    *
87    * There is another level of cacheability involved when actually rendering
88    * built menu trees (i.e. when invoking RendererInterface::render() on the
89    * return value of MenuLinkTreeInterface::build()): the cacheability of the
90    * generated URLs.
91    * Fortunately, that doesn't need additional test coverage here because that
92    * cacheability is handled at the level of the Renderer (i.e. menu.html.twig
93    * template's link() function invocation). It also has its own test coverage.
94    *
95    * @see \Drupal\menu_link_content\Tests\MenuLinkContentCacheabilityBubblingTest
96    *
97    * @dataProvider providerTestBuildCacheability
98    */
99   public function testBuildCacheability($description, $tree, $expected_build, $access, array $access_cache_contexts = []) {
100     if ($access !== NULL) {
101       $access->addCacheContexts($access_cache_contexts);
102     }
103     $build = $this->menuLinkTree->build($tree);
104     sort($expected_build['#cache']['contexts']);
105     $this->assertEquals($expected_build, $build, $description);
106   }
107
108   /**
109    * Provides the test cases to test for ::testBuildCacheability().
110    *
111    * As explained in the documentation for ::testBuildCacheability(), this
112    * generates 1 + (3 * 2 * 3) = 19 test cases.
113    *
114    * @see testBuildCacheability
115    */
116   public function providerTestBuildCacheability() {
117     $base_expected_build_empty = [
118       '#cache' => [
119         'contexts' => [],
120         'tags' => [],
121         'max-age' => Cache::PERMANENT,
122       ],
123     ];
124     $base_expected_build = [
125       '#cache' => [
126         'contexts' => [],
127         'tags' => [
128           'config:system.menu.mock',
129         ],
130         'max-age' => Cache::PERMANENT,
131       ],
132       '#sorted' => TRUE,
133       '#menu_name' => 'mock',
134       '#theme' => 'menu__mock',
135       '#items' => [
136         // To be filled when generating test cases, using $get_built_element().
137       ]
138     ];
139
140     $get_built_element = function (MenuLinkTreeElement $element) {
141       $return = [
142         'attributes' => new Attribute(),
143         'title' => $element->link->getTitle(),
144         'url' => new Url($element->link->getRouteName(), $element->link->getRouteParameters(), ['set_active_class' => TRUE]),
145         'below' => [],
146         'original_link' => $element->link,
147         'is_expanded' => FALSE,
148         'is_collapsed' => FALSE,
149         'in_active_trail' => FALSE,
150       ];
151
152       if ($element->hasChildren && !empty($element->subtree)) {
153         $return['is_expanded'] = TRUE;
154       }
155       elseif ($element->hasChildren) {
156         $return['is_collapsed'] = TRUE;
157       }
158       if ($element->inActiveTrail) {
159         $return['in_active_trail'] = TRUE;
160       }
161
162       return $return;
163     };
164
165     // The three access scenarios described in this method's documentation.
166     $access_scenarios = [
167       [NULL, []],
168       [AccessResult::allowed(), ['access:allowed']],
169       [AccessResult::neutral(), ['access:neutral']],
170     ];
171
172     // The two links scenarios described in this method's documentation.
173     $cache_defaults = ['cache_max_age' => Cache::PERMANENT, 'cache_tags' => []];
174     $links_scenarios = [
175       [
176         MenuLinkMock::create(['id' => 'test.example1', 'route_name' => 'example1', 'title' => 'Example 1']),
177         MenuLinkMock::create(['id' => 'test.example2', 'route_name' => 'example1', 'title' => 'Example 2', 'metadata' => ['cache_contexts' => ['llama']] + $cache_defaults]),
178       ],
179       [
180         MenuLinkMock::create(['id' => 'test.example1', 'route_name' => 'example1', 'title' => 'Example 1', 'metadata' => ['cache_contexts' => ['foo']] + $cache_defaults]),
181         MenuLinkMock::create(['id' => 'test.example2', 'route_name' => 'example1', 'title' => 'Example 2', 'metadata' => ['cache_contexts' => ['bar']] + $cache_defaults]),
182       ],
183     ];
184
185     $data = [];
186
187     // Empty tree.
188     $data[] = [
189       'description' => 'Empty tree.',
190       'tree' => [],
191       'expected_build' => $base_expected_build_empty,
192       'access' => NULL,
193       'access_cache_contexts' => [],
194     ];
195
196     for ($i = 0; $i < count($access_scenarios); $i++) {
197       list($access, $access_cache_contexts) = $access_scenarios[$i];
198
199       for ($j = 0; $j < count($links_scenarios); $j++) {
200         $links = $links_scenarios[$j];
201
202         // Single-element tree.
203         $tree = [
204           new MenuLinkTreeElement($links[0], FALSE, 0, FALSE, []),
205         ];
206         $tree[0]->access = $access;
207         if ($access === NULL || $access->isAllowed()) {
208           $expected_build = $base_expected_build;
209           $expected_build['#items']['test.example1'] = $get_built_element($tree[0]);
210         }
211         else {
212           $expected_build = $base_expected_build_empty;
213         }
214         $expected_build['#cache']['contexts'] = array_merge($expected_build['#cache']['contexts'], $access_cache_contexts, $links[0]->getCacheContexts());
215         $data[] = [
216           'description' => "Single-item tree; access=$i; link=$j.",
217           'tree' => $tree,
218           'expected_build' => $expected_build,
219           'access' => $access,
220           'access_cache_contexts' => $access_cache_contexts,
221         ];
222
223         // Single-level tree.
224         $tree = [
225           new MenuLinkTreeElement($links[0], FALSE, 0, FALSE, []),
226           new MenuLinkTreeElement($links[1], FALSE, 0, FALSE, []),
227         ];
228         $tree[0]->access = $access;
229         $expected_build = $base_expected_build;
230         if ($access === NULL || $access->isAllowed()) {
231           $expected_build['#items']['test.example1'] = $get_built_element($tree[0]);
232         }
233         $expected_build['#items']['test.example2'] = $get_built_element($tree[1]);
234         $expected_build['#cache']['contexts'] = array_merge($expected_build['#cache']['contexts'], $access_cache_contexts, $links[0]->getCacheContexts(), $links[1]->getCacheContexts());
235         $data[] = [
236           'description' => "Single-level tree; access=$i; link=$j.",
237           'tree' => $tree,
238           'expected_build' => $expected_build,
239           'access' => $access,
240           'access_cache_contexts' => $access_cache_contexts,
241         ];
242
243         // Multi-level tree.
244         $multi_level_root_a = MenuLinkMock::create(['id' => 'test.roota', 'route_name' => 'roota', 'title' => 'Root A']);
245         $multi_level_root_b = MenuLinkMock::create(['id' => 'test.rootb', 'route_name' => 'rootb', 'title' => 'Root B']);
246         $multi_level_parent_c = MenuLinkMock::create(['id' => 'test.parentc', 'route_name' => 'parentc', 'title' => 'Parent C']);
247         $tree = [
248           new MenuLinkTreeElement($multi_level_root_a, TRUE, 0, FALSE, [
249             new MenuLinkTreeElement($multi_level_parent_c, TRUE, 0, FALSE, [
250               new MenuLinkTreeElement($links[0], FALSE, 0, FALSE, []),
251             ])
252           ]),
253           new MenuLinkTreeElement($multi_level_root_b, TRUE, 0, FALSE, [
254             new MenuLinkTreeElement($links[1], FALSE, 1, FALSE, [])
255           ]),
256         ];
257         $tree[0]->subtree[0]->subtree[0]->access = $access;
258         $expected_build = $base_expected_build;
259         $expected_build['#items']['test.roota'] = $get_built_element($tree[0]);
260         $expected_build['#items']['test.roota']['below']['test.parentc'] = $get_built_element($tree[0]->subtree[0]);
261         if ($access === NULL || $access->isAllowed()) {
262           $expected_build['#items']['test.roota']['below']['test.parentc']['below']['test.example1'] = $get_built_element($tree[0]->subtree[0]->subtree[0]);
263         }
264         $expected_build['#items']['test.rootb'] = $get_built_element($tree[1]);
265         $expected_build['#items']['test.rootb']['below']['test.example2'] = $get_built_element($tree[1]->subtree[0]);
266         $expected_build['#cache']['contexts'] = array_merge($expected_build['#cache']['contexts'], $access_cache_contexts, $links[0]->getCacheContexts(), $links[1]->getCacheContexts());
267         $data[] = [
268           'description' => "Multi-level tree; access=$i; link=$j.",
269           'tree' => $tree,
270           'expected_build' => $expected_build,
271           'access' => $access,
272           'access_cache_contexts' => $access_cache_contexts,
273         ];
274       }
275     }
276
277     return $data;
278   }
279
280 }