You are here

public function MenuLinkTreeTest::testCreateLinksInMenu in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Menu/MenuLinkTreeTest.php \Drupal\system\Tests\Menu\MenuLinkTreeTest::testCreateLinksInMenu()

Tests creating links with an expected tree structure.

File

core/modules/system/src/Tests/Menu/MenuLinkTreeTest.php, line 91
Contains \Drupal\system\Tests\Menu\MenuLinkTreeTest.

Class

MenuLinkTreeTest
Tests the menu link tree.

Namespace

Drupal\system\Tests\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 = array(
    1 => MenuLinkMock::create(array(
      'id' => 'test.example1',
      'route_name' => 'example1',
      'title' => 'foo',
      'parent' => '',
    )),
    2 => MenuLinkMock::create(array(
      'id' => 'test.example2',
      'route_name' => 'example2',
      'title' => 'bar',
      'parent' => 'test.example1',
      'route_parameters' => array(
        'foo' => 'bar',
      ),
    )),
    3 => MenuLinkMock::create(array(
      'id' => 'test.example3',
      'route_name' => 'example3',
      'title' => 'baz',
      'parent' => 'test.example2',
      'route_parameters' => array(
        'baz' => 'qux',
      ),
    )),
    4 => MenuLinkMock::create(array(
      'id' => 'test.example4',
      'route_name' => 'example4',
      'title' => 'qux',
      'parent' => 'test.example3',
    )),
    5 => MenuLinkMock::create(array(
      'id' => 'test.example5',
      'route_name' => 'example5',
      'title' => 'foofoo',
      'parent' => '',
    )),
    6 => MenuLinkMock::create(array(
      'id' => 'test.example6',
      'route_name' => '',
      'url' => 'https://www.drupal.org/',
      'title' => 'barbar',
      'parent' => '',
    )),
    7 => MenuLinkMock::create(array(
      'id' => 'test.example7',
      'route_name' => 'example7',
      'title' => 'bazbaz',
      'parent' => '',
    )),
    8 => MenuLinkMock::create(array(
      '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
    ->assertEqual(count($top_link->subtree), 1);
  $child = reset($top_link->subtree);
  $this
    ->assertEqual($child->link
    ->getPluginId(), $links[3]
    ->getPluginId());
  $height = $this->linkTree
    ->getSubtreeHeight('test.example2');
  $this
    ->assertEqual($height, 3);
}