Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[lhc/web/wiklou.git] / tests / phpunit / unit / includes / json / FormatJsonUnitTest.php
1 <?php
2
3 /**
4 * @covers FormatJson
5 */
6 class FormatJsonUnitTest extends MediaWikiUnitTestCase {
7
8 public static function provideEncoderPrettyPrinting() {
9 return [
10 // Four spaces
11 [ true, ' ' ],
12 [ ' ', ' ' ],
13 // Two spaces
14 [ ' ', ' ' ],
15 // One tab
16 [ "\t", "\t" ],
17 ];
18 }
19
20 /**
21 * @dataProvider provideEncoderPrettyPrinting
22 */
23 public function testEncoderPrettyPrinting( $pretty, $expectedIndent ) {
24 $obj = [
25 'emptyObject' => new stdClass,
26 'emptyArray' => [],
27 'string' => 'foobar\\',
28 'filledArray' => [
29 [
30 123,
31 456,
32 ],
33 // Nested json works without problems
34 '"7":["8",{"9":"10"}]',
35 // Whitespace clean up doesn't touch strings that look alike
36 "{\n\t\"emptyObject\": {\n\t},\n\t\"emptyArray\": [ ]\n}",
37 ],
38 ];
39
40 // No trailing whitespace, no trailing linefeed
41 $json = '{
42 "emptyObject": {},
43 "emptyArray": [],
44 "string": "foobar\\\\",
45 "filledArray": [
46 [
47 123,
48 456
49 ],
50 "\"7\":[\"8\",{\"9\":\"10\"}]",
51 "{\n\t\"emptyObject\": {\n\t},\n\t\"emptyArray\": [ ]\n}"
52 ]
53 }';
54
55 $json = str_replace( "\r", '', $json ); // Windows compat
56 $json = str_replace( "\t", $expectedIndent, $json );
57 $this->assertSame( $json, FormatJson::encode( $obj, $pretty ) );
58 }
59
60 public static function provideEncodeDefault() {
61 return self::getEncodeTestCases( [] );
62 }
63
64 /**
65 * @dataProvider provideEncodeDefault
66 */
67 public function testEncodeDefault( $from, $to ) {
68 $this->assertSame( $to, FormatJson::encode( $from ) );
69 }
70
71 public static function provideEncodeUtf8() {
72 return self::getEncodeTestCases( [ 'unicode' ] );
73 }
74
75 /**
76 * @dataProvider provideEncodeUtf8
77 */
78 public function testEncodeUtf8( $from, $to ) {
79 $this->assertSame( $to, FormatJson::encode( $from, false, FormatJson::UTF8_OK ) );
80 }
81
82 public static function provideEncodeXmlMeta() {
83 return self::getEncodeTestCases( [ 'xmlmeta' ] );
84 }
85
86 /**
87 * @dataProvider provideEncodeXmlMeta
88 */
89 public function testEncodeXmlMeta( $from, $to ) {
90 $this->assertSame( $to, FormatJson::encode( $from, false, FormatJson::XMLMETA_OK ) );
91 }
92
93 public static function provideEncodeAllOk() {
94 return self::getEncodeTestCases( [ 'unicode', 'xmlmeta' ] );
95 }
96
97 /**
98 * @dataProvider provideEncodeAllOk
99 */
100 public function testEncodeAllOk( $from, $to ) {
101 $this->assertSame( $to, FormatJson::encode( $from, false, FormatJson::ALL_OK ) );
102 }
103
104 public function testEncodePhpBug46944() {
105 $this->assertNotEquals(
106 '\ud840\udc00',
107 strtolower( FormatJson::encode( "\xf0\xa0\x80\x80" ) ),
108 'Test encoding an broken json_encode character (U+20000)'
109 );
110 }
111
112 public function testEncodeFail() {
113 // Set up a recursive object that can't be encoded.
114 $a = new stdClass;
115 $b = new stdClass;
116 $a->b = $b;
117 $b->a = $a;
118 $this->assertFalse( FormatJson::encode( $a ) );
119 }
120
121 public function testDecodeReturnType() {
122 $this->assertInternalType(
123 'object',
124 FormatJson::decode( '{"Name": "Cheeso", "Rank": 7}' ),
125 'Default to object'
126 );
127
128 $this->assertInternalType(
129 'array',
130 FormatJson::decode( '{"Name": "Cheeso", "Rank": 7}', true ),
131 'Optional array'
132 );
133 }
134
135 public static function provideParse() {
136 return [
137 [ null ],
138 [ true ],
139 [ false ],
140 [ 0 ],
141 [ 1 ],
142 [ 1.2 ],
143 [ '' ],
144 [ 'str' ],
145 [ [ 0, 1, 2 ] ],
146 [ [ 'a' => 'b' ] ],
147 [ [ 'a' => 'b' ] ],
148 [ [ 'a' => 'b', 'x' => [ 'c' => 'd' ] ] ],
149 ];
150 }
151
152 /**
153 * Recursively convert arrays into stdClass
154 * @param array|string|bool|int|float|null $value
155 * @return stdClass|string|bool|int|float|null
156 */
157 public static function toObject( $value ) {
158 return !is_array( $value ) ? $value : (object)array_map( __METHOD__, $value );
159 }
160
161 /**
162 * @dataProvider provideParse
163 * @param mixed $value
164 */
165 public function testParse( $value ) {
166 $expected = self::toObject( $value );
167 $json = FormatJson::encode( $expected, false, FormatJson::ALL_OK );
168 $this->assertJson( $json );
169
170 $st = FormatJson::parse( $json );
171 $this->assertInstanceOf( Status::class, $st );
172 $this->assertTrue( $st->isGood() );
173 $this->assertEquals( $expected, $st->getValue() );
174
175 $st = FormatJson::parse( $json, FormatJson::FORCE_ASSOC );
176 $this->assertInstanceOf( Status::class, $st );
177 $this->assertTrue( $st->isGood() );
178 $this->assertEquals( $value, $st->getValue() );
179 }
180
181 public static function provideParseErrors() {
182 return [
183 [ 'aaa' ],
184 [ '{"j": 1 ] }' ],
185 ];
186 }
187
188 /**
189 * @dataProvider provideParseErrors
190 * @param mixed $value
191 */
192 public function testParseErrors( $value ) {
193 $st = FormatJson::parse( $value );
194 $this->assertInstanceOf( Status::class, $st );
195 $this->assertFalse( $st->isOK() );
196 }
197
198 public function provideStripComments() {
199 return [
200 [ '{"a":"b"}', '{"a":"b"}' ],
201 [ "{\"a\":\"b\"}\n", "{\"a\":\"b\"}\n" ],
202 [ '/*c*/{"c":"b"}', '{"c":"b"}' ],
203 [ '{"a":"c"}/*c*/', '{"a":"c"}' ],
204 [ '/*c//d*/{"c":"b"}', '{"c":"b"}' ],
205 [ '{/*c*/"c":"b"}', '{"c":"b"}' ],
206 [ "/*\nc\r\n*/{\"c\":\"b\"}", '{"c":"b"}' ],
207 [ "//c\n{\"c\":\"b\"}", '{"c":"b"}' ],
208 [ "//c\r\n{\"c\":\"b\"}", '{"c":"b"}' ],
209 [ '{"a":"c"}//c', '{"a":"c"}' ],
210 [ "{\"a-c\"://c\n\"b\"}", '{"a-c":"b"}' ],
211 [ '{"/*a":"b"}', '{"/*a":"b"}' ],
212 [ '{"a":"//b"}', '{"a":"//b"}' ],
213 [ '{"a":"b/*c*/"}', '{"a":"b/*c*/"}' ],
214 [ "{\"\\\"/*a\":\"b\"}", "{\"\\\"/*a\":\"b\"}" ],
215 [ '', '' ],
216 [ '/*c', '' ],
217 [ '//c', '' ],
218 [ '"http://example.com"', '"http://example.com"' ],
219 [ "\0", "\0" ],
220 [ '"Blåbærsyltetøy"', '"Blåbærsyltetøy"' ],
221 ];
222 }
223
224 /**
225 * @covers FormatJson::stripComments
226 * @dataProvider provideStripComments
227 * @param string $json
228 * @param string $expect
229 */
230 public function testStripComments( $json, $expect ) {
231 $this->assertSame( $expect, FormatJson::stripComments( $json ) );
232 }
233
234 public function provideParseStripComments() {
235 return [
236 [ '/* blah */true', true ],
237 [ "// blah \ntrue", true ],
238 [ '[ "a" , /* blah */ "b" ]', [ 'a', 'b' ] ],
239 ];
240 }
241
242 /**
243 * @covers FormatJson::parse
244 * @covers FormatJson::stripComments
245 * @dataProvider provideParseStripComments
246 * @param string $json
247 * @param mixed $expect
248 */
249 public function testParseStripComments( $json, $expect ) {
250 $st = FormatJson::parse( $json, FormatJson::STRIP_COMMENTS );
251 $this->assertInstanceOf( Status::class, $st );
252 $this->assertTrue( $st->isGood() );
253 $this->assertEquals( $expect, $st->getValue() );
254 }
255
256 /**
257 * Generate a set of test cases for a particular combination of encoder options.
258 *
259 * @param array $unescapedGroups List of character groups to leave unescaped
260 * @return array Arrays of unencoded strings and corresponding encoded strings
261 */
262 private static function getEncodeTestCases( array $unescapedGroups ) {
263 $groups = [
264 'always' => [
265 // Forward slash (always unescaped)
266 '/' => '/',
267
268 // Control characters
269 "\0" => '\u0000',
270 "\x08" => '\b',
271 "\t" => '\t',
272 "\n" => '\n',
273 "\r" => '\r',
274 "\f" => '\f',
275 "\x1f" => '\u001f', // representative example
276
277 // Double quotes
278 '"' => '\"',
279
280 // Backslashes
281 '\\' => '\\\\',
282 '\\\\' => '\\\\\\\\',
283 '\\u00e9' => '\\\u00e9', // security check for Unicode unescaping
284
285 // Line terminators
286 "\xe2\x80\xa8" => '\u2028',
287 "\xe2\x80\xa9" => '\u2029',
288 ],
289 'unicode' => [
290 "\xc3\xa9" => '\u00e9',
291 "\xf0\x9d\x92\x9e" => '\ud835\udc9e', // U+1D49E, outside the BMP
292 ],
293 'xmlmeta' => [
294 '<' => '\u003C', // JSON_HEX_TAG uses uppercase hex digits
295 '>' => '\u003E',
296 '&' => '\u0026',
297 ],
298 ];
299
300 $cases = [];
301 foreach ( $groups as $name => $rules ) {
302 $leaveUnescaped = in_array( $name, $unescapedGroups );
303 foreach ( $rules as $from => $to ) {
304 $cases[] = [ $from, '"' . ( $leaveUnescaped ? $from : $to ) . '"' ];
305 }
306 }
307
308 return $cases;
309 }
310
311 public function provideEmptyJsonKeyStrings() {
312 return [
313 [
314 '{"":"foo"}',
315 '{"":"foo"}',
316 ''
317 ],
318 [
319 '{"_empty_":"foo"}',
320 '{"_empty_":"foo"}',
321 '_empty_' ],
322 [
323 '{"\u005F\u0065\u006D\u0070\u0074\u0079\u005F":"foo"}',
324 '{"_empty_":"foo"}',
325 '_empty_'
326 ],
327 [
328 '{"_empty_":"bar","":"foo"}',
329 '{"_empty_":"bar","":"foo"}',
330 ''
331 ],
332 [
333 '{"":"bar","_empty_":"foo"}',
334 '{"":"bar","_empty_":"foo"}',
335 '_empty_'
336 ]
337 ];
338 }
339
340 /**
341 * @covers FormatJson::encode
342 * @covers FormatJson::decode
343 * @dataProvider provideEmptyJsonKeyStrings
344 * @param string $json
345 *
346 * Decoding behavior with empty keys can be surprising.
347 * See https://phabricator.wikimedia.org/T206411
348 */
349 public function testEmptyJsonKeyArray( $json, $expect, $php71Name ) {
350 // Decoding to array is consistent across supported PHP versions
351 $this->assertSame( $expect, FormatJson::encode(
352 FormatJson::decode( $json, true ) ) );
353
354 // Decoding to object differs between supported PHP versions
355 $obj = FormatJson::decode( $json );
356 if ( version_compare( PHP_VERSION, '7.1', '<' ) ) {
357 $this->assertEquals( 'foo', $obj->_empty_ );
358 } else {
359 $this->assertEquals( 'foo', $obj->{$php71Name} );
360 }
361 }
362 }