You are here

public function OgRoleCacheContextTest::testMembershipsWithOrphanedRole in Organic groups 8

Tests that no cache context key is returned if a user has lost membership.

This can happen if for example if a user is a member with a certain role in a group, and then the role is removed from config. In this case the membership entity will still exist, but the user will not have any roles, so no cache context key should be generated.

@covers ::getContext

File

tests/src/Unit/Cache/Context/OgRoleCacheContextTest.php, line 97

Class

OgRoleCacheContextTest
Tests the OG role cache context.

Namespace

Drupal\Tests\og\Unit\Cache\Context

Code

public function testMembershipsWithOrphanedRole() : void {

  // Mock the membership with the orphaned role. It will return a group and
  // group entity type, but no roles.

  /** @var \Drupal\og\OgMembershipInterface|\Prophecy\Prophecy\ObjectProphecy $membership */
  $membership = $this
    ->prophesize(OgMembershipInterface::class);
  $membership
    ->getRolesIds()
    ->willReturn([]);

  // The membership with the orphaned role will be returned by the membership
  // manager.

  /** @var \Drupal\Core\Session\AccountInterface|\Prophecy\Prophecy\ObjectProphecy $user */
  $user = $this
    ->prophesize(AccountInterface::class)
    ->reveal();
  $this->membershipManager
    ->getMemberships($user
    ->id())
    ->willReturn([
    $membership,
  ]);

  // The result should be the predefined 'NO_CONTEXT' value.
  $result = $this
    ->getContextResult($user);
  $this
    ->assertEquals(OgRoleCacheContext::NO_CONTEXT, $result);
}