3 namespace Drupal\Tests\system\Unit\Menu;
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;
12 use Drupal\Tests\Core\Menu\MenuLinkMock;
13 use Drupal\Tests\UnitTestCase;
16 * @coversDefaultClass \Drupal\Core\Menu\MenuLinkTree
19 class MenuLinkTreeTest extends UnitTestCase {
22 * The tested menu link tree service.
24 * @var \Drupal\Core\Menu\MenuLinkTree
26 protected $menuLinkTree;
31 protected function setUp() {
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')
42 $cache_contexts_manager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager')
43 ->disableOriginalConstructor()
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);
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
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
76 * Finally, there are four important shapes of trees, all of which we want to
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
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.
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
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.
95 * @see \Drupal\menu_link_content\Tests\MenuLinkContentCacheabilityBubblingTest
97 * @dataProvider providerTestBuildCacheability
99 public function testBuildCacheability($description, $tree, $expected_build, $access, array $access_cache_contexts = []) {
100 if ($access !== NULL) {
101 $access->addCacheContexts($access_cache_contexts);
103 $build = $this->menuLinkTree->build($tree);
104 sort($expected_build['#cache']['contexts']);
105 $this->assertEquals($expected_build, $build, $description);
109 * Provides the test cases to test for ::testBuildCacheability().
111 * As explained in the documentation for ::testBuildCacheability(), this
112 * generates 1 + (3 * 2 * 3) = 19 test cases.
114 * @see testBuildCacheability
116 public function providerTestBuildCacheability() {
117 $base_expected_build_empty = [
121 'max-age' => Cache::PERMANENT,
124 $base_expected_build = [
128 'config:system.menu.mock',
130 'max-age' => Cache::PERMANENT,
133 '#menu_name' => 'mock',
134 '#theme' => 'menu__mock',
136 // To be filled when generating test cases, using $get_built_element().
140 $get_built_element = function (MenuLinkTreeElement $element) {
142 'attributes' => new Attribute(),
143 'title' => $element->link->getTitle(),
144 'url' => new Url($element->link->getRouteName(), $element->link->getRouteParameters(), ['set_active_class' => TRUE]),
146 'original_link' => $element->link,
147 'is_expanded' => FALSE,
148 'is_collapsed' => FALSE,
149 'in_active_trail' => FALSE,
152 if ($element->hasChildren && !empty($element->subtree)) {
153 $return['is_expanded'] = TRUE;
155 elseif ($element->hasChildren) {
156 $return['is_collapsed'] = TRUE;
158 if ($element->inActiveTrail) {
159 $return['in_active_trail'] = TRUE;
165 // The three access scenarios described in this method's documentation.
166 $access_scenarios = [
168 [AccessResult::allowed(), ['access:allowed']],
169 [AccessResult::neutral(), ['access:neutral']],
172 // The two links scenarios described in this method's documentation.
173 $cache_defaults = ['cache_max_age' => Cache::PERMANENT, 'cache_tags' => []];
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]),
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]),
189 'description' => 'Empty tree.',
191 'expected_build' => $base_expected_build_empty,
193 'access_cache_contexts' => [],
196 for ($i = 0; $i < count($access_scenarios); $i++) {
197 list($access, $access_cache_contexts) = $access_scenarios[$i];
199 for ($j = 0; $j < count($links_scenarios); $j++) {
200 $links = $links_scenarios[$j];
202 // Single-element tree.
204 new MenuLinkTreeElement($links[0], FALSE, 0, FALSE, []),
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]);
212 $expected_build = $base_expected_build_empty;
214 $expected_build['#cache']['contexts'] = array_merge($expected_build['#cache']['contexts'], $access_cache_contexts, $links[0]->getCacheContexts());
216 'description' => "Single-item tree; access=$i; link=$j.",
218 'expected_build' => $expected_build,
220 'access_cache_contexts' => $access_cache_contexts,
223 // Single-level tree.
225 new MenuLinkTreeElement($links[0], FALSE, 0, FALSE, []),
226 new MenuLinkTreeElement($links[1], FALSE, 0, FALSE, []),
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]);
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());
236 'description' => "Single-level tree; access=$i; link=$j.",
238 'expected_build' => $expected_build,
240 'access_cache_contexts' => $access_cache_contexts,
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']);
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, []),
253 new MenuLinkTreeElement($multi_level_root_b, TRUE, 0, FALSE, [
254 new MenuLinkTreeElement($links[1], FALSE, 1, FALSE, []),
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]);
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());
268 'description' => "Multi-level tree; access=$i; link=$j.",
270 'expected_build' => $expected_build,
272 'access_cache_contexts' => $access_cache_contexts,