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\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() {
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) {
$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 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();
$allowed = AccessResult::allowed();
$forbidden = AccessResult::forbidden();
$unused_access_result_due_to_lazy_evaluation = $this
->getMock('\\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
->assertDefaultCacheability($access);
$access = $allowed
->andIf($forbidden);
$this
->assertFalse($access
->isAllowed());
$this
->assertTrue($access
->isForbidden());
$this
->assertFalse($access
->isNeutral());
$this
->assertDefaultCacheability($access);
$access = $neutral
->andIf($allowed);
$this
->assertFalse($access
->isAllowed());
$this
->assertFalse($access
->isForbidden());
$this
->assertTrue($access
->isNeutral());
$this
->assertDefaultCacheability($access);
$access = $neutral
->andIf($neutral);
$this
->assertFalse($access
->isAllowed());
$this
->assertFalse($access
->isForbidden());
$this
->assertTrue($access
->isNeutral());
$this
->assertDefaultCacheability($access);
$access = $neutral
->andIf($forbidden);
$this
->assertFalse($access
->isAllowed());
$this
->assertTrue($access
->isForbidden());
$this
->assertFalse($access
->isNeutral());
$this
->assertDefaultCacheability($access);
$access = $forbidden
->andif($allowed);
$this
->assertFalse($access
->isAllowed());
$this
->assertTrue($access
->isForbidden());
$this
->assertFalse($access
->isNeutral());
$this
->assertDefaultCacheability($access);
$access = $forbidden
->andif($neutral);
$this
->assertFalse($access
->isAllowed());
$this
->assertTrue($access
->isForbidden());
$this
->assertFalse($access
->isNeutral());
$this
->assertDefaultCacheability($access);
$access = $forbidden
->andif($forbidden);
$this
->assertFalse($access
->isAllowed());
$this
->assertTrue($access
->isForbidden());
$this
->assertFalse($access
->isNeutral());
$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
->assertDefaultCacheability($access);
}
public function testOrIf() {
$neutral = AccessResult::neutral();
$allowed = AccessResult::allowed();
$forbidden = AccessResult::forbidden();
$unused_access_result_due_to_lazy_evaluation = $this
->getMock('\\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
->assertDefaultCacheability($access);
$access = $neutral
->orIf($neutral);
$this
->assertFalse($access
->isAllowed());
$this
->assertFalse($access
->isForbidden());
$this
->assertTrue($access
->isNeutral());
$this
->assertDefaultCacheability($access);
$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
->assertDefaultCacheability($access);
$access = $forbidden
->orIf($allowed);
$this
->assertFalse($access
->isAllowed());
$this
->assertTrue($access
->isForbidden());
$this
->assertFalse($access
->isNeutral());
$this
->assertDefaultCacheability($access);
$access = $forbidden
->orIf($allowed);
$this
->assertFalse($access
->isAllowed());
$this
->assertTrue($access
->isForbidden());
$this
->assertFalse($access
->isNeutral());
$this
->assertDefaultCacheability($access);
$access = $forbidden
->orIf($allowed);
$this
->assertFalse($access
->isAllowed());
$this
->assertTrue($access
->isForbidden());
$this
->assertFalse($access
->isNeutral());
$this
->assertDefaultCacheability($access);
$access = $forbidden
->orIf($unused_access_result_due_to_lazy_evaluation);
$this
->assertFalse($access
->isAllowed());
$this
->assertTrue($access
->isForbidden());
$this
->assertFalse($access
->isNeutral());
$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
->assertSame($contexts, $access
->getCacheContexts());
$this
->assertSame([], $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 = array(
'user.permissions',
);
$a = AccessResult::neutral()
->addCacheContexts($contexts);
$verify($a, $contexts);
$b = AccessResult::neutral()
->cachePerPermissions();
$verify($b, $contexts);
$this
->assertEquals($a, $b);
$contexts = array(
'user',
);
$a = AccessResult::neutral()
->addCacheContexts($contexts);
$verify($a, $contexts);
$b = AccessResult::neutral()
->cachePerUser();
$verify($b, $contexts);
$this
->assertEquals($a, $b);
$contexts = array(
'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
->assertEquals($a, $b);
$this
->assertEquals($a, $c);
$account = $this
->getMock('\\Drupal\\Core\\Session\\AccountInterface');
$account
->expects($this
->any())
->method('hasPermission')
->with('may herd llamas')
->will($this
->returnValue(FALSE));
$contexts = array(
'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
->assertSame($contexts, $access
->getCacheContexts());
$this
->assertSame($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
->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()
->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
->assertTrue($access
->inheritCacheability($other) instanceof AccessResult);
$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
->assertTrue($access
->inheritCacheability($other) instanceof AccessResult);
$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);
}
else {
if ($op === 'AND') {
$result = $first
->andIf($second);
}
else {
throw new \LogicException('Invalid operator specified');
}
}
if ($implements_cacheable_dependency_interface) {
$this
->assertTrue($result instanceof CacheableDependencyInterface, 'Result is an instance of CacheableDependencyInterface.');
if ($result instanceof CacheableDependencyInterface) {
$this
->assertSame($is_cacheable, $result
->getCacheMaxAge() !== 0, 'getCacheMaxAge() matches expectations.');
}
}
else {
$this
->assertFalse($result instanceof CacheableDependencyInterface, 'Result is not an instance of CacheableDependencyInterface.');
}
}
public function testOrIfCacheabilityMerging() {
$merge_both_directions = function (AccessResult $a, AccessResult $b) {
$a
->setCacheMaxAge(3600);
$b
->setCacheMaxAge(86400)
->cachePerPermissions();
$r1 = $a
->orIf($b);
$this
->assertTrue($r1
->getCacheMaxAge() === 3600);
$this
->assertSame([
'user.permissions',
], $r1
->getCacheContexts());
$r2 = $b
->orIf($a);
$this
->assertTrue($r2
->getCacheMaxAge() === 3600);
$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
->getMock('\\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() {
return [
[
[],
'AND',
AccessResult::allowedIf(FALSE),
],
[
[],
'OR',
AccessResult::allowedIf(FALSE),
],
[
[
'allowed',
],
'OR',
AccessResult::allowedIf(TRUE),
],
[
[
'allowed',
],
'AND',
AccessResult::allowedIf(TRUE),
],
[
[
'denied',
],
'OR',
AccessResult::allowedIf(FALSE),
],
[
[
'denied',
],
'AND',
AccessResult::allowedIf(FALSE),
],
[
[
'allowed',
'denied',
],
'OR',
AccessResult::allowedIf(TRUE),
],
[
[
'denied',
'allowed',
],
'OR',
AccessResult::allowedIf(TRUE),
],
[
[
'allowed',
'denied',
'other',
],
'OR',
AccessResult::allowedIf(TRUE),
],
[
[
'allowed',
'denied',
],
'AND',
AccessResult::allowedIf(FALSE),
],
];
}
}
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');
}
}
}