View source
<?php
namespace Drupal\Tests\Core\PageCache;
use Drupal\Core\PageCache\RequestPolicyInterface;
use Drupal\Core\PageCache\ChainRequestPolicy;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\Request;
class ChainRequestPolicyTest extends UnitTestCase {
protected $policy;
protected $request;
public function setUp() {
$this->policy = new ChainRequestPolicy();
$this->request = new Request();
}
public function testEmptyChain() {
$result = $this->policy
->check($this->request);
$this
->assertSame(NULL, $result);
}
public function testNullRuleChain() {
$rule = $this
->getMock('Drupal\\Core\\PageCache\\RequestPolicyInterface');
$rule
->expects($this
->once())
->method('check')
->with($this->request)
->will($this
->returnValue(NULL));
$this->policy
->addPolicy($rule);
$result = $this->policy
->check($this->request);
$this
->assertSame(NULL, $result);
}
public function testChainExceptionOnInvalidReturnValue($return_value) {
$rule = $this
->getMock('Drupal\\Core\\PageCache\\RequestPolicyInterface');
$rule
->expects($this
->once())
->method('check')
->with($this->request)
->will($this
->returnValue($return_value));
$this->policy
->addPolicy($rule);
$this->policy
->check($this->request);
}
public function providerChainExceptionOnInvalidReturnValue() {
return [
[
FALSE,
],
[
0,
],
[
1,
],
[
TRUE,
],
[
[
1,
2,
3,
],
],
[
new \stdClass(),
],
];
}
public function testAllowIfAnyRuleReturnedAllow($return_values) {
foreach ($return_values as $return_value) {
$rule = $this
->getMock('Drupal\\Core\\PageCache\\RequestPolicyInterface');
$rule
->expects($this
->once())
->method('check')
->with($this->request)
->will($this
->returnValue($return_value));
$this->policy
->addPolicy($rule);
}
$actual_result = $this->policy
->check($this->request);
$this
->assertSame(RequestPolicyInterface::ALLOW, $actual_result);
}
public function providerAllowIfAnyRuleReturnedAllow() {
return [
[
[
RequestPolicyInterface::ALLOW,
],
],
[
[
NULL,
RequestPolicyInterface::ALLOW,
],
],
];
}
public function testStopChainOnFirstDeny() {
$rule1 = $this
->getMock('Drupal\\Core\\PageCache\\RequestPolicyInterface');
$rule1
->expects($this
->once())
->method('check')
->with($this->request)
->will($this
->returnValue(RequestPolicyInterface::ALLOW));
$this->policy
->addPolicy($rule1);
$deny_rule = $this
->getMock('Drupal\\Core\\PageCache\\RequestPolicyInterface');
$deny_rule
->expects($this
->once())
->method('check')
->with($this->request)
->will($this
->returnValue(RequestPolicyInterface::DENY));
$this->policy
->addPolicy($deny_rule);
$ignored_rule = $this
->getMock('Drupal\\Core\\PageCache\\RequestPolicyInterface');
$ignored_rule
->expects($this
->never())
->method('check');
$this->policy
->addPolicy($ignored_rule);
$actual_result = $this->policy
->check($this->request);
$this
->assertsame(RequestPolicyInterface::DENY, $actual_result);
}
}