You are here

class OgMembershipStateCacheContextTest in Organic groups 8

Tests OG membership state cache context.

@group og @coversDefaultClass \Drupal\og\Cache\Context\OgMembershipStateCacheContext

Hierarchy

Expanded class hierarchy of OgMembershipStateCacheContextTest

File

tests/src/Unit/Cache/Context/OgMembershipStateCacheContextTest.php, line 18

Namespace

Drupal\Tests\og\Unit\Cache\Context
View source
class OgMembershipStateCacheContextTest extends OgContextCacheContextTestBase {

  /**
   * The OG membership entity.
   *
   * @var \Drupal\og\OgMembershipInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $membership;

  /**
   * The membership manager service.
   *
   * @var \Drupal\og\MembershipManagerInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $membershipManager;

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $user;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->user = $this
      ->prophesize(AccountInterface::class);
    $this->membership = $this
      ->prophesize(OgMembershipInterface::class);
    $this->membershipManager = $this
      ->prophesize(MembershipManagerInterface::class);
  }

  /**
   * {@inheritdoc}
   */
  public function testWithoutContext() {
    $this
      ->expectGroupContext();
    $result = $this
      ->getContextResult();
    $this
      ->assertEquals(OgMembershipStateCacheContext::NO_CONTEXT, $result);
  }

  /**
   * {@inheritdoc}
   */
  protected function setupExpectedContext($context) {
    $this
      ->expectGroupContext($this->group
      ->reveal());
    $this
      ->expectMembership($context);
  }

  /**
   * {@inheritdoc}
   */
  protected function getCacheContext() {
    return new OgMembershipStateCacheContext($this->user
      ->reveal(), $this->ogContext
      ->reveal(), $this->membershipManager
      ->reveal());
  }

  /**
   * Sets an expectation that the current user has the given membership state.
   *
   * @param string|false $state
   *   The membership state, or FALSE if the user is not a member.
   */
  protected function expectMembership($state) {

    // If the user is a member, it is expected that the membership state will be
    // retrieved.
    if ($state) {
      $this->membership
        ->getState()
        ->willReturn($state);
      $state = $this->membership;
    }

    // It is expected that the user membership will be retrieved, of any
    // possible membership state.
    $states = [
      OgMembershipInterface::STATE_ACTIVE,
      OgMembershipInterface::STATE_PENDING,
      OgMembershipInterface::STATE_BLOCKED,
    ];
    $this->membershipManager
      ->getMembership($this->group
      ->reveal(), $this->user
      ->reveal()
      ->id(), $states)
      ->willReturn($state);
  }

  /**
   * {@inheritdoc}
   */
  public function contextProvider() {
    return [
      [
        FALSE,
        OgMembershipStateCacheContext::NO_CONTEXT,
      ],
      [
        OgMembershipInterface::STATE_ACTIVE,
        OgMembershipInterface::STATE_ACTIVE,
      ],
      [
        OgMembershipInterface::STATE_PENDING,
        OgMembershipInterface::STATE_PENDING,
      ],
      [
        OgMembershipInterface::STATE_BLOCKED,
        OgMembershipInterface::STATE_BLOCKED,
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
OgCacheContextTestBase::getContextResult protected function Return the context result.
OgContextCacheContextTestBase::$group protected property A mocked group entity.
OgContextCacheContextTestBase::$ogContext protected property The mocked OG context service.
OgContextCacheContextTestBase::expectGroupContext protected function Sets an expectation that OgContext will return the given group.
OgContextCacheContextTestBase::testWithContext public function Tests the result of the cache context service with active context objects.
OgMembershipStateCacheContextTest::$membership protected property The OG membership entity.
OgMembershipStateCacheContextTest::$membershipManager protected property The membership manager service.
OgMembershipStateCacheContextTest::$user protected property The current user.
OgMembershipStateCacheContextTest::contextProvider public function Provides test data for the test with active context objects. Overrides OgContextCacheContextTestBase::contextProvider
OgMembershipStateCacheContextTest::expectMembership protected function Sets an expectation that the current user has the given membership state.
OgMembershipStateCacheContextTest::getCacheContext protected function Returns the instantiated cache context service which is being tested. Overrides OgCacheContextTestBase::getCacheContext
OgMembershipStateCacheContextTest::setUp protected function Overrides OgContextCacheContextTestBase::setUp
OgMembershipStateCacheContextTest::setupExpectedContext protected function Set up expectations for tests that have an active context object. Overrides OgContextCacheContextTestBase::setupExpectedContext
OgMembershipStateCacheContextTest::testWithoutContext public function Tests the result of the cache context service without active context. Overrides OgContextCacheContextTestBase::testWithoutContext
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.