Passed
Pull Request — master (#18)
by Robbie
03:29
created

testModificationDateFromDataObjects()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 20
rs 9.4285
cc 1
eloc 12
nc 1
nop 0
1
<?php
2
3
namespace SilverStripe\ControllerPolicy\Tests;
4
5
use SilverStripe\Control\Director;
6
use SilverStripe\Control\HTTP;
7
use SilverStripe\Control\HTTPRequest;
8
use SilverStripe\Control\Session;
9
use SilverStripe\ControllerPolicy\ControllerPolicyMiddleware;
10
use SilverStripe\ControllerPolicy\Policies\CachingPolicy;
11
use SilverStripe\ControllerPolicy\Tests\CachingPolicyTest\CachingPolicyController;
12
use SilverStripe\ControllerPolicy\Tests\CachingPolicyTest\CallbackCachingPolicyController;
13
use SilverStripe\ControllerPolicy\Tests\CachingPolicyTest\UnrelatedController;
14
use SilverStripe\Core\Config\Config;
15
use SilverStripe\Core\Injector\Injector;
16
use SilverStripe\Dev\FunctionalTest;
17
use SilverStripe\ORM\DataObject;
18
19
class CachingPolicyTest extends FunctionalTest
20
{
21
    protected static $extra_controllers = [
22
        CachingPolicyController::class,
23
        CallbackCachingPolicyController::class,
24
        UnrelatedController::class,
25
    ];
26
27
    private $configCachingPolicy = [
28
        CachingPolicy::class => [
29
            'class' => CachingPolicy::class,
30
            'properties' => [
31
                'cacheAge' => '999',
32
                'vary' => 'X-EyeColour',
33
            ],
34
        ],
35
    ];
36
37
    protected function setUp()
38
    {
39
        parent::setUp();
40
41
        Config::modify()->set(CachingPolicy::class, 'disable_cache_age_in_dev', false);
42
    }
43
44
    /**
45
     * Remove any policies from the middleware, since it's assigned to the Director singleton and shared between
46
     * tests.
47
     */
48
    protected function tearDown()
49
    {
50
        foreach (Director::singleton()->getMiddlewares() as $middleware) {
51
            if ($middleware instanceof ControllerPolicyMiddleware) {
52
                $middleware->clearPolicies();
53
            }
54
        }
55
56
        parent::tearDown();
57
    }
58
59
    public function makeRequest($config, $controller, $url)
0 ignored issues
show
Unused Code introduced by
The parameter $url is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

59
    public function makeRequest($config, $controller, /** @scrutinizer ignore-unused */ $url)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
60
    {
61
        Injector::inst()->load($config);
62
63
        $middleware = Injector::inst()->create(ControllerPolicyMiddleware::class);
64
65
        // Exercise the controller.
66
        $controller = Injector::inst()->create($controller);
67
        $controller->setMiddleware($middleware);
68
        $controller->doInit();
69
70
        $request = new HTTPRequest('GET', $controller->Link('test'));
71
        $request->setSession(new Session([]));
72
        $response = Director::singleton()->handleRequest($request);
73
74
        return $response;
75
    }
76
77
    public function testConfigured()
78
    {
79
        $response = $this->makeRequest(
80
            $this->configCachingPolicy,
81
            CachingPolicyController::class,
82
            'CachingPolicyController/test'
83
        );
84
85
        $this->assertEquals(
86
            'max-age=999, must-revalidate, no-transform',
87
            $response->getHeader('Cache-Control'),
88
            'Header appears as configured'
89
        );
90
        $this->assertEquals(
91
            'X-EyeColour',
92
            $response->getHeader('Vary'),
93
            'Header appears as configured'
94
        );
95
    }
96
97
    public function testCallbackOverride()
98
    {
99
        $response = $this->makeRequest(
100
            $this->configCachingPolicy,
101
            CallbackCachingPolicyController::class,
102
            'CallbackCachingPolicyController/test'
103
        );
104
105
        $this->assertEquals(
106
            'max-age=1001, must-revalidate, no-transform',
107
            $response->getHeader('Cache-Control'),
108
            'Controller\'s getCacheAge() overrides the configuration'
109
        );
110
        $this->assertEquals(
111
            'X-HeightWeight',
112
            $response->getHeader('Vary'),
113
            'Controller\'s getVary() overrides the configuration'
114
        );
115
        $this->assertEquals(
116
            HTTP::gmt_date('5000'),
117
            $response->getHeader('Last-Modified'),
118
            'Controller\'s getModificationTimestamp overrides the HTTP::$modification_date'
119
        );
120
    }
121
122
    public function testUnrelated()
123
    {
124
        $response = $this->makeRequest(
125
            $this->configCachingPolicy,
126
            UnrelatedController::class,
127
            'UnrelatedController/test'
128
        );
129
130
        $this->assertNull($response->getHeader('Vary'), 'Headers on unrelated controller are unaffected');
131
    }
132
133
    public function testModificationDateFromDataObjects()
134
    {
135
        // Trigger updates to HTTP::$modification_date.
136
        new DataObject(['LastEdited' => '1970-01-01 00:02']);
137
        new DataObject(['LastEdited' => '1970-01-01 00:01']);
138
        new DataObject(['LastEdited' => '1970-01-01 00:03']);
139
140
        $response = $this->makeRequest(
141
            $this->configCachingPolicy,
142
            CachingPolicyController::class,
143
            'CachingPolicyController/test'
144
        );
145
146
        $this->assertEquals(
147
            HTTP::gmt_date(strtotime('1970-01-01 00:03')),
148
            $response->getHeader('Last-Modified'),
149
            'Most recent LastEdited value prevails over the older ones'
150
        );
151
    }
152
}
153