You are here

public function SystemMenuBlockTest::testConfigLevelDepth in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/system/src/Tests/Block/SystemMenuBlockTest.php \Drupal\system\Tests\Block\SystemMenuBlockTest::testConfigLevelDepth()

Tests the config start level and depth.

File

core/modules/system/src/Tests/Block/SystemMenuBlockTest.php, line 183
Contains \Drupal\system\Tests\Block\SystemMenuBlockTest.

Class

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

Namespace

Drupal\system\Tests\Block

Code

public function testConfigLevelDepth() {

  // Helper function to generate a configured block instance.
  $place_block = function ($level, $depth) {
    return $this->blockManager
      ->createInstance('system_menu_block:' . $this->menu
      ->id(), array(
      'region' => 'footer',
      'id' => 'machinename',
      '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'] = [
    'test.example7' => [],
  ];
  $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 = isset($block_build['#items']) ? $block_build['#items'] : [];
    $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,
    ]));
  }

  // 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);
  $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' => [],
    'test.example7' => [],
  ];
  $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' => [],
    ],
    'test.example7' => [],
  ];
  $active_trail_expectations['level_3_and_beyond'] = $active_trail_expectations['level_3_only'];
  foreach ($blocks as $id => $block) {
    $block_build = $block
      ->build();
    $items = isset($block_build['#items']) ? $block_build['#items'] : [];
    $this
      ->assertIdentical($active_trail_expectations[$id], $this
      ->convertBuiltMenuToIdTree($items), format_string('Menu block %id with an active trail renders the expected tree.', [
      '%id' => $id,
    ]));
  }
}