You are here

public function PermissionEventTest::testOverwritingGroupContentOperationPermission in Organic groups 8

Tests that it is possible to overwrite an existing operation permission.

Operation permissions are not identified by their machine name, but by the unique combination of entity type, bundle, operation and ownership. It should be possible to change the name, title and other properties.

File

tests/src/Unit/PermissionEventTest.php, line 614

Class

PermissionEventTest
Tests permission events.

Namespace

Drupal\Tests\og\Unit

Code

public function testOverwritingGroupContentOperationPermission() {
  $event = new PermissionEvent($this
    ->randomMachineName(), $this
    ->randomMachineName(), []);
  $entity_type_id = $this
    ->randomMachineName();
  $bundle_id = $this
    ->randomMachineName();
  $operation = $this
    ->randomMachineName();
  $ownership = TRUE;
  $original_permission = new GroupContentOperationPermission([
    'name' => 'the original permission',
    'title' => $this
      ->t('This is the original permission.'),
    'description' => $this
      ->t('This is the original description.'),
    'entity type' => $entity_type_id,
    'bundle' => $bundle_id,
    'operation' => $operation,
    'owner' => $ownership,
    'default roles' => [
      OgRoleInterface::ADMINISTRATOR,
    ],
    'restrict access' => TRUE,
  ]);
  $altered_permission = new GroupContentOperationPermission([
    'name' => 'the altered permission',
    'title' => $this
      ->t('This is the altered permission.'),
    'description' => $this
      ->t('This is the altered description.'),
    'entity type' => $entity_type_id,
    'bundle' => $bundle_id,
    'operation' => $operation,
    'owner' => $ownership,
    'default roles' => [
      OgRoleInterface::AUTHENTICATED,
    ],
    'restrict access' => FALSE,
  ]);

  // Check that the original permission can be set and retrieved.
  $event
    ->setPermission($original_permission);
  $this
    ->assertEquals($original_permission, $event
    ->getGroupContentOperationPermission($entity_type_id, $bundle_id, $operation, $ownership));

  // Check that the altered permission can be set and retrieved, even though
  // it does not have the same machine name as the original permission.
  $event
    ->setPermission($altered_permission);
  $this
    ->assertEquals($altered_permission, $event
    ->getGroupContentOperationPermission($entity_type_id, $bundle_id, $operation, $ownership));
}