View source
<?php
namespace Drupal\Tests\Core\Session {
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Session\PermissionsHashGenerator;
use Drupal\Core\Site\Settings;
use Drupal\Tests\UnitTestCase;
class PermissionsHashGeneratorTest extends UnitTestCase {
protected $account1;
protected $account2;
protected $account2Updated;
protected $account3;
protected $privateKey;
protected $cache;
protected $staticCache;
protected $permissionsHash;
protected function setUp() {
parent::setUp();
new Settings(array(
'hash_salt' => 'test',
));
$this->account1 = $this
->getMockBuilder('Drupal\\user\\Entity\\User')
->disableOriginalConstructor()
->setMethods(array(
'getRoles',
'id',
))
->getMock();
$this->account1
->expects($this
->any())
->method('id')
->willReturn(1);
$this->account1
->expects($this
->never())
->method('getRoles');
$roles_1 = array(
'administrator',
'authenticated',
);
$this->account2 = $this
->getMockBuilder('Drupal\\user\\Entity\\User')
->disableOriginalConstructor()
->setMethods(array(
'getRoles',
'id',
))
->getMock();
$this->account2
->expects($this
->any())
->method('getRoles')
->will($this
->returnValue($roles_1));
$this->account2
->expects($this
->any())
->method('id')
->willReturn(2);
$roles_3 = array(
'authenticated',
'administrator',
);
$this->account3 = $this
->getMockBuilder('Drupal\\user\\Entity\\User')
->disableOriginalConstructor()
->setMethods(array(
'getRoles',
'id',
))
->getMock();
$this->account3
->expects($this
->any())
->method('getRoles')
->will($this
->returnValue($roles_3));
$this->account3
->expects($this
->any())
->method('id')
->willReturn(3);
$roles_2_updated = array(
'editor',
'administrator',
'authenticated',
);
$this->account2Updated = $this
->getMockBuilder('Drupal\\user\\Entity\\User')
->disableOriginalConstructor()
->setMethods(array(
'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);
$random = Crypt::randomBytesBase64(55);
$this->privateKey = $this
->getMockBuilder('Drupal\\Core\\PrivateKey')
->disableOriginalConstructor()
->setMethods(array(
'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);
}
public function testGenerate() {
$super_user_hash = $this->permissionsHash
->generate($this->account1);
$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');
$updated_hash_2 = $this->permissionsHash
->generate($this->account2Updated);
$this
->assertNotSame($hash_2, $updated_hash_2, 'Same user with updated roles generates different permissions hash.');
}
public function testGeneratePersistentCache() {
$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);
}
public function testGenerateStaticCache() {
$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);
}
public function testGenerateNoCache() {
$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);
}
}
}
namespace {
if (!function_exists('user_role_permissions')) {
function user_role_permissions(array $roles) {
$role_permissions = array();
foreach ($roles as $rid) {
$role_permissions[$rid] = array();
}
return $role_permissions;
}
}
}