diff --git a/core/core.services.yml b/core/core.services.yml index 60673fa..3da23c7 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -143,7 +143,7 @@ services: cache.backend.memory: class: Drupal\Core\Cache\MemoryBackendFactory arguments: ['@cache_tags.invalidator.checksum'] - # A special cache bin that by default does not persist beyond the length of the request. + # A special cache bin that does not persist beyond the length of the request. cache.static: class: Drupal\Core\Cache\CacheBackendInterface tags: diff --git a/core/lib/Drupal/Core/Session/PermissionsHashGenerator.php b/core/lib/Drupal/Core/Session/PermissionsHashGenerator.php index 5c79d04..19178ba 100644 --- a/core/lib/Drupal/Core/Session/PermissionsHashGenerator.php +++ b/core/lib/Drupal/Core/Session/PermissionsHashGenerator.php @@ -45,7 +45,9 @@ class PermissionsHashGenerator implements PermissionsHashGeneratorInterface { * @param \Drupal\Core\PrivateKey $private_key * The private key service. * @param \Drupal\Core\Cache\CacheBackendInterface $cache - * The cache backend interface to use for the permission hash cache. + * The cache backend interface to use for the persistent cache. + * @param \Drupal\Core\Cache\CacheBackendInterface + * The cache backend interface to use for the static cache. */ public function __construct(PrivateKey $private_key, CacheBackendInterface $cache, CacheBackendInterface $static) { $this->privateKey = $private_key; @@ -62,19 +64,19 @@ public function generate(AccountInterface $account) { $sorted_roles = $account->getRoles(); sort($sorted_roles); $role_list = implode(',', $sorted_roles); - $cid = "user_permissions_hasn:$role_list"; + $cid = "user_permissions_hash:$role_list"; if ($static_cache = $this->static->get($cid)) { return $static_cache->data; } else { $tags = Cache::buildTags('config:user.role', $sorted_roles, '.'); - if ($cache = $this->cache->get("user_permissions_hash:$role_list")) { + if ($cache = $this->cache->get($cid)) { $permissions_hash = $cache->data; $this->static->set($cid, $permissions_hash, Cache::PERMANENT, $tags); } else { $permissions_hash = $this->doGenerate($sorted_roles); - $this->cache->set("user_permissions_hash:$role_list", $permissions_hash, Cache::PERMANENT, $tags); + $this->cache->set($cid, $permissions_hash, Cache::PERMANENT, $tags); } } diff --git a/core/tests/Drupal/Tests/Core/Session/PermissionsHashTest.php b/core/tests/Drupal/Tests/Core/Session/PermissionsHashTest.php index d760daa..8ba99b4 100644 --- a/core/tests/Drupal/Tests/Core/Session/PermissionsHashTest.php +++ b/core/tests/Drupal/Tests/Core/Session/PermissionsHashTest.php @@ -55,6 +55,13 @@ class PermissionsHashTest extends UnitTestCase { 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 @@ -111,12 +118,15 @@ protected function setUp() { $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, new \Drupal\Core\Cache\MemoryBackend('test')); + $this->permissionsHash = new PermissionsHashGenerator($this->privateKey, $this->cache, $this->staticCache); } /** - * Tests the generate() method. + * @covers ::generate */ public function testGenerate() { // Ensure that two user accounts with the same roles generate the same hash. @@ -130,15 +140,20 @@ public function testGenerate() { } /** - * Tests the generate method with persistent cache returned. + * @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) @@ -150,20 +165,22 @@ public function testGeneratePersistentCache() { } /** - * Tests the generate method with static cache returned. + * @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->permissionsHasn = new PermissionsHashGenerator($this->privateKey, new \Drupal\Core\Cache\MemoryBackend('test'), $this->cache); - $this->cache->expects($this->once()) + $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'); @@ -177,6 +194,13 @@ 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) diff --git a/sites/development.services.yml b/sites/development.services.yml index cc21211..73cc998 100644 --- a/sites/development.services.yml +++ b/sites/development.services.yml @@ -3,7 +3,5 @@ # To activate this feature, follow the instructions at the top of the # 'example.settings.local.php' file, which sits next to this file. services: - cache.backend.memory: - class: Drupal\Core\Cache\MemoryBackendFactory cache.backend.null: class: Drupal\Core\Cache\NullBackendFactory