|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\Control\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Core\Injector\Injector; |
|
6
|
|
|
use SilverStripe\Dev\SapphireTest; |
|
7
|
|
|
use SilverStripe\Control\CookieJar; |
|
8
|
|
|
use SilverStripe\Control\Cookie; |
|
9
|
|
|
|
|
10
|
|
|
class CookieTest extends SapphireTest |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
protected function setUp() |
|
14
|
|
|
{ |
|
15
|
|
|
parent::setUp(); |
|
16
|
|
|
Injector::inst()->registerService(new CookieJar($_COOKIE), 'SilverStripe\\Control\\Cookie_Backend'); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Check a new cookie inst will be loaded with the superglobal by default |
|
21
|
|
|
*/ |
|
22
|
|
|
public function testCheckNewInstTakesSuperglobal() |
|
23
|
|
|
{ |
|
24
|
|
|
//store the superglobal state |
|
25
|
|
|
$existingCookies = $_COOKIE; |
|
26
|
|
|
|
|
27
|
|
|
//set a mock state for the superglobal |
|
28
|
|
|
$_COOKIE = array( |
|
29
|
|
|
'cookie1' => 1, |
|
30
|
|
|
'cookie2' => 'cookies', |
|
31
|
|
|
'cookie3' => 'test', |
|
32
|
|
|
'cookie_4' => 'value', |
|
33
|
|
|
); |
|
34
|
|
|
|
|
35
|
|
|
Injector::inst()->unregisterNamedObject('SilverStripe\\Control\\Cookie_Backend'); |
|
36
|
|
|
|
|
37
|
|
|
$this->assertEquals($_COOKIE['cookie1'], Cookie::get('cookie1')); |
|
38
|
|
|
$this->assertEquals($_COOKIE['cookie2'], Cookie::get('cookie2')); |
|
39
|
|
|
$this->assertEquals($_COOKIE['cookie3'], Cookie::get('cookie3')); |
|
40
|
|
|
$this->assertEquals($_COOKIE['cookie_4'], Cookie::get('cookie.4')); |
|
41
|
|
|
$this->assertEquals($_COOKIE['cookie_4'], Cookie::get('cookie_4')); |
|
42
|
|
|
|
|
43
|
|
|
//for good measure check the CookieJar hasn't stored anything extra |
|
44
|
|
|
$this->assertEquals($_COOKIE, Cookie::get_inst()->getAll(false)); |
|
45
|
|
|
|
|
46
|
|
|
//restore the superglobal state |
|
47
|
|
|
$_COOKIE = $existingCookies; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Check we don't mess with super globals when manipulating cookies |
|
52
|
|
|
* |
|
53
|
|
|
* State should be managed sperately to the super global |
|
54
|
|
|
*/ |
|
55
|
|
|
public function testCheckSuperglobalsArentTouched() |
|
56
|
|
|
{ |
|
57
|
|
|
|
|
58
|
|
|
//store the current state |
|
59
|
|
|
$before = $_COOKIE; |
|
60
|
|
|
|
|
61
|
|
|
//change some cookies |
|
62
|
|
|
Cookie::set('cookie', 'not me'); |
|
63
|
|
|
Cookie::force_expiry('cookie2'); |
|
64
|
|
|
|
|
65
|
|
|
//assert it hasn't changed |
|
66
|
|
|
$this->assertEquals($before, $_COOKIE); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Check we can actually change a backend |
|
71
|
|
|
*/ |
|
72
|
|
|
public function testChangeBackend() |
|
73
|
|
|
{ |
|
74
|
|
|
|
|
75
|
|
|
Cookie::set('test', 'testvalue'); |
|
76
|
|
|
|
|
77
|
|
|
$this->assertEquals('testvalue', Cookie::get('test')); |
|
78
|
|
|
|
|
79
|
|
|
Injector::inst()->registerService(new CookieJar(array()), 'SilverStripe\\Control\\Cookie_Backend'); |
|
80
|
|
|
|
|
81
|
|
|
$this->assertEmpty(Cookie::get('test')); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Check we can actually get the backend inst out |
|
86
|
|
|
*/ |
|
87
|
|
|
public function testGetInst() |
|
88
|
|
|
{ |
|
89
|
|
|
|
|
90
|
|
|
$inst = new CookieJar(array('test' => 'testvalue')); |
|
91
|
|
|
|
|
92
|
|
|
Injector::inst()->registerService($inst, 'SilverStripe\\Control\\Cookie_Backend'); |
|
93
|
|
|
|
|
94
|
|
|
$this->assertEquals($inst, Cookie::get_inst()); |
|
95
|
|
|
|
|
96
|
|
|
$this->assertEquals('testvalue', Cookie::get('test')); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Test that we can set and get cookies |
|
101
|
|
|
*/ |
|
102
|
|
|
public function testSetAndGet() |
|
103
|
|
|
{ |
|
104
|
|
|
$this->assertEmpty(Cookie::get('testCookie')); |
|
105
|
|
|
|
|
106
|
|
|
//set a test cookie |
|
107
|
|
|
Cookie::set('testCookie', 'testVal'); |
|
108
|
|
|
|
|
109
|
|
|
//make sure it was set |
|
110
|
|
|
$this->assertEquals('testVal', Cookie::get('testCookie')); |
|
111
|
|
|
|
|
112
|
|
|
//make sure we can distinguise it from ones that were "existing" |
|
113
|
|
|
$this->assertEmpty(Cookie::get('testCookie', false)); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Test that we can distinguish between vars that were loaded on instantiation |
|
118
|
|
|
* and those added later |
|
119
|
|
|
*/ |
|
120
|
|
|
public function testExistingVersusNew() |
|
121
|
|
|
{ |
|
122
|
|
|
//load with a cookie |
|
123
|
|
|
$cookieJar = new CookieJar( |
|
124
|
|
|
array( |
|
125
|
|
|
'cookieExisting' => 'i woz here', |
|
126
|
|
|
) |
|
127
|
|
|
); |
|
128
|
|
|
Injector::inst()->registerService($cookieJar, 'SilverStripe\\Control\\Cookie_Backend'); |
|
129
|
|
|
|
|
130
|
|
|
//set a new cookie |
|
131
|
|
|
Cookie::set('cookieNew', 'i am new'); |
|
132
|
|
|
|
|
133
|
|
|
//check we can fetch new and old cookie values |
|
134
|
|
|
$this->assertEquals('i woz here', Cookie::get('cookieExisting')); |
|
135
|
|
|
$this->assertEquals('i woz here', Cookie::get('cookieExisting', false)); |
|
136
|
|
|
$this->assertEquals('i am new', Cookie::get('cookieNew')); |
|
137
|
|
|
//there should be no original value for the new cookie |
|
138
|
|
|
$this->assertEmpty(Cookie::get('cookieNew', false)); |
|
139
|
|
|
|
|
140
|
|
|
//change the existing cookie, can we fetch the new and old value |
|
141
|
|
|
Cookie::set('cookieExisting', 'i woz changed'); |
|
142
|
|
|
|
|
143
|
|
|
$this->assertEquals('i woz changed', Cookie::get('cookieExisting')); |
|
144
|
|
|
$this->assertEquals('i woz here', Cookie::get('cookieExisting', false)); |
|
145
|
|
|
|
|
146
|
|
|
//check we can get all cookies |
|
147
|
|
|
$this->assertEquals( |
|
148
|
|
|
array( |
|
149
|
|
|
'cookieExisting' => 'i woz changed', |
|
150
|
|
|
'cookieNew' => 'i am new', |
|
151
|
|
|
), |
|
152
|
|
|
Cookie::get_all() |
|
153
|
|
|
); |
|
154
|
|
|
|
|
155
|
|
|
//check we can get all original cookies |
|
156
|
|
|
$this->assertEquals( |
|
157
|
|
|
array( |
|
158
|
|
|
'cookieExisting' => 'i woz here', |
|
159
|
|
|
), |
|
160
|
|
|
Cookie::get_all(false) |
|
161
|
|
|
); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Check we can remove cookies and we can access their original values |
|
166
|
|
|
*/ |
|
167
|
|
|
public function testForceExpiry() |
|
168
|
|
|
{ |
|
169
|
|
|
//load an existing cookie |
|
170
|
|
|
$cookieJar = new CookieJar( |
|
171
|
|
|
array( |
|
172
|
|
|
'cookieExisting' => 'i woz here', |
|
173
|
|
|
) |
|
174
|
|
|
); |
|
175
|
|
|
Injector::inst()->registerService($cookieJar, 'SilverStripe\\Control\\Cookie_Backend'); |
|
176
|
|
|
|
|
177
|
|
|
//make sure it's available |
|
178
|
|
|
$this->assertEquals('i woz here', Cookie::get('cookieExisting')); |
|
179
|
|
|
|
|
180
|
|
|
//remove the cookie |
|
181
|
|
|
Cookie::force_expiry('cookieExisting'); |
|
182
|
|
|
|
|
183
|
|
|
//check it's gone |
|
184
|
|
|
$this->assertEmpty(Cookie::get('cookieExisting')); |
|
185
|
|
|
|
|
186
|
|
|
//check we can get it's original value |
|
187
|
|
|
$this->assertEquals('i woz here', Cookie::get('cookieExisting', false)); |
|
188
|
|
|
|
|
189
|
|
|
|
|
190
|
|
|
//check we can add a new cookie and remove it and it doesn't leave any phantom values |
|
191
|
|
|
Cookie::set('newCookie', 'i am new'); |
|
192
|
|
|
|
|
193
|
|
|
//check it's set by not recieved |
|
194
|
|
|
$this->assertEquals('i am new', Cookie::get('newCookie')); |
|
195
|
|
|
$this->assertEmpty(Cookie::get('newCookie', false)); |
|
196
|
|
|
|
|
197
|
|
|
//remove it |
|
198
|
|
|
Cookie::force_expiry('newCookie'); |
|
199
|
|
|
|
|
200
|
|
|
//check it's neither set nor reveived |
|
201
|
|
|
$this->assertEmpty(Cookie::get('newCookie')); |
|
202
|
|
|
$this->assertEmpty(Cookie::get('newCookie', false)); |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|