You are here

public function MenuUiTest::addMenuLink in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/menu_ui/tests/src/Functional/MenuUiTest.php \Drupal\Tests\menu_ui\Functional\MenuUiTest::addMenuLink()

Adds a menu link using the UI.

Parameters

string $parent: Optional parent menu link id.

string $path: The path to enter on the form. Defaults to the front page.

string $menu_name: Menu name. Defaults to 'tools'.

bool $expanded: Whether or not this menu link is expanded. Setting this to TRUE should test whether it works when we do the authenticatedUser tests. Defaults to FALSE.

string $weight: Menu weight. Defaults to 0.

Return value

\Drupal\menu_link_content\Entity\MenuLinkContent A menu link entity.

5 calls to MenuUiTest::addMenuLink()
MenuUiTest::doMenuTests in core/modules/menu_ui/tests/src/Functional/MenuUiTest.php
Tests menu functionality.
MenuUiTest::testExpandAllItems in core/modules/menu_ui/tests/src/Functional/MenuUiTest.php
Test the "expand all items" feature.
MenuUiTest::testMenuQueryAndFragment in core/modules/menu_ui/tests/src/Functional/MenuUiTest.php
Adds and removes a menu link with a query string and fragment.
MenuUiTest::testMenuUiWithPendingRevisions in core/modules/menu_ui/tests/src/Functional/MenuUiTest.php
Test that menu links with pending revisions can not be re-parented.
MenuUiTest::testUnpublishedNodeMenuItem in core/modules/menu_ui/tests/src/Functional/MenuUiTest.php
Tests that menu items pointing to unpublished nodes are editable.

File

core/modules/menu_ui/tests/src/Functional/MenuUiTest.php, line 611

Class

MenuUiTest
Add a custom menu, add menu links to the custom menu and Tools menu, check their data, and delete them using the UI.

Namespace

Drupal\Tests\menu_ui\Functional

Code

public function addMenuLink($parent = '', $path = '/', $menu_name = 'tools', $expanded = FALSE, $weight = '0') {

  // View add menu link page.
  $this
    ->drupalGet("admin/structure/menu/manage/{$menu_name}/add");
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $title = '!link_' . $this
    ->randomMachineName(16);
  $edit = [
    'link[0][uri]' => $path,
    'title[0][value]' => $title,
    'description[0][value]' => '',
    'enabled[value]' => 1,
    'expanded[value]' => $expanded,
    'menu_parent' => $menu_name . ':' . $parent,
    'weight[0][value]' => $weight,
  ];

  // Add menu link.
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertText('The menu link has been saved.');
  $menu_links = \Drupal::entityTypeManager()
    ->getStorage('menu_link_content')
    ->loadByProperties([
    'title' => $title,
  ]);
  $menu_link = reset($menu_links);
  $this
    ->assertInstanceOf(MenuLinkContent::class, $menu_link);
  $this
    ->assertMenuLink([
    'menu_name' => $menu_name,
    'children' => [],
    'parent' => $parent,
  ], $menu_link
    ->getPluginId());
  return $menu_link;
}