You are here

protected function MenuItemExtrasMenuConfigTest::setUp in Menu Item Extras 8

Overrides BrowserTestBase::setUp

File

tests/src/Functional/MenuItemExtrasMenuConfigTest.php, line 44

Class

MenuItemExtrasMenuConfigTest
Rendering menu items tests.

Namespace

Drupal\Tests\menu_item_extras\Functional

Code

protected function setUp() {
  parent::setUp();

  // Add a new custom menu.
  $menu_name = 'testmenu';
  $label = $this
    ->randomMachineName(16);
  $this->menu = Menu::create([
    'id' => $menu_name,
    'label' => $label,
    'description' => $this
      ->randomString(32),
  ]);
  $this->menu
    ->save();

  // Set default configs for menu items.
  $defaults = [
    'title' => 'Extras Link',
    'link' => 'https://example.com',
    'enabled' => TRUE,
    'description' => 'Test Description',
    'expanded' => TRUE,
    'menu_name' => $this->menu
      ->id(),
    'parent' => "{$this->menu->id()}:",
    'weight' => -10,
    'body' => '___ Menu Item Extras Field Value Level ___',
  ];

  // Generate menu items.
  for ($i = 1; $i <= $this->linksNumber; $i++) {
    if ($i > 1) {

      /** @var \Drupal\menu_link_content\Entity\MenuLinkContent $previous_link */
      $previous_link = $this->links[$i - 1]['entity'];
    }
    $link = MenuLinkContent::create(NestedArray::mergeDeep($defaults, [
      'title' => $defaults['title'] . "[{$i}]",
      'body' => $defaults['body'] . "[{$i}]",
      'parent' => isset($previous_link) ? $previous_link
        ->getPluginId() : $defaults['parent'],
    ]));
    $link
      ->save();
    $this->links[$i] = [
      'title' => $link
        ->get('title')
        ->getString(),
      'body' => $link
        ->get('body')
        ->getString(),
      'entity' => $link,
    ];
  }
}