function AccessResultTest::testCacheTags
Tests cache tags.
@legacy-covers ::addCacheTags @legacy-covers ::addCacheableDependency @legacy-covers ::getCacheTags @legacy-covers ::resetCacheTags
File
-
core/
tests/ Drupal/ Tests/ Core/ Access/ AccessResultTest.php, line 501
Class
Namespace
Drupal\Tests\Core\AccessCode
public function testCacheTags() : void {
$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($max_age, $access->getCacheMaxAge());
$this->assertEqualsCanonicalizing($contexts, $access->getCacheContexts());
$this->assertEqualsCanonicalizing($tags, $access->getCacheTags());
};
$access = AccessResult::neutral()->addCacheTags([
'foo:bar',
]);
$verify($access, [
'foo:bar',
]);
// Verify resetting works.
$access->resetCacheTags();
$verify($access, []);
// Verify idempotency.
$access->addCacheTags([
'foo:bar',
])
->addCacheTags([
'foo:bar',
]);
$verify($access, [
'foo:bar',
]);
// Verify same values in different call order yields the same result.
$access->resetCacheTags()
->addCacheTags([
'bar:baz',
])
->addCacheTags([
'bar:qux',
])
->addCacheTags([
'foo:bar',
])
->addCacheTags([
'foo:baz',
]);
$verify($access, [
'bar:baz',
'bar:qux',
'foo:bar',
'foo:baz',
]);
$access->resetCacheTags()
->addCacheTags([
'foo:bar',
])
->addCacheTags([
'bar:qux',
])
->addCacheTags([
'foo:baz',
])
->addCacheTags([
'bar:baz',
]);
$verify($access, [
'bar:baz',
'bar:qux',
'foo:bar',
'foo:baz',
]);
// ::addCacheableDependency() convenience method.
$node = $this->createMock('\\Drupal\\node\\NodeInterface');
$node->expects($this->any())
->method('getCacheTags')
->willReturn([
'node:20011988',
]);
$node->expects($this->any())
->method('getCacheMaxAge')
->willReturn(600);
$node->expects($this->any())
->method('getCacheContexts')
->willReturn([
'user',
]);
$tags = [
'node:20011988',
];
$a = AccessResult::neutral()->addCacheTags($tags);
$verify($a, $tags);
$b = AccessResult::neutral()->addCacheableDependency($node);
$verify($b, $tags, [
'user',
], 600);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.