function SystemMenuBlockTest::testConfigExpanded

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

Tests the config expanded option.

Attributes

#[DataProvider('configExpandedTestCases')]

File

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

Class

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

Namespace

Drupal\Tests\system\Kernel\Block

Code

public function testConfigExpanded(string $active_route, int $menu_block_level, array $expected_items, array $expected_active_trail_items) : void {
  // Replace the path.matcher service so it always returns FALSE when
  // checking whether a route is the front page. Otherwise, the default
  // service throws an exception when checking routes because all of these
  // are mocked.
  $service_definition = $this->container
    ->getDefinition('path.matcher');
  $service_definition->setClass(StubPathMatcher::class);
  $block = $this->blockManager
    ->createInstance('system_menu_block:' . $this->menu
    ->id(), [
    'region' => 'footer',
    'id' => 'machine_name',
    'theme' => 'stark',
    'level' => $menu_block_level,
    'depth' => NULL,
    'expand_all_items' => TRUE,
  ]);
  $route = $this->container
    ->get('router.route_provider')
    ->getRouteByName($active_route);
  $request = new Request();
  $request->attributes
    ->set(RouteObjectInterface::ROUTE_NAME, $active_route);
  $request->attributes
    ->set(RouteObjectInterface::ROUTE_OBJECT, $route);
  $request->setSession(new Session(new MockArraySessionStorage()));
  $this->container
    ->get('request_stack')
    ->push($request);
  \Drupal::service('menu.active_trail')->clear();
  $block_build = $block->build();
  $items = $block_build['#items'] ?? [];
  $this->assertEquals($expected_items, $this->convertBuiltMenuToIdTree($items));
  $active_trail_items = $this->getActiveTrailItems($items);
  $this->assertEquals($expected_active_trail_items, $active_trail_items);
  $this->assertContains("route.menu_active_trails:{$this->menu->id()}", $block->getCacheContexts());
  $block->setConfigurationValue('ignore_active_trail', TRUE);
  $block_build = $block->build();
  $items = $block_build['#items'] ?? [];
  $this->assertEquals($expected_items, $this->convertBuiltMenuToIdTree($items));
  $active_trail_items = $this->getActiveTrailItems($items);
  // Setting "ignore_active_trail" to TRUE when the menu block level is not 1
  // technically fails configuration validation, but test that logic for
  // adding the active trail cache context is correct regardless.
  if ($menu_block_level === 1) {
    $this->assertEmpty($active_trail_items);
    $this->assertNotContains("route.menu_active_trails:{$this->menu->id()}", $block->getCacheContexts());
  }
  else {
    $this->assertEquals($expected_active_trail_items, $active_trail_items);
    $this->assertContains("route.menu_active_trails:{$this->menu->id()}", $block->getCacheContexts());
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.