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;
protected function setUp() : void {
$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
->createMock('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
->createMock('Drupal\\Core\\PageCache\\RequestPolicyInterface');
$rule
->expects($this
->once())
->method('check')
->with($this->request)
->will($this
->returnValue($return_value));
$this->policy
->addPolicy($rule);
$this
->expectException(\UnexpectedValueException::class);
$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
->createMock('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
->createMock('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
->createMock('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
->createMock('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);
}
}