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