Passed
Push — master ( 16e851...25995c )
by Daniel
09:34
created

HTTPCacheControlIntegrationTest::testFormCSRF()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Control\Tests;
4
5
use SilverStripe\Control\HTTP;
6
use SilverStripe\Control\Middleware\HTTPCacheControlMiddleware;
7
use SilverStripe\Control\Tests\HTTPCacheControlIntegrationTest\RuleController;
8
use SilverStripe\Control\Tests\HTTPCacheControlIntegrationTest\SessionController;
9
use SilverStripe\Core\Config\Config;
10
use SilverStripe\Dev\FunctionalTest;
11
12
class HTTPCacheControlIntegrationTest extends FunctionalTest
13
{
14
    protected static $extra_controllers = [
15
        SessionController::class,
16
        RuleController::class,
17
    ];
18
19
    protected function setUp()
20
    {
21
        parent::setUp();
22
        HTTPCacheControlMiddleware::config()
23
            ->set('defaultState', 'disabled')
24
            ->set('defaultForcingLevel', 0);
25
        HTTPCacheControlMiddleware::reset();
26
    }
27
28
    public function testFormCSRF()
29
    {
30
        // CSRF sets caching to disabled
31
        $response = $this->get('HTTPCacheControlIntegrationTest_SessionController/showform');
32
        $header = $response->getHeader('Cache-Control');
33
        $this->assertFalse($response->isError());
34
        $this->assertNotContains('public', $header);
35
        $this->assertNotContains('private', $header);
36
        $this->assertContains('no-cache', $header);
37
        $this->assertContains('no-store', $header);
38
        $this->assertContains('must-revalidate', $header);
39
    }
40
41
    public function testPublicForm()
42
    {
43
        // Public forms (http get) allow public caching
44
        $response = $this->get('HTTPCacheControlIntegrationTest_SessionController/showpublicform');
45
        $header = $response->getHeader('Cache-Control');
46
        $this->assertFalse($response->isError());
47
        $this->assertContains('public', $header);
48
        $this->assertContains('must-revalidate', $header);
49
        $this->assertNotContains('no-cache', $response->getHeader('Cache-Control'));
50
        $this->assertNotContains('no-store', $response->getHeader('Cache-Control'));
51
    }
52
53
    public function testPrivateActionsError()
54
    {
55
        // disallowed private actions don't cache
56
        $response = $this->get('HTTPCacheControlIntegrationTest_SessionController/privateaction');
57
        $header = $response->getHeader('Cache-Control');
58
        $this->assertTrue($response->isError());
59
        $this->assertContains('no-cache', $header);
60
        $this->assertContains('no-store', $header);
61
        $this->assertContains('must-revalidate', $header);
62
    }
63
64
    public function testPrivateActionsAuthenticated()
65
    {
66
        $this->logInWithPermission('ADMIN');
67
        // Authenticated actions are private cache
68
        $response = $this->get('HTTPCacheControlIntegrationTest_SessionController/privateaction');
69
        $header = $response->getHeader('Cache-Control');
70
        $this->assertFalse($response->isError());
71
        $this->assertContains('private', $header);
72
        $this->assertContains('must-revalidate', $header);
73
        $this->assertNotContains('no-cache', $header);
74
        $this->assertNotContains('no-store', $header);
75
    }
76
77
    public function testPrivateCache()
78
    {
79
        $response = $this->get('HTTPCacheControlIntegrationTest_RuleController/privateaction');
80
        $header = $response->getHeader('Cache-Control');
81
        $this->assertFalse($response->isError());
82
        $this->assertContains('private', $header);
83
        $this->assertContains('must-revalidate', $header);
84
        $this->assertNotContains('no-cache', $header);
85
        $this->assertNotContains('no-store', $header);
86
    }
87
88
    public function testPublicCache()
89
    {
90
        $response = $this->get('HTTPCacheControlIntegrationTest_RuleController/publicaction');
91
        $header = $response->getHeader('Cache-Control');
92
        $this->assertFalse($response->isError());
93
        $this->assertContains('public', $header);
94
        $this->assertContains('must-revalidate', $header);
95
        $this->assertNotContains('no-cache', $header);
96
        $this->assertNotContains('no-store', $header);
97
        $this->assertContains('max-age=9000', $header);
98
    }
99
100
    public function testDisabledCache()
101
    {
102
        $response = $this->get('HTTPCacheControlIntegrationTest_RuleController/disabledaction');
103
        $header = $response->getHeader('Cache-Control');
104
        $this->assertFalse($response->isError());
105
        $this->assertNotContains('public', $header);
106
        $this->assertNotContains('private', $header);
107
        $this->assertContains('no-cache', $header);
108
        $this->assertContains('no-store', $header);
109
        $this->assertContains('must-revalidate', $header);
110
    }
111
}
112