View source
<?php
namespace Drupal\Tests\Core\Access;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\Access\AccessResultNeutral;
use Drupal\Core\Access\AccessResultReasonInterface;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Tests\UnitTestCase;
class AccessResultTest extends UnitTestCase {
protected $cacheContextsManager;
protected function setUp() : void {
parent::setUp();
$this->cacheContextsManager = $this
->getMockBuilder('Drupal\\Core\\Cache\\Context\\CacheContextsManager')
->disableOriginalConstructor()
->getMock();
$this->cacheContextsManager
->method('assertValidTokens')
->willReturn(TRUE);
$container = new ContainerBuilder();
$container
->set('cache_contexts_manager', $this->cacheContextsManager);
\Drupal::setContainer($container);
}
protected function assertDefaultCacheability(AccessResult $access) : void {
$this
->assertSame([], $access
->getCacheContexts());
$this
->assertSame([], $access
->getCacheTags());
$this
->assertSame(Cache::PERMANENT, $access
->getCacheMaxAge());
}
public function testConstruction() {
$verify = function (AccessResult $access) {
$this
->assertFalse($access
->isAllowed());
$this
->assertFalse($access
->isForbidden());
$this
->assertTrue($access
->isNeutral());
$this
->assertDefaultCacheability($access);
};
$a = new AccessResultNeutral();
$verify($a);
$b = AccessResult::neutral();
$verify($b);
$this
->assertEquals($a, $b);
}
public function testAccessAllowed() {
$verify = function (AccessResult $access) {
$this
->assertTrue($access
->isAllowed());
$this
->assertFalse($access
->isForbidden());
$this
->assertFalse($access
->isNeutral());
$this
->assertDefaultCacheability($access);
};
$b = AccessResult::allowed();
$verify($b);
}
public function testAccessForbidden() {
$verify = function (AccessResult $access) {
$this
->assertFalse($access
->isAllowed());
$this
->assertTrue($access
->isForbidden());
$this
->assertFalse($access
->isNeutral());
$this
->assertDefaultCacheability($access);
};
$b = AccessResult::forbidden();
$verify($b);
}
public function testAccessForbiddenReason() {
$verify = function (AccessResult $access, $reason) {
$this
->assertInstanceOf(AccessResultReasonInterface::class, $access);
$this
->assertSame($reason, $access
->getReason());
};
$b = AccessResult::forbidden();
$verify($b, '');
$reason = $this
->getRandomGenerator()
->string();
$b = AccessResult::forbidden($reason);
$verify($b, $reason);
$b = AccessResult::forbiddenIf(TRUE, $reason);
$verify($b, $reason);
}
public function testAccessConditionallyAllowed() {
$verify = function (AccessResult $access, $allowed) {
$this
->assertSame($allowed, $access
->isAllowed());
$this
->assertFalse($access
->isForbidden());
$this
->assertSame(!$allowed, $access
->isNeutral());
$this
->assertDefaultCacheability($access);
};
$b1 = AccessResult::allowedIf(TRUE);
$verify($b1, TRUE);
$b2 = AccessResult::allowedIf(FALSE);
$verify($b2, FALSE);
}
public function testAccessConditionallyForbidden() {
$verify = function (AccessResult $access, $forbidden) {
$this
->assertFalse($access
->isAllowed());
$this
->assertSame($forbidden, $access
->isForbidden());
$this
->assertSame(!$forbidden, $access
->isNeutral());
$this
->assertDefaultCacheability($access);
};
$b1 = AccessResult::forbiddenIf(TRUE);
$verify($b1, TRUE);
$b2 = AccessResult::forbiddenIf(FALSE);
$verify($b2, FALSE);
}
public function testAndIf() {
$neutral = AccessResult::neutral('neutral message');
$allowed = AccessResult::allowed();
$forbidden = AccessResult::forbidden('forbidden message');
$unused_access_result_due_to_lazy_evaluation = $this
->createMock('\\Drupal\\Core\\Access\\AccessResultInterface');
$unused_access_result_due_to_lazy_evaluation
->expects($this
->never())
->method($this
->anything());
$access = $allowed
->andIf($allowed);
$this
->assertTrue($access
->isAllowed());
$this
->assertFalse($access
->isForbidden());
$this
->assertFalse($access
->isNeutral());
$this
->assertDefaultCacheability($access);
$access = $allowed
->andIf($neutral);
$this
->assertFalse($access
->isAllowed());
$this
->assertFalse($access
->isForbidden());
$this
->assertTrue($access
->isNeutral());
$this
->assertEquals('neutral message', $access
->getReason());
$this
->assertDefaultCacheability($access);
$access = $allowed
->andIf($forbidden);
$this
->assertFalse($access
->isAllowed());
$this
->assertTrue($access
->isForbidden());
$this
->assertFalse($access
->isNeutral());
$this
->assertEquals('forbidden message', $access
->getReason());
$this
->assertDefaultCacheability($access);
$access = $neutral
->andIf($allowed);
$this
->assertFalse($access
->isAllowed());
$this
->assertFalse($access
->isForbidden());
$this
->assertTrue($access
->isNeutral());
$this
->assertEquals('neutral message', $access
->getReason());
$this
->assertDefaultCacheability($access);
$access = $neutral
->andIf($neutral);
$this
->assertFalse($access
->isAllowed());
$this
->assertFalse($access
->isForbidden());
$this
->assertTrue($access
->isNeutral());
$this
->assertEquals('neutral message', $access
->getReason());
$this
->assertDefaultCacheability($access);
$access = $neutral
->andIf($forbidden);
$this
->assertFalse($access
->isAllowed());
$this
->assertTrue($access
->isForbidden());
$this
->assertFalse($access
->isNeutral());
$this
->assertEquals('forbidden message', $access
->getReason());
$this
->assertDefaultCacheability($access);
$access = $forbidden
->andif($allowed);
$this
->assertFalse($access
->isAllowed());
$this
->assertTrue($access
->isForbidden());
$this
->assertFalse($access
->isNeutral());
$this
->assertEquals('forbidden message', $access
->getReason());
$this
->assertDefaultCacheability($access);
$access = $forbidden
->andif($neutral);
$this
->assertFalse($access
->isAllowed());
$this
->assertTrue($access
->isForbidden());
$this
->assertFalse($access
->isNeutral());
$this
->assertEquals('forbidden message', $access
->getReason());
$this
->assertDefaultCacheability($access);
$access = $forbidden
->andif($forbidden);
$this
->assertFalse($access
->isAllowed());
$this
->assertTrue($access
->isForbidden());
$this
->assertFalse($access
->isNeutral());
$this
->assertEquals('forbidden message', $access
->getReason());
$this
->assertDefaultCacheability($access);
$access = $forbidden
->andIf($unused_access_result_due_to_lazy_evaluation);
$this
->assertFalse($access
->isAllowed());
$this
->assertTrue($access
->isForbidden());
$this
->assertFalse($access
->isNeutral());
$this
->assertEquals('forbidden message', $access
->getReason());
$this
->assertDefaultCacheability($access);
}
public function testOrIf() {
$neutral = AccessResult::neutral('neutral message');
$neutral_other = AccessResult::neutral('other neutral message');
$neutral_reasonless = AccessResult::neutral();
$allowed = AccessResult::allowed();
$forbidden = AccessResult::forbidden('forbidden message');
$forbidden_other = AccessResult::forbidden('other forbidden message');
$forbidden_reasonless = AccessResult::forbidden();
$unused_access_result_due_to_lazy_evaluation = $this
->createMock('\\Drupal\\Core\\Access\\AccessResultInterface');
$unused_access_result_due_to_lazy_evaluation
->expects($this
->never())
->method($this
->anything());
$access = $allowed
->orIf($allowed);
$this
->assertTrue($access
->isAllowed());
$this
->assertFalse($access
->isForbidden());
$this
->assertFalse($access
->isNeutral());
$this
->assertDefaultCacheability($access);
$access = $allowed
->orIf($neutral);
$this
->assertTrue($access
->isAllowed());
$this
->assertFalse($access
->isForbidden());
$this
->assertFalse($access
->isNeutral());
$this
->assertDefaultCacheability($access);
$access = $allowed
->orIf($forbidden);
$this
->assertFalse($access
->isAllowed());
$this
->assertTrue($access
->isForbidden());
$this
->assertFalse($access
->isNeutral());
$this
->assertEquals('forbidden message', $access
->getReason());
$this
->assertDefaultCacheability($access);
$access = $neutral
->orIf($neutral);
$this
->assertFalse($access
->isAllowed());
$this
->assertFalse($access
->isForbidden());
$this
->assertTrue($access
->isNeutral());
$this
->assertEquals('neutral message', $access
->getReason());
$this
->assertDefaultCacheability($access);
$access = $neutral
->orIf($neutral_other);
$this
->assertEquals('neutral message', $access
->getReason());
$access = $neutral_other
->orIf($neutral);
$this
->assertEquals('other neutral message', $access
->getReason());
$access = $neutral
->orIf($neutral_reasonless);
$this
->assertEquals('neutral message', $access
->getReason());
$access = $neutral_reasonless
->orIf($neutral);
$this
->assertEquals('neutral message', $access
->getReason());
$access = $neutral_reasonless
->orIf($neutral_reasonless);
$this
->assertEquals('', $access
->getReason());
$access = $neutral
->orIf($allowed);
$this
->assertTrue($access
->isAllowed());
$this
->assertFalse($access
->isForbidden());
$this
->assertFalse($access
->isNeutral());
$this
->assertDefaultCacheability($access);
$access = $neutral
->orIf($forbidden);
$this
->assertFalse($access
->isAllowed());
$this
->assertTrue($access
->isForbidden());
$this
->assertFalse($access
->isNeutral());
$this
->assertEquals('forbidden message', $access
->getReason());
$this
->assertDefaultCacheability($access);
$access = $forbidden
->orIf($allowed);
$this
->assertFalse($access
->isAllowed());
$this
->assertTrue($access
->isForbidden());
$this
->assertFalse($access
->isNeutral());
$this
->assertEquals('forbidden message', $access
->getReason());
$this
->assertDefaultCacheability($access);
$access = $forbidden
->orIf($neutral);
$this
->assertFalse($access
->isAllowed());
$this
->assertTrue($access
->isForbidden());
$this
->assertFalse($access
->isNeutral());
$this
->assertEquals('forbidden message', $access
->getReason());
$this
->assertDefaultCacheability($access);
$access = $forbidden
->orIf($forbidden);
$this
->assertFalse($access
->isAllowed());
$this
->assertTrue($access
->isForbidden());
$this
->assertFalse($access
->isNeutral());
$this
->assertEquals('forbidden message', $access
->getReason());
$this
->assertDefaultCacheability($access);
$access = $forbidden
->orIf($forbidden_other);
$this
->assertEquals('forbidden message', $access
->getReason());
$access = $forbidden_other
->orIf($forbidden);
$this
->assertEquals('other forbidden message', $access
->getReason());
$access = $forbidden
->orIf($forbidden_reasonless);
$this
->assertEquals('forbidden message', $access
->getReason());
$access = $forbidden_reasonless
->orIf($forbidden);
$this
->assertEquals('forbidden message', $access
->getReason());
$access = $forbidden_reasonless
->orIf($forbidden_reasonless);
$this
->assertEquals('', $access
->getReason());
$access = $forbidden
->orIf($unused_access_result_due_to_lazy_evaluation);
$this
->assertFalse($access
->isAllowed());
$this
->assertTrue($access
->isForbidden());
$this
->assertFalse($access
->isNeutral());
$this
->assertEquals('forbidden message', $access
->getReason());
$this
->assertDefaultCacheability($access);
}
public function testCacheMaxAge() {
$this
->assertSame(Cache::PERMANENT, AccessResult::neutral()
->getCacheMaxAge());
$this
->assertSame(1337, AccessResult::neutral()
->setCacheMaxAge(1337)
->getCacheMaxAge());
}
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',
]);
$access
->resetCacheContexts();
$verify($access, []);
$access
->addCacheContexts([
'foo',
])
->addCacheContexts([
'foo',
]);
$verify($access, [
'foo',
]);
$access
->resetCacheContexts()
->addCacheContexts([
'foo',
])
->addCacheContexts([
'bar',
]);
$verify($access, [
'bar',
'foo',
]);
$access
->resetCacheContexts()
->addCacheContexts([
'bar',
])
->addCacheContexts([
'foo',
]);
$verify($access, [
'bar',
'foo',
]);
$contexts = [
'user.permissions',
];
$a = AccessResult::neutral()
->addCacheContexts($contexts);
$verify($a, $contexts);
$b = AccessResult::neutral()
->cachePerPermissions();
$verify($b, $contexts);
$this
->assertEquals($a, $b);
$contexts = [
'user',
];
$a = AccessResult::neutral()
->addCacheContexts($contexts);
$verify($a, $contexts);
$b = AccessResult::neutral()
->cachePerUser();
$verify($b, $contexts);
$this
->assertEquals($a, $b);
$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);
$account = $this
->createMock('\\Drupal\\Core\\Session\\AccountInterface');
$account
->expects($this
->any())
->method('hasPermission')
->with('may herd llamas')
->will($this
->returnValue(FALSE));
$contexts = [
'user.permissions',
];
$b = AccessResult::allowedIfHasPermission($account, 'may herd llamas');
$verify($b, $contexts);
}
public function testCacheTags() {
$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',
]);
$access
->resetCacheTags();
$verify($access, []);
$access
->addCacheTags([
'foo:bar',
])
->addCacheTags([
'foo:bar',
]);
$verify($access, [
'foo:bar',
]);
$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',
]);
$node = $this
->createMock('\\Drupal\\node\\NodeInterface');
$node
->expects($this
->any())
->method('getCacheTags')
->will($this
->returnValue([
'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);
$non_cacheable_dependency = new \stdClass();
$non_cacheable = AccessResult::neutral()
->addCacheableDependency($non_cacheable_dependency);
$verify($non_cacheable, [], [], 0);
}
public function testInheritCacheability() {
$access = AccessResult::allowed();
$other = AccessResult::allowed()
->setCacheMaxAge(1500)
->cachePerPermissions()
->addCacheTags([
'node:20011988',
]);
$this
->assertInstanceOf(AccessResult::class, $access
->inheritCacheability($other));
$this
->assertSame([
'user.permissions',
], $access
->getCacheContexts());
$this
->assertSame([
'node:20011988',
], $access
->getCacheTags());
$this
->assertSame(1500, $access
->getCacheMaxAge());
$access = AccessResult::allowed()
->cachePerUser()
->setCacheMaxAge(43200);
$other = AccessResult::forbidden()
->addCacheTags([
'node:14031991',
])
->setCacheMaxAge(86400);
$this
->assertInstanceOf(AccessResult::class, $access
->inheritCacheability($other));
$this
->assertSame([
'user',
], $access
->getCacheContexts());
$this
->assertSame([
'node:14031991',
], $access
->getCacheTags());
$this
->assertSame(43200, $access
->getCacheMaxAge());
}
public function andOrCacheabilityPropagationProvider() {
$allowed_ct = AccessResult::allowed();
$allowed_cf = AccessResult::allowed()
->setCacheMaxAge(0);
$allowed_un = new UncacheableTestAccessResult('ALLOWED');
$forbidden_ct = AccessResult::forbidden();
$forbidden_cf = AccessResult::forbidden()
->setCacheMaxAge(0);
$forbidden_un = new UncacheableTestAccessResult('FORBIDDEN');
$neutral_ct = AccessResult::neutral();
$neutral_cf = AccessResult::neutral()
->setCacheMaxAge(0);
$neutral_un = new UncacheableTestAccessResult('NEUTRAL');
return [
[
$allowed_ct,
'OR',
$allowed_ct,
TRUE,
TRUE,
],
[
$allowed_ct,
'OR',
$allowed_cf,
TRUE,
TRUE,
],
[
$allowed_ct,
'OR',
$allowed_un,
TRUE,
TRUE,
],
[
$allowed_cf,
'OR',
$allowed_ct,
TRUE,
TRUE,
],
[
$allowed_cf,
'OR',
$allowed_cf,
TRUE,
FALSE,
],
[
$allowed_cf,
'OR',
$allowed_un,
TRUE,
FALSE,
],
[
$allowed_un,
'OR',
$allowed_ct,
FALSE,
NULL,
],
[
$allowed_un,
'OR',
$allowed_cf,
FALSE,
NULL,
],
[
$allowed_un,
'OR',
$allowed_un,
FALSE,
NULL,
],
[
$allowed_ct,
'OR',
$forbidden_ct,
TRUE,
TRUE,
],
[
$allowed_ct,
'OR',
$forbidden_cf,
TRUE,
FALSE,
],
[
$allowed_ct,
'OR',
$forbidden_un,
TRUE,
FALSE,
],
[
$allowed_cf,
'OR',
$forbidden_ct,
TRUE,
TRUE,
],
[
$allowed_cf,
'OR',
$forbidden_cf,
TRUE,
FALSE,
],
[
$allowed_cf,
'OR',
$forbidden_un,
TRUE,
FALSE,
],
[
$allowed_un,
'OR',
$forbidden_ct,
FALSE,
NULL,
],
[
$allowed_un,
'OR',
$forbidden_cf,
FALSE,
NULL,
],
[
$allowed_un,
'OR',
$forbidden_un,
FALSE,
NULL,
],
[
$allowed_ct,
'OR',
$neutral_ct,
TRUE,
TRUE,
],
[
$allowed_ct,
'OR',
$neutral_cf,
TRUE,
TRUE,
],
[
$allowed_ct,
'OR',
$neutral_un,
TRUE,
TRUE,
],
[
$allowed_cf,
'OR',
$neutral_ct,
TRUE,
FALSE,
],
[
$allowed_cf,
'OR',
$neutral_cf,
TRUE,
FALSE,
],
[
$allowed_cf,
'OR',
$neutral_un,
TRUE,
FALSE,
],
[
$allowed_un,
'OR',
$neutral_ct,
FALSE,
NULL,
],
[
$allowed_un,
'OR',
$neutral_cf,
FALSE,
NULL,
],
[
$allowed_un,
'OR',
$neutral_un,
FALSE,
NULL,
],
[
$forbidden_ct,
'OR',
$allowed_ct,
TRUE,
TRUE,
],
[
$forbidden_ct,
'OR',
$allowed_cf,
TRUE,
TRUE,
],
[
$forbidden_ct,
'OR',
$allowed_un,
TRUE,
TRUE,
],
[
$forbidden_cf,
'OR',
$allowed_ct,
TRUE,
FALSE,
],
[
$forbidden_cf,
'OR',
$allowed_cf,
TRUE,
FALSE,
],
[
$forbidden_cf,
'OR',
$allowed_un,
TRUE,
FALSE,
],
[
$forbidden_un,
'OR',
$allowed_ct,
FALSE,
NULL,
],
[
$forbidden_un,
'OR',
$allowed_cf,
FALSE,
NULL,
],
[
$forbidden_un,
'OR',
$allowed_un,
FALSE,
NULL,
],
[
$forbidden_ct,
'OR',
$neutral_ct,
TRUE,
TRUE,
],
[
$forbidden_ct,
'OR',
$neutral_cf,
TRUE,
TRUE,
],
[
$forbidden_ct,
'OR',
$neutral_un,
TRUE,
TRUE,
],
[
$forbidden_cf,
'OR',
$neutral_ct,
TRUE,
FALSE,
],
[
$forbidden_cf,
'OR',
$neutral_cf,
TRUE,
FALSE,
],
[
$forbidden_cf,
'OR',
$neutral_un,
TRUE,
FALSE,
],
[
$forbidden_un,
'OR',
$neutral_ct,
FALSE,
NULL,
],
[
$forbidden_un,
'OR',
$neutral_cf,
FALSE,
NULL,
],
[
$forbidden_un,
'OR',
$neutral_un,
FALSE,
NULL,
],
[
$forbidden_ct,
'OR',
$forbidden_ct,
TRUE,
TRUE,
],
[
$forbidden_ct,
'OR',
$forbidden_cf,
TRUE,
TRUE,
],
[
$forbidden_ct,
'OR',
$forbidden_un,
TRUE,
TRUE,
],
[
$forbidden_cf,
'OR',
$forbidden_ct,
TRUE,
TRUE,
],
[
$forbidden_cf,
'OR',
$forbidden_cf,
TRUE,
FALSE,
],
[
$forbidden_cf,
'OR',
$forbidden_un,
TRUE,
FALSE,
],
[
$forbidden_un,
'OR',
$forbidden_ct,
FALSE,
NULL,
],
[
$forbidden_un,
'OR',
$forbidden_cf,
FALSE,
NULL,
],
[
$forbidden_un,
'OR',
$forbidden_un,
FALSE,
NULL,
],
[
$neutral_ct,
'OR',
$allowed_ct,
TRUE,
TRUE,
],
[
$neutral_ct,
'OR',
$allowed_cf,
TRUE,
FALSE,
],
[
$neutral_ct,
'OR',
$allowed_un,
TRUE,
FALSE,
],
[
$neutral_cf,
'OR',
$allowed_ct,
TRUE,
TRUE,
],
[
$neutral_cf,
'OR',
$allowed_cf,
TRUE,
FALSE,
],
[
$neutral_cf,
'OR',
$allowed_un,
TRUE,
FALSE,
],
[
$neutral_un,
'OR',
$allowed_ct,
FALSE,
NULL,
],
[
$neutral_un,
'OR',
$allowed_cf,
FALSE,
NULL,
],
[
$neutral_un,
'OR',
$allowed_un,
FALSE,
NULL,
],
[
$neutral_ct,
'OR',
$neutral_ct,
TRUE,
TRUE,
],
[
$neutral_ct,
'OR',
$neutral_cf,
TRUE,
TRUE,
],
[
$neutral_ct,
'OR',
$neutral_un,
TRUE,
TRUE,
],
[
$neutral_cf,
'OR',
$neutral_ct,
TRUE,
TRUE,
],
[
$neutral_cf,
'OR',
$neutral_cf,
TRUE,
FALSE,
],
[
$neutral_cf,
'OR',
$neutral_un,
TRUE,
FALSE,
],
[
$neutral_un,
'OR',
$neutral_ct,
FALSE,
NULL,
],
[
$neutral_un,
'OR',
$neutral_cf,
FALSE,
NULL,
],
[
$neutral_un,
'OR',
$neutral_un,
FALSE,
NULL,
],
[
$neutral_ct,
'OR',
$forbidden_ct,
TRUE,
TRUE,
],
[
$neutral_ct,
'OR',
$forbidden_cf,
TRUE,
FALSE,
],
[
$neutral_ct,
'OR',
$forbidden_un,
TRUE,
FALSE,
],
[
$neutral_cf,
'OR',
$forbidden_ct,
TRUE,
TRUE,
],
[
$neutral_cf,
'OR',
$forbidden_cf,
TRUE,
FALSE,
],
[
$neutral_cf,
'OR',
$forbidden_un,
TRUE,
FALSE,
],
[
$neutral_un,
'OR',
$forbidden_ct,
FALSE,
NULL,
],
[
$neutral_un,
'OR',
$forbidden_cf,
FALSE,
NULL,
],
[
$neutral_un,
'OR',
$forbidden_un,
FALSE,
NULL,
],
[
$allowed_ct,
'AND',
$allowed_ct,
TRUE,
TRUE,
],
[
$allowed_ct,
'AND',
$allowed_cf,
TRUE,
FALSE,
],
[
$allowed_ct,
'AND',
$allowed_un,
TRUE,
FALSE,
],
[
$allowed_cf,
'AND',
$allowed_ct,
TRUE,
FALSE,
],
[
$allowed_cf,
'AND',
$allowed_cf,
TRUE,
FALSE,
],
[
$allowed_cf,
'AND',
$allowed_un,
TRUE,
FALSE,
],
[
$allowed_un,
'AND',
$allowed_ct,
FALSE,
NULL,
],
[
$allowed_un,
'AND',
$allowed_cf,
FALSE,
NULL,
],
[
$allowed_un,
'AND',
$allowed_un,
FALSE,
NULL,
],
[
$allowed_ct,
'AND',
$forbidden_ct,
TRUE,
TRUE,
],
[
$allowed_ct,
'AND',
$forbidden_cf,
TRUE,
FALSE,
],
[
$allowed_ct,
'AND',
$forbidden_un,
TRUE,
FALSE,
],
[
$allowed_cf,
'AND',
$forbidden_ct,
TRUE,
TRUE,
],
[
$allowed_cf,
'AND',
$forbidden_cf,
TRUE,
FALSE,
],
[
$allowed_cf,
'AND',
$forbidden_un,
TRUE,
FALSE,
],
[
$allowed_un,
'AND',
$forbidden_ct,
FALSE,
NULL,
],
[
$allowed_un,
'AND',
$forbidden_cf,
FALSE,
NULL,
],
[
$allowed_un,
'AND',
$forbidden_un,
FALSE,
NULL,
],
[
$allowed_ct,
'AND',
$neutral_ct,
TRUE,
TRUE,
],
[
$allowed_ct,
'AND',
$neutral_cf,
TRUE,
FALSE,
],
[
$allowed_ct,
'AND',
$neutral_un,
TRUE,
FALSE,
],
[
$allowed_cf,
'AND',
$neutral_ct,
TRUE,
FALSE,
],
[
$allowed_cf,
'AND',
$neutral_cf,
TRUE,
FALSE,
],
[
$allowed_cf,
'AND',
$neutral_un,
TRUE,
FALSE,
],
[
$allowed_un,
'AND',
$neutral_ct,
FALSE,
NULL,
],
[
$allowed_un,
'AND',
$neutral_cf,
FALSE,
NULL,
],
[
$allowed_un,
'AND',
$neutral_un,
FALSE,
NULL,
],
[
$forbidden_ct,
'AND',
$allowed_ct,
TRUE,
TRUE,
],
[
$forbidden_ct,
'AND',
$allowed_cf,
TRUE,
TRUE,
],
[
$forbidden_ct,
'AND',
$allowed_un,
TRUE,
TRUE,
],
[
$forbidden_cf,
'AND',
$allowed_ct,
TRUE,
FALSE,
],
[
$forbidden_cf,
'AND',
$allowed_cf,
TRUE,
FALSE,
],
[
$forbidden_cf,
'AND',
$allowed_un,
TRUE,
FALSE,
],
[
$forbidden_un,
'AND',
$allowed_ct,
FALSE,
NULL,
],
[
$forbidden_un,
'AND',
$allowed_cf,
FALSE,
NULL,
],
[
$forbidden_un,
'AND',
$allowed_un,
FALSE,
NULL,
],
[
$forbidden_ct,
'AND',
$neutral_ct,
TRUE,
TRUE,
],
[
$forbidden_ct,
'AND',
$neutral_cf,
TRUE,
TRUE,
],
[
$forbidden_ct,
'AND',
$neutral_un,
TRUE,
TRUE,
],
[
$forbidden_cf,
'AND',
$neutral_ct,
TRUE,
FALSE,
],
[
$forbidden_cf,
'AND',
$neutral_cf,
TRUE,
FALSE,
],
[
$forbidden_cf,
'AND',
$neutral_un,
TRUE,
FALSE,
],
[
$forbidden_un,
'AND',
$neutral_ct,
FALSE,
NULL,
],
[
$forbidden_un,
'AND',
$neutral_cf,
FALSE,
NULL,
],
[
$forbidden_un,
'AND',
$neutral_un,
FALSE,
NULL,
],
[
$forbidden_ct,
'AND',
$forbidden_ct,
TRUE,
TRUE,
],
[
$forbidden_ct,
'AND',
$forbidden_cf,
TRUE,
TRUE,
],
[
$forbidden_ct,
'AND',
$forbidden_un,
TRUE,
TRUE,
],
[
$forbidden_cf,
'AND',
$forbidden_ct,
TRUE,
FALSE,
],
[
$forbidden_cf,
'AND',
$forbidden_cf,
TRUE,
FALSE,
],
[
$forbidden_cf,
'AND',
$forbidden_un,
TRUE,
FALSE,
],
[
$forbidden_un,
'AND',
$forbidden_ct,
FALSE,
NULL,
],
[
$forbidden_un,
'AND',
$forbidden_cf,
FALSE,
NULL,
],
[
$forbidden_un,
'AND',
$forbidden_un,
FALSE,
NULL,
],
[
$neutral_ct,
'AND',
$allowed_ct,
TRUE,
TRUE,
],
[
$neutral_ct,
'AND',
$allowed_cf,
TRUE,
TRUE,
],
[
$neutral_ct,
'AND',
$allowed_un,
TRUE,
TRUE,
],
[
$neutral_cf,
'AND',
$allowed_ct,
TRUE,
FALSE,
],
[
$neutral_cf,
'AND',
$allowed_cf,
TRUE,
FALSE,
],
[
$neutral_cf,
'AND',
$allowed_un,
TRUE,
FALSE,
],
[
$neutral_un,
'AND',
$allowed_ct,
FALSE,
NULL,
],
[
$neutral_un,
'AND',
$allowed_cf,
FALSE,
NULL,
],
[
$neutral_un,
'AND',
$allowed_un,
FALSE,
NULL,
],
[
$neutral_ct,
'AND',
$neutral_ct,
TRUE,
TRUE,
],
[
$neutral_ct,
'AND',
$neutral_cf,
TRUE,
TRUE,
],
[
$neutral_ct,
'AND',
$neutral_un,
TRUE,
TRUE,
],
[
$neutral_cf,
'AND',
$neutral_ct,
TRUE,
FALSE,
],
[
$neutral_cf,
'AND',
$neutral_cf,
TRUE,
FALSE,
],
[
$neutral_cf,
'AND',
$neutral_un,
TRUE,
FALSE,
],
[
$neutral_un,
'AND',
$neutral_ct,
FALSE,
NULL,
],
[
$neutral_un,
'AND',
$neutral_cf,
FALSE,
NULL,
],
[
$neutral_un,
'AND',
$neutral_un,
FALSE,
NULL,
],
[
$neutral_ct,
'AND',
$forbidden_ct,
TRUE,
TRUE,
],
[
$neutral_ct,
'AND',
$forbidden_cf,
TRUE,
FALSE,
],
[
$neutral_ct,
'AND',
$forbidden_un,
TRUE,
FALSE,
],
[
$neutral_cf,
'AND',
$forbidden_ct,
TRUE,
TRUE,
],
[
$neutral_cf,
'AND',
$forbidden_cf,
TRUE,
FALSE,
],
[
$neutral_cf,
'AND',
$forbidden_un,
TRUE,
FALSE,
],
[
$neutral_un,
'AND',
$forbidden_ct,
FALSE,
NULL,
],
[
$neutral_un,
'AND',
$forbidden_cf,
FALSE,
NULL,
],
[
$neutral_un,
'AND',
$forbidden_un,
FALSE,
NULL,
],
];
}
public function testAndOrCacheabilityPropagation(AccessResultInterface $first, $op, AccessResultInterface $second, $implements_cacheable_dependency_interface, $is_cacheable) {
if ($op === 'OR') {
$result = $first
->orIf($second);
}
elseif ($op === 'AND') {
$result = $first
->andIf($second);
}
else {
throw new \LogicException('Invalid operator specified');
}
if ($implements_cacheable_dependency_interface) {
$this
->assertInstanceOf(CacheableDependencyInterface::class, $result);
if ($result instanceof CacheableDependencyInterface) {
$this
->assertSame($is_cacheable, $result
->getCacheMaxAge() !== 0, 'getCacheMaxAge() matches expectations.');
}
}
else {
$this
->assertNotInstanceOf(CacheableDependencyInterface::class, $result);
}
}
public function testOrIfCacheabilityMerging() {
$merge_both_directions = function (AccessResult $a, AccessResult $b) {
$a
->setCacheMaxAge(3600);
$b
->setCacheMaxAge(86400)
->cachePerPermissions();
$r1 = $a
->orIf($b);
$this
->assertSame(3600, $r1
->getCacheMaxAge());
$this
->assertSame([
'user.permissions',
], $r1
->getCacheContexts());
$r2 = $b
->orIf($a);
$this
->assertSame(3600, $r2
->getCacheMaxAge());
$this
->assertSame([
'user.permissions',
], $r2
->getCacheContexts());
};
$merge_both_directions(AccessResult::allowed(), AccessResult::allowed());
$merge_both_directions(AccessResult::allowed(), AccessResult::neutral());
$merge_both_directions(AccessResult::neutral(), AccessResult::neutral());
$merge_both_directions(AccessResult::neutral(), AccessResult::allowed());
}
public function testAllowedIfHasPermissions($permissions, $conjunction, AccessResult $expected_access) {
$account = $this
->createMock('\\Drupal\\Core\\Session\\AccountInterface');
$account
->expects($this
->any())
->method('hasPermission')
->willReturnMap([
[
'allowed',
TRUE,
],
[
'denied',
FALSE,
],
]);
if ($permissions) {
$expected_access
->cachePerPermissions();
}
$access_result = AccessResult::allowedIfHasPermissions($account, $permissions, $conjunction);
$this
->assertEquals($expected_access, $access_result);
}
public function providerTestAllowedIfHasPermissions() {
$access_result = AccessResult::allowedIf(FALSE);
$data[] = [
[],
'AND',
$access_result,
];
$data[] = [
[],
'OR',
$access_result,
];
$access_result = AccessResult::allowedIf(TRUE);
$data[] = [
[
'allowed',
],
'OR',
$access_result,
];
$data[] = [
[
'allowed',
],
'AND',
$access_result,
];
$access_result = AccessResult::allowedIf(FALSE);
$access_result
->setReason("The 'denied' permission is required.");
$data[] = [
[
'denied',
],
'OR',
$access_result,
];
$data[] = [
[
'denied',
],
'AND',
$access_result,
];
$access_result = AccessResult::allowedIf(TRUE);
$data[] = [
[
'allowed',
'denied',
],
'OR',
$access_result,
];
$data[] = [
[
'denied',
'allowed',
],
'OR',
$access_result,
];
$access_result = AccessResult::allowedIf(TRUE);
$data[] = [
[
'allowed',
'denied',
'other',
],
'OR',
$access_result,
];
$access_result = AccessResult::allowedIf(FALSE);
$access_result
->setReason("The following permissions are required: 'allowed' AND 'denied'.");
$data[] = [
[
'allowed',
'denied',
],
'AND',
$access_result,
];
return $data;
}
}
class UncacheableTestAccessResult implements AccessResultInterface {
protected $value;
public function __construct($value) {
$this->value = $value;
}
public function isAllowed() {
return $this->value === 'ALLOWED';
}
public function isForbidden() {
return $this->value === 'FORBIDDEN';
}
public function isNeutral() {
return $this->value === 'NEUTRAL';
}
public function orIf(AccessResultInterface $other) {
if ($this
->isForbidden() || $other
->isForbidden()) {
return new static('FORBIDDEN');
}
elseif ($this
->isAllowed() || $other
->isAllowed()) {
return new static('ALLOWED');
}
else {
return new static('NEUTRAL');
}
}
public function andIf(AccessResultInterface $other) {
if ($this
->isForbidden() || $other
->isForbidden()) {
return new static('FORBIDDEN');
}
elseif ($this
->isAllowed() && $other
->isAllowed()) {
return new static('ALLOWED');
}
else {
return new static('NEUTRAL');
}
}
}