Add a menu link using the menu module UI.
integer $plid Parent menu link id.:
string $link Link path.:
string $menu_name Menu name.:
string $weight Menu weight:
\Drupal\menu_link\Plugin\Core\Entity\MenuLink $menu_link A menu link entity.
function addMenuLink($plid = 0, $link = '<front>', $menu_name = 'tools', $expanded = TRUE, $weight = '0') {
// View add menu link page.
$this
->drupalGet("admin/structure/menu/manage/{$menu_name}/add");
$this
->assertResponse(200);
$title = '!link_' . $this
->randomName(16);
$edit = array(
'link_path' => $link,
'link_title' => $title,
'description' => '',
'enabled' => TRUE,
// Use this to disable the menu and test.
'expanded' => $expanded,
// Setting this to true should test whether it works when we do the std_user tests.
'parent' => $menu_name . ':' . $plid,
'weight' => $weight,
);
// Add menu link.
$this
->drupalPost(NULL, $edit, t('Save'));
$this
->assertResponse(200);
$this
->assertText('The menu link has been saved.');
$menu_links = entity_load_multiple_by_properties('menu_link', array(
'link_title' => $title,
));
$menu_link = reset($menu_links);
$this
->assertTrue('Menu link was found in database.');
$this
->assertMenuLink($menu_link
->id(), array(
'menu_name' => $menu_name,
'link_path' => $link,
'has_children' => 0,
'plid' => $plid,
));
return $menu_link;
}