public function GetUserGroupsTest::testGetGroupsByRoles in Organic groups 8
Tests retrieval of groups filtered by roles.
@covers ::getUserGroupIdsByRoleIds
File
- tests/
src/ Kernel/ Entity/ GetUserGroupsTest.php, line 257
Class
- GetUserGroupsTest
- Tests getting the memberships of an entity.
Namespace
Drupal\Tests\og\Kernel\EntityCode
public function testGetGroupsByRoles() {
// Create a test role.
$extra_role_1 = OgRole::create();
$extra_role_1
->setName('extra_role_1')
->setLabel(mb_strtolower($this
->randomString()))
->setGroupType('entity_test')
->setGroupBundle($this->groupBundle)
->save();
// Retrieve the default role for a member.
$member_role = OgRole::getRole('entity_test', $this->groupBundle, OgRoleInterface::AUTHENTICATED);
// Create memberships for the test user in the groups. The user will have
// the normal member role in group 1 and both the normal member role and the
// test role in group 2. In group 2 the user will have the blocked status so
// we can test filtering by status.
$this
->createOgMembership($this->group1, $this->user3);
$this
->createOgMembership($this->group2, $this->user3, [
$extra_role_1
->getName(),
], OgMembershipInterface::STATE_BLOCKED);
// By default only active memberships are retrieved, so if we ask the
// groups where the user is a normal member of the result should not include
// group 2 where our test user is blocked.
$groups = $this->membershipManager
->getUserGroupIdsByRoleIds($this->user3
->id(), [
$member_role
->id(),
]);
$this
->assertCount(1, $groups['entity_test']);
$actual = reset($groups['entity_test']);
$this
->assertEquals($this->group1
->id(), $actual);
// When asking for the groups where our user has the test role, the result
// should not include the blocked membership, so it should be empty.
$groups = $this->membershipManager
->getUserGroupsByRoleIds($this->user3
->id(), [
$extra_role_1
->id(),
]);
$this
->assertCount(0, $groups);
// Include all states.
$groups = $this->membershipManager
->getUserGroupIdsByRoleIds($this->user3
->id(), [
$member_role
->id(),
], OgMembershipInterface::ALL_STATES, FALSE);
$this
->assertCount(2, $groups['entity_test']);
// Request any of multiple roles.
$groups = $this->membershipManager
->getUserGroupsByRoleIds($this->user3
->id(), [
$member_role
->id(),
$extra_role_1
->id(),
], OgMembershipInterface::ALL_STATES, FALSE);
$this
->assertCount(2, $groups['entity_test']);
// Request all of multiple roles.
$groups = $this->membershipManager
->getUserGroupsByRoleIds($this->user3
->id(), [
$member_role
->id(),
$extra_role_1
->id(),
], OgMembershipInterface::ALL_STATES, TRUE);
$this
->assertCount(1, $groups['entity_test']);
$actual = reset($groups['entity_test']);
$this
->assertEquals($this->group2
->id(), $actual
->id());
}