You are here

protected function MenuBlockTest::createLinkHierarchy in Menu Block 8

Creates a simple hierarchy of links.

1 call to MenuBlockTest::createLinkHierarchy()
MenuBlockTest::setUp in tests/src/Functional/MenuBlockTest.php

File

tests/src/Functional/MenuBlockTest.php, line 106

Class

MenuBlockTest
Tests for the menu_block module.

Namespace

Drupal\Tests\menu_block\Functional

Code

protected function createLinkHierarchy() {

  // First remove all the menu links in the menu.
  $this->menuLinkManager
    ->deleteLinksInMenu('main');

  // Then create a simple link hierarchy:
  // - parent menu item
  //   - child-1 menu item
  //     - child-1-1 menu item
  //     - child-1-2 menu item
  //   - child-2 menu item.
  $base_options = [
    'provider' => 'menu_block',
    'menu_name' => 'main',
  ];
  $parent = $base_options + [
    'title' => 'parent menu item',
    'link' => [
      'uri' => 'internal:/menu-block-test/hierarchy/parent',
    ],
  ];

  /** @var \Drupal\menu_link_content\MenuLinkContentInterface $link */
  $link = $this->menuLinkContentStorage
    ->create($parent);
  $link
    ->save();
  $links['parent'] = $link
    ->getPluginId();
  $child_1 = $base_options + [
    'title' => 'child-1 menu item',
    'link' => [
      'uri' => 'internal:/menu-block-test/hierarchy/parent/child-1',
    ],
    'parent' => $links['parent'],
  ];
  $link = $this->menuLinkContentStorage
    ->create($child_1);
  $link
    ->save();
  $links['child-1'] = $link
    ->getPluginId();
  $child_1_1 = $base_options + [
    'title' => 'child-1-1 menu item',
    'link' => [
      'uri' => 'internal:/menu-block-test/hierarchy/parent/child-1/child-1-1',
    ],
    'parent' => $links['child-1'],
  ];
  $link = $this->menuLinkContentStorage
    ->create($child_1_1);
  $link
    ->save();
  $links['child-1-1'] = $link
    ->getPluginId();
  $child_1_2 = $base_options + [
    'title' => 'child-1-2 menu item',
    'link' => [
      'uri' => 'internal:/menu-block-test/hierarchy/parent/child-1/child-1-2',
    ],
    'parent' => $links['child-1'],
  ];
  $link = $this->menuLinkContentStorage
    ->create($child_1_2);
  $link
    ->save();
  $links['child-1-2'] = $link
    ->getPluginId();
  $child_2 = $base_options + [
    'title' => 'child-2 menu item',
    'link' => [
      'uri' => 'internal:/menu-block-test/hierarchy/parent/child-2',
    ],
    'parent' => $links['parent'],
  ];
  $link = $this->menuLinkContentStorage
    ->create($child_2);
  $link
    ->save();
  $links['child-2'] = $link
    ->getPluginId();
  return $links;
}