You are here

public function MenuAdminPerMenuAccessTest::testMenuItemAccess in Menu Admin per Menu 8

Access check for menu link content access callback.

@covers ::menuItemAccess

File

tests/src/Kernel/MenuAdminPerMenuAccessTest.php, line 307

Class

MenuAdminPerMenuAccessTest
Class MenuAdminPerMenuAccessTest.

Namespace

Drupal\Tests\menu_admin_per_menu\Kernel

Code

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(),
  ]);

  // Anonymous users has no access to one of the menu items.
  $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));

  // Authenticated user has no access to one of the menu items.
  $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));

  // Admin user has access to all of the menu items.
  $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));

  // User with 'administer menu' permission has access to all menus items.
  $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));

  // Menu 1 user has only access to menu items of menu 1.
  $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));

  // Menu 2 user has only access to menu items of menu 2.
  $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));

  // Menu 3 user has only access to menu items of menu 3 because of the hook
  // implementation.
  $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));

  // Make sure that calling the menuItemAccess method without menu link does
  // not result in a fatal error.
  $this
    ->assertEquals(new AccessResultNeutral(), $this->menuAdminPerMenuAllowedMenus
    ->menuItemAccess($this->adminUser));
}