3 namespace Drupal\Tests\Core\PageCache;
5 use Drupal\Core\PageCache\RequestPolicyInterface;
6 use Drupal\Core\PageCache\ChainRequestPolicy;
7 use Drupal\Tests\UnitTestCase;
8 use Symfony\Component\HttpFoundation\Request;
11 * @coversDefaultClass \Drupal\Core\PageCache\ChainRequestPolicy
14 class ChainRequestPolicyTest extends UnitTestCase {
17 * The chain request policy under test.
19 * @var \Drupal\Core\PageCache\ChainRequestPolicy
26 * @var \Symfony\Component\HttpFoundation\Request
30 protected function setUp() {
31 $this->policy = new ChainRequestPolicy();
32 $this->request = new Request();
36 * Asserts that check() returns NULL if the chain is empty.
40 public function testEmptyChain() {
41 $result = $this->policy->check($this->request);
42 $this->assertSame(NULL, $result);
46 * Asserts that check() returns NULL if a rule returns NULL.
50 public function testNullRuleChain() {
51 $rule = $this->getMock('Drupal\Core\PageCache\RequestPolicyInterface');
52 $rule->expects($this->once())
54 ->with($this->request)
55 ->will($this->returnValue(NULL));
57 $this->policy->addPolicy($rule);
59 $result = $this->policy->check($this->request);
60 $this->assertSame(NULL, $result);
64 * Asserts that check() throws an exception if a rule returns an invalid value.
66 * @dataProvider providerChainExceptionOnInvalidReturnValue
69 public function testChainExceptionOnInvalidReturnValue($return_value) {
70 $rule = $this->getMock('Drupal\Core\PageCache\RequestPolicyInterface');
71 $rule->expects($this->once())
73 ->with($this->request)
74 ->will($this->returnValue($return_value));
76 $this->policy->addPolicy($rule);
78 $this->setExpectedException(\UnexpectedValueException::class);
79 $this->policy->check($this->request);
83 * Provides test data for testChainExceptionOnInvalidReturnValue.
86 * Test input and expected result.
88 public function providerChainExceptionOnInvalidReturnValue() {
100 * Asserts that check() returns ALLOW if any of the rules returns ALLOW.
102 * @dataProvider providerAllowIfAnyRuleReturnedAllow
105 public function testAllowIfAnyRuleReturnedAllow($return_values) {
106 foreach ($return_values as $return_value) {
107 $rule = $this->getMock('Drupal\Core\PageCache\RequestPolicyInterface');
108 $rule->expects($this->once())
110 ->with($this->request)
111 ->will($this->returnValue($return_value));
113 $this->policy->addPolicy($rule);
116 $actual_result = $this->policy->check($this->request);
117 $this->assertSame(RequestPolicyInterface::ALLOW, $actual_result);
121 * Provides test data for testAllowIfAnyRuleReturnedAllow.
124 * Test input and expected result.
126 public function providerAllowIfAnyRuleReturnedAllow() {
128 [[RequestPolicyInterface::ALLOW]],
129 [[NULL, RequestPolicyInterface::ALLOW]],
134 * Asserts that check() returns immediately when a rule returned DENY.
136 public function testStopChainOnFirstDeny() {
137 $rule1 = $this->getMock('Drupal\Core\PageCache\RequestPolicyInterface');
138 $rule1->expects($this->once())
140 ->with($this->request)
141 ->will($this->returnValue(RequestPolicyInterface::ALLOW));
142 $this->policy->addPolicy($rule1);
144 $deny_rule = $this->getMock('Drupal\Core\PageCache\RequestPolicyInterface');
145 $deny_rule->expects($this->once())
147 ->with($this->request)
148 ->will($this->returnValue(RequestPolicyInterface::DENY));
149 $this->policy->addPolicy($deny_rule);
151 $ignored_rule = $this->getMock('Drupal\Core\PageCache\RequestPolicyInterface');
152 $ignored_rule->expects($this->never())
154 $this->policy->addPolicy($ignored_rule);
156 $actual_result = $this->policy->check($this->request);
157 $this->assertsame(RequestPolicyInterface::DENY, $actual_result);