View source
<?php
namespace Drupal\Tests\menu_admin_per_menu\Kernel;
use Drupal\Core\Access\AccessResultAllowed;
use Drupal\Core\Access\AccessResultNeutral;
use Drupal\Core\Session\AnonymousUserSession;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\user\Traits\UserCreationTrait;
class MenuAdminPerMenuAccessTest extends KernelTestBase {
use UserCreationTrait;
protected static $modules = [
'menu_admin_per_menu',
'menu_admin_per_menu_test',
'link',
'menu_link_content',
'menu_ui',
'system',
'user',
];
protected $menu1;
protected $menu2;
protected $menu3;
protected $anonymousUser;
protected $authenticatedUser;
protected $adminUser;
protected $adminMenuUser;
protected $menu1User;
protected $menu2User;
protected $menu3User;
protected $menuAdminPerMenuAllowedMenus;
protected $menuLinkContentStorage;
protected $menuLinkManager;
protected $menuStorage;
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('menu');
$this
->installEntitySchema('menu_link_content');
$this
->installSchema('system', 'sequences');
$this
->installConfig('system');
$this
->installEntitySchema('user');
$this
->installConfig('user');
$this
->installConfig('menu_admin_per_menu_test');
$this->menuAdminPerMenuAllowedMenus = $this->container
->get('menu_admin_per_menu.allowed_menus');
$this->menuLinkContentStorage = $this->container
->get('entity_type.manager')
->getStorage('menu_link_content');
$this->menuLinkManager = $this->container
->get('plugin.manager.menu.link');
$this->menuStorage = $this->container
->get('entity_type.manager')
->getStorage('menu');
$this->menu1 = $this->menuStorage
->load('menu_1');
$this->menu2 = $this->menuStorage
->load('menu_2');
$this->menu3 = $this->menuStorage
->load('menu_3');
$this->anonymousUser = new AnonymousUserSession();
$this->adminUser = $this
->createUser([], 'Admin', TRUE);
$this->authenticatedUser = $this
->createUser([], 'Authenticated user');
$this->adminMenuUser = $this
->createUser([
'administer menu',
], 'Admin menu user');
$this->menu1User = $this
->createUser([
'administer menu_1 menu items',
], 'Menu 1 user');
$this->menu2User = $this
->createUser([
'administer menu_2 menu items',
], 'Menu 2 user');
$this->menu3User = $this
->createUser([], 'Menu 3 user');
$this->container
->get('router.builder')
->rebuild();
}
public function testGetMenuPermissions() {
$this
->assertEmpty(array_keys($this->menuAdminPerMenuAllowedMenus
->getPerMenuPermissions($this->anonymousUser)));
$this
->assertEmpty(array_keys($this->menuAdminPerMenuAllowedMenus
->getPerMenuPermissions($this->authenticatedUser)));
$this
->assertEquals([
'administer admin menu items',
'administer footer menu items',
'administer main menu items',
'administer menu_1 menu items',
'administer menu_2 menu items',
'administer menu_3 menu items',
'administer tools menu items',
'administer account menu items',
], array_keys($this->menuAdminPerMenuAllowedMenus
->getPerMenuPermissions($this->adminUser)));
$this
->assertEmpty(array_keys($this->menuAdminPerMenuAllowedMenus
->getPerMenuPermissions($this->adminMenuUser)));
$this
->assertEquals([
'administer menu_1 menu items',
], array_keys($this->menuAdminPerMenuAllowedMenus
->getPerMenuPermissions($this->menu1User)));
$this
->assertEquals([
'administer menu_2 menu items',
], array_keys($this->menuAdminPerMenuAllowedMenus
->getPerMenuPermissions($this->menu2User)));
$this
->assertEquals([
'administer menu_3 menu items',
], array_keys($this->menuAdminPerMenuAllowedMenus
->getPerMenuPermissions($this->menu3User)));
}
public function testMenusOverviewAccess() {
$this
->assertEquals(new AccessResultNeutral(), $this->menuAdminPerMenuAllowedMenus
->menusOverviewAccess($this->anonymousUser));
$this
->assertEquals(new AccessResultNeutral(), $this->menuAdminPerMenuAllowedMenus
->menusOverviewAccess($this->authenticatedUser));
$this
->assertEquals(new AccessResultAllowed(), $this->menuAdminPerMenuAllowedMenus
->menusOverviewAccess($this->adminUser));
$this
->assertEquals(new AccessResultAllowed(), $this->menuAdminPerMenuAllowedMenus
->menusOverviewAccess($this->adminMenuUser));
$this
->assertEquals(new AccessResultAllowed(), $this->menuAdminPerMenuAllowedMenus
->menusOverviewAccess($this->menu1User));
$this
->assertEquals(new AccessResultAllowed(), $this->menuAdminPerMenuAllowedMenus
->menusOverviewAccess($this->menu2User));
$this
->assertEquals(new AccessResultAllowed(), $this->menuAdminPerMenuAllowedMenus
->menusOverviewAccess($this->menu3User));
}
public function testMenuAccess() {
$this
->assertEquals(new AccessResultNeutral(), $this->menuAdminPerMenuAllowedMenus
->menuAccess($this->anonymousUser, $this->menu1));
$this
->assertEquals(new AccessResultNeutral(), $this->menuAdminPerMenuAllowedMenus
->menuAccess($this->anonymousUser, $this->menu2));
$this
->assertEquals(new AccessResultNeutral(), $this->menuAdminPerMenuAllowedMenus
->menuAccess($this->anonymousUser, $this->menu3));
$this
->assertEquals(new AccessResultNeutral(), $this->menuAdminPerMenuAllowedMenus
->menuAccess($this->authenticatedUser, $this->menu1));
$this
->assertEquals(new AccessResultNeutral(), $this->menuAdminPerMenuAllowedMenus
->menuAccess($this->authenticatedUser, $this->menu2));
$this
->assertEquals(new AccessResultNeutral(), $this->menuAdminPerMenuAllowedMenus
->menuAccess($this->authenticatedUser, $this->menu3));
$this
->assertEquals(new AccessResultAllowed(), $this->menuAdminPerMenuAllowedMenus
->menuAccess($this->adminUser, $this->menu1));
$this
->assertEquals(new AccessResultAllowed(), $this->menuAdminPerMenuAllowedMenus
->menuAccess($this->adminUser, $this->menu2));
$this
->assertEquals(new AccessResultAllowed(), $this->menuAdminPerMenuAllowedMenus
->menuAccess($this->adminUser, $this->menu3));
$this
->assertEquals(new AccessResultAllowed(), $this->menuAdminPerMenuAllowedMenus
->menuAccess($this->adminMenuUser, $this->menu1));
$this
->assertEquals(new AccessResultAllowed(), $this->menuAdminPerMenuAllowedMenus
->menuAccess($this->adminMenuUser, $this->menu2));
$this
->assertEquals(new AccessResultAllowed(), $this->menuAdminPerMenuAllowedMenus
->menuAccess($this->adminMenuUser, $this->menu3));
$this
->assertEquals(new AccessResultAllowed(), $this->menuAdminPerMenuAllowedMenus
->menuAccess($this->menu1User, $this->menu1));
$this
->assertEquals(new AccessResultNeutral(), $this->menuAdminPerMenuAllowedMenus
->menuAccess($this->menu1User, $this->menu2));
$this
->assertEquals(new AccessResultNeutral(), $this->menuAdminPerMenuAllowedMenus
->menuAccess($this->menu1User, $this->menu3));
$this
->assertEquals(new AccessResultNeutral(), $this->menuAdminPerMenuAllowedMenus
->menuAccess($this->menu2User, $this->menu1));
$this
->assertEquals(new AccessResultAllowed(), $this->menuAdminPerMenuAllowedMenus
->menuAccess($this->menu2User, $this->menu2));
$this
->assertEquals(new AccessResultNeutral(), $this->menuAdminPerMenuAllowedMenus
->menuAccess($this->menu2User, $this->menu3));
$this
->assertEquals(new AccessResultNeutral(), $this->menuAdminPerMenuAllowedMenus
->menuAccess($this->menu3User, $this->menu1));
$this
->assertEquals(new AccessResultNeutral(), $this->menuAdminPerMenuAllowedMenus
->menuAccess($this->menu3User, $this->menu2));
$this
->assertEquals(new AccessResultAllowed(), $this->menuAdminPerMenuAllowedMenus
->menuAccess($this->menu3User, $this->menu3));
}
public function testMenuItemAccess() {
$menu_1_link = $this->menuLinkContentStorage
->create([
'menu_name' => $this->menu1
->id(),
]);
$menu_2_link = $this->menuLinkContentStorage
->create([
'menu_name' => $this->menu2
->id(),
]);
$menu_3_link = $this->menuLinkContentStorage
->create([
'menu_name' => $this->menu3
->id(),
]);
$this
->assertEquals(new AccessResultNeutral(), $this->menuAdminPerMenuAllowedMenus
->menuItemAccess($this->anonymousUser, $menu_1_link));
$this
->assertEquals(new AccessResultNeutral(), $this->menuAdminPerMenuAllowedMenus
->menuItemAccess($this->anonymousUser, $menu_2_link));
$this
->assertEquals(new AccessResultNeutral(), $this->menuAdminPerMenuAllowedMenus
->menuItemAccess($this->anonymousUser, $menu_3_link));
$this
->assertEquals(new AccessResultNeutral(), $this->menuAdminPerMenuAllowedMenus
->menuItemAccess($this->authenticatedUser, $menu_1_link));
$this
->assertEquals(new AccessResultNeutral(), $this->menuAdminPerMenuAllowedMenus
->menuItemAccess($this->authenticatedUser, $menu_2_link));
$this
->assertEquals(new AccessResultNeutral(), $this->menuAdminPerMenuAllowedMenus
->menuItemAccess($this->authenticatedUser, $menu_3_link));
$this
->assertEquals(new AccessResultAllowed(), $this->menuAdminPerMenuAllowedMenus
->menuItemAccess($this->adminUser, $menu_1_link));
$this
->assertEquals(new AccessResultAllowed(), $this->menuAdminPerMenuAllowedMenus
->menuItemAccess($this->adminUser, $menu_2_link));
$this
->assertEquals(new AccessResultAllowed(), $this->menuAdminPerMenuAllowedMenus
->menuItemAccess($this->adminUser, $menu_3_link));
$this
->assertEquals(new AccessResultAllowed(), $this->menuAdminPerMenuAllowedMenus
->menuItemAccess($this->adminMenuUser, $menu_1_link));
$this
->assertEquals(new AccessResultAllowed(), $this->menuAdminPerMenuAllowedMenus
->menuItemAccess($this->adminMenuUser, $menu_2_link));
$this
->assertEquals(new AccessResultAllowed(), $this->menuAdminPerMenuAllowedMenus
->menuItemAccess($this->adminMenuUser, $menu_3_link));
$this
->assertEquals(new AccessResultAllowed(), $this->menuAdminPerMenuAllowedMenus
->menuItemAccess($this->menu1User, $menu_1_link));
$this
->assertEquals(new AccessResultNeutral(), $this->menuAdminPerMenuAllowedMenus
->menuItemAccess($this->menu1User, $menu_2_link));
$this
->assertEquals(new AccessResultNeutral(), $this->menuAdminPerMenuAllowedMenus
->menuItemAccess($this->menu1User, $menu_3_link));
$this
->assertEquals(new AccessResultNeutral(), $this->menuAdminPerMenuAllowedMenus
->menuItemAccess($this->menu2User, $menu_1_link));
$this
->assertEquals(new AccessResultAllowed(), $this->menuAdminPerMenuAllowedMenus
->menuItemAccess($this->menu2User, $menu_2_link));
$this
->assertEquals(new AccessResultNeutral(), $this->menuAdminPerMenuAllowedMenus
->menuItemAccess($this->menu2User, $menu_3_link));
$this
->assertEquals(new AccessResultNeutral(), $this->menuAdminPerMenuAllowedMenus
->menuItemAccess($this->menu3User, $menu_1_link));
$this
->assertEquals(new AccessResultNeutral(), $this->menuAdminPerMenuAllowedMenus
->menuItemAccess($this->menu3User, $menu_2_link));
$this
->assertEquals(new AccessResultAllowed(), $this->menuAdminPerMenuAllowedMenus
->menuItemAccess($this->menu3User, $menu_3_link));
$this
->assertEquals(new AccessResultNeutral(), $this->menuAdminPerMenuAllowedMenus
->menuItemAccess($this->adminUser));
}
public function testMenuLinkAccess() {
$menu_1_link = $this->menuLinkManager
->getInstance([
'id' => 'menu_1.link',
]);
$menu_2_link = $this->menuLinkManager
->getInstance([
'id' => 'menu_2.link',
]);
$menu_3_link = $this->menuLinkManager
->getInstance([
'id' => 'menu_3.link',
]);
$this
->assertEquals(new AccessResultNeutral(), $this->menuAdminPerMenuAllowedMenus
->menuLinkAccess($this->anonymousUser, $menu_1_link));
$this
->assertEquals(new AccessResultNeutral(), $this->menuAdminPerMenuAllowedMenus
->menuLinkAccess($this->anonymousUser, $menu_2_link));
$this
->assertEquals(new AccessResultNeutral(), $this->menuAdminPerMenuAllowedMenus
->menuLinkAccess($this->anonymousUser, $menu_3_link));
$this
->assertEquals(new AccessResultNeutral(), $this->menuAdminPerMenuAllowedMenus
->menuLinkAccess($this->authenticatedUser, $menu_1_link));
$this
->assertEquals(new AccessResultNeutral(), $this->menuAdminPerMenuAllowedMenus
->menuLinkAccess($this->authenticatedUser, $menu_2_link));
$this
->assertEquals(new AccessResultNeutral(), $this->menuAdminPerMenuAllowedMenus
->menuLinkAccess($this->authenticatedUser, $menu_3_link));
$this
->assertEquals(new AccessResultAllowed(), $this->menuAdminPerMenuAllowedMenus
->menuLinkAccess($this->adminUser, $menu_1_link));
$this
->assertEquals(new AccessResultAllowed(), $this->menuAdminPerMenuAllowedMenus
->menuLinkAccess($this->adminUser, $menu_2_link));
$this
->assertEquals(new AccessResultAllowed(), $this->menuAdminPerMenuAllowedMenus
->menuLinkAccess($this->adminUser, $menu_3_link));
$this
->assertEquals(new AccessResultAllowed(), $this->menuAdminPerMenuAllowedMenus
->menuLinkAccess($this->adminMenuUser, $menu_1_link));
$this
->assertEquals(new AccessResultAllowed(), $this->menuAdminPerMenuAllowedMenus
->menuLinkAccess($this->adminMenuUser, $menu_2_link));
$this
->assertEquals(new AccessResultAllowed(), $this->menuAdminPerMenuAllowedMenus
->menuLinkAccess($this->adminMenuUser, $menu_3_link));
$this
->assertEquals(new AccessResultAllowed(), $this->menuAdminPerMenuAllowedMenus
->menuLinkAccess($this->menu1User, $menu_1_link));
$this
->assertEquals(new AccessResultNeutral(), $this->menuAdminPerMenuAllowedMenus
->menuLinkAccess($this->menu1User, $menu_2_link));
$this
->assertEquals(new AccessResultNeutral(), $this->menuAdminPerMenuAllowedMenus
->menuLinkAccess($this->menu1User, $menu_3_link));
$this
->assertEquals(new AccessResultNeutral(), $this->menuAdminPerMenuAllowedMenus
->menuLinkAccess($this->menu2User, $menu_1_link));
$this
->assertEquals(new AccessResultAllowed(), $this->menuAdminPerMenuAllowedMenus
->menuLinkAccess($this->menu2User, $menu_2_link));
$this
->assertEquals(new AccessResultNeutral(), $this->menuAdminPerMenuAllowedMenus
->menuLinkAccess($this->menu2User, $menu_3_link));
$this
->assertEquals(new AccessResultNeutral(), $this->menuAdminPerMenuAllowedMenus
->menuLinkAccess($this->menu3User, $menu_1_link));
$this
->assertEquals(new AccessResultNeutral(), $this->menuAdminPerMenuAllowedMenus
->menuLinkAccess($this->menu3User, $menu_2_link));
$this
->assertEquals(new AccessResultAllowed(), $this->menuAdminPerMenuAllowedMenus
->menuLinkAccess($this->menu3User, $menu_3_link));
$this
->assertEquals(new AccessResultNeutral(), $this->menuAdminPerMenuAllowedMenus
->menuLinkAccess($this->adminUser));
}
}