class GroupPermissionHashGeneratorTest in Group 8
Same name and namespace in other branches
- 2.0.x tests/src/Unit/GroupPermissionHashGeneratorTest.php \Drupal\Tests\group\Unit\GroupPermissionHashGeneratorTest
Tests the group permission hash generator service.
@coversDefaultClass \Drupal\group\Access\GroupPermissionsHashGenerator @group group
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\group\Unit\GroupPermissionHashGeneratorTest
Expanded class hierarchy of GroupPermissionHashGeneratorTest
File
- tests/
src/ Unit/ GroupPermissionHashGeneratorTest.php, line 25
Namespace
Drupal\Tests\group\UnitView source
class GroupPermissionHashGeneratorTest extends UnitTestCase {
/**
* The group permissions hash generator service.
*
* @var \Drupal\group\Access\GroupPermissionsHashGeneratorInterface
*/
protected $hashGenerator;
/**
* The group permission calculator.
*
* @var \Drupal\group\Access\ChainGroupPermissionCalculatorInterface|\Prophecy\Prophecy\ProphecyInterface
*/
protected $permissionCalculator;
/**
* The static cache.
*
* @var \Drupal\Core\Cache\CacheBackendInterface|\Prophecy\Prophecy\ProphecyInterface
*/
protected $static;
/**
* A dummy account to test with.
*
* @var \Drupal\Core\Session\AccountInterface|\Prophecy\Prophecy\ProphecyInterface
*/
protected $account;
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
new Settings([
'hash_salt' => 'SALT',
]);
$private_key = $this
->prophesize(PrivateKey::class);
$private_key
->get()
->willReturn('');
$this->static = $this
->prophesize(CacheBackendInterface::class);
$this->permissionCalculator = $this
->prophesize(ChainGroupPermissionCalculatorInterface::class);
$this->hashGenerator = new GroupPermissionsHashGenerator($private_key
->reveal(), $this->static
->reveal(), $this->permissionCalculator
->reveal());
$account = $this
->prophesize(AccountInterface::class);
$account
->id()
->willReturn(24101986);
$this->account = $account
->reveal();
}
/**
* Tests the generation of the account's hash.
*
* @covers ::generateHash
*/
public function testGenerateHash() {
$scope_gt = CalculatedGroupPermissionsItemInterface::SCOPE_GROUP_TYPE;
$scope_g = CalculatedGroupPermissionsItemInterface::SCOPE_GROUP;
$cid = 'group_permissions_hash_24101986';
$calculated_permissions = new RefinableCalculatedGroupPermissions();
$this->permissionCalculator
->calculatePermissions($this->account)
->willReturn($calculated_permissions);
$sorted_permissions = [
'alice' => [
'bob',
],
'foo' => [
'bar',
'baz',
],
16 => [
'sweet',
],
];
$expected_hash = hash('sha256', 'SALT' . serialize($sorted_permissions));
$this->static
->get($cid)
->willReturn(FALSE);
$this->static
->set($cid, $expected_hash, Cache::PERMANENT, [])
->shouldBeCalledTimes(1);
$calculated_permissions
->addItem(new CalculatedGroupPermissionsItem($scope_gt, 'foo', [
'baz',
'bar',
]))
->addItem(new CalculatedGroupPermissionsItem($scope_gt, 'alice', [
'bob',
]))
->addItem(new CalculatedGroupPermissionsItem($scope_g, 16, [
'sweet',
]));
$this
->assertEquals($expected_hash, $this->hashGenerator
->generateHash($this->account), 'The hash was generated based on the sorted calculated permissions.');
$sorted_permissions[100] = 'is-admin';
$expected_hash = hash('sha256', 'SALT' . serialize($sorted_permissions));
$this->static
->set($cid, $expected_hash, Cache::PERMANENT, [])
->shouldBeCalledTimes(1);
$calculated_permissions
->addItem(new CalculatedGroupPermissionsItem($scope_g, 100, [
'irrelevant',
], TRUE));
$this
->assertEquals($expected_hash, $this->hashGenerator
->generateHash($this->account), 'The hash uses a simple flag instead of permissions for admin entries.');
$cache = (object) [
'data' => 'foobar',
];
$this->static
->get($cid)
->willReturn($cache);
$this->static
->set($cid, 'foobar', Cache::PERMANENT, [])
->shouldNotBeCalled();
$this
->assertEquals('foobar', $this->hashGenerator
->generateHash($this->account), 'The hash was retrieved from the static cache.');
}
/**
* Tests getting the cacheable metadata from the calculated permissions.
*
* @covers ::getCacheableMetadata
*/
public function testGetCacheableMetadata() {
$calculated_permissions = $this
->prophesize(CalculatedGroupPermissionsInterface::class);
$calculated_permissions
->getCacheContexts()
->willReturn([]);
$calculated_permissions
->getCacheTags()
->willReturn([
"config:group.role.foo-bar",
]);
$calculated_permissions
->getCacheMaxAge()
->willReturn(-1);
$calculated_permissions = $calculated_permissions
->reveal();
$this->permissionCalculator
->calculatePermissions($this->account)
->willReturn($calculated_permissions);
$this
->assertEquals(CacheableMetadata::createFromObject($calculated_permissions), $this->hashGenerator
->getCacheableMetadata($this->account));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
GroupPermissionHashGeneratorTest:: |
protected | property | A dummy account to test with. | |
GroupPermissionHashGeneratorTest:: |
protected | property | The group permissions hash generator service. | |
GroupPermissionHashGeneratorTest:: |
protected | property | The group permission calculator. | |
GroupPermissionHashGeneratorTest:: |
protected | property | The static cache. | |
GroupPermissionHashGeneratorTest:: |
public | function |
Overrides UnitTestCase:: |
|
GroupPermissionHashGeneratorTest:: |
public | function | Tests the generation of the account's hash. | |
GroupPermissionHashGeneratorTest:: |
public | function | Tests getting the cacheable metadata from the calculated permissions. | |
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. |