You are here

public function MenuLinkTreeTest::testCreateLinksInMenu in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Menu/MenuLinkTreeTest.php \Drupal\KernelTests\Core\Menu\MenuLinkTreeTest::testCreateLinksInMenu()
  2. 10 core/tests/Drupal/KernelTests/Core/Menu/MenuLinkTreeTest.php \Drupal\KernelTests\Core\Menu\MenuLinkTreeTest::testCreateLinksInMenu()

Tests creating links with an expected tree structure.

File

core/tests/Drupal/KernelTests/Core/Menu/MenuLinkTreeTest.php, line 88

Class

MenuLinkTreeTest
Tests the menu link tree.

Namespace

Drupal\KernelTests\Core\Menu

Code

public function testCreateLinksInMenu() {

  // This creates a tree with the following structure:
  // - 1
  // - 2
  //   - 3
  //     - 4
  // - 5
  //   - 7
  // - 6
  // - 8
  // With link 6 being the only external link.
  $links = [
    1 => MenuLinkMock::create([
      'id' => 'test.example1',
      'route_name' => 'example1',
      'title' => 'foo',
      'parent' => '',
    ]),
    2 => MenuLinkMock::create([
      'id' => 'test.example2',
      'route_name' => 'example2',
      'title' => 'bar',
      'parent' => 'test.example1',
      'route_parameters' => [
        'foo' => 'bar',
      ],
    ]),
    3 => MenuLinkMock::create([
      'id' => 'test.example3',
      'route_name' => 'example3',
      'title' => 'baz',
      'parent' => 'test.example2',
      'route_parameters' => [
        'baz' => 'qux',
      ],
    ]),
    4 => MenuLinkMock::create([
      'id' => 'test.example4',
      'route_name' => 'example4',
      'title' => 'qux',
      'parent' => 'test.example3',
    ]),
    5 => MenuLinkMock::create([
      'id' => 'test.example5',
      'route_name' => 'example5',
      'title' => 'foofoo',
      'parent' => '',
    ]),
    6 => MenuLinkMock::create([
      'id' => 'test.example6',
      'route_name' => '',
      'url' => 'https://www.drupal.org/',
      'title' => 'barbar',
      'parent' => '',
    ]),
    7 => MenuLinkMock::create([
      'id' => 'test.example7',
      'route_name' => 'example7',
      'title' => 'bazbaz',
      'parent' => '',
    ]),
    8 => MenuLinkMock::create([
      'id' => 'test.example8',
      'route_name' => 'example8',
      'title' => 'quxqux',
      'parent' => '',
    ]),
  ];
  foreach ($links as $instance) {
    $this->menuLinkManager
      ->addDefinition($instance
      ->getPluginId(), $instance
      ->getPluginDefinition());
  }
  $parameters = new MenuTreeParameters();
  $tree = $this->linkTree
    ->load('mock', $parameters);
  $count = function (array $tree) {
    $sum = function ($carry, MenuLinkTreeElement $item) {
      return $carry + $item
        ->count();
    };
    return array_reduce($tree, $sum);
  };
  $this
    ->assertEqual($count($tree), 8);
  $parameters = new MenuTreeParameters();
  $parameters
    ->setRoot('test.example2');
  $tree = $this->linkTree
    ->load($instance
    ->getMenuName(), $parameters);
  $top_link = reset($tree);
  $this
    ->assertCount(1, $top_link->subtree);
  $child = reset($top_link->subtree);
  $this
    ->assertEqual($child->link
    ->getPluginId(), $links[3]
    ->getPluginId());
  $height = $this->linkTree
    ->getSubtreeHeight('test.example2');
  $this
    ->assertEqual($height, 3);
}