You are here

public function ChainGroupPermissionCalculatorTest::testCalculateMultipleMembersPermissions in Group 8

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

Test members permissions with multiple users, groups and group roles.

File

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

Class

ChainGroupPermissionCalculatorTest
Tests the calculation of group permissions.

Namespace

Drupal\Tests\group\Kernel

Code

public function testCalculateMultipleMembersPermissions() {
  $account1 = $this
    ->createUser();
  $account2 = $this
    ->createUser();
  $group1 = $this
    ->createGroup([
    'type' => 'default',
  ]);
  $group2 = $this
    ->createGroup([
    'type' => 'default',
  ]);

  // Expected values.
  $permissions1 = [];
  $permissions2 = [];

  // Assert permissions for accounts when users are not group members.
  $calculated_permissions1 = $this->permissionCalculator
    ->calculateMemberPermissions($account1);
  $converted1 = $this
    ->convertCalculatedPermissionsToArray($calculated_permissions1);
  $this
    ->assertEqualsCanonicalizing($permissions1, $converted1, 'Member permissions are returned per group ID.');
  $calculated_permissions2 = $this->permissionCalculator
    ->calculateMemberPermissions($account2);
  $converted2 = $this
    ->convertCalculatedPermissionsToArray($calculated_permissions2);
  $this
    ->assertEqualsCanonicalizing($permissions2, $converted2, 'Member permissions are returned per group ID.');

  // Add account1 to group1.
  $group1
    ->addMember($account1);
  $permissions1[$group1
    ->id()][] = 'view group';
  $permissions1[$group1
    ->id()][] = 'leave group';
  $calculated_permissions1 = $this->permissionCalculator
    ->calculateMemberPermissions($account1);
  $converted1 = $this
    ->convertCalculatedPermissionsToArray($calculated_permissions1);
  $this
    ->assertEqualsCanonicalizing($permissions1, $converted1, 'Member permissions are returned per group ID after joining a group.');

  // Add account2 to group2 and apply an additional role.
  $group2
    ->addMember($account2);
  $member2 = $group2
    ->getMember($account2);
  $membership2 = $member2
    ->getGroupContent();
  $membership2->group_roles[] = 'default-custom';
  $membership2
    ->save();
  $permissions2[$group2
    ->id()][] = 'view group';
  $permissions2[$group2
    ->id()][] = 'leave group';
  $permissions2[$group2
    ->id()][] = 'join group';

  // Permissions for account1 did not change.
  $calculated_permissions1 = $this->permissionCalculator
    ->calculateMemberPermissions($account1);
  $converted1 = $this
    ->convertCalculatedPermissionsToArray($calculated_permissions1);
  $this
    ->assertEqualsCanonicalizing($permissions1, $converted1, 'Member permissions are returned per group ID after joining a group.');

  // Assert permissions for account2.
  $calculated_permissions2 = $this->permissionCalculator
    ->calculateMemberPermissions($account2);
  $converted2 = $this
    ->convertCalculatedPermissionsToArray($calculated_permissions2);
  $this
    ->assertEqualsCanonicalizing($permissions2, $converted2, 'Updated member permissions are returned per group ID.');

  // Remove "default-custom" group role for account2.
  $membership2->group_roles = [
    'default-member',
  ];
  $membership2
    ->save();
  $permissions2 = [
    $group2
      ->id() => [
      'view group',
      'leave group',
    ],
  ];

  // Assert permissions have changed for account2.
  $calculated_permissions2 = $this->permissionCalculator
    ->calculateMemberPermissions($account2);
  $converted2 = $this
    ->convertCalculatedPermissionsToArray($calculated_permissions2);
  $this
    ->assertEqualsCanonicalizing($permissions2, $converted2, 'Updated member permissions are returned per group ID.');

  // Remove account2 from group2.
  $group2
    ->removeMember($account2);
  $permissions2 = [];
  $calculated_permissions2 = $this->permissionCalculator
    ->calculateMemberPermissions($account2);
  $converted2 = $this
    ->convertCalculatedPermissionsToArray($calculated_permissions2);
  $this
    ->assertEqualsCanonicalizing($permissions2, $converted2, 'Updated member permissions are returned per group ID.');
}