You are here

public function GroupPermissionCheckerTest::testHasPermissionInGroup in Group 8

Same name and namespace in other branches
  1. 2.0.x tests/src/Unit/GroupPermissionCheckerTest.php \Drupal\Tests\group\Unit\GroupPermissionCheckerTest::testHasPermissionInGroup()

Tests checking whether a user has a permission in a group.

@covers ::hasPermissionInGroup @dataProvider provideHasPermissionInGroupScenarios

Parameters

bool $can_bypass: Whether the user can bypass group access.

bool $is_anon: Whether the user is anonymous.

array $group_type_permissions: The permissions the user has in the group type scope.

array $group_permissions: The permissions the user has in the group scope.

string $permission: The permission to check for.

bool $has_permission: Whether the user should have the permission.

string $message: The message to use in the assertion.

File

tests/src/Unit/GroupPermissionCheckerTest.php, line 66

Class

GroupPermissionCheckerTest
Tests the group permission checker service.

Namespace

Drupal\Tests\group\Unit

Code

public function testHasPermissionInGroup($can_bypass, $is_anon, $group_type_permissions, $group_permissions, $permission, $has_permission, $message) {
  $account = $this
    ->prophesize(AccountInterface::class);
  $account
    ->hasPermission('bypass group access')
    ->willReturn($can_bypass);
  $account
    ->isAnonymous()
    ->willReturn($is_anon);
  $group = $this
    ->prophesize(GroupInterface::class);
  $group
    ->id()
    ->willReturn(1);
  $group
    ->bundle()
    ->willReturn('foo');
  $scope_gt = CalculatedGroupPermissionsItemInterface::SCOPE_GROUP_TYPE;
  $scope_g = CalculatedGroupPermissionsItemInterface::SCOPE_GROUP;
  $calculated_permissions = new RefinableCalculatedGroupPermissions();
  foreach ($group_type_permissions as $identifier => $permissions) {
    $calculated_permissions
      ->addItem(new CalculatedGroupPermissionsItem($scope_gt, $identifier, $permissions));
  }
  foreach ($group_permissions as $identifier => $permissions) {
    $calculated_permissions
      ->addItem(new CalculatedGroupPermissionsItem($scope_g, $identifier, $permissions));
  }
  $this->permissionCalculator
    ->calculatePermissions($account
    ->reveal())
    ->willReturn($calculated_permissions);
  $result = $this->permissionChecker
    ->hasPermissionInGroup($permission, $account
    ->reveal(), $group
    ->reveal());
  $this
    ->assertSame($has_permission, $result, $message);
}