You are here

public function MemberCountBlockTest::testMemberCountBlock in Organic groups 8

Tests the member count block.

File

tests/src/Kernel/Plugin/Block/MemberCountBlockTest.php, line 167

Class

MemberCountBlockTest
Tests the member count block.

Namespace

Drupal\Tests\og\Kernel\Plugin\Block

Code

public function testMemberCountBlock() {

  // Before the blocks are rendered for the first time, no cache entries
  // should exist for them. We have two groups, so let's test both blocks.
  $this
    ->assertNotCached(0);
  $this
    ->assertNotCached(1);

  // After rendering the first block, only this block should be cached, the
  // other should be unaffected.
  $this
    ->renderBlock(0);
  $this
    ->assertCached(0);
  $this
    ->assertNotCached(1);
  $this
    ->renderBlock(1);
  $this
    ->assertCached(1);

  // Initially the blocks should have 0 members.
  $this
    ->assertMemberCount(0, 0);
  $this
    ->assertMemberCount(1, 0);

  // In the default configuration the block should only count active users,
  // and ignore blocked and pending users. Also check that making changes to
  // the members of the group only invalidates the cache of the related block.
  $this
    ->addMember(0, OgMembershipInterface::STATE_BLOCKED);
  $this
    ->addMember(0, OgMembershipInterface::STATE_PENDING);
  $this
    ->assertNotCached(0);
  $this
    ->assertCached(1);
  $this
    ->assertMemberCount(0, 0);

  // However adding an active user increases the member count.
  $active_membership = $this
    ->addMember(0, OgMembershipInterface::STATE_ACTIVE);
  $this
    ->assertMemberCount(0, 1);

  // If we block the active user, the member count should be updated
  // accordingly.
  $active_membership
    ->setState(OgMembershipInterface::STATE_BLOCKED)
    ->save();
  $this
    ->assertMemberCount(0, 0);

  // Check that both blocks are invalidated when the block settings are
  // changed.
  $this
    ->updateBlockSetting('count_pending_users', TRUE);
  $this
    ->assertNotCached(0);
  $this
    ->assertNotCached(1);

  // The block has now been configured to also count pending members. Check if
  // the count is updated accordingly.
  $this
    ->assertMemberCount(0, 1);
  $this
    ->assertMemberCount(1, 0);

  // Turn on the counting of blocked members and check the resulting value.
  $this
    ->updateBlockSetting('count_blocked_users', TRUE);
  $this
    ->assertMemberCount(0, 3);
  $this
    ->assertMemberCount(1, 0);

  // Now delete one of the memberships of the first group. This should
  // decrease the counter.
  $active_membership
    ->delete();
  $this
    ->assertMemberCount(0, 2);

  // Since the deletion of the user only affected the first group, the block
  // of the second group should still be unchanged and happily cached.
  $this
    ->assertCached(1);
  $this
    ->assertMemberCount(1, 0);

  // For good measure, try to add a user to the second group and check that
  // all is in order.
  $this
    ->addMember(1, OgMembershipInterface::STATE_ACTIVE);
  $this
    ->assertMemberCount(1, 1);

  // The block from the first group should not be affected by this.
  $this
    ->assertCached(0);
  $this
    ->assertMemberCount(0, 2);
}