You are here

class GroupPermissionsCacheContextTest in Group 8

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

Tests the user.group_permissions cache context.

@coversDefaultClass \Drupal\group\Cache\Context\GroupPermissionsCacheContext @group group

Hierarchy

Expanded class hierarchy of GroupPermissionsCacheContextTest

File

tests/src/Unit/GroupPermissionsCacheContextTest.php, line 17

Namespace

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

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

  /**
   * The permissions hash generator.
   *
   * @var \Drupal\group\Access\GroupPermissionsHashGeneratorInterface|\Prophecy\Prophecy\ProphecyInterface
   */
  protected $permissionsHashGenerator;

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

  /**
   * Tests getting the context value for the current user.
   *
   * @covers ::getContext
   */
  public function testGetContext() {
    $this->permissionsHashGenerator
      ->generateHash($this->currentUser
      ->reveal())
      ->willReturn('foo');
    $cache_context = new GroupPermissionsCacheContext($this->currentUser
      ->reveal(), $this->permissionsHashGenerator
      ->reveal());
    $this
      ->assertSame('foo', $cache_context
      ->getContext(), 'The cache context gets its value directly from the hash generator.');
  }

  /**
   * Tests getting the cacheable metadata from the hash generator.
   *
   * @covers ::getCacheableMetadata
   */
  public function testGetCacheableMetadata() {
    $cacheable_metadata = new CacheableMetadata();
    $cacheable_metadata
      ->addCacheTags([
      "config:group.role.foo-bar",
    ]);
    $this->permissionsHashGenerator
      ->getCacheableMetadata($this->currentUser
      ->reveal())
      ->willReturn($cacheable_metadata);
    $cache_context = new GroupPermissionsCacheContext($this->currentUser
      ->reveal(), $this->permissionsHashGenerator
      ->reveal());
    $this
      ->assertEquals($cacheable_metadata, $cache_context
      ->getCacheableMetadata(), 'The cache context gets its cacheable metadata directly from the hash generator.');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GroupPermissionsCacheContextTest::$currentUser protected property The current user.
GroupPermissionsCacheContextTest::$permissionsHashGenerator protected property The permissions hash generator.
GroupPermissionsCacheContextTest::setUp public function Overrides UnitTestCase::setUp
GroupPermissionsCacheContextTest::testGetCacheableMetadata public function Tests getting the cacheable metadata from the hash generator.
GroupPermissionsCacheContextTest::testGetContext public function Tests getting the context value for the current user.
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.