Same name and namespace in other branches
- 8.9.x core/tests/Drupal/Tests/Core/Session/PermissionsHashGeneratorTest.php \Drupal\Tests\Core\Session\PermissionsHashGeneratorTest
- 9 core/tests/Drupal/Tests/Core/Session/PermissionsHashGeneratorTest.php \Drupal\Tests\Core\Session\PermissionsHashGeneratorTest
@coversDefaultClass \Drupal\Core\Session\PermissionsHashGenerator @group Session
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, RandomGeneratorTrait, PhpUnitWarnings
- class \Drupal\Tests\Core\Session\PermissionsHashGeneratorTest
Expanded class hierarchy of PermissionsHashGeneratorTest
File
- core/
tests/ Drupal/ Tests/ Core/ Session/ PermissionsHashGeneratorTest.php, line 25
Namespace
Drupal\Tests\Core\SessionView source
class PermissionsHashGeneratorTest extends UnitTestCase {
/**
* The mocked user 1 account.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $account1;
/**
* The mocked user 2 account.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $account2;
/**
* The mocked cache backend.
*
* @var \Drupal\Core\Cache\CacheBackendInterface|\Prophecy\Prophecy\ObjectProphecy
*/
protected $staticCache;
/**
* The mocked access policy processor.
*
* @var \Drupal\Core\Session\AccessPolicyProcessorInterface|\Prophecy\Prophecy\ObjectProphecy
*/
protected $processor;
/**
* The permission hash class being tested.
*
* @var \Drupal\Core\Session\PermissionsHashGeneratorInterface
*/
protected $permissionsHash;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
new Settings([
'hash_salt' => 'test',
]);
$this->account1 = $this
->prophesize(AccountInterface::class);
$this->account1
->id()
->willReturn(1);
$this->account1 = $this->account1
->reveal();
$this->account2 = $this
->prophesize(AccountInterface::class);
$this->account2
->id()
->willReturn(2);
$this->account2 = $this->account2
->reveal();
$private_key = $this
->prophesize(PrivateKey::class);
$private_key
->get()
->willReturn(Crypt::randomBytesBase64(55));
$this->staticCache = $this
->prophesize(CacheBackendInterface::class);
$this->staticCache
->get(Argument::any())
->willReturn(FALSE);
$this->staticCache
->set(Argument::cetera())
->shouldBeCalled();
$this->processor = $this
->prophesize(AccessPolicyProcessorInterface::class);
$this->permissionsHash = new PermissionsHashGenerator($private_key
->reveal(), $this->staticCache
->reveal(), $this->processor
->reveal());
}
/**
* Tests the generate method for regular accounts.
*
* @covers ::generate
*/
public function testGenerateRegular() {
$permissions = new CalculatedPermissions((new RefinableCalculatedPermissions())
->addItem(new CalculatedPermissionsItem([
'permission foo',
'permission bar',
])));
$this->processor
->processAccessPolicies($this->account1)
->willReturn($permissions);
$this->processor
->processAccessPolicies($this->account2)
->willReturn($permissions);
// Check that two accounts with the same permissions generate the same hash.
$hash_1 = $this->permissionsHash
->generate($this->account1);
$hash_2 = $this->permissionsHash
->generate($this->account2);
$this
->assertSame($hash_1, $hash_2, 'Different users with the same permissions generate the same permissions hash.');
}
/**
* Tests the generate method for admin users.
*
* @covers ::generate
*/
public function testGenerateAdmin() {
$permissions = new CalculatedPermissions((new RefinableCalculatedPermissions())
->addItem(new CalculatedPermissionsItem([], TRUE)));
$this->processor
->processAccessPolicies($this->account1)
->willReturn($permissions);
$this->processor
->processAccessPolicies($this->account2)
->willReturn($permissions);
// Check that two accounts with the same permissions generate the same hash.
$hash_1 = $this->permissionsHash
->generate($this->account1);
$hash_2 = $this->permissionsHash
->generate($this->account2);
$this
->assertSame($hash_1, $hash_2, 'Different admins generate the same permissions hash.');
// Check that the generated hash is simply 'is-admin'.
$this
->assertSame('is-admin', $hash_1, 'Admins generate the string "is-admin" as their permissions hash.');
}
/**
* Tests the generate method with no access policies.
*
* @covers ::generate
*/
public function testGenerateNoAccessPolicies() {
$permissions = new CalculatedPermissions(new RefinableCalculatedPermissions());
$this->processor
->processAccessPolicies($this->account1)
->willReturn($permissions);
$this->processor
->processAccessPolicies($this->account2)
->willReturn($permissions);
// Check that two accounts with the same permissions generate the same hash.
$hash_1 = $this->permissionsHash
->generate($this->account1);
$hash_2 = $this->permissionsHash
->generate($this->account2);
$this
->assertSame($hash_1, $hash_2, 'Different accounts generate the same permissions hash when there are no policies.');
// Check that the generated hash is simply 'no-access-policies'.
$this
->assertSame('no-access-policies', $hash_1, 'Accounts generate the string "is-admin" as their permissions hash when no policies are defined.');
}
/**
* Tests the generate method's caching.
*
* @covers ::generate
*/
public function testGenerateCache() {
$permissions = new CalculatedPermissions(new RefinableCalculatedPermissions());
$this->processor
->processAccessPolicies($this->account1)
->willReturn($permissions);
$this->processor
->processAccessPolicies($this->account2)
->willReturn($permissions);
// Test that set is called with the right cache ID.
$this->staticCache
->set('permissions_hash_1', 'no-access-policies', Cache::PERMANENT, [])
->shouldBeCalledOnce();
$this->staticCache
->set('permissions_hash_2', 'no-access-policies', Cache::PERMANENT, [])
->shouldBeCalledOnce();
$this->permissionsHash
->generate($this->account1);
$this->permissionsHash
->generate($this->account2);
// Verify that ::set() isn't called more when ::get() returns something.
$cache_return = new \stdClass();
$cache_return->data = 'no-access-policies';
$this->staticCache
->get('permissions_hash_1')
->willReturn($cache_return);
$this->staticCache
->get('permissions_hash_2')
->willReturn($cache_return);
$this->permissionsHash
->generate($this->account1);
$this->permissionsHash
->generate($this->account2);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PermissionsHashGeneratorTest:: |
protected | property | The mocked user 1 account. | |
PermissionsHashGeneratorTest:: |
protected | property | The mocked user 2 account. | |
PermissionsHashGeneratorTest:: |
protected | property | The permission hash class being tested. | |
PermissionsHashGeneratorTest:: |
protected | property | The mocked access policy processor. | |
PermissionsHashGeneratorTest:: |
protected | property | The mocked cache backend. | |
PermissionsHashGeneratorTest:: |
protected | function |
Overrides UnitTestCase:: |
|
PermissionsHashGeneratorTest:: |
public | function | Tests the generate method for admin users. | |
PermissionsHashGeneratorTest:: |
public | function | Tests the generate method's caching. | |
PermissionsHashGeneratorTest:: |
public | function | Tests the generate method with no access policies. | |
PermissionsHashGeneratorTest:: |
public | function | Tests the generate method for regular accounts. | |
PhpUnitWarnings:: |
private static | property | Deprecation warnings from PHPUnit to raise with @trigger_error(). | |
PhpUnitWarnings:: |
public | function | Converts PHPUnit deprecation warnings to E_USER_DEPRECATED. | |
RandomGeneratorTrait:: |
protected | function | Gets the random generator for the utility methods. | |
RandomGeneratorTrait:: |
protected | function | Generates a unique random string containing letters and numbers. | |
RandomGeneratorTrait:: |
public | function | Generates a random PHP object. | |
RandomGeneratorTrait:: |
public | function | Generates a pseudo-random string of ASCII characters of codes 32 to 126. | |
RandomGeneratorTrait:: |
public | function | Callback for random string validation. | |
UnitTestCase:: |
protected | property | The app root. | 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:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public static | function | ||
UnitTestCase:: |
public | function |