View source
<?php
namespace Drupal\Tests\menu_item_extras\Functional;
use Drupal\Component\Utility\NestedArray;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\system\Entity\Menu;
use Drupal\Tests\BrowserTestBase;
class MenuItemExtrasRenderTest extends BrowserTestBase {
protected $block;
protected $menu;
protected $links = [];
protected $linksNumber = 3;
public static $modules = [
'menu_item_extras',
];
protected function setUp() {
parent::setUp();
$menu_name = 'testmenu';
$label = $this
->randomMachineName(16);
$this->menu = Menu::create([
'id' => $menu_name,
'label' => $label,
'description' => $this
->randomString(32),
]);
$this->container
->get('config.factory')
->getEditable('menu_item_extras.settings')
->set('allowed_menus', [
$menu_name,
])
->save();
$this->menu
->save();
$this->block = $this
->drupalPlaceBlock('system_menu_block:' . $this->menu
->id(), [
'region' => 'header',
'level' => 1,
'depth' => $this->linksNumber,
]);
$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 ___',
];
for ($i = 1; $i <= $this->linksNumber; $i++) {
if ($i > 1) {
$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,
];
}
}
public function testMultilevelItems() {
$assert = $this
->assertSession();
$this
->drupalGet('<front>');
foreach ($this->links as $link) {
$assert
->pageTextContains($link['title']);
$assert
->pageTextContains($link['body']);
}
}
}