You are here

public function DefaultRoleEventIntegrationTest::testPermissionEventIntegration in Organic groups 8

Tests that OG correctly provides the group administrator default role.

File

tests/src/Kernel/DefaultRoleEventIntegrationTest.php, line 65

Class

DefaultRoleEventIntegrationTest
Tests the implementation of the DefaultRoleEvent in the 'og' module.

Namespace

Drupal\Tests\og\Kernel

Code

public function testPermissionEventIntegration() {

  /** @var \Drupal\og\Event\DefaultRoleEvent $event */
  $event = new DefaultRoleEvent();

  // Query the event listener directly to see if the administrator role is
  // present.
  $this->eventDispatcher
    ->dispatch(DefaultRoleEventInterface::EVENT_NAME, $event);
  $this
    ->assertEquals([
    OgRoleInterface::ADMINISTRATOR,
  ], array_keys($event
    ->getRoles()));

  // Check that the role was created with the correct values.
  $role = $event
    ->getRole(OgRoleInterface::ADMINISTRATOR);
  $this
    ->assertEquals(OgRoleInterface::ADMINISTRATOR, $role
    ->getName());
  $this
    ->assertEquals('Administrator', $role
    ->getLabel());
  $this
    ->assertEquals(OgRoleInterface::ROLE_TYPE_STANDARD, $role
    ->getRoleType());
  $this
    ->assertTrue($role
    ->isAdmin());

  // Check that the per-group-type default roles are populated.
  $expected_roles = [
    OgRoleInterface::ANONYMOUS,
    OgRoleInterface::AUTHENTICATED,
    OgRoleInterface::ADMINISTRATOR,
  ];
  $actual_roles = $this->ogRoleStorage
    ->loadByProperties([
    'group_type' => 'entity_test',
    'group_bundle' => $this->groupBundleId,
  ]);
  $this
    ->assertEquals(count($expected_roles), count($actual_roles));
  foreach ($expected_roles as $expected_role) {

    // The role ID consists of the entity type, bundle and role name.
    $expected_key = implode('-', [
      'entity_test',
      $this->groupBundleId,
      $expected_role,
    ]);
    $this
      ->assertArrayHasKey($expected_key, $actual_roles);
  }
}