class PermissionsHashGeneratorTest in Drupal 8
Same name and namespace in other branches
- 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 PhpunitCompatibilityTrait
- class \Drupal\Tests\Core\Session\PermissionsHashGeneratorTest
Expanded class hierarchy of PermissionsHashGeneratorTest
File
- core/
tests/ Drupal/ Tests/ Core/ Session/ PermissionsHashGeneratorTest.php, line 14
Namespace
Drupal\Tests\Core\SessionView source
class PermissionsHashGeneratorTest extends UnitTestCase {
/**
* The mocked super user account.
*
* @var \Drupal\user\UserInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $account1;
/**
* A mocked account.
*
* @var \Drupal\user\UserInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $account2;
/**
* An "updated" mocked account.
*
* @var \Drupal\user\UserInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $account2Updated;
/**
* A different account.
*
* @var \Drupal\user\UserInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $account3;
/**
* The mocked private key service.
*
* @var \Drupal\Core\PrivateKey|\PHPUnit\Framework\MockObject\MockObject
*/
protected $privateKey;
/**
* The mocked cache backend.
*
* @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $cache;
/**
* The mocked cache backend.
*
* @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $staticCache;
/**
* The permission hash class being tested.
*
* @var \Drupal\Core\Session\PermissionsHashGeneratorInterface
*/
protected $permissionsHash;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
new Settings([
'hash_salt' => 'test',
]);
// The mocked super user account, with the same roles as Account 2.
$this->account1 = $this
->getMockBuilder('Drupal\\user\\Entity\\User')
->disableOriginalConstructor()
->setMethods([
'getRoles',
'id',
])
->getMock();
$this->account1
->expects($this
->any())
->method('id')
->willReturn(1);
$this->account1
->expects($this
->never())
->method('getRoles');
// Account 2: 'administrator' and 'authenticated' roles.
$roles_1 = [
'administrator',
'authenticated',
];
$this->account2 = $this
->getMockBuilder('Drupal\\user\\Entity\\User')
->disableOriginalConstructor()
->setMethods([
'getRoles',
'id',
])
->getMock();
$this->account2
->expects($this
->any())
->method('getRoles')
->will($this
->returnValue($roles_1));
$this->account2
->expects($this
->any())
->method('id')
->willReturn(2);
// Account 3: 'authenticated' and 'administrator' roles (different order).
$roles_3 = [
'authenticated',
'administrator',
];
$this->account3 = $this
->getMockBuilder('Drupal\\user\\Entity\\User')
->disableOriginalConstructor()
->setMethods([
'getRoles',
'id',
])
->getMock();
$this->account3
->expects($this
->any())
->method('getRoles')
->will($this
->returnValue($roles_3));
$this->account3
->expects($this
->any())
->method('id')
->willReturn(3);
// Updated account 2: now also 'editor' role.
$roles_2_updated = [
'editor',
'administrator',
'authenticated',
];
$this->account2Updated = $this
->getMockBuilder('Drupal\\user\\Entity\\User')
->disableOriginalConstructor()
->setMethods([
'getRoles',
'id',
])
->getMock();
$this->account2Updated
->expects($this
->any())
->method('getRoles')
->will($this
->returnValue($roles_2_updated));
$this->account2Updated
->expects($this
->any())
->method('id')
->willReturn(2);
// Mocked private key + cache services.
$random = Crypt::randomBytesBase64(55);
$this->privateKey = $this
->getMockBuilder('Drupal\\Core\\PrivateKey')
->disableOriginalConstructor()
->setMethods([
'get',
])
->getMock();
$this->privateKey
->expects($this
->any())
->method('get')
->will($this
->returnValue($random));
$this->cache = $this
->getMockBuilder('Drupal\\Core\\Cache\\CacheBackendInterface')
->disableOriginalConstructor()
->getMock();
$this->staticCache = $this
->getMockBuilder('Drupal\\Core\\Cache\\CacheBackendInterface')
->disableOriginalConstructor()
->getMock();
$this->permissionsHash = new PermissionsHashGenerator($this->privateKey, $this->cache, $this->staticCache);
}
/**
* @covers ::generate
*/
public function testGenerate() {
// Ensure that the super user (user 1) always gets the same hash.
$super_user_hash = $this->permissionsHash
->generate($this->account1);
// Ensure that two user accounts with the same roles generate the same hash.
$hash_2 = $this->permissionsHash
->generate($this->account2);
$hash_3 = $this->permissionsHash
->generate($this->account3);
$this
->assertSame($hash_2, $hash_3, 'Different users with the same roles generate the same permissions hash.');
$this
->assertNotSame($hash_2, $super_user_hash, 'User 1 has a different hash despite having the same roles');
// Compare with hash for user account 1 with an additional role.
$updated_hash_2 = $this->permissionsHash
->generate($this->account2Updated);
$this
->assertNotSame($hash_2, $updated_hash_2, 'Same user with updated roles generates different permissions hash.');
}
/**
* @covers ::generate
*/
public function testGeneratePersistentCache() {
// Set expectations for the mocked cache backend.
$expected_cid = 'user_permissions_hash:administrator,authenticated';
$mock_cache = new \stdClass();
$mock_cache->data = 'test_hash_here';
$this->staticCache
->expects($this
->once())
->method('get')
->with($expected_cid)
->will($this
->returnValue(FALSE));
$this->staticCache
->expects($this
->once())
->method('set')
->with($expected_cid, $this
->isType('string'));
$this->cache
->expects($this
->once())
->method('get')
->with($expected_cid)
->will($this
->returnValue($mock_cache));
$this->cache
->expects($this
->never())
->method('set');
$this->permissionsHash
->generate($this->account2);
}
/**
* @covers ::generate
*/
public function testGenerateStaticCache() {
// Set expectations for the mocked cache backend.
$expected_cid = 'user_permissions_hash:administrator,authenticated';
$mock_cache = new \stdClass();
$mock_cache->data = 'test_hash_here';
$this->staticCache
->expects($this
->once())
->method('get')
->with($expected_cid)
->will($this
->returnValue($mock_cache));
$this->staticCache
->expects($this
->never())
->method('set');
$this->cache
->expects($this
->never())
->method('get');
$this->cache
->expects($this
->never())
->method('set');
$this->permissionsHash
->generate($this->account2);
}
/**
* Tests the generate method with no cache returned.
*/
public function testGenerateNoCache() {
// Set expectations for the mocked cache backend.
$expected_cid = 'user_permissions_hash:administrator,authenticated';
$this->staticCache
->expects($this
->once())
->method('get')
->with($expected_cid)
->will($this
->returnValue(FALSE));
$this->staticCache
->expects($this
->once())
->method('set')
->with($expected_cid, $this
->isType('string'));
$this->cache
->expects($this
->once())
->method('get')
->with($expected_cid)
->will($this
->returnValue(FALSE));
$this->cache
->expects($this
->once())
->method('set')
->with($expected_cid, $this
->isType('string'));
$this->permissionsHash
->generate($this->account2);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PermissionsHashGeneratorTest:: |
protected | property | The mocked super user account. | |
PermissionsHashGeneratorTest:: |
protected | property | A mocked account. | |
PermissionsHashGeneratorTest:: |
protected | property | An "updated" mocked account. | |
PermissionsHashGeneratorTest:: |
protected | property | A different account. | |
PermissionsHashGeneratorTest:: |
protected | property | The mocked cache backend. | |
PermissionsHashGeneratorTest:: |
protected | property | The permission hash class being tested. | |
PermissionsHashGeneratorTest:: |
protected | property | The mocked private key service. | |
PermissionsHashGeneratorTest:: |
protected | property | The mocked cache backend. | |
PermissionsHashGeneratorTest:: |
protected | function |
Overrides UnitTestCase:: |
|
PermissionsHashGeneratorTest:: |
public | function | @covers ::generate | |
PermissionsHashGeneratorTest:: |
public | function | Tests the generate method with no cache returned. | |
PermissionsHashGeneratorTest:: |
public | function | @covers ::generate | |
PermissionsHashGeneratorTest:: |
public | function | @covers ::generate | |
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. |