You are here

public function GroupLevelAccessTest::testGroupEntityOperationPermissions in Organic groups 8

Test access to the entity operation permissions on groups.

This tests that the entity operations "update" and "delete" are controlled by the respective group level permissions.

@covers ::userAccessEntityOperation @dataProvider groupEntityOperationPermissionsTestProvider

File

tests/src/Kernel/Access/GroupLevelAccessTest.php, line 271

Class

GroupLevelAccessTest
Tests user access to group level entity operations and permissions.

Namespace

Drupal\Tests\og\Kernel\Access

Code

public function testGroupEntityOperationPermissions(string $user, array $access_matrix) : void {
  $users = $this
    ->setupGroupEntityOperationPermissions();
  $user = $users[$user];
  foreach ($access_matrix as $operation => $expected_access) {

    // Check that the correct access is returned.
    $result = $this->ogAccess
      ->userAccessEntityOperation($operation, $this->group, $user);
    $this
      ->assertEquals($expected_access, $result
      ->isAllowed());

    // Also check that the access result is correctly communicated to
    // hook_entity_access().
    $arguments = [
      $this->group,
      $operation,
      $user,
    ];
    $hook_result = \Drupal::moduleHandler()
      ->invokeAll('entity_access', $arguments);

    // The hook returns an array of access results, add them all up.
    if (empty($hook_result)) {
      $result = AccessResult::neutral();
    }
    else {

      /** @var \Drupal\Core\Access\AccessResultInterface $result */
      $result = array_shift($hook_result);
      foreach ($hook_result as $other) {
        $result = $result
          ->orIf($other);
      }
    }
    $this
      ->assertEquals($expected_access, $result
      ->isAllowed());
  }
}