|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\Control\Tests\Middleware; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Control\Middleware\HTTPCacheControlMiddleware; |
|
6
|
|
|
use SilverStripe\Dev\SapphireTest; |
|
7
|
|
|
|
|
8
|
|
|
class HTTPCacheControlMiddlewareTest extends SapphireTest |
|
9
|
|
|
{ |
|
10
|
|
|
protected function setUp() |
|
11
|
|
|
{ |
|
12
|
|
|
parent::setUp(); |
|
13
|
|
|
// Set to disabled at null forcing level |
|
14
|
|
|
HTTPCacheControlMiddleware::config() |
|
15
|
|
|
->set('defaultState', 'disabled') |
|
16
|
|
|
->set('defaultForcingLevel', 0); |
|
17
|
|
|
HTTPCacheControlMiddleware::reset(); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public function testCachingPriorities() |
|
21
|
|
|
{ |
|
22
|
|
|
$hcc = new HTTPCacheControlMiddleware(); |
|
23
|
|
|
$this->assertTrue($this->isDisabled($hcc), 'caching starts as disabled'); |
|
24
|
|
|
|
|
25
|
|
|
$hcc->enableCache(); |
|
26
|
|
|
$this->assertFalse($this->isDisabled($hcc)); |
|
27
|
|
|
|
|
28
|
|
|
$hcc->publicCache(); |
|
29
|
|
|
$this->assertTrue($this->isPublic($hcc), 'public can be set at start'); |
|
30
|
|
|
|
|
31
|
|
|
$hcc->privateCache(); |
|
32
|
|
|
$this->assertTrue($this->isPrivate($hcc), 'private overrides public'); |
|
33
|
|
|
|
|
34
|
|
|
$hcc->publicCache(); |
|
35
|
|
|
$this->assertFalse($this->isPublic($hcc), 'public does not overrides private'); |
|
36
|
|
|
|
|
37
|
|
|
$hcc->disableCache(); |
|
38
|
|
|
$this->assertTrue($this->isDisabled($hcc), 'disabled overrides private'); |
|
39
|
|
|
|
|
40
|
|
|
$hcc->privateCache(); |
|
41
|
|
|
$this->assertFalse($this->isPrivate($hcc), 'private does not override disabled'); |
|
42
|
|
|
|
|
43
|
|
|
$hcc->enableCache(true); |
|
44
|
|
|
$this->assertFalse($this->isDisabled($hcc)); |
|
45
|
|
|
|
|
46
|
|
|
$hcc->publicCache(true); |
|
47
|
|
|
$this->assertTrue($this->isPublic($hcc), 'force-public overrides disabled'); |
|
48
|
|
|
|
|
49
|
|
|
$hcc->privateCache(); |
|
50
|
|
|
$this->assertFalse($this->isPrivate($hcc), 'private does not overrdie force-public'); |
|
51
|
|
|
|
|
52
|
|
|
$hcc->privateCache(true); |
|
53
|
|
|
$this->assertTrue($this->isPrivate($hcc), 'force-private overrides force-public'); |
|
54
|
|
|
|
|
55
|
|
|
$hcc->publicCache(true); |
|
56
|
|
|
$this->assertFalse($this->isPublic($hcc), 'force-public does not override force-private'); |
|
57
|
|
|
|
|
58
|
|
|
$hcc->disableCache(true); |
|
59
|
|
|
$this->assertTrue($this->isDisabled($hcc), 'force-disabled overrides force-private'); |
|
60
|
|
|
|
|
61
|
|
|
$hcc->publicCache(true); |
|
62
|
|
|
$this->assertFalse($this->isPublic($hcc), 'force-public does not overrides force-disabled'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
protected function isPrivate(HTTPCacheControlMiddleware $hcc) |
|
66
|
|
|
{ |
|
67
|
|
|
return $hcc->hasDirective('private') && !$hcc->hasDirective('public') && !$hcc->hasDirective('no-cache'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
protected function isPublic(HTTPCacheControlMiddleware $hcc) |
|
71
|
|
|
{ |
|
72
|
|
|
return $hcc->hasDirective('public') && !$hcc->hasDirective('private') && !$hcc->hasDirective('no-cache'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
protected function isDisabled(HTTPCacheControlMiddleware $hcc) |
|
76
|
|
|
{ |
|
77
|
|
|
return $hcc->hasDirective('no-cache') && !$hcc->hasDirective('private') && !$hcc->hasDirective('public'); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|