You are here

public function OgRoleTest::testRequiredRoles in Organic groups 8

Tests the creation and deletion of required roles.

File

tests/src/Kernel/Entity/OgRoleTest.php, line 252

Class

OgRoleTest
Test OG role creation.

Namespace

Drupal\Tests\og\Kernel\Entity

Code

public function testRequiredRoles() {

  // Check that the required roles are created when a new group type is
  // declared.
  foreach ([
    'node',
    'entity_test_with_bundle',
  ] as $entity_type_id) {
    $this->groupTypeManager
      ->addGroup($entity_type_id, 'group');
  }
  $required_roles = [];
  foreach ([
    OgRole::ANONYMOUS,
    OgRole::AUTHENTICATED,
  ] as $role_name) {
    foreach ([
      'node',
      'entity_test_with_bundle',
    ] as $group_type) {
      $role_id = "{$group_type}-group-{$role_name}";
      $required_role = OgRole::load($role_id);

      // Check that the role is actually a required role.
      $this
        ->assertTrue($required_role
        ->isRequired());

      // Check that the other data is correct.
      $this
        ->assertEquals($group_type, $required_role
        ->getGroupType());
      $this
        ->assertEquals('group', $required_role
        ->getGroupBundle());
      $this
        ->assertEquals($role_name, $required_role
        ->getName());

      // Keep track of the role so we can later test if they can be deleted.
      $required_roles[] = $required_role;
    }
  }

  // Required roles cannot be deleted, so an exception should be thrown when
  // trying to delete them when the group type still exists.
  foreach ($required_roles as $required_role) {
    try {
      $required_role
        ->delete();
      $this
        ->fail('A default role cannot be deleted.');
    } catch (OgRoleException $e) {
    }
  }

  // Delete the group types.
  foreach ($this->groupTypes as $group_type) {
    $group_type
      ->delete();
  }

  // The required roles are dependent on the group types so this action should
  // result in the deletion of the roles.
  foreach ($required_roles as $required_role) {
    $this
      ->assertEmpty($this
      ->loadUnchangedOgRole($required_role
      ->id()));
  }
}