View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\og\Kernel\Entity;
use Drupal\Core\Config\ConfigValueException;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\KernelTests\KernelTestBase;
use Drupal\og\Entity\OgRole;
use Drupal\og\Exception\OgRoleException;
use Drupal\og\OgAccess;
use Drupal\system\Entity\Action;
class OgRoleTest extends KernelTestBase {
public static $modules = [
'entity_test',
'field',
'node',
'og',
'system',
'user',
];
protected $actionStorage;
protected $groupTypeManager;
protected $entityTypeManager;
protected $groupTypes;
protected function setUp() : void {
parent::setUp();
$this
->installConfig([
'og',
]);
$this
->installEntitySchema('entity_test');
$this->actionStorage = $this->container
->get('entity_type.manager')
->getStorage('action');
$this->groupTypeManager = $this->container
->get('og.group_type_manager');
$this->entityTypeManager = $this->container
->get('entity_type.manager');
foreach ([
'node_type',
'entity_test_bundle',
] as $entity_type_id) {
$definition = $this->entityTypeManager
->getDefinition($entity_type_id);
$values = [
$definition
->getKey('id') => 'group',
$definition
->getKey('label') => 'Group',
];
$group_type = $this->entityTypeManager
->getStorage($entity_type_id)
->create($values);
$group_type
->save();
$this->groupTypes[$entity_type_id] = $group_type;
}
}
public function testRoleCreate() {
$og_role = OgRole::create();
$og_role
->setName('content_editor')
->setLabel('Content editor')
->grantPermission(OgAccess::ADMINISTER_GROUP_PERMISSION);
try {
$og_role
->save();
$this
->fail('Creating OG role without group type/bundle is not allowed.');
} catch (ConfigValueException $e) {
$this
->assertTrue(TRUE, 'OG role without bundle/group was not saved.');
}
$og_role
->setGroupType('node')
->setGroupBundle('group')
->save();
$saved_role = $this
->loadUnchangedOgRole('node-group-content_editor');
$this
->assertNotEmpty($saved_role, 'The role was created with the expected ID.');
$this
->assertEquals($og_role
->id(), $saved_role
->id());
$this
->assertEquals($og_role
->getPermissions(), [
OgAccess::ADMINISTER_GROUP_PERMISSION,
]);
$this
->assertFalse($og_role
->isRequired());
$action_ids = [
'og_membership_add_single_role_action.content_editor',
'og_membership_remove_single_role_action.content_editor',
];
$actions = Action::loadMultiple($action_ids);
foreach ($action_ids as $action_id) {
$this
->assertTrue(array_key_exists($action_id, $actions));
$this
->assertEquals($action_id, $actions[$action_id]
->id());
}
try {
$og_role = OgRole::create();
$og_role
->setName('content_editor')
->setLabel('Content editor')
->setGroupType('node')
->setGroupBundle('group')
->grantPermission(OgAccess::ADMINISTER_GROUP_PERMISSION)
->save();
$this
->fail('OG role with the same ID can be saved.');
} catch (EntityStorageException $e) {
$this
->assertTrue(TRUE, "OG role with the same ID can not be saved.");
}
$og_role = OgRole::create();
$og_role
->setName('content_editor')
->setLabel('Content editor')
->setGroupType('entity_test_with_bundle')
->setGroupBundle('group')
->save();
$this
->assertEquals('entity_test_with_bundle-group-content_editor', $og_role
->id());
$og_role
->save();
try {
$og_role
->setId($og_role
->id() . 'foo');
$this
->fail('Existing OG role ID can change.');
} catch (OgRoleException $e) {
}
try {
$og_role = OgRole::create();
$og_role
->setName('content_editor')
->setLabel('Content editor')
->setGroupType('entity_test_with_bundle')
->setGroupBundle('group')
->save();
$this
->fail('OG role with the same ID on the same group can be saved.');
} catch (EntityStorageException $e) {
$this
->assertTrue(TRUE, "OG role with the same ID on the same group can not be saved.");
}
$og_role = OgRole::create([
'id' => 'entity_test_with_bundle-group-configurator',
'label' => 'Configurator',
'group_type' => 'entity_test_with_bundle',
'group_bundle' => 'group',
]);
$og_role
->save();
$this
->assertNotEmpty($this
->loadUnchangedOgRole('entity_test_with_bundle-group-configurator'));
$this
->assertEquals('configurator', $og_role
->getName());
try {
$og_role = OgRole::create();
$og_role
->setId('entity_test_with_bundle-group-wrong_id')
->setName('content_editor')
->setLabel('Content editor')
->setGroupType('entity_test_with_bundle')
->setGroupBundle('group')
->save();
$this
->fail('OG role with a non-matching ID can be saved.');
} catch (ConfigValueException $e) {
$this
->assertTrue(TRUE, "OG role with a non-matching ID can not be saved.");
}
$this->groupTypes['node_type']
->delete();
$role = OgRole::getRole('node', 'group', 'content_editor');
$this
->assertEmpty($role);
foreach ($action_ids as $action_id) {
$action = $this->actionStorage
->loadUnchanged($action_id);
$this
->assertEquals($action_id, $action
->id());
}
OgRole::getRole('entity_test_with_bundle', 'group', 'content_editor')
->delete();
foreach ($action_ids as $action_id) {
$action = $this->actionStorage
->loadUnchanged($action_id);
$this
->assertEmpty($action);
}
}
public function testRequiredRoles() {
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);
$this
->assertTrue($required_role
->isRequired());
$this
->assertEquals($group_type, $required_role
->getGroupType());
$this
->assertEquals('group', $required_role
->getGroupBundle());
$this
->assertEquals($role_name, $required_role
->getName());
$required_roles[] = $required_role;
}
}
foreach ($required_roles as $required_role) {
try {
$required_role
->delete();
$this
->fail('A default role cannot be deleted.');
} catch (OgRoleException $e) {
}
}
foreach ($this->groupTypes as $group_type) {
$group_type
->delete();
}
foreach ($required_roles as $required_role) {
$this
->assertEmpty($this
->loadUnchangedOgRole($required_role
->id()));
}
}
protected function loadUnchangedOgRole($id) {
return $this->entityTypeManager
->getStorage('og_role')
->loadUnchanged($id);
}
}