More updates to stop using dev or alpha or beta versions.
[yaffs-website] / web / core / tests / Drupal / Tests / Core / PageCache / ChainRequestPolicyTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\PageCache;
4
5 use Drupal\Core\PageCache\RequestPolicyInterface;
6 use Drupal\Core\PageCache\ChainRequestPolicy;
7 use Drupal\Tests\UnitTestCase;
8 use Symfony\Component\HttpFoundation\Request;
9
10 /**
11  * @coversDefaultClass \Drupal\Core\PageCache\ChainRequestPolicy
12  * @group PageCache
13  */
14 class ChainRequestPolicyTest extends UnitTestCase {
15
16   /**
17    * The chain request policy under test.
18    *
19    * @var \Drupal\Core\PageCache\ChainRequestPolicy
20    */
21   protected $policy;
22
23   /**
24    * A request object.
25    *
26    * @var \Symfony\Component\HttpFoundation\Request
27    */
28   protected $request;
29
30   protected function setUp() {
31     $this->policy = new ChainRequestPolicy();
32     $this->request = new Request();
33   }
34
35   /**
36    * Asserts that check() returns NULL if the chain is empty.
37    *
38    * @covers ::check
39    */
40   public function testEmptyChain() {
41     $result = $this->policy->check($this->request);
42     $this->assertSame(NULL, $result);
43   }
44
45   /**
46    * Asserts that check() returns NULL if a rule returns NULL.
47    *
48    * @covers ::check
49    */
50   public function testNullRuleChain() {
51     $rule = $this->getMock('Drupal\Core\PageCache\RequestPolicyInterface');
52     $rule->expects($this->once())
53       ->method('check')
54       ->with($this->request)
55       ->will($this->returnValue(NULL));
56
57     $this->policy->addPolicy($rule);
58
59     $result = $this->policy->check($this->request);
60     $this->assertSame(NULL, $result);
61   }
62
63   /**
64    * Asserts that check() throws an exception if a rule returns an invalid value.
65    *
66    * @dataProvider providerChainExceptionOnInvalidReturnValue
67    * @covers ::check
68    */
69   public function testChainExceptionOnInvalidReturnValue($return_value) {
70     $rule = $this->getMock('Drupal\Core\PageCache\RequestPolicyInterface');
71     $rule->expects($this->once())
72       ->method('check')
73       ->with($this->request)
74       ->will($this->returnValue($return_value));
75
76     $this->policy->addPolicy($rule);
77
78     $this->setExpectedException(\UnexpectedValueException::class);
79     $this->policy->check($this->request);
80   }
81
82   /**
83    * Provides test data for testChainExceptionOnInvalidReturnValue.
84    *
85    * @return array
86    *   Test input and expected result.
87    */
88   public function providerChainExceptionOnInvalidReturnValue() {
89     return [
90       [FALSE],
91       [0],
92       [1],
93       [TRUE],
94       [[1, 2, 3]],
95       [new \stdClass()],
96     ];
97   }
98
99   /**
100    * Asserts that check() returns ALLOW if any of the rules returns ALLOW.
101    *
102    * @dataProvider providerAllowIfAnyRuleReturnedAllow
103    * @covers ::check
104    */
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())
109         ->method('check')
110         ->with($this->request)
111         ->will($this->returnValue($return_value));
112
113       $this->policy->addPolicy($rule);
114     }
115
116     $actual_result = $this->policy->check($this->request);
117     $this->assertSame(RequestPolicyInterface::ALLOW, $actual_result);
118   }
119
120   /**
121    * Provides test data for testAllowIfAnyRuleReturnedAllow.
122    *
123    * @return array
124    *   Test input and expected result.
125    */
126   public function providerAllowIfAnyRuleReturnedAllow() {
127     return [
128       [[RequestPolicyInterface::ALLOW]],
129       [[NULL, RequestPolicyInterface::ALLOW]],
130     ];
131   }
132
133   /**
134    * Asserts that check() returns immediately when a rule returned DENY.
135    */
136   public function testStopChainOnFirstDeny() {
137     $rule1 = $this->getMock('Drupal\Core\PageCache\RequestPolicyInterface');
138     $rule1->expects($this->once())
139       ->method('check')
140       ->with($this->request)
141       ->will($this->returnValue(RequestPolicyInterface::ALLOW));
142     $this->policy->addPolicy($rule1);
143
144     $deny_rule = $this->getMock('Drupal\Core\PageCache\RequestPolicyInterface');
145     $deny_rule->expects($this->once())
146       ->method('check')
147       ->with($this->request)
148       ->will($this->returnValue(RequestPolicyInterface::DENY));
149     $this->policy->addPolicy($deny_rule);
150
151     $ignored_rule = $this->getMock('Drupal\Core\PageCache\RequestPolicyInterface');
152     $ignored_rule->expects($this->never())
153       ->method('check');
154     $this->policy->addPolicy($ignored_rule);
155
156     $actual_result = $this->policy->check($this->request);
157     $this->assertsame(RequestPolicyInterface::DENY, $actual_result);
158   }
159
160 }