3 namespace Drupal\Tests\Core\PageCache;
5 use Drupal\Core\PageCache\ResponsePolicyInterface;
6 use Drupal\Core\PageCache\ChainResponsePolicy;
7 use Drupal\Tests\UnitTestCase;
8 use Symfony\Component\HttpFoundation\Request;
9 use Symfony\Component\HttpFoundation\Response;
12 * @coversDefaultClass \Drupal\Core\PageCache\ChainResponsePolicy
15 class ChainResponsePolicyTest extends UnitTestCase {
18 * The chain response policy under test.
20 * @var \Drupal\Core\PageCache\ChainResponsePolicy
27 * @var \Symfony\Component\HttpFoundation\Request
34 * @var \Symfony\Component\HttpFoundation\Response
38 protected function setUp() {
39 $this->policy = new ChainResponsePolicy();
40 $this->response = new Response();
41 $this->request = new Request();
45 * Asserts that check() returns NULL if the chain is empty.
49 public function testEmptyChain() {
50 $result = $this->policy->check($this->response, $this->request);
51 $this->assertSame(NULL, $result);
55 * Asserts that check() returns NULL if a rule returns NULL.
59 public function testNullRuleChain() {
60 $rule = $this->getMock('Drupal\Core\PageCache\ResponsePolicyInterface');
61 $rule->expects($this->once())
63 ->with($this->response, $this->request)
64 ->will($this->returnValue(NULL));
66 $this->policy->addPolicy($rule);
68 $result = $this->policy->check($this->response, $this->request);
69 $this->assertSame(NULL, $result);
73 * Asserts that check() throws an exception if a rule returns an invalid value.
75 * @dataProvider providerChainExceptionOnInvalidReturnValue
78 public function testChainExceptionOnInvalidReturnValue($return_value) {
79 $rule = $this->getMock('Drupal\Core\PageCache\ResponsePolicyInterface');
80 $rule->expects($this->once())
82 ->with($this->response, $this->request)
83 ->will($this->returnValue($return_value));
85 $this->policy->addPolicy($rule);
87 $this->setExpectedException(\UnexpectedValueException::class);
88 $this->policy->check($this->response, $this->request);
92 * Provides test data for testChainExceptionOnInvalidReturnValue.
95 * Test input and expected result.
97 public function providerChainExceptionOnInvalidReturnValue() {
109 * Asserts that check() returns immediately when a rule returned DENY.
111 public function testStopChainOnFirstDeny() {
112 $rule1 = $this->getMock('Drupal\Core\PageCache\ResponsePolicyInterface');
113 $rule1->expects($this->once())
115 ->with($this->response, $this->request);
116 $this->policy->addPolicy($rule1);
118 $deny_rule = $this->getMock('Drupal\Core\PageCache\ResponsePolicyInterface');
119 $deny_rule->expects($this->once())
121 ->with($this->response, $this->request)
122 ->will($this->returnValue(ResponsePolicyInterface::DENY));
123 $this->policy->addPolicy($deny_rule);
125 $ignored_rule = $this->getMock('Drupal\Core\PageCache\ResponsePolicyInterface');
126 $ignored_rule->expects($this->never())
128 $this->policy->addPolicy($ignored_rule);
130 $actual_result = $this->policy->check($this->response, $this->request);
131 $this->assertsame(ResponsePolicyInterface::DENY, $actual_result);