You are here

public function GetUserGroupsTest::testOtherGroups in Organic groups 8

Tests other groups users are added to.

@todo Convert Og::isMember() calls to $this->membershipManager->isMember().

File

tests/src/Kernel/Entity/GetUserGroupsTest.php, line 162

Class

GetUserGroupsTest
Tests getting the memberships of an entity.

Namespace

Drupal\Tests\og\Kernel\Entity

Code

public function testOtherGroups() {

  // Should not be a part of any groups.
  $this
    ->assertEquals([], $this->membershipManager
    ->getUserGroups($this->user3
    ->id()));
  $this
    ->assertFalse(Og::isMember($this->group1, $this->user3));
  $this
    ->assertFalse(Og::isMember($this->group2, $this->user3));

  // Invalidate the caches so the static cache is cleared and group data is
  // fetched again for the user.
  Og::invalidateCache();

  // Add user to group 1 should now return that group only.
  $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();

  // Add to group 2 should also return that.
  $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));
}