OgMembershipRoleReferenceTest.php in Organic groups 8
File
tests/src/Kernel/Entity/OgMembershipRoleReferenceTest.php
View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\og\Kernel\Entity;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\og\Entity\OgRole;
use Drupal\og\Og;
use Drupal\og\OgAccess;
use Drupal\user\Entity\User;
class OgMembershipRoleReferenceTest extends KernelTestBase {
public static $modules = [
'og',
'field',
'node',
'user',
'system',
];
protected $groupBundle;
protected $group;
protected $user;
protected function setUp() : void {
parent::setUp();
$this
->installConfig([
'og',
]);
$this
->installEntitySchema('og_membership');
$this
->installEntitySchema('user');
$this
->installEntitySchema('node');
$this
->installSchema('system', 'sequences');
$this->groupBundle = mb_strtolower($this
->randomMachineName());
NodeType::create([
'type' => $this->groupBundle,
'name' => $this
->randomString(),
])
->save();
Og::groupTypeManager()
->addGroup('node', $this->groupBundle);
$this->user = User::create([
'name' => $this
->randomString(),
]);
$this->user
->save();
$this->group = Node::create([
'title' => $this
->randomString(),
'uid' => $this->user
->id(),
'type' => $this->groupBundle,
]);
$this->group
->save();
}
public function testRoleCreate() {
$content_editor = OgRole::create();
$content_editor
->setGroupType('node')
->setGroupBundle($this->groupBundle)
->setName('content_editor')
->setLabel('Content editor')
->grantPermission(OgAccess::ADMINISTER_GROUP_PERMISSION);
$content_editor
->save();
$group_member = OgRole::create();
$group_member
->setGroupType('node')
->setGroupBundle($this->groupBundle)
->setName('group_member')
->setLabel('Group member');
$group_member
->save();
$membership = Og::getMembership($this->group, $this->user);
$membership
->setRoles([
$content_editor,
])
->save();
$this
->assertTrue($membership
->hasRole($content_editor
->id()), 'The membership has the content editor role.');
$membership
->addRole($group_member);
$this
->assertTrue($membership
->hasRole($content_editor
->id()), 'The membership has the content editor role.');
$this
->assertTrue($membership
->hasRole($group_member
->id()), 'The membership has the group member role.');
$membership
->revokeRole($content_editor);
$this
->assertFalse($membership
->hasRole($content_editor
->id()), 'The membership does not have the content editor role after is has been revoked.');
$this
->assertTrue($membership
->hasRole($group_member
->id()), 'The membership has the group member role.');
$this
->assertFalse($membership
->hasPermission(OgAccess::ADMINISTER_GROUP_PERMISSION), 'The user has permission to administer groups.');
$membership
->addRole($content_editor);
$this
->assertTrue($membership
->hasPermission(OgAccess::ADMINISTER_GROUP_PERMISSION), 'The user has permission to administer groups.');
$membership
->revokeRoleById($group_member
->id());
$roles_ids = $membership
->getRolesIds();
$this
->assertFalse(in_array($group_member
->id(), $roles_ids), 'The group member role has been revoked.');
}
}