You are here

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

Test the access callbacks for menu links provided by *.links.menu.yml.

@covers ::menuLinkAccess

File

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

Class

MenuAdminPerMenuAccessTest
Class MenuAdminPerMenuAccessTest.

Namespace

Drupal\Tests\menu_admin_per_menu\Kernel

Code

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

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

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

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

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

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

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

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

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