core/lib/Drupal/Core/Access/AccessResult.php | 15 +++++++-------- core/tests/Drupal/Tests/Core/Access/AccessResultTest.php | 3 +-- .../Drupal/Tests/Core/Entity/EntityAccessCheckTest.php | 1 - .../tests/Drupal/Tests/Core/Route/RoleAccessCheckTest.php | 1 - 4 files changed, 8 insertions(+), 12 deletions(-) diff --git a/core/lib/Drupal/Core/Access/AccessResult.php b/core/lib/Drupal/Core/Access/AccessResult.php index 661ee55..bf88316 100644 --- a/core/lib/Drupal/Core/Access/AccessResult.php +++ b/core/lib/Drupal/Core/Access/AccessResult.php @@ -368,12 +368,19 @@ public function andIf(AccessResultInterface $other) { /** * Inherits the cacheability of the other access result, if any. * + * inheritCacheability() differs from addCacheableDependency() in how it + * handles max-age, because it is designed to inherit the cacheability of the + * second operand in the andIf() and orIf() operations. There, the situation + * "allowed, max-age=0 OR allowed, max-age=1000" needs to yield max-age 1000 + * as the end result. + * * @param \Drupal\Core\Access\AccessResultInterface $other * The other access result, whose cacheability (if any) to inherit. * * @return $this */ public function inheritCacheability(AccessResultInterface $other) { + $this->addCacheableDependency($other); if ($other instanceof CacheableDependencyInterface) { if ($this->getCacheMaxAge() !== 0 && $other->getCacheMaxAge() !== 0) { $this->setCacheMaxAge(Cache::mergeMaxAges($this->getCacheMaxAge(), $other->getCacheMaxAge())); @@ -381,14 +388,6 @@ public function inheritCacheability(AccessResultInterface $other) { else { $this->setCacheMaxAge($other->getCacheMaxAge()); } - $this->addCacheContexts($other->getCacheContexts()); - $this->addCacheTags($other->getCacheTags()); - } - // If any of the access results don't provide cacheability metadata, then - // we cannot cache the combined access result, for we may not make - // assumptions. - else { - $this->setCacheMaxAge(0); } return $this; } diff --git a/core/tests/Drupal/Tests/Core/Access/AccessResultTest.php b/core/tests/Drupal/Tests/Core/Access/AccessResultTest.php index f482386..ae5dadc 100644 --- a/core/tests/Drupal/Tests/Core/Access/AccessResultTest.php +++ b/core/tests/Drupal/Tests/Core/Access/AccessResultTest.php @@ -855,8 +855,7 @@ public function testOrIfCacheabilityMerging() { * @param string[] $permissions * The permissions to check for. * @param string $conjunction - * The conjunction to use when checking for permission. Either 'AND" or - * 'OR'. + * The conjunction to use when checking for permission. 'AND' or 'OR'. * @param \Drupal\Core\Access\AccessResult $expected_access * The expected access check result. */ diff --git a/core/tests/Drupal/Tests/Core/Entity/EntityAccessCheckTest.php b/core/tests/Drupal/Tests/Core/Entity/EntityAccessCheckTest.php index 06c9a16..0865933 100644 --- a/core/tests/Drupal/Tests/Core/Entity/EntityAccessCheckTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/EntityAccessCheckTest.php @@ -27,7 +27,6 @@ class EntityAccessCheckTest extends UnitTestCase { * Tests the method for checking access to routes. */ public function testAccess() { - $cache_contexts_manager = $this->prophesize(CacheContextsManager::class)->reveal(); $container = new Container(); $container->set('cache_contexts_manager', $cache_contexts_manager); diff --git a/core/tests/Drupal/Tests/Core/Route/RoleAccessCheckTest.php b/core/tests/Drupal/Tests/Core/Route/RoleAccessCheckTest.php index 7725c9e..9d0d148 100644 --- a/core/tests/Drupal/Tests/Core/Route/RoleAccessCheckTest.php +++ b/core/tests/Drupal/Tests/Core/Route/RoleAccessCheckTest.php @@ -145,7 +145,6 @@ public function roleAccessProvider() { * @dataProvider roleAccessProvider */ public function testRoleAccess($path, $grant_accounts, $deny_accounts) { - $cache_contexts_manager = $this->prophesize(CacheContextsManager::class)->reveal(); $container = new Container(); $container->set('cache_contexts_manager', $cache_contexts_manager);