MenuPerRoleFunctionalTestBase.php in Menu Per Role 8
File
tests/src/Functional/MenuPerRoleFunctionalTestBase.php
View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\menu_per_role\Functional;
use Drupal\system\Entity\Menu;
use Drupal\Tests\BrowserTestBase;
abstract class MenuPerRoleFunctionalTestBase extends BrowserTestBase {
protected $defaultTheme = 'stark';
protected static $modules = [
'block',
'menu_ui',
'menu_link_content',
'menu_per_role',
];
protected $entityTypeManager;
protected $configFactory;
protected function setUp() : void {
parent::setUp();
$this->entityTypeManager = $this->container
->get('entity_type.manager');
$this->configFactory = $this->container
->get('config.factory');
$this
->createMenu('menu1');
$this
->drupalPlaceBlock('system_menu_block:menu1', [
'region' => 'header',
]);
}
protected function createMenu(string $menuId) : void {
$menu = Menu::create([
'id' => $menuId,
'label' => $this
->randomMachineName(16),
]);
$menu
->save();
}
protected function createOrUpdateMenuLink(string $menuLinkTitle, array $showMenuRoles, array $hideMenuRoles) : void {
$menu_link_storage = $this->entityTypeManager
->getStorage('menu_link_content');
$existing_menu_links = $menu_link_storage
->loadByProperties([
'title' => $menuLinkTitle,
]);
if (empty($existing_menu_links)) {
$menuLink = $menu_link_storage
->create([
'title' => $menuLinkTitle,
'link' => [
'uri' => 'internal:/',
],
'menu_name' => 'menu1',
'menu_per_role__show_role' => $showMenuRoles,
'menu_per_role__hide_role' => $hideMenuRoles,
]);
}
else {
$menuLink = array_shift($existing_menu_links);
$menuLink
->set('menu_per_role__show_role', $showMenuRoles);
$menuLink
->set('menu_per_role__hide_role', $hideMenuRoles);
}
$menuLink
->save();
}
}