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