function SystemMenuNavigationBlockTest::testConfigLevelDepth
Same name in other branches
- 10 core/modules/navigation/tests/src/Kernel/SystemMenuNavigationBlockTest.php \Drupal\Tests\navigation\Kernel\SystemMenuNavigationBlockTest::testConfigLevelDepth()
Tests the config start level and depth.
File
-
core/
modules/ navigation/ tests/ src/ Kernel/ SystemMenuNavigationBlockTest.php, line 188
Class
- SystemMenuNavigationBlockTest
- Tests \Drupal\navigation\Plugin\Block\SystemMenuNavigationBlock.
Namespace
Drupal\Tests\navigation\KernelCode
public function testConfigLevelDepth() : void {
// Helper function to generate a configured navigation block instance.
$place_block = function ($level, $depth) {
return $this->blockManager
->createInstance('navigation_menu:' . $this->menu
->id(), [
'region' => 'content',
'id' => 'machine_name',
'level' => $level,
'depth' => $depth,
]);
};
// All the different navigation block instances we're going to test.
$blocks = [
'level_1_only' => $place_block(1, 0),
'level_2_only' => $place_block(2, 0),
'level_3_only' => $place_block(3, 0),
'level_1_and_beyond' => $place_block(1, NavigationMenuBlock::NAVIGATION_MAX_DEPTH - 1),
'level_2_and_beyond' => $place_block(2, NavigationMenuBlock::NAVIGATION_MAX_DEPTH - 1),
'level_3_and_beyond' => $place_block(3, NavigationMenuBlock::NAVIGATION_MAX_DEPTH - 1),
];
// Expectations are independent of the active trail.
$expectations = [];
$expectations['level_1_only'] = [
'test.example1' => [],
'test.example2' => [],
'test.example5' => [],
'test.example6' => [],
'test.example8' => [],
];
$expectations['level_2_only'] = [
'test.example3' => [],
'test.example7' => [],
];
$expectations['level_3_only'] = [
'test.example4' => [],
];
$expectations['level_1_and_beyond'] = [
'test.example1' => [],
'test.example2' => [
'test.example3' => [
'test.example4' => [],
],
],
'test.example5' => [
'test.example7' => [],
],
'test.example6' => [],
'test.example8' => [],
];
$expectations['level_2_and_beyond'] = [
'test.example3' => [
'test.example4' => [
'test.example9' => [],
],
],
'test.example7' => [],
];
$expectations['level_3_and_beyond'] = [
'test.example4' => [
'test.example9' => [],
],
];
// Scenario 1: test all navigation block instances when there's no active
// trail.
foreach ($blocks as $id => $block) {
$block_build = $block->build();
$items = $block_build['#items'] ?? [];
$this->assertSame($expectations[$id], $this->convertBuiltMenuToIdTree($items), "Menu block {$id} with no active trail renders the expected tree.");
}
// Scenario 2: test all navigation block instances when there's an active
// trail.
$route = $this->container
->get('router.route_provider')
->getRouteByName('example3');
$request = new Request();
$request->attributes
->set(RouteObjectInterface::ROUTE_NAME, 'example3');
$request->attributes
->set(RouteObjectInterface::ROUTE_OBJECT, $route);
$request->setSession(new Session(new MockArraySessionStorage()));
$this->container
->get('request_stack')
->push($request);
// \Drupal\Core\Menu\MenuActiveTrail uses the cache collector pattern, which
// includes static caching. Since this second scenario simulates a second
// request, we must also simulate it for the MenuActiveTrail service, by
// clearing the cache collector's static cache.
\Drupal::service('menu.active_trail')->clear();
foreach ($blocks as $id => $block) {
$block_build = $block->build();
$items = $block_build['#items'] ?? [];
$this->assertSame($expectations[$id], $this->convertBuiltMenuToIdTree($items), "Menu navigation block {$id} with an active trail renders the expected tree.");
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.