View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\og\Kernel\Entity;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\og\Traits\OgMembershipCreationTrait;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\og\Entity\OgRole;
use Drupal\og\Og;
use Drupal\og\OgMembershipInterface;
use Drupal\og\OgRoleInterface;
use Drupal\user\Entity\User;
class GetUserGroupsTest extends KernelTestBase {
use OgMembershipCreationTrait;
public static $modules = [
'system',
'user',
'field',
'og',
'entity_test',
];
protected $membershipManager;
protected $user1;
protected $user2;
protected $user3;
protected $group1;
protected $group2;
protected $groupBundle;
protected function setUp() : void {
parent::setUp();
$this
->installConfig([
'og',
]);
$this
->installEntitySchema('og_membership');
$this
->installEntitySchema('user');
$this
->installEntitySchema('entity_test');
$this
->installSchema('system', 'sequences');
$this->membershipManager = $this->container
->get('og.membership_manager');
$this->groupBundle = mb_strtolower($this
->randomMachineName());
$this->user1 = User::create([
'name' => $this
->randomString(),
]);
$this->user1
->save();
$this->user2 = User::create([
'name' => $this
->randomString(),
]);
$this->user2
->save();
$this->user3 = User::create([
'name' => $this
->randomString(),
]);
$this->user3
->save();
Og::groupTypeManager()
->addGroup('entity_test', $this->groupBundle);
$this->group1 = EntityTest::create([
'type' => $this->groupBundle,
'name' => $this
->randomString(),
'user_id' => $this->user1
->id(),
]);
$this->group1
->save();
$this->group2 = EntityTest::create([
'type' => $this->groupBundle,
'name' => $this
->randomString(),
'user_id' => $this->user2
->id(),
]);
$this->group2
->save();
}
public function testOwnerGroupsOnly() {
$actual = $this->membershipManager
->getUserGroups($this->user1
->id());
$this
->assertCount(1, $actual['entity_test']);
$this
->assertGroupExistsInResults($this->group1, $actual);
$this
->assertTrue(Og::isMember($this->group1, $this->user1));
$this
->assertFalse(Og::isMember($this->group1, $this->user2));
$actual = $this->membershipManager
->getUserGroups($this->user2
->id());
$this
->assertCount(1, $actual['entity_test']);
$this
->assertGroupExistsInResults($this->group2, $actual);
$this
->assertTrue(Og::isMember($this->group2, $this->user2));
$this
->assertFalse(Og::isMember($this->group2, $this->user1));
}
public function testOtherGroups() {
$this
->assertEquals([], $this->membershipManager
->getUserGroups($this->user3
->id()));
$this
->assertFalse(Og::isMember($this->group1, $this->user3));
$this
->assertFalse(Og::isMember($this->group2, $this->user3));
Og::invalidateCache();
$this
->createOgMembership($this->group1, $this->user3);
$actual = $this->membershipManager
->getUserGroups($this->user3
->id());
$this
->assertCount(1, $actual['entity_test']);
$this
->assertGroupExistsInResults($this->group1, $actual);
$this
->assertTrue(Og::isMember($this->group1, $this->user3));
$this
->assertFalse(Og::isMember($this->group2, $this->user3));
Og::invalidateCache();
$this
->createOgMembership($this->group2, $this->user3);
$actual = $this->membershipManager
->getUserGroups($this->user3
->id());
$this
->assertCount(2, $actual['entity_test']);
$this
->assertGroupExistsInResults($this->group1, $actual);
$this
->assertGroupExistsInResults($this->group2, $actual);
$this
->assertTrue(Og::isMember($this->group1, $this->user3));
$this
->assertTrue(Og::isMember($this->group2, $this->user3));
}
public function testIsMemberStates() {
$membership = $this
->createOgMembership($this->group1, $this->user3);
$this
->assertTrue(Og::isMember($this->group1, $this->user3));
$this
->assertFalse(Og::isMember($this->group1, $this->user3, [
OgMembershipInterface::STATE_PENDING,
]));
$this
->assertFalse(Og::isMember($this->group1, $this->user3, [
OgMembershipInterface::STATE_BLOCKED,
]));
$this
->assertFalse(Og::isMember($this->group1, $this->user3, [
OgMembershipInterface::STATE_PENDING,
OgMembershipInterface::STATE_BLOCKED,
]));
$this
->assertFalse(Og::isMemberPending($this->group1, $this->user3));
$this
->assertFalse(Og::isMemberBlocked($this->group1, $this->user3));
$membership
->setState(OgMembershipInterface::STATE_PENDING)
->save();
Og::invalidateCache();
$this
->assertTrue(Og::isMember($this->group1, $this->user3, [
OgMembershipInterface::STATE_PENDING,
]));
$this
->assertTrue(Og::isMemberPending($this->group1, $this->user3));
$this
->assertTrue(Og::isMember($this->group1, $this->user3, [
OgMembershipInterface::STATE_PENDING,
OgMembershipInterface::STATE_BLOCKED,
]));
$this
->assertFalse(Og::isMember($this->group1, $this->user3));
$this
->assertFalse(Og::isMember($this->group1, $this->user3, [
OgMembershipInterface::STATE_BLOCKED,
]));
$this
->assertFalse(Og::isMemberBlocked($this->group1, $this->user3));
$membership
->setState(OgMembershipInterface::STATE_BLOCKED)
->save();
Og::invalidateCache();
$this
->assertTrue(Og::isMember($this->group1, $this->user3, [
OgMembershipInterface::STATE_BLOCKED,
]));
$this
->assertTrue(Og::isMemberBlocked($this->group1, $this->user3));
$this
->assertTrue(Og::isMember($this->group1, $this->user3, [
OgMembershipInterface::STATE_PENDING,
OgMembershipInterface::STATE_BLOCKED,
]));
$this
->assertFalse(Og::isMember($this->group1, $this->user3));
$this
->assertFalse(Og::isMember($this->group1, $this->user3, [
OgMembershipInterface::STATE_PENDING,
]));
$this
->assertFalse(Og::isMemberPending($this->group1, $this->user3));
}
public function testGetGroupsByRoles() {
$extra_role_1 = OgRole::create();
$extra_role_1
->setName('extra_role_1')
->setLabel(mb_strtolower($this
->randomString()))
->setGroupType('entity_test')
->setGroupBundle($this->groupBundle)
->save();
$member_role = OgRole::getRole('entity_test', $this->groupBundle, OgRoleInterface::AUTHENTICATED);
$this
->createOgMembership($this->group1, $this->user3);
$this
->createOgMembership($this->group2, $this->user3, [
$extra_role_1
->getName(),
], OgMembershipInterface::STATE_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);
$groups = $this->membershipManager
->getUserGroupsByRoleIds($this->user3
->id(), [
$extra_role_1
->id(),
]);
$this
->assertCount(0, $groups);
$groups = $this->membershipManager
->getUserGroupIdsByRoleIds($this->user3
->id(), [
$member_role
->id(),
], OgMembershipInterface::ALL_STATES, FALSE);
$this
->assertCount(2, $groups['entity_test']);
$groups = $this->membershipManager
->getUserGroupsByRoleIds($this->user3
->id(), [
$member_role
->id(),
$extra_role_1
->id(),
], OgMembershipInterface::ALL_STATES, FALSE);
$this
->assertCount(2, $groups['entity_test']);
$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());
}
protected function assertGroupExistsInResults(EntityTest $group_to_check, array $results) {
$found = FALSE;
foreach ($results['entity_test'] as $group) {
if ($group
->id() == $group_to_check
->id()) {
$found = TRUE;
break;
}
}
$this
->assertTrue($found);
}
}