MediaWiki REL1_31
SetCookieCompatTest.php
Go to the documentation of this file.
1<?php
2
3use PHPUnit\Framework\TestCase;
5
9class SetCookieCompatTest extends TestCase {
10 public static function provideSetCookieEmulated() {
11 // Expected values are all copied from PHP 7.3
12 // phpcs:disable Generic.Files.LineLength.TooLong
13 return [
14 'unrecognised key' => [
15 true, 'a', '',
16 [
17 'path' => '/',
18 'foo' => 'bar'
19 ],
20 [
21 'headers' => [
22 'Set-Cookie: a=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; Max-Age=0; path=/',
23 ],
24 'returnValue' => true,
25 'errors' => [
26 'setcookie(): Unrecognized key \'foo\' found in the options array',
27 ],
28 ],
29 ],
30 'no valid options' => [
31 true, 'a', '',
32 [
33 'foo' => 'bar'
34 ],
35 [
36 'headers' => [
37 'Set-Cookie: a=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; Max-Age=0',
38 ],
39 'returnValue' => true,
40 'errors' => [
41 'setcookie(): Unrecognized key \'foo\' found in the options array',
42 'setcookie(): No valid options were found in the given array',
43 ],
44 ]
45 ],
46 'empty name' => [
47 true, '', '', [],
48 [
49 'headers' => [],
50 'returnValue' => false,
51 'errors' => [
52 'Cookie names must not be empty',
53 ],
54 ],
55 ],
56 'invalid name' => [
57 true, "\n", '', [],
58 [
59 'headers' => [],
60 'returnValue' => false,
61 'errors' => [
62 'Cookie names cannot contain any of the following \'=,; \\t\\r\\n\\013\\014\'',
63 ],
64 ]
65 ],
66 'invalid value' => [
67 false, 'a', "\n", [],
68 [
69 'headers' => [],
70 'returnValue' => false,
71 'errors' => [
72 'Cookie values cannot contain any of the following \',; \\t\\r\\n\\013\\014\'',
73 ],
74 ]
75 ],
76 'escaped invalid value' => [
77 true, 'a', "\n", [],
78 [
79 'headers' => [
80 'Set-Cookie: a=%0A',
81 ],
82 'returnValue' => true,
83 'errors' => [],
84 ]
85 ],
86 'invalid path' => [
87 true, 'a', 'b', [ 'path' => "\n" ],
88 [
89 'headers' => [],
90 'returnValue' => false,
91 'errors' => [
92 'Cookie paths cannot contain any of the following \',; \\t\\r\\n\\013\\014\'',
93 ],
94 ]
95 ],
96 'invalid domain' => [
97 true, 'a', 'b', [ 'domain' => "\013" ],
98 [
99 'headers' => [],
100 'returnValue' => false,
101 'errors' => [
102 'Cookie domains cannot contain any of the following \',; \\t\\r\\n\\013\\014\'',
103 ],
104 ]
105 ],
106 'empty value' => [
107 true, 'a', '', [],
108 [
109 'headers' => [
110 'Set-Cookie: a=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; Max-Age=0',
111 ],
112 'returnValue' => true,
113 'errors' => [],
114 ]
115 ],
116 'encoded value' => [
117 true, 'a', '%', [],
118 [
119 'headers' => [
120 'Set-Cookie: a=%25',
121 ],
122 'returnValue' => true,
123 'errors' => [],
124 ]
125 ],
126 'raw value' => [
127 false, 'a', '%', [],
128 [
129 'headers' => [
130 'Set-Cookie: a=%',
131 ],
132 'returnValue' => true,
133 'errors' => [],
134 ]
135 ],
136 'expires too large' => [
137 true, 'a', 'b', [ 'expires' => 253430725200 ],
138 [
139 'headers' => [],
140 'returnValue' => false,
141 'errors' => [
142 'Expiry date cannot have a year greater than 9999',
143 ],
144 ]
145 ],
146 'expires in the past' => [
147 true, 'a', 'b', [ 'expires' => 979477200 ],
148 [
149 'headers' => [
150 'Set-Cookie: a=b; expires=Sun, 14-Jan-2001 13:00:00 GMT; Max-Age=0',
151 ],
152 'returnValue' => true,
153 'errors' => [],
154 ]
155 ],
156 'expires in the future' => [
157 true, 'a', 'b', [ 'expires' => 32504889600 ],
158 [
159 'headers' => [
160 'Set-Cookie: a=b; expires=Wed, 15-Jan-3000 00:00:00 GMT; Max-Age=30911234470',
161 ],
162 'returnValue' => true,
163 'errors' => [],
164 ]
165 ],
166 'path' => [
167 false, 'a', 'b', [ 'path' => '%%' ],
168 [
169 'headers' => [
170 'Set-Cookie: a=b; path=%%',
171 ],
172 'returnValue' => true,
173 'errors' => [],
174 ]
175 ],
176 'domain' => [
177 false, 'a', 'b', [ 'domain' => '%%' ],
178 [
179 'headers' => [
180 'Set-Cookie: a=b; domain=%%',
181 ],
182 'returnValue' => true,
183 'errors' => [],
184 ]
185 ],
186 'secure' => [
187 false, 'a', 'b', [ 'secure' => true ],
188 [
189 'headers' => [
190 'Set-Cookie: a=b; secure',
191 ],
192 'returnValue' => true,
193 'errors' => [],
194 ]
195 ],
196 'httponly' => [
197 false, 'a', 'b', [ 'httponly' => true ],
198 [
199 'headers' => [
200 'Set-Cookie: a=b; HttpOnly',
201 ],
202 'returnValue' => true,
203 'errors' => [],
204 ]
205 ],
206 'samesite' => [
207 false, 'a', 'b', [ 'samesite' => 'None' ],
208 [
209 'headers' => [
210 'Set-Cookie: a=b; SameSite=None',
211 ],
212 'returnValue' => true,
213 'errors' => [],
214 ]
215 ],
216 'multiple options' => [
217 false, 'a', 'b', [
218 'expires' => 32504889600,
219 'path' => '/%',
220 'domain' => '%.com',
221 'secure' => true,
222 'httponly' => true
223 ],
224 [
225 'headers' => [
226 'Set-Cookie: a=b; expires=Wed, 15-Jan-3000 00:00:00 GMT; Max-Age=30911234470; path=/%; domain=%.com; secure; HttpOnly',
227 ],
228 'returnValue' => true,
229 'errors' => [],
230 ]
231 ],
232
233 ];
234 }
235
239 public function testSetCookieEmulated( $urlEncode, $name, $value, $options, $expected ) {
240 $instance = new class extends SetCookieCompat {
241 public $headers = [];
242 public $errors = [];
243
244 protected function time() {
245 return 1593655130;
246 }
247
248 protected function error( $error ) {
249 $this->errors[] = $error;
250 }
251
252 protected function headers_sent() {
253 return false;
254 }
255
256 protected function header( $header ) {
257 $this->headers[] = $header;
258 }
259 };
260
261 $ret = $instance->setCookieEmulated( $urlEncode, $name, $value, $options );
262 $this->assertSame( $expected['headers'], $instance->headers );
263 $this->assertSame( $expected['errors'], $instance->errors );
264 $this->assertSame( $expected['returnValue'], $ret );
265 }
266}
testSetCookieEmulated( $urlEncode, $name, $value, $options, $expected)
provideSetCookieEmulated
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults error
Definition hooks.txt:2612
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition hooks.txt:2001
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition hooks.txt:2006
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition hooks.txt:2005
$header