View source
<?php
namespace Drupal\Tests\bigmenu\Functional;
use Behat\Mink\Exception\ElementNotFoundException;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\menu_link_content\MenuLinkContentInterface;
use Drupal\system\Entity\Menu;
use Drupal\Tests\BrowserTestBase;
class BigMenuUiTest extends BrowserTestBase {
protected $adminUser;
protected $menu;
protected $defaultTheme = 'classy';
protected static $modules = [
'bigmenu',
'menu_link_content',
'menu_ui',
];
public function setUp() : void {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser([
'access administration pages',
'administer menu',
]);
$this->menu = Menu::load('main');
}
public function testBigMenuUi() {
$assert = $this
->assertSession();
$this
->drupalLogin($this->adminUser);
$item1 = $this
->createMenuLink('Item 1');
$item1_1 = $this
->createMenuLink('Item 1 - 1', 'internal:/', $item1
->uuid());
$this
->createMenuLink('Item 1 - 1 - 1', 'internal:/', $item1_1
->uuid());
$item2 = $this
->createMenuLink('Item 2 (with disabled children)');
$this
->createMenuLink('Item 2 - 1 (disabled)', 'internal:/', $item2
->uuid(), FALSE);
$this
->drupalGet('admin/structure/menu/manage/main', [
'query' => [
'menu_link' => $this
->randomMachineName(),
],
]);
$assert
->statusCodeEquals(200);
$assert
->pageTextContains('There are no menu links yet');
$this
->drupalGet('admin/structure/menu/manage/main');
$this
->assertLinkExists('#menu-overview', 'Item 1');
$this
->assertLinkNotExists('#menu-overview', 'Item 1 - 1');
$this
->assertLinkNotExists('#menu-overview', 'Item 1 - 1 - 1');
$assert
->elementNotExists('css', '.breadcrumb');
$href = $this->menu
->toUrl('edit-form', [
'query' => [
'menu_link' => 'menu_link_content:' . $item1
->uuid(),
],
])
->toString();
$assert
->linkByHrefExists($href);
$href = $this->menu
->toUrl('edit-form', [
'query' => [
'menu_link' => 'menu_link_content:' . $item2
->uuid(),
],
])
->toString();
$assert
->linkByHrefExists($href);
$this
->clickLink('Edit child items');
$this
->assertLinkExists('#menu-overview', 'Item 1');
$this
->assertLinkExists('#menu-overview', 'Item 1 - 1');
$this
->assertLinkNotExists('#menu-overview', 'Item 1 - 1 - 1');
$this
->assertLinkExists('.breadcrumb', 'Back to Main navigation top level');
$this
->clickLink('Edit child items');
$this
->assertLinkNotExists('#menu-overview', 'Item 1');
$this
->assertLinkExists('#menu-overview', 'Item 1 - 1');
$this
->assertLinkExists('#menu-overview', 'Item 1 - 1 - 1');
$this
->assertLinkExists('.breadcrumb', 'Back to Main navigation top level');
$this
->assertLinkExists('.breadcrumb', 'Item 1');
$this
->config('bigmenu.settings')
->set('max_depth', 2)
->save();
$this
->drupalGet('admin/structure/menu/manage/main');
$this
->assertLinkExists('#menu-overview', 'Item 1');
$this
->assertLinkExists('#menu-overview', 'Item 1 - 1');
$this
->assertLinkNotExists('#menu-overview', 'Item 1 - 1 - 1');
}
public function testBigMenuUiDeleteMenuLink() {
$assert = $this
->assertSession();
$this
->drupalLogin($this->adminUser);
$item1 = $this
->createMenuLink('Item 1');
$this
->createMenuLink('Item 1 - 1', 'internal:/', $item1
->uuid());
$this
->drupalGet('admin/structure/menu/manage/main');
$this
->clickLink('Edit child items');
$this
->clickLinkInRow('Item 1 - 1', 'Delete');
$this
->submitForm([], 'Delete');
$assert
->pageTextContains('The menu link Item 1 - 1 has been deleted.');
$this
->clickLinkInRow('Item 1', 'Delete');
$this
->submitForm([], 'Delete');
$assert
->statusCodeEquals(200);
$assert
->pageTextContains('The menu link Item 1 has been deleted.');
$assert
->pageTextContains('There are no menu links yet');
}
protected function createMenuLink($title, $uri = 'internal:/', $parent = NULL, $enabled = TRUE) : MenuLinkContentInterface {
$values = [
'title' => $title,
'link' => [
[
'uri' => $uri,
],
],
'menu_name' => $this->menu
->id(),
'enabled' => $enabled,
];
if ($parent) {
$values['parent'] = 'menu_link_content:' . $parent;
}
$link = MenuLinkContent::create($values);
$link
->save();
return $link;
}
protected function assertLinkNotExists($container, $label) {
$links = $this
->getSession()
->getPage()
->find('css', $container)
->findAll('named_exact', [
'link',
$label,
]);
$this
->assertEmpty($links);
}
protected function assertLinkExists($container, $label) {
$links = $this
->getSession()
->getPage()
->find('css', $container)
->findAll('named_exact', [
'link',
$label,
]);
$this
->assertNotEmpty($links);
}
protected function clickLinkInRow($row_text, $link) {
$row = $this
->getSession()
->getPage()
->find('css', sprintf('table tr:contains("%s")', $row_text));
if (!$row) {
throw new ElementNotFoundException($this
->getSession()
->getDriver(), 'table row', 'value', $row_text);
}
$row
->clickLink($link);
}
}