You are here

class OgGroupContextCacheContextTest in Organic groups 8

Tests the OG group context cache context.

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

Hierarchy

Expanded class hierarchy of OgGroupContextCacheContextTest

File

tests/src/Unit/Cache/Context/OgGroupContextCacheContextTest.php, line 15

Namespace

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

  /**
   * Tests getting cache context when there is no matching group on the route.
   *
   * @covers ::getContext
   */
  public function testWithoutContext() {
    $this
      ->expectGroupContext();
    $result = $this
      ->getContextResult();
    $this
      ->assertEquals(OgGroupContextCacheContext::NO_CONTEXT, $result);
  }

  /**
   * {@inheritdoc}
   */
  protected function setupExpectedContext($context) {
    if ($context) {
      $this->group
        ->getEntityTypeId()
        ->willReturn($context['entity_type'])
        ->shouldBeCalled();
      $this->group
        ->id()
        ->willReturn($context['id'])
        ->shouldBeCalled();
      $this
        ->expectGroupContext($this->group
        ->reveal());
    }
    else {
      $this
        ->expectGroupContext();
    }
  }

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

  /**
   * {@inheritdoc}
   */
  public function contextProvider() {
    return [
      // Test the expected result if no valid group exists in the active
      // context.
      [
        FALSE,
        OgGroupContextCacheContext::NO_CONTEXT,
      ],
      // Test having an entity with a numeric ID present in the active context.
      [
        [
          'entity_type' => 'node',
          'id' => 3,
        ],
        'node:3',
      ],
      // Test having an entity with a string ID present in the active context.
      [
        [
          'entity_type' => 'entity_test',
          'id' => 'Shibo Yangcong-San',
        ],
        'entity_test:Shibo Yangcong-San',
      ],
    ];
  }

}

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::setUp protected function Overrides UnitTestCase::setUp 1
OgContextCacheContextTestBase::testWithContext public function Tests the result of the cache context service with active context objects.
OgGroupContextCacheContextTest::contextProvider public function Provides test data for the test with active context objects. Overrides OgContextCacheContextTestBase::contextProvider
OgGroupContextCacheContextTest::getCacheContext protected function Returns the instantiated cache context service which is being tested. Overrides OgCacheContextTestBase::getCacheContext
OgGroupContextCacheContextTest::setupExpectedContext protected function Set up expectations for tests that have an active context object. Overrides OgContextCacheContextTestBase::setupExpectedContext
OgGroupContextCacheContextTest::testWithoutContext public function Tests getting cache context when there is no matching group on the route. 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.