DefaultRoleEventIntegrationTest.php in Organic groups 8
File
tests/src/Kernel/DefaultRoleEventIntegrationTest.php
View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\og\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\og\Event\DefaultRoleEvent;
use Drupal\og\Event\DefaultRoleEventInterface;
use Drupal\og\Og;
use Drupal\og\OgRoleInterface;
class DefaultRoleEventIntegrationTest extends KernelTestBase {
public static $modules = [
'entity_test',
'og',
'system',
'user',
'field',
];
protected $eventDispatcher;
protected $groupBundleId;
protected $ogRoleStorage;
protected function setUp() : void {
parent::setUp();
$this->eventDispatcher = $this->container
->get('event_dispatcher');
$this->ogRoleStorage = $this->container
->get('entity_type.manager')
->getStorage('og_role');
$this->groupBundleId = $this
->randomMachineName();
Og::groupTypeManager()
->addGroup('entity_test', $this->groupBundleId);
}
public function testPermissionEventIntegration() {
$event = new DefaultRoleEvent();
$this->eventDispatcher
->dispatch(DefaultRoleEventInterface::EVENT_NAME, $event);
$this
->assertEquals([
OgRoleInterface::ADMINISTRATOR,
], array_keys($event
->getRoles()));
$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());
$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) {
$expected_key = implode('-', [
'entity_test',
$this->groupBundleId,
$expected_role,
]);
$this
->assertArrayHasKey($expected_key, $actual_roles);
}
}
}