You are here

public function MenuPerRoleAccessTest::testRoleAccess in Menu Per Role 8

Check if users have access to menu links.

Throws

\Drupal\Core\Entity\EntityStorageException

\Behat\Mink\Exception\ExpectationException

1 method overrides MenuPerRoleAccessTest::testRoleAccess()
MenuPerRoleAdminAccessTest::testRoleAccess in tests/src/Functional/MenuPerRoleAdminAccessTest.php
Check if users have access to menu links.

File

tests/src/Functional/MenuPerRoleAccessTest.php, line 200

Class

MenuPerRoleAccessTest
Test access control to menu links.

Namespace

Drupal\Tests\menu_per_role\Functional

Code

public function testRoleAccess() {

  // Test "Show roles" by role.
  $this
    ->prepareMenuLinkAndExpectations('link 1 - show anonymous', [
    AccountInterface::ANONYMOUS_ROLE,
  ], [], [
    'anonymous' => TRUE,
    'user1' => FALSE,
    'user2' => FALSE,
    'user3' => TRUE,
    'user4' => FALSE,
  ]);
  $this
    ->prepareMenuLinkAndExpectations('link 2 - show role 1', [
    $this->user1Role,
  ], [], [
    'anonymous' => FALSE,
    'user1' => TRUE,
    'user2' => FALSE,
    'user3' => TRUE,
    'user4' => FALSE,
  ]);
  $this
    ->prepareMenuLinkAndExpectations('link 3 - show role 2', [
    $this->user2Role,
  ], [], [
    'anonymous' => FALSE,
    'user1' => FALSE,
    'user2' => TRUE,
    'user3' => TRUE,
    'user4' => FALSE,
  ]);
  $this
    ->prepareMenuLinkAndExpectations('link 4 - show role 3', [
    $this->user3Role,
  ], [], [
    'anonymous' => FALSE,
    'user1' => FALSE,
    'user2' => FALSE,
    'user3' => TRUE,
    'user4' => FALSE,
  ]);

  // Test "Hide roles" by role.
  $this
    ->prepareMenuLinkAndExpectations('link 5 - hide anonymous', [], [
    AccountInterface::ANONYMOUS_ROLE,
  ], [
    'anonymous' => FALSE,
    'user1' => TRUE,
    'user2' => TRUE,
    'user3' => TRUE,
    'user4' => TRUE,
  ]);
  $this
    ->prepareMenuLinkAndExpectations('link 6 - hide role 1', [], [
    $this->user1Role,
  ], [
    'anonymous' => TRUE,
    'user1' => FALSE,
    'user2' => TRUE,
    'user3' => TRUE,
    'user4' => TRUE,
  ]);
  $this
    ->prepareMenuLinkAndExpectations('link 7 - hide role 2', [], [
    $this->user2Role,
  ], [
    'anonymous' => TRUE,
    'user1' => TRUE,
    'user2' => FALSE,
    'user3' => TRUE,
    'user4' => TRUE,
  ]);
  $this
    ->prepareMenuLinkAndExpectations('link 8 - hide role 3', [], [
    $this->user3Role,
  ], [
    'anonymous' => TRUE,
    'user1' => TRUE,
    'user2' => TRUE,
    'user3' => TRUE,
    'user4' => TRUE,
  ]);

  // Test combinations of roles to show.
  $this
    ->prepareMenuLinkAndExpectations('link 9 - show anonymous and role 1', [
    AccountInterface::ANONYMOUS_ROLE,
    $this->user1Role,
  ], [], [
    'anonymous' => TRUE,
    'user1' => TRUE,
    'user2' => FALSE,
    'user3' => TRUE,
    'user4' => FALSE,
  ]);
  $this
    ->prepareMenuLinkAndExpectations('link 10 - show role 1 and role 2', [
    $this->user1Role,
    $this->user2Role,
  ], [], [
    'anonymous' => FALSE,
    'user1' => TRUE,
    'user2' => TRUE,
    'user3' => TRUE,
    'user4' => FALSE,
  ]);
  $this
    ->prepareMenuLinkAndExpectations('link 11 - show role 2 and role 3', [
    $this->user2Role,
    $this->user3Role,
  ], [], [
    'anonymous' => FALSE,
    'user1' => FALSE,
    'user2' => TRUE,
    'user3' => TRUE,
    'user4' => FALSE,
  ]);
  $this
    ->prepareMenuLinkAndExpectations('link 12 - show anonymous and role 3', [
    AccountInterface::ANONYMOUS_ROLE,
    $this->user3Role,
  ], [], [
    'anonymous' => TRUE,
    'user1' => FALSE,
    'user2' => FALSE,
    'user3' => TRUE,
    'user4' => FALSE,
  ]);

  // Test combinations of roles to hide.
  $this
    ->prepareMenuLinkAndExpectations('link 13 - hide anonymous and role 1', [], [
    AccountInterface::ANONYMOUS_ROLE,
    $this->user1Role,
  ], [
    'anonymous' => FALSE,
    'user1' => FALSE,
    'user2' => TRUE,
    'user3' => TRUE,
    'user4' => TRUE,
  ]);
  $this
    ->prepareMenuLinkAndExpectations('link 14 - hide role 1 and role 2', [], [
    $this->user1Role,
    $this->user2Role,
  ], [
    'anonymous' => TRUE,
    'user1' => FALSE,
    'user2' => FALSE,
    'user3' => TRUE,
    'user4' => TRUE,
  ]);
  $this
    ->prepareMenuLinkAndExpectations('link 15 - hide role 1 and role 3', [], [
    $this->user1Role,
    $this->user3Role,
  ], [
    'anonymous' => TRUE,
    'user1' => FALSE,
    'user2' => TRUE,
    'user3' => TRUE,
    'user4' => TRUE,
  ]);
  $this
    ->prepareMenuLinkAndExpectations('link 16 - hide anonymous and role 2', [], [
    AccountInterface::ANONYMOUS_ROLE,
    $this->user2Role,
  ], [
    'anonymous' => FALSE,
    'user1' => TRUE,
    'user2' => FALSE,
    'user3' => TRUE,
    'user4' => TRUE,
  ]);

  // Test combinations of roles to show and hide.
  $this
    ->prepareMenuLinkAndExpectations('link 17 - show anonymous and hide role 1', [
    AccountInterface::ANONYMOUS_ROLE,
  ], [
    $this->user1Role,
  ], [
    'anonymous' => TRUE,
    'user1' => FALSE,
    'user2' => FALSE,
    'user3' => TRUE,
    'user4' => FALSE,
  ]);
  $this
    ->prepareMenuLinkAndExpectations('link 18 - show role 2 and hide role 1', [
    $this->user2Role,
  ], [
    $this->user1Role,
  ], [
    'anonymous' => FALSE,
    'user1' => FALSE,
    'user2' => TRUE,
    'user3' => TRUE,
    'user4' => FALSE,
  ]);
  $this
    ->prepareMenuLinkAndExpectations('link 19 - show anonymous and hide role 1 and role 3', [
    AccountInterface::ANONYMOUS_ROLE,
  ], [
    $this->user1Role,
    $this->user3Role,
  ], [
    'anonymous' => TRUE,
    'user1' => FALSE,
    'user2' => FALSE,
    'user3' => TRUE,
    'user4' => FALSE,
  ]);
  $this
    ->prepareMenuLinkAndExpectations('link 20 - show anonymous and role 1 and role 3 and hide role 2', [
    AccountInterface::ANONYMOUS_ROLE,
    $this->user1Role,
    $this->user3Role,
  ], [
    $this->user2Role,
  ], [
    'anonymous' => TRUE,
    'user1' => TRUE,
    'user2' => FALSE,
    'user3' => TRUE,
    'user4' => FALSE,
  ]);
  $this
    ->linksAccessTest();
}