class IsGroupMemberCacheContextTest in Group 8
Same name and namespace in other branches
- 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
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\group\Unit\IsGroupMemberCacheContextTest
Expanded class hierarchy of IsGroupMemberCacheContextTest
File
- tests/
src/ Unit/ IsGroupMemberCacheContextTest.php, line 23
Namespace
Drupal\Tests\group\UnitView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
IsGroupMemberCacheContextTest:: |
protected | property | The current user. | |
IsGroupMemberCacheContextTest:: |
protected | property | A dummy group to use in other prophecies. | |
IsGroupMemberCacheContextTest:: |
protected | function | Creates an EntityTypeManagerInterface prophecy. | |
IsGroupMemberCacheContextTest:: |
protected | function | Creates a GroupMembershipLoaderInterface prophecy. | |
IsGroupMemberCacheContextTest:: |
public | function |
Overrides UnitTestCase:: |
|
IsGroupMemberCacheContextTest:: |
public | function | Tests getting the cacheable metadata for a valid cache context. | |
IsGroupMemberCacheContextTest:: |
public | function | Tests getting the cacheable metadata from a non-calculated cache context. | |
IsGroupMemberCacheContextTest:: |
public | function | Tests getting the context value when the user is a member. | |
IsGroupMemberCacheContextTest:: |
public | function | Tests getting the context value when the user is not a member. | |
IsGroupMemberCacheContextTest:: |
public | function | Tests getting the context value while specifying a non-existent group. | |
IsGroupMemberCacheContextTest:: |
public | function | Tests getting the context value from a non-calculated cache context. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |