You are here

public function ChainGroupPermissionCalculatorTest::testCalculateMemberPermissions in Group 8

Same name and namespace in other branches
  1. 2.0.x tests/src/Kernel/ChainGroupPermissionCalculatorTest.php \Drupal\Tests\group\Kernel\ChainGroupPermissionCalculatorTest::testCalculateMemberPermissions()

Tests the calculation of the member permissions.

@covers ::calculateMemberPermissions

File

tests/src/Kernel/ChainGroupPermissionCalculatorTest.php, line 166

Class

ChainGroupPermissionCalculatorTest
Tests the calculation of group permissions.

Namespace

Drupal\Tests\group\Kernel

Code

public function testCalculateMemberPermissions() {

  // @todo Use a proper set-up instead of the one from GroupKernelTestBase?
  $account = $this
    ->createUser();
  $group = $this
    ->createGroup([
    'type' => 'default',
  ]);
  $permissions = [];
  $cache_tags = [
    'group_permissions',
    'group_content_list:plugin:group_membership:entity:' . $account
      ->id(),
  ];
  sort($cache_tags);
  $calculated_permissions = $this->permissionCalculator
    ->calculateMemberPermissions($account);
  $converted = $this
    ->convertCalculatedPermissionsToArray($calculated_permissions);
  $this
    ->assertEqualsCanonicalizing($permissions, $converted, 'Member permissions are returned per group ID.');
  $this
    ->assertSame(-1, $calculated_permissions
    ->getCacheMaxAge(), 'Member permissions have the right max cache age.');
  $this
    ->assertSame($cache_tags, $calculated_permissions
    ->getCacheTags(), 'Member permissions have the right cache tags.');
  $group
    ->addMember($account);
  $member = $group
    ->getMember($account);
  $permissions[$group
    ->id()][] = 'view group';
  $permissions[$group
    ->id()][] = 'leave group';
  $cache_tags[] = 'config:group.role.default-member';
  $cache_tags = array_unique(array_merge($member
    ->getCacheTags(), $cache_tags));
  sort($cache_tags);
  $calculated_permissions = $this->permissionCalculator
    ->calculateMemberPermissions($account);
  $converted = $this
    ->convertCalculatedPermissionsToArray($calculated_permissions);
  $this
    ->assertEqualsCanonicalizing($permissions, $converted, 'Member permissions are returned per group ID after joining a group.');
  $this
    ->assertSame(-1, $calculated_permissions
    ->getCacheMaxAge(), 'Member permissions have the right max cache age after joining a group.');
  $this
    ->assertSame($cache_tags, $calculated_permissions
    ->getCacheTags(), 'Member permissions have the right cache tags after joining a group.');

  // @todo This displays a desperate need for addRole() and removeRole().
  $membership = $member
    ->getGroupContent();
  $membership->group_roles[] = 'default-custom';
  $membership
    ->save();
  $permissions[$group
    ->id()][] = 'join group';
  $cache_tags[] = 'config:group.role.default-custom';
  sort($cache_tags);
  $calculated_permissions = $this->permissionCalculator
    ->calculateMemberPermissions($account);
  $converted = $this
    ->convertCalculatedPermissionsToArray($calculated_permissions);
  $this
    ->assertEqualsCanonicalizing($permissions, $converted, 'Updated member permissions are returned per group ID.');
  $this
    ->assertSame(-1, $calculated_permissions
    ->getCacheMaxAge(), 'Updated member permissions have the right max cache age.');
  $this
    ->assertSame($cache_tags, $calculated_permissions
    ->getCacheTags(), 'Updated member permissions have the right cache tags.');
}