3 namespace Drupal\Tests\system\Kernel\Block;
5 use Drupal\KernelTests\KernelTestBase;
6 use Drupal\system\Entity\Menu;
7 use Drupal\block\Entity\Block;
8 use Drupal\Core\Render\Element;
9 use Drupal\system\Tests\Routing\MockRouteProvider;
10 use Drupal\Tests\Core\Menu\MenuLinkMock;
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 use Symfony\Component\Routing\RouteCollection;
18 * Tests \Drupal\system\Plugin\Block\SystemMenuBlock.
21 * @todo Expand test coverage to all SystemMenuBlock functionality, including
22 * block_menu_delete().
24 * @see \Drupal\system\Plugin\Derivative\SystemMenuBlock
25 * @see \Drupal\system\Plugin\Block\SystemMenuBlock
27 class SystemMenuBlockTest extends KernelTestBase {
34 public static $modules = [
45 * The block under test.
47 * @var \Drupal\system\Plugin\Block\SystemMenuBlock
52 * The menu for testing.
54 * @var \Drupal\system\MenuInterface
59 * The menu link tree service.
61 * @var \Drupal\Core\Menu\MenuLinkTree
66 * The menu link plugin manager service.
68 * @var \Drupal\Core\Menu\MenuLinkManagerInterface
70 protected $menuLinkManager;
73 * The block manager service.
75 * @var \Drupal\Core\block\BlockManagerInterface
77 protected $blockManager;
82 protected function setUp() {
84 $this->installSchema('system', 'sequences');
85 $this->installEntitySchema('user');
86 $this->installEntitySchema('menu_link_content');
88 $account = User::create([
89 'name' => $this->randomMachineName(),
93 $this->container->get('current_user')->setAccount($account);
95 $this->menuLinkManager = $this->container->get('plugin.manager.menu.link');
96 $this->linkTree = $this->container->get('menu.link_tree');
97 $this->blockManager = $this->container->get('plugin.manager.block');
99 $routes = new RouteCollection();
100 $requirements = ['_access' => 'TRUE'];
101 $options = ['_access_checks' => ['access_check.default']];
102 $routes->add('example1', new Route('/example1', [], $requirements, $options));
103 $routes->add('example2', new Route('/example2', [], $requirements, $options));
104 $routes->add('example3', new Route('/example3', [], $requirements, $options));
105 $routes->add('example4', new Route('/example4', [], $requirements, $options));
106 $routes->add('example5', new Route('/example5', [], $requirements, $options));
107 $routes->add('example6', new Route('/example6', [], $requirements, $options));
108 $routes->add('example7', new Route('/example7', [], $requirements, $options));
109 $routes->add('example8', new Route('/example8', [], $requirements, $options));
111 $mock_route_provider = new MockRouteProvider($routes);
112 $this->container->set('router.route_provider', $mock_route_provider);
114 // Add a new custom menu.
116 $label = $this->randomMachineName(16);
118 $this->menu = Menu::create([
121 'description' => 'Description text',
125 // This creates a tree with the following structure:
134 // With link 6 being the only external link.
136 1 => MenuLinkMock::create(['id' => 'test.example1', 'route_name' => 'example1', 'title' => 'foo', 'parent' => '', 'weight' => 0]),
137 2 => MenuLinkMock::create(['id' => 'test.example2', 'route_name' => 'example2', 'title' => 'bar', 'parent' => '', 'route_parameters' => ['foo' => 'bar'], 'weight' => 1]),
138 3 => MenuLinkMock::create(['id' => 'test.example3', 'route_name' => 'example3', 'title' => 'baz', 'parent' => 'test.example2', 'weight' => 2]),
139 4 => MenuLinkMock::create(['id' => 'test.example4', 'route_name' => 'example4', 'title' => 'qux', 'parent' => 'test.example3', 'weight' => 3]),
140 5 => MenuLinkMock::create(['id' => 'test.example5', 'route_name' => 'example5', 'title' => 'foofoo', 'parent' => '', 'expanded' => TRUE, 'weight' => 4]),
141 6 => MenuLinkMock::create(['id' => 'test.example6', 'route_name' => '', 'url' => 'https://www.drupal.org/', 'title' => 'barbar', 'parent' => '', 'weight' => 5]),
142 7 => MenuLinkMock::create(['id' => 'test.example7', 'route_name' => 'example7', 'title' => 'bazbaz', 'parent' => 'test.example5', 'weight' => 6]),
143 8 => MenuLinkMock::create(['id' => 'test.example8', 'route_name' => 'example8', 'title' => 'quxqux', 'parent' => '', 'weight' => 7]),
145 foreach ($links as $instance) {
146 $this->menuLinkManager->addDefinition($instance->getPluginId(), $instance->getPluginDefinition());
151 * Tests calculation of a system menu block's configuration dependencies.
153 public function testSystemMenuBlockConfigDependencies() {
155 $block = Block::create([
156 'plugin' => 'system_menu_block:' . $this->menu->id(),
157 'region' => 'footer',
158 'id' => 'machinename',
162 $dependencies = $block->calculateDependencies()->getDependencies();
165 'system.menu.' . $this->menu->id(),
174 $this->assertIdentical($expected, $dependencies);
178 * Tests the config start level and depth.
180 public function testConfigLevelDepth() {
181 // Helper function to generate a configured block instance.
182 $place_block = function ($level, $depth) {
183 return $this->blockManager->createInstance('system_menu_block:' . $this->menu->id(), [
184 'region' => 'footer',
185 'id' => 'machinename',
192 // All the different block instances we're going to test.
194 'all' => $place_block(1, 0),
195 'level_1_only' => $place_block(1, 1),
196 'level_2_only' => $place_block(2, 1),
197 'level_3_only' => $place_block(3, 1),
198 'level_1_and_beyond' => $place_block(1, 0),
199 'level_2_and_beyond' => $place_block(2, 0),
200 'level_3_and_beyond' => $place_block(3, 0),
203 // Scenario 1: test all block instances when there's no active trail.
204 $no_active_trail_expectations = [];
205 $no_active_trail_expectations['all'] = [
206 'test.example1' => [],
207 'test.example2' => [],
209 'test.example7' => [],
211 'test.example6' => [],
212 'test.example8' => [],
214 $no_active_trail_expectations['level_1_only'] = [
215 'test.example1' => [],
216 'test.example2' => [],
217 'test.example5' => [],
218 'test.example6' => [],
219 'test.example8' => [],
221 $no_active_trail_expectations['level_2_only'] = [];
222 $no_active_trail_expectations['level_3_only'] = [];
223 $no_active_trail_expectations['level_1_and_beyond'] = $no_active_trail_expectations['all'];
224 $no_active_trail_expectations['level_2_and_beyond'] = $no_active_trail_expectations['level_2_only'];
225 $no_active_trail_expectations['level_3_and_beyond'] = [];
226 foreach ($blocks as $id => $block) {
227 $block_build = $block->build();
228 $items = isset($block_build['#items']) ? $block_build['#items'] : [];
229 $this->assertIdentical($no_active_trail_expectations[$id], $this->convertBuiltMenuToIdTree($items), format_string('Menu block %id with no active trail renders the expected tree.', ['%id' => $id]));
232 // Scenario 2: test all block instances when there's an active trail.
233 $route = $this->container->get('router.route_provider')->getRouteByName('example3');
234 $request = new Request();
235 $request->attributes->set(RouteObjectInterface::ROUTE_NAME, 'example3');
236 $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, $route);
237 $this->container->get('request_stack')->push($request);
238 // \Drupal\Core\Menu\MenuActiveTrail uses the cache collector pattern, which
239 // includes static caching. Since this second scenario simulates a second
240 // request, we must also simulate it for the MenuActiveTrail service, by
241 // clearing the cache collector's static cache.
242 \Drupal::service('menu.active_trail')->clear();
244 $active_trail_expectations = [];
245 $active_trail_expectations['all'] = [
246 'test.example1' => [],
249 'test.example4' => [],
253 'test.example7' => [],
255 'test.example6' => [],
256 'test.example8' => [],
258 $active_trail_expectations['level_1_only'] = [
259 'test.example1' => [],
260 'test.example2' => [],
261 'test.example5' => [],
262 'test.example6' => [],
263 'test.example8' => [],
265 $active_trail_expectations['level_2_only'] = [
266 'test.example3' => [],
268 $active_trail_expectations['level_3_only'] = [
269 'test.example4' => [],
271 $active_trail_expectations['level_1_and_beyond'] = $active_trail_expectations['all'];
272 $active_trail_expectations['level_2_and_beyond'] = [
274 'test.example4' => [],
277 $active_trail_expectations['level_3_and_beyond'] = $active_trail_expectations['level_3_only'];
278 foreach ($blocks as $id => $block) {
279 $block_build = $block->build();
280 $items = isset($block_build['#items']) ? $block_build['#items'] : [];
281 $this->assertIdentical($active_trail_expectations[$id], $this->convertBuiltMenuToIdTree($items), format_string('Menu block %id with an active trail renders the expected tree.', ['%id' => $id]));
286 * Helper method to allow for easy menu link tree structure assertions.
288 * Converts the result of MenuLinkTree::build() in a "menu link ID tree".
290 * @param array $build
291 * The return value of MenuLinkTree::build()
294 * The "menu link ID tree" representation of the given render array.
296 protected function convertBuiltMenuToIdTree(array $build) {
298 foreach (Element::children($build) as $id) {
300 if (isset($build[$id]['below'])) {
301 $level[$id] = $this->convertBuiltMenuToIdTree($build[$id]['below']);