You are here

class IsGroupMemberCacheContextTest in Group 8

Same name and namespace in other branches
  1. 2.0.x tests/src/Unit/IsGroupMemberCacheContextTest.php \Drupal\Tests\group\Unit\IsGroupMemberCacheContextTest

Tests the user.is_group_member:%group_id cache context.

@coversDefaultClass \Drupal\group\Cache\Context\IsGroupMemberCacheContext @group group

Hierarchy

Expanded class hierarchy of IsGroupMemberCacheContextTest

File

tests/src/Unit/IsGroupMemberCacheContextTest.php, line 23

Namespace

Drupal\Tests\group\Unit
View source
class IsGroupMemberCacheContextTest extends UnitTestCase {

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $currentUser;

  /**
   * A dummy group to use in other prophecies.
   *
   * @var \Drupal\group\Entity\GroupInterface
   */
  protected $group;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    $this->currentUser = $this
      ->prophesize(AccountProxyInterface::class)
      ->reveal();
    $this->group = $this
      ->prophesize(GroupInterface::class)
      ->reveal();
  }

  /**
   * Tests getting the context value from a non-calculated cache context.
   *
   * @covers ::getContext
   */
  public function testGetContextWithoutId() {
    $cache_context = new IsGroupMemberCacheContext($this->currentUser, $this
      ->createEntityTypeManager(1)
      ->reveal(), $this
      ->createGroupMembershipLoader(FALSE)
      ->reveal());
    $this
      ->expectException(\LogicException::class);
    $this
      ->expectExceptionMessage('No group ID provided for user.is_group_member cache context.');
    $cache_context
      ->getContext();
  }

  /**
   * Tests getting the context value while specifying a non-existent group.
   *
   * @covers ::getContext
   */
  public function testGetContextWithInvalidGroupId() {
    $cache_context = new IsGroupMemberCacheContext($this->currentUser, $this
      ->createEntityTypeManager(1)
      ->reveal(), $this
      ->createGroupMembershipLoader(FALSE)
      ->reveal());
    $this
      ->expectException(\LogicException::class);
    $this
      ->expectExceptionMessage('Incorrect group ID provided for user.is_group_member cache context.');
    $cache_context
      ->getContext(2);
  }

  /**
   * Tests getting the context value when the user is a member.
   *
   * @covers ::getContext
   */
  public function testGetContextMember() {
    $cache_context = new IsGroupMemberCacheContext($this->currentUser, $this
      ->createEntityTypeManager(1)
      ->reveal(), $this
      ->createGroupMembershipLoader(TRUE)
      ->reveal());
    $this
      ->assertSame('1', $cache_context
      ->getContext(1));
  }

  /**
   * Tests getting the context value when the user is not a member.
   *
   * @covers ::getContext
   */
  public function testGetContextNotMember() {
    $cache_context = new IsGroupMemberCacheContext($this->currentUser, $this
      ->createEntityTypeManager(1)
      ->reveal(), $this
      ->createGroupMembershipLoader(FALSE)
      ->reveal());
    $this
      ->assertSame('0', $cache_context
      ->getContext(1));
  }

  /**
   * Tests getting the cacheable metadata from a non-calculated cache context.
   *
   * @covers ::getCacheableMetadata
   */
  public function testGetCacheableMetadataWithoutId() {
    $cache_context = new IsGroupMemberCacheContext($this->currentUser, $this
      ->createEntityTypeManager(1)
      ->reveal(), $this
      ->createGroupMembershipLoader(FALSE)
      ->reveal());
    $this
      ->expectException(\LogicException::class);
    $this
      ->expectExceptionMessage('No group ID provided for user.is_group_member cache context.');
    $cache_context
      ->getCacheableMetadata();
  }

  /**
   * Tests getting the cacheable metadata for a valid cache context.
   *
   * @covers ::getCacheableMetadata
   */
  public function testGetCacheableMetadata() {
    $user = $this
      ->prophesize(UserInterface::class);
    $user
      ->getCacheContexts()
      ->willReturn([]);
    $user
      ->getCacheTags()
      ->willReturn([
      'user:1',
    ]);
    $user
      ->getCacheMaxAge()
      ->willReturn(-1);
    $user = $user
      ->reveal();
    $current_user = $this
      ->prophesize(AccountProxyInterface::class);
    $current_user
      ->getAccount()
      ->willReturn($user);
    $cache_context = new IsGroupMemberCacheContext($current_user
      ->reveal(), $this
      ->createEntityTypeManager(1)
      ->reveal(), $this
      ->createGroupMembershipLoader(TRUE)
      ->reveal());
    $this
      ->assertEquals(CacheableMetadata::createFromObject($user), $cache_context
      ->getCacheableMetadata(1));
  }

  /**
   * Creates an EntityTypeManagerInterface prophecy.
   *
   * @param int $group_id
   *   The group ID that the group storage will be able to load.
   *
   * @return \Prophecy\Prophecy\ObjectProphecy
   *   The prophesized entity type manager.
   */
  protected function createEntityTypeManager($group_id) {
    $prophecy = $this
      ->prophesize(EntityTypeManagerInterface::class);
    $storage = $this
      ->prophesize(ContentEntityStorageInterface::class);
    $storage
      ->load(Argument::any())
      ->willReturn(NULL);
    $storage
      ->load($group_id)
      ->willReturn($this->group);
    $prophecy
      ->getStorage('group')
      ->willReturn($storage
      ->reveal());
    return $prophecy;
  }

  /**
   * Creates a GroupMembershipLoaderInterface prophecy.
   *
   * @param bool $is_member
   *   Whether this will find the member or not.
   *
   * @return \Prophecy\Prophecy\ObjectProphecy
   *   The prophesized group membership loader.
   */
  protected function createGroupMembershipLoader($is_member) {
    $prophecy = $this
      ->prophesize(GroupMembershipLoaderInterface::class);
    $return = $is_member ? $this
      ->prophesize(GroupMembership::class)
      ->reveal() : $is_member;
    $prophecy
      ->load($this->group, $this->currentUser)
      ->willReturn($return);
    return $prophecy;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
IsGroupMemberCacheContextTest::$currentUser protected property The current user.
IsGroupMemberCacheContextTest::$group protected property A dummy group to use in other prophecies.
IsGroupMemberCacheContextTest::createEntityTypeManager protected function Creates an EntityTypeManagerInterface prophecy.
IsGroupMemberCacheContextTest::createGroupMembershipLoader protected function Creates a GroupMembershipLoaderInterface prophecy.
IsGroupMemberCacheContextTest::setUp public function Overrides UnitTestCase::setUp
IsGroupMemberCacheContextTest::testGetCacheableMetadata public function Tests getting the cacheable metadata for a valid cache context.
IsGroupMemberCacheContextTest::testGetCacheableMetadataWithoutId public function Tests getting the cacheable metadata from a non-calculated cache context.
IsGroupMemberCacheContextTest::testGetContextMember public function Tests getting the context value when the user is a member.
IsGroupMemberCacheContextTest::testGetContextNotMember public function Tests getting the context value when the user is not a member.
IsGroupMemberCacheContextTest::testGetContextWithInvalidGroupId public function Tests getting the context value while specifying a non-existent group.
IsGroupMemberCacheContextTest::testGetContextWithoutId public function Tests getting the context value from a non-calculated cache context.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.