Completed
Push — master ( 5591c4...1f9686 )
by Daniel
37:10 queued 28:40
created

testPublicCacheWithMaxAgeAppliesWhenLevelDoesNot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Control\Tests\Middleware;
4
5
use SilverStripe\Control\HTTPResponse;
6
use SilverStripe\Control\Middleware\HTTPCacheControlMiddleware;
7
use SilverStripe\Dev\SapphireTest;
8
9
class HTTPCacheControlMiddlewareTest extends SapphireTest
10
{
11
    protected function setUp()
12
    {
13
        parent::setUp();
14
        // Set to disabled at null forcing level
15
        HTTPCacheControlMiddleware::config()
16
            ->set('defaultState', HTTPCacheControlMiddleware::STATE_ENABLED)
17
            ->set('defaultForcingLevel', 0);
18
        HTTPCacheControlMiddleware::reset();
19
    }
20
21
    public function provideCacheStates()
22
    {
23
        return [
24
            ['enableCache', false],
25
            ['publicCache', false],
26
            ['privateCache', false],
27
            ['disableCache', true],
28
        ];
29
    }
30
31
    /**
32
     * @dataProvider provideCacheStates
33
     */
34
    public function testCheckDefaultStates($state, $immutable)
35
    {
36
        $cc = HTTPCacheControlMiddleware::singleton();
37
        $cc->{$state}();
38
39
        $response = new HTTPResponse();
40
        $cc->applyToResponse($response);
41
42
        $this->assertContains('must-revalidate', $response->getHeader('cache-control'));
43
    }
44
45
    /**
46
     * @dataProvider provideCacheStates
47
     */
48
    public function testSetMaxAge($state, $immutable)
49
    {
50
        $cc = HTTPCacheControlMiddleware::singleton();
51
        $cc->{$state}();
52
53
        $originalResponse = new HTTPResponse();
54
        $cc->applyToResponse($originalResponse);
55
56
        $cc->setMaxAge('300');
57
58
        $response = new HTTPResponse();
59
60
        $cc->applyToResponse($response);
61
62
        if ($immutable) {
63
            $this->assertEquals($originalResponse->getHeader('cache-control'), $response->getHeader('cache-control'));
64
        } else {
65
            $this->assertContains('max-age=300', $response->getHeader('cache-control'));
66
            $this->assertNotContains('no-cache', $response->getHeader('cache-control'));
67
            $this->assertNotContains('no-store', $response->getHeader('cache-control'));
68
        }
69
    }
70
71
    public function testEnableCacheWithMaxAge()
72
    {
73
        $maxAge = 300;
74
75
        $cc = HTTPCacheControlMiddleware::singleton();
76
        $cc->enableCache(false, $maxAge);
77
78
        $response = new HTTPResponse();
79
        $cc->applyToResponse($response);
80
81
        $this->assertContains('max-age=300', $response->getHeader('cache-control'));
82
        $this->assertNotContains('no-cache', $response->getHeader('cache-control'));
83
        $this->assertNotContains('no-store', $response->getHeader('cache-control'));
84
    }
85
86
    public function testEnableCacheWithMaxAgeAppliesWhenLevelDoesNot()
87
    {
88
        $maxAge = 300;
89
90
        $cc = HTTPCacheControlMiddleware::singleton();
91
        $cc->privateCache(true);
92
        $cc->enableCache(false, $maxAge);
93
94
        $response = new HTTPResponse();
95
        $cc->applyToResponse($response);
96
97
        $this->assertContains('max-age=300', $response->getHeader('cache-control'));
98
    }
99
100
    public function testPublicCacheWithMaxAge()
101
    {
102
        $maxAge = 300;
103
104
        $cc = HTTPCacheControlMiddleware::singleton();
105
        $cc->publicCache(false, $maxAge);
106
107
        $response = new HTTPResponse();
108
        $cc->applyToResponse($response);
109
110
        $this->assertContains('max-age=300', $response->getHeader('cache-control'));
111
        // STATE_PUBLIC doesn't contain no-cache or no-store headers to begin with,
112
        // so can't test their removal effectively
113
        $this->assertNotContains('no-cache', $response->getHeader('cache-control'));
114
    }
115
116
    public function testPublicCacheWithMaxAgeAppliesWhenLevelDoesNot()
117
    {
118
        $maxAge = 300;
119
120
        $cc = HTTPCacheControlMiddleware::singleton();
121
        $cc->privateCache(true);
122
        $cc->publicCache(false, $maxAge);
123
124
        $response = new HTTPResponse();
125
        $cc->applyToResponse($response);
126
127
        $this->assertContains('max-age=300', $response->getHeader('cache-control'));
128
    }
129
130
    /**
131
     * @dataProvider provideCacheStates
132
     */
133
    public function testSetNoStore($state, $immutable)
134
    {
135
        $cc = HTTPCacheControlMiddleware::singleton();
136
        $cc->setMaxAge('300');
137
        $cc->setSharedMaxAge('300');
138
139
        $cc->{$state}();
140
141
        $originalResponse = new HTTPResponse();
142
        $cc->applyToResponse($originalResponse);
143
144
        $cc->setNoStore();
145
146
        $response = new HTTPResponse();
147
148
        $cc->applyToResponse($response);
149
150
        if ($immutable) {
151
            $this->assertEquals($originalResponse->getHeader('cache-control'), $response->getHeader('cache-control'));
152
        } else {
153
            $this->assertContains('no-store', $response->getHeader('cache-control'));
154
            $this->assertNotContains('max-age', $response->getHeader('cache-control'));
155
            $this->assertNotContains('s-maxage', $response->getHeader('cache-control'));
156
        }
157
    }
158
159
    /**
160
     * @dataProvider provideCacheStates
161
     */
162
    public function testSetNoCache($state, $immutable)
163
    {
164
        $cc = HTTPCacheControlMiddleware::singleton();
165
        $cc->setMaxAge('300');
166
        $cc->setSharedMaxAge('300');
167
168
        $cc->{$state}();
169
170
        $originalResponse = new HTTPResponse();
171
        $cc->applyToResponse($originalResponse);
172
173
        $cc->setNoCache();
174
175
        $response = new HTTPResponse();
176
177
        $cc->applyToResponse($response);
178
179
        if ($immutable) {
180
            $this->assertEquals($originalResponse->getHeader('cache-control'), $response->getHeader('cache-control'));
181
        } else {
182
            $this->assertContains('no-cache', $response->getHeader('cache-control'));
183
            $this->assertNotContains('max-age', $response->getHeader('cache-control'));
184
            $this->assertNotContains('s-maxage', $response->getHeader('cache-control'));
185
        }
186
    }
187
188
    /**
189
     * @dataProvider provideCacheStates
190
     */
191
    public function testSetSharedMaxAge($state, $immutable)
192
    {
193
        $cc = HTTPCacheControlMiddleware::singleton();
194
195
        $cc->{$state}();
196
197
        $originalResponse = new HTTPResponse();
198
        $cc->applyToResponse($originalResponse);
199
200
        $cc->setSharedMaxAge('300');
201
202
        $response = new HTTPResponse();
203
204
        $cc->applyToResponse($response);
205
206
        if ($immutable) {
207
            $this->assertEquals($originalResponse->getHeader('cache-control'), $response->getHeader('cache-control'));
208
        } else {
209
            $this->assertContains('s-maxage=300', $response->getHeader('cache-control'));
210
            $this->assertNotContains('no-cache', $response->getHeader('cache-control'));
211
            $this->assertNotContains('no-store', $response->getHeader('cache-control'));
212
        }
213
    }
214
215
    /**
216
     * @dataProvider provideCacheStates
217
     */
218
    public function testSetMustRevalidate($state, $immutable)
219
    {
220
        $cc = HTTPCacheControlMiddleware::singleton();
221
222
        $cc->{$state}();
223
224
        $originalResponse = new HTTPResponse();
225
        $cc->applyToResponse($originalResponse);
226
227
        $cc->setMustRevalidate();
228
229
        $response = new HTTPResponse();
230
231
        $cc->applyToResponse($response);
232
233
        if ($immutable) {
234
            $this->assertEquals($originalResponse->getHeader('cache-control'), $response->getHeader('cache-control'));
235
        } else {
236
            $this->assertContains('must-revalidate', $response->getHeader('cache-control'));
237
            $this->assertNotContains('max-age', $response->getHeader('cache-control'));
238
            $this->assertNotContains('s-maxage', $response->getHeader('cache-control'));
239
        }
240
    }
241
242
    public function testCachingPriorities()
243
    {
244
        $hcc = new HTTPCacheControlMiddleware();
245
        $this->assertFalse($this->isDisabled($hcc), 'caching starts as disabled');
246
247
        $hcc->enableCache();
248
        $this->assertFalse($this->isDisabled($hcc));
249
250
        $hcc->publicCache();
251
        $this->assertTrue($this->isPublic($hcc), 'public can be set at start');
252
253
        $hcc->privateCache();
254
        $this->assertTrue($this->isPrivate($hcc), 'private overrides public');
255
256
        $hcc->publicCache();
257
        $this->assertFalse($this->isPublic($hcc), 'public does not overrides private');
258
259
        $hcc->disableCache();
260
        $this->assertTrue($this->isDisabled($hcc), 'disabled overrides private');
261
262
        $hcc->privateCache();
263
        $this->assertFalse($this->isPrivate($hcc), 'private does not override disabled');
264
265
        $hcc->enableCache(true);
266
        $this->assertFalse($this->isDisabled($hcc));
267
268
        $hcc->publicCache(true);
269
        $this->assertTrue($this->isPublic($hcc), 'force-public overrides disabled');
270
271
        $hcc->privateCache();
272
        $this->assertFalse($this->isPrivate($hcc), 'private does not overrdie force-public');
273
274
        $hcc->privateCache(true);
275
        $this->assertTrue($this->isPrivate($hcc), 'force-private overrides force-public');
276
277
        $hcc->publicCache(true);
278
        $this->assertFalse($this->isPublic($hcc), 'force-public does not override force-private');
279
280
        $hcc->disableCache(true);
281
        $this->assertTrue($this->isDisabled($hcc), 'force-disabled overrides force-private');
282
283
        $hcc->publicCache(true);
284
        $this->assertFalse($this->isPublic($hcc), 'force-public does not overrides force-disabled');
285
    }
286
287
    protected function isPrivate(HTTPCacheControlMiddleware $hcc)
288
    {
289
        return $hcc->hasDirective('private') && !$hcc->hasDirective('public') && !$hcc->hasDirective('no-cache');
290
    }
291
292
    protected function isPublic(HTTPCacheControlMiddleware $hcc)
293
    {
294
        return $hcc->hasDirective('public') && !$hcc->hasDirective('private') && !$hcc->hasDirective('no-cache');
295
    }
296
297
    protected function isDisabled(HTTPCacheControlMiddleware $hcc)
298
    {
299
        return $hcc->hasDirective('no-store') && !$hcc->hasDirective('private') && !$hcc->hasDirective('public');
300
    }
301
}
302