function SystemMenuBlockTest::testConfigLevelDepth

Same name in other branches
  1. 9 core/modules/system/tests/src/Kernel/Block/SystemMenuBlockTest.php \Drupal\Tests\system\Kernel\Block\SystemMenuBlockTest::testConfigLevelDepth()
  2. 8.9.x core/modules/system/tests/src/Kernel/Block/SystemMenuBlockTest.php \Drupal\Tests\system\Kernel\Block\SystemMenuBlockTest::testConfigLevelDepth()
  3. 10 core/modules/system/tests/src/Kernel/Block/SystemMenuBlockTest.php \Drupal\Tests\system\Kernel\Block\SystemMenuBlockTest::testConfigLevelDepth()

Tests the config start level and depth.

File

core/modules/system/tests/src/Kernel/Block/SystemMenuBlockTest.php, line 181

Class

SystemMenuBlockTest
Tests \Drupal\system\Plugin\Block\SystemMenuBlock.

Namespace

Drupal\Tests\system\Kernel\Block

Code

public function testConfigLevelDepth() : void {
    // Helper function to generate a configured block instance.
    $place_block = function ($level, $depth) {
        return $this->blockManager
            ->createInstance('system_menu_block:' . $this->menu
            ->id(), [
            'region' => 'footer',
            'id' => 'machine_name',
            'theme' => 'stark',
            'level' => $level,
            'depth' => $depth,
        ]);
    };
    // All the different block instances we're going to test.
    $blocks = [
        'all' => $place_block(1, 0),
        'level_1_only' => $place_block(1, 1),
        'level_2_only' => $place_block(2, 1),
        'level_3_only' => $place_block(3, 1),
        'level_1_and_beyond' => $place_block(1, 0),
        'level_2_and_beyond' => $place_block(2, 0),
        'level_3_and_beyond' => $place_block(3, 0),
    ];
    // Scenario 1: test all block instances when there's no active trail.
    $no_active_trail_expectations = [];
    $no_active_trail_expectations['all'] = [
        'test.example1' => [],
        'test.example2' => [],
        'test.example5' => [
            'test.example7' => [],
        ],
        'test.example6' => [],
        'test.example8' => [],
    ];
    $no_active_trail_expectations['level_1_only'] = [
        'test.example1' => [],
        'test.example2' => [],
        'test.example5' => [],
        'test.example6' => [],
        'test.example8' => [],
    ];
    $no_active_trail_expectations['level_2_only'] = [];
    $no_active_trail_expectations['level_3_only'] = [];
    $no_active_trail_expectations['level_1_and_beyond'] = $no_active_trail_expectations['all'];
    $no_active_trail_expectations['level_2_and_beyond'] = $no_active_trail_expectations['level_2_only'];
    $no_active_trail_expectations['level_3_and_beyond'] = [];
    foreach ($blocks as $id => $block) {
        $block_build = $block->build();
        $items = $block_build['#items'] ?? [];
        $this->assertSame($no_active_trail_expectations[$id], $this->convertBuiltMenuToIdTree($items), "Menu block {$id} with no active trail renders the expected tree.");
    }
    // Scenario 2: test all 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();
    $active_trail_expectations = [];
    $active_trail_expectations['all'] = [
        'test.example1' => [],
        'test.example2' => [
            'test.example3' => [
                'test.example4' => [],
            ],
        ],
        'test.example5' => [
            'test.example7' => [],
        ],
        'test.example6' => [],
        'test.example8' => [],
    ];
    $active_trail_expectations['level_1_only'] = [
        'test.example1' => [],
        'test.example2' => [],
        'test.example5' => [],
        'test.example6' => [],
        'test.example8' => [],
    ];
    $active_trail_expectations['level_2_only'] = [
        'test.example3' => [],
    ];
    $active_trail_expectations['level_3_only'] = [
        'test.example4' => [],
    ];
    $active_trail_expectations['level_1_and_beyond'] = $active_trail_expectations['all'];
    $active_trail_expectations['level_2_and_beyond'] = [
        'test.example3' => [
            'test.example4' => [],
        ],
    ];
    $active_trail_expectations['level_3_and_beyond'] = $active_trail_expectations['level_3_only'];
    foreach ($blocks as $id => $block) {
        $block_build = $block->build();
        $items = $block_build['#items'] ?? [];
        $this->assertSame($active_trail_expectations[$id], $this->convertBuiltMenuToIdTree($items), "Menu 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.