You are here

public function GroupContentAccessControlHandlerTest::testRelationAccess in Group 8

Tests the relation operation access.

@covers ::relationAccess @dataProvider relationAccessProvider

Parameters

\Closure $expected: A closure returning the expected access result.

string $plugin_id: The plugin ID.

array $definition: The plugin definition.

bool $has_admin_permission: Whether the account has the admin permission.

bool $has_permission: Whether the account has the required permission.

bool $has_own_permission: Whether the account has the required owner permission.

string|false $permission: The operation permission.

string|false $own_permission: The owner operation permission.

bool $is_owner: Whether the account owns the relation.

File

tests/src/Unit/GroupContentAccessControlHandlerTest.php, line 92

Class

GroupContentAccessControlHandlerTest
Tests the default GroupContentEnabler access handler.

Namespace

Drupal\Tests\group\Unit

Code

public function testRelationAccess(\Closure $expected, $plugin_id, array $definition, $has_admin_permission, $has_permission, $has_own_permission, $permission, $own_permission, $is_owner) {
  $operation = $this
    ->randomMachineName();
  $permission_provider = $this
    ->prophesize(GroupContentPermissionProviderInterface::class);
  $permission_provider
    ->getAdminPermission()
    ->willReturn($definition['admin_permission']);
  $permission_provider
    ->getPermission($operation, 'relation', 'any')
    ->willReturn($permission);
  $permission_provider
    ->getPermission($operation, 'relation', 'own')
    ->willReturn($own_permission);
  $manager = $this
    ->prophesize(GroupContentEnablerManagerInterface::class);
  $manager
    ->hasHandler($plugin_id, 'permission_provider')
    ->willReturn(TRUE);
  $manager
    ->getPermissionProvider($plugin_id)
    ->willReturn($permission_provider
    ->reveal());
  $this->container
    ->get('plugin.manager.group_content_enabler')
    ->willReturn($manager
    ->reveal());
  $access_control_handler = GroupContentAccessControlHandler::createInstance($this->container
    ->reveal(), $plugin_id, $definition);
  $account_id = rand(1, 100);
  $account = $this
    ->prophesize(AccountInterface::class);
  $account
    ->id()
    ->willReturn($account_id);
  $account = $account
    ->reveal();
  $group = $this
    ->prophesize(GroupInterface::class);
  $group_content = $this
    ->prophesize(GroupContentInterface::class);
  $group_content
    ->getGroup()
    ->willReturn($group
    ->reveal());
  $group_content
    ->getOwnerId()
    ->willReturn($is_owner ? $account_id : $account_id + 1);
  $group_content
    ->getCacheContexts()
    ->willReturn([]);
  $group_content
    ->getCachetags()
    ->willReturn([
    'group_content:foo',
  ]);
  $group_content
    ->getCacheMaxAge()
    ->willReturn(9999);
  if ($definition['admin_permission']) {
    $group
      ->hasPermission($definition['admin_permission'], $account)
      ->willReturn($has_admin_permission);
  }
  else {
    $group
      ->hasPermission($definition['admin_permission'], $account)
      ->shouldNotBeCalled();
  }
  if ($permission) {
    $group
      ->hasPermission($permission, $account)
      ->willReturn($has_permission);
  }
  else {
    $group
      ->hasPermission($permission, $account)
      ->shouldNotBeCalled();
  }
  if ($own_permission) {
    $group
      ->hasPermission($own_permission, $account)
      ->willReturn($has_own_permission);
  }
  else {
    $group
      ->hasPermission($own_permission, $account)
      ->shouldNotBeCalled();
  }
  $result = $access_control_handler
    ->relationAccess($group_content
    ->reveal(), $operation, $account, TRUE);
  $this
    ->assertEquals($expected(), $result);
}