1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Control\Tests; |
4
|
|
|
|
5
|
|
|
use ReflectionMethod; |
6
|
|
|
use SilverStripe\Dev\SapphireTest; |
7
|
|
|
use SilverStripe\Control\CookieJar; |
8
|
|
|
use SilverStripe\Control\Session; |
9
|
|
|
use SilverStripe\Core\Config\Config; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Testing CookieJar |
13
|
|
|
* |
14
|
|
|
* Testing the CookieJar acts as expected and keeps track of Cookies it is loaded |
15
|
|
|
* with as well as new cookies that are set during the running of an application |
16
|
|
|
*/ |
17
|
|
|
class CookieJarTest extends SapphireTest |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Test that the construction argument is stored and returned as expected |
22
|
|
|
*/ |
23
|
|
|
public function testConstruct() |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
//some default cookies to load |
27
|
|
|
$defaultCookies = [ |
28
|
|
|
'cookie1' => 1, |
29
|
|
|
'cookie2' => 'cookies', |
30
|
|
|
'cookie3' => 'test', |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
$cookieJar = new CookieJar($defaultCookies); |
34
|
|
|
|
35
|
|
|
//make sure all the "received" cookies are as expected |
36
|
|
|
$this->assertEquals($defaultCookies, $cookieJar->getAll(false)); |
37
|
|
|
|
38
|
|
|
//make sure there are no "phantom" cookies |
39
|
|
|
$this->assertEquals($defaultCookies, $cookieJar->getAll(true)); |
40
|
|
|
|
41
|
|
|
//check an empty array is accepted |
42
|
|
|
$cookieJar = new CookieJar([]); |
43
|
|
|
$this->assertEmpty($cookieJar->getAll(false)); |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
//check no argument is accepted |
47
|
|
|
$cookieJar = new CookieJar(); |
48
|
|
|
$this->assertEmpty($cookieJar->getAll(false)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Test that we can set and get cookies |
53
|
|
|
*/ |
54
|
|
|
public function testSetAndGet() |
55
|
|
|
{ |
56
|
|
|
$cookieJar = new CookieJar(); |
57
|
|
|
|
58
|
|
|
$this->assertEmpty($cookieJar->get('testCookie')); |
59
|
|
|
|
60
|
|
|
//set a test cookie |
61
|
|
|
$cookieJar->set('testCookie', 'testVal'); |
62
|
|
|
|
63
|
|
|
//make sure it was set |
64
|
|
|
$this->assertEquals('testVal', $cookieJar->get('testCookie')); |
65
|
|
|
|
66
|
|
|
//make sure we can distinguish it from ones that were "existing" |
67
|
|
|
$this->assertEmpty($cookieJar->get('testCookie', false)); |
68
|
|
|
|
69
|
|
|
//PHP will replace an incoming COOKIE called 'var.with.dots' to 'var_with_dots' |
70
|
|
|
$cookieJar = new CookieJar( |
71
|
|
|
[ |
72
|
|
|
'var_with_dots' => 'value', |
73
|
|
|
] |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
$cookieJar->set('test.dots', 'dots'); |
77
|
|
|
|
78
|
|
|
//check we can access with '.' and with '_' |
79
|
|
|
$this->assertEquals('value', $cookieJar->get('var.with.dots')); |
80
|
|
|
$this->assertEquals('value', $cookieJar->get('var_with_dots')); |
81
|
|
|
$this->assertEquals('dots', $cookieJar->get('test.dots')); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Test that we can distinguish between vars that were loaded on instantiation |
86
|
|
|
* and those added later |
87
|
|
|
*/ |
88
|
|
|
public function testExistingVersusNew() |
89
|
|
|
{ |
90
|
|
|
//load with a cookie |
91
|
|
|
$cookieJar = new CookieJar( |
92
|
|
|
[ |
93
|
|
|
'cookieExisting' => 'i woz here', |
94
|
|
|
] |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
//set a new cookie |
98
|
|
|
$cookieJar->set('cookieNew', 'i am new'); |
99
|
|
|
|
100
|
|
|
//check we can fetch new and old cookie values |
101
|
|
|
$this->assertEquals('i woz here', $cookieJar->get('cookieExisting')); |
102
|
|
|
$this->assertEquals('i woz here', $cookieJar->get('cookieExisting', false)); |
103
|
|
|
$this->assertEquals('i am new', $cookieJar->get('cookieNew')); |
104
|
|
|
//there should be no original value for the new cookie |
105
|
|
|
$this->assertEmpty($cookieJar->get('cookieNew', false)); |
106
|
|
|
|
107
|
|
|
//change the existing cookie, can we fetch the new and old value |
108
|
|
|
$cookieJar->set('cookieExisting', 'i woz changed'); |
109
|
|
|
|
110
|
|
|
$this->assertEquals('i woz changed', $cookieJar->get('cookieExisting')); |
111
|
|
|
$this->assertEquals('i woz here', $cookieJar->get('cookieExisting', false)); |
112
|
|
|
|
113
|
|
|
//check we can get all cookies |
114
|
|
|
$this->assertEquals( |
115
|
|
|
[ |
116
|
|
|
'cookieExisting' => 'i woz changed', |
117
|
|
|
'cookieNew' => 'i am new', |
118
|
|
|
], |
119
|
|
|
$cookieJar->getAll() |
120
|
|
|
); |
121
|
|
|
|
122
|
|
|
//check we can get all original cookies |
123
|
|
|
$this->assertEquals( |
124
|
|
|
[ |
125
|
|
|
'cookieExisting' => 'i woz here', |
126
|
|
|
], |
127
|
|
|
$cookieJar->getAll(false) |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Check we can remove cookies and we can access their original values |
133
|
|
|
*/ |
134
|
|
|
public function testForceExpiry() |
135
|
|
|
{ |
136
|
|
|
//load an existing cookie |
137
|
|
|
$cookieJar = new CookieJar( |
138
|
|
|
[ |
139
|
|
|
'cookieExisting' => 'i woz here', |
140
|
|
|
] |
141
|
|
|
); |
142
|
|
|
|
143
|
|
|
//make sure it's available |
144
|
|
|
$this->assertEquals('i woz here', $cookieJar->get('cookieExisting')); |
145
|
|
|
|
146
|
|
|
//remove the cookie |
147
|
|
|
$cookieJar->forceExpiry('cookieExisting'); |
148
|
|
|
|
149
|
|
|
//check it's gone |
150
|
|
|
$this->assertEmpty($cookieJar->get('cookieExisting')); |
151
|
|
|
|
152
|
|
|
//check we can get it's original value |
153
|
|
|
$this->assertEquals('i woz here', $cookieJar->get('cookieExisting', false)); |
154
|
|
|
|
155
|
|
|
|
156
|
|
|
//check we can add a new cookie and remove it and it doesn't leave any phantom values |
157
|
|
|
$cookieJar->set('newCookie', 'i am new'); |
158
|
|
|
|
159
|
|
|
//check it's set by not received |
160
|
|
|
$this->assertEquals('i am new', $cookieJar->get('newCookie')); |
161
|
|
|
$this->assertEmpty($cookieJar->get('newCookie', false)); |
162
|
|
|
|
163
|
|
|
//remove it |
164
|
|
|
$cookieJar->forceExpiry('newCookie'); |
165
|
|
|
|
166
|
|
|
//check it's neither set nor received |
167
|
|
|
$this->assertEmpty($cookieJar->get('newCookie')); |
168
|
|
|
$this->assertEmpty($cookieJar->get('newCookie', false)); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Check that the session cookie samesite configuration is used for session cookies. |
173
|
|
|
*/ |
174
|
|
|
public function testGetSameSite(): void |
175
|
|
|
{ |
176
|
|
|
$cookieJar = new CookieJar(); |
177
|
|
|
$methodGetSameSite = new ReflectionMethod($cookieJar, 'getSameSite'); |
178
|
|
|
$methodGetSameSite->setAccessible(true); |
179
|
|
|
Config::modify()->set(Session::class, 'cookie_samesite', 'None'); |
180
|
|
|
|
181
|
|
|
$this->assertSame('None', $methodGetSameSite->invoke($cookieJar, session_name())); |
182
|
|
|
$this->assertSame('Lax', $methodGetSameSite->invoke($cookieJar, 'some-random-cookie')); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Check that the cookies are correctly set as secure for samesite === "None" |
187
|
|
|
* The passed in value for secure should be respected otherwise. |
188
|
|
|
*/ |
189
|
|
|
public function testCookieIsSecure(): void |
190
|
|
|
{ |
191
|
|
|
$cookieJar = new CookieJar(); |
192
|
|
|
$methodCookieIsSecure = new ReflectionMethod($cookieJar, 'cookieIsSecure'); |
193
|
|
|
$methodCookieIsSecure->setAccessible(true); |
194
|
|
|
|
195
|
|
|
$this->assertTrue($methodCookieIsSecure->invoke($cookieJar, 'None', false)); |
196
|
|
|
$this->assertTrue($methodCookieIsSecure->invoke($cookieJar, 'None', true)); |
197
|
|
|
$this->assertTrue($methodCookieIsSecure->invoke($cookieJar, 'Lax', true)); |
198
|
|
|
$this->assertFalse($methodCookieIsSecure->invoke($cookieJar, 'Lax', false)); |
199
|
|
|
$this->assertTrue($methodCookieIsSecure->invoke($cookieJar, 'Strict', true)); |
200
|
|
|
$this->assertFalse($methodCookieIsSecure->invoke($cookieJar, 'Strict', false)); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|