public function AccessResultTest::testCacheContexts in Drupal 9
Same name and namespace in other branches
- 8 core/tests/Drupal/Tests/Core/Access/AccessResultTest.php \Drupal\Tests\Core\Access\AccessResultTest::testCacheContexts()
- 10 core/tests/Drupal/Tests/Core/Access/AccessResultTest.php \Drupal\Tests\Core\Access\AccessResultTest::testCacheContexts()
@covers ::addCacheContexts @covers ::resetCacheContexts @covers ::getCacheContexts @covers ::cachePerPermissions @covers ::cachePerUser @covers ::allowedIfHasPermission
File
- core/
tests/ Drupal/ Tests/ Core/ Access/ AccessResultTest.php, line 401 - Contains \Drupal\Tests\Core\Access\AccessResultTest.
Class
- AccessResultTest
- @coversDefaultClass \Drupal\Core\Access\AccessResult @group Access
Namespace
Drupal\Tests\Core\AccessCode
public function testCacheContexts() {
$verify = function (AccessResult $access, array $contexts) {
$this
->assertFalse($access
->isAllowed());
$this
->assertFalse($access
->isForbidden());
$this
->assertTrue($access
->isNeutral());
$this
->assertSame(Cache::PERMANENT, $access
->getCacheMaxAge());
$this
->assertEqualsCanonicalizing($contexts, $access
->getCacheContexts());
$this
->assertEqualsCanonicalizing([], $access
->getCacheTags());
};
$access = AccessResult::neutral()
->addCacheContexts([
'foo',
]);
$verify($access, [
'foo',
]);
// Verify resetting works.
$access
->resetCacheContexts();
$verify($access, []);
// Verify idempotency.
$access
->addCacheContexts([
'foo',
])
->addCacheContexts([
'foo',
]);
$verify($access, [
'foo',
]);
// Verify same values in different call order yields the same result.
$access
->resetCacheContexts()
->addCacheContexts([
'foo',
])
->addCacheContexts([
'bar',
]);
$verify($access, [
'bar',
'foo',
]);
$access
->resetCacheContexts()
->addCacheContexts([
'bar',
])
->addCacheContexts([
'foo',
]);
$verify($access, [
'bar',
'foo',
]);
// ::cachePerPermissions() convenience method.
$contexts = [
'user.permissions',
];
$a = AccessResult::neutral()
->addCacheContexts($contexts);
$verify($a, $contexts);
$b = AccessResult::neutral()
->cachePerPermissions();
$verify($b, $contexts);
$this
->assertEquals($a, $b);
// ::cachePerUser() convenience method.
$contexts = [
'user',
];
$a = AccessResult::neutral()
->addCacheContexts($contexts);
$verify($a, $contexts);
$b = AccessResult::neutral()
->cachePerUser();
$verify($b, $contexts);
$this
->assertEquals($a, $b);
// Both.
$contexts = [
'user',
'user.permissions',
];
$a = AccessResult::neutral()
->addCacheContexts($contexts);
$verify($a, $contexts);
$b = AccessResult::neutral()
->cachePerPermissions()
->cachePerUser();
$verify($b, $contexts);
$c = AccessResult::neutral()
->cachePerUser()
->cachePerPermissions();
$verify($c, $contexts);
$this
->assertEqualsCanonicalizing($a, $b);
$this
->assertEquals($a, $c);
// ::allowIfHasPermission and ::allowedIfHasPermission convenience methods.
$account = $this
->createMock('\\Drupal\\Core\\Session\\AccountInterface');
$account
->expects($this
->any())
->method('hasPermission')
->with('may herd llamas')
->will($this
->returnValue(FALSE));
$contexts = [
'user.permissions',
];
// Verify the object when using the ::allowedIfHasPermission() convenience
// static method.
$b = AccessResult::allowedIfHasPermission($account, 'may herd llamas');
$verify($b, $contexts);
}