diff --git a/core/lib/Drupal/Core/Access/AccessResult.php b/core/lib/Drupal/Core/Access/AccessResult.php index cab5e1a..b3a59d4 100644 --- a/core/lib/Drupal/Core/Access/AccessResult.php +++ b/core/lib/Drupal/Core/Access/AccessResult.php @@ -352,28 +352,33 @@ public function cacheUntilConfigurationChanges(ConfigBase $configuration) { * * @return $this */ - public function addCacheableDependency(CacheableDependencyInterface $other_object) { - // This is called many times per request, so avoid merging unless absolutely - // necessary. - if (empty($this->contexts)) { - $this->contexts = $other_object->getCacheContexts(); - } - elseif ($contexts = $other_object->getCacheContexts()) { - $this->contexts = Cache::mergeContexts($this->contexts, $contexts); - } + public function addCacheableDependency($other_object) { + if ($other_object instanceof CacheableDependencyInterface) { + // This is called many times per request, so avoid merging unless absolutely + // necessary. + if (empty($this->contexts)) { + $this->contexts = $other_object->getCacheContexts(); + } + elseif ($contexts = $other_object->getCacheContexts()) { + $this->contexts = Cache::mergeContexts($this->contexts, $contexts); + } - if (empty($this->tags)) { - $this->tags = $other_object->getCacheTags(); - } - elseif ($tags = $other_object->getCacheTags()) { - $this->tags = Cache::mergeTags($this->tags, $tags); - } + if (empty($this->tags)) { + $this->tags = $other_object->getCacheTags(); + } + elseif ($tags = $other_object->getCacheTags()) { + $this->tags = Cache::mergeTags($this->tags, $tags); + } - if ($this->maxAge === Cache::PERMANENT) { - $this->maxAge = $other_object->getCacheMaxAge(); + if ($this->maxAge === Cache::PERMANENT) { + $this->maxAge = $other_object->getCacheMaxAge(); + } + elseif (($max_age = $other_object->getCacheMaxAge()) && $max_age !== Cache::PERMANENT) { + $this->maxAge = Cache::mergeMaxAges($this->maxAge, $max_age); + } } - elseif (($max_age = $other_object->getCacheMaxAge()) && $max_age !== Cache::PERMANENT) { - $this->maxAge = Cache::mergeMaxAges($this->maxAge, $max_age); + else { + $this->maxAge = 0; } return $this; } diff --git a/core/tests/Drupal/Tests/Core/Access/AccessResultTest.php b/core/tests/Drupal/Tests/Core/Access/AccessResultTest.php index 123888a..cbc9c97 100644 --- a/core/tests/Drupal/Tests/Core/Access/AccessResultTest.php +++ b/core/tests/Drupal/Tests/Core/Access/AccessResultTest.php @@ -388,12 +388,12 @@ public function testCacheContexts() { * @covers ::cacheUntilConfigurationChanges */ public function testCacheTags() { - $verify = function (AccessResult $access, array $tags) { + $verify = function (AccessResult $access, array $tags, array $contexts = [], $max_age = Cache::PERMANENT) { $this->assertFalse($access->isAllowed()); $this->assertFalse($access->isForbidden()); $this->assertTrue($access->isNeutral()); - $this->assertSame(Cache::PERMANENT, $access->getCacheMaxAge()); - $this->assertSame([], $access->getCacheContexts()); + $this->assertSame($max_age, $access->getCacheMaxAge()); + $this->assertSame($contexts, $access->getCacheContexts()); $this->assertSame($tags, $access->getCacheTags()); }; @@ -420,29 +420,26 @@ public function testCacheTags() { ->addCacheTags(['bar:baz']); $verify($access, ['bar:baz', 'bar:qux', 'foo:bar', 'foo:baz']); - // ::cacheUntilEntityChanges() convenience method. + // ::addCacheableDependency() convenience method. $node = $this->getMock('\Drupal\node\NodeInterface'); $node->expects($this->any()) ->method('getCacheTags') ->will($this->returnValue(array('node:20011988'))); + $node->expects($this->any()) + ->method('getCacheMaxAge') + ->willReturn(600); + $node->expects($this->any()) + ->method('getCacheContexts') + ->willReturn(['user']); $tags = array('node:20011988'); $a = AccessResult::neutral()->addCacheTags($tags); $verify($a, $tags); - $b = AccessResult::neutral()->cacheUntilEntityChanges($node); - $verify($b, $tags); - $this->assertEquals($a, $b); + $b = AccessResult::neutral()->addCacheableDependency($node); + $verify($b, $tags, ['user'], 600); - // ::cacheUntilConfigurationChanges() convenience method. - $configuration = $this->getMock('\Drupal\Core\Config\ConfigBase'); - $configuration->expects($this->any()) - ->method('getCacheTags') - ->will($this->returnValue(array('config:foo.bar.baz'))); - $tags = array('config:foo.bar.baz'); - $a = AccessResult::neutral()->addCacheTags($tags); - $verify($a, $tags); - $b = AccessResult::neutral()->cacheUntilConfigurationChanges($configuration); - $verify($b, $tags); - $this->assertEquals($a, $b); + $non_cacheable_dependency = new \stdClass(); + $non_cacheable = AccessResult::neutral()->addCacheableDependency($non_cacheable_dependency); + $verify($non_cacheable, [], [], 0); } /**