Merge "CoreTagHooks: Use parse() for output to HTML rather than text()"
[lhc/web/wiklou.git] / tests / phpunit / includes / json / FormatJsonTest.php
1 <?php
2
3 /**
4 * @covers FormatJson
5 */
6 class FormatJsonTest extends MediaWikiTestCase {
7
8 public static function provideEncoderPrettyPrinting() {
9 return array(
10 // Four spaces
11 array( true, ' ' ),
12 array( ' ', ' ' ),
13 // Two spaces
14 array( ' ', ' ' ),
15 // One tab
16 array( "\t", "\t" ),
17 );
18 }
19
20 /**
21 * @dataProvider provideEncoderPrettyPrinting
22 */
23 public function testEncoderPrettyPrinting( $pretty, $expectedIndent ) {
24 $obj = array(
25 'emptyObject' => new stdClass,
26 'emptyArray' => array(),
27 'string' => 'foobar\\',
28 'filledArray' => array(
29 array(
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( array() );
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( array( '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( array( '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( array( '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 testDecodeReturnType() {
113 $this->assertInternalType(
114 'object',
115 FormatJson::decode( '{"Name": "Cheeso", "Rank": 7}' ),
116 'Default to object'
117 );
118
119 $this->assertInternalType(
120 'array',
121 FormatJson::decode( '{"Name": "Cheeso", "Rank": 7}', true ),
122 'Optional array'
123 );
124 }
125
126 public static function provideParse() {
127 return array(
128 array( null ),
129 array( true ),
130 array( false ),
131 array( 0 ),
132 array( 1 ),
133 array( 1.2 ),
134 array( '' ),
135 array( 'str' ),
136 array( array( 0, 1, 2 ) ),
137 array( array( 'a' => 'b' ) ),
138 array( array( 'a' => 'b' ) ),
139 array( array( 'a' => 'b', 'x' => array( 'c' => 'd' ) ) ),
140 );
141 }
142
143 /**
144 * Recursively convert arrays into stdClass
145 * @param array|string|bool|int|float|null $value
146 * @return stdClass|string|bool|int|float|null
147 */
148 public static function toObject( $value ) {
149 return !is_array( $value ) ? $value : (object) array_map( __METHOD__, $value );
150 }
151
152 /**
153 * @dataProvider provideParse
154 * @param mixed $value
155 */
156 public function testParse( $value ) {
157 $expected = self::toObject( $value );
158 $json = FormatJson::encode( $expected, false, FormatJson::ALL_OK );
159 $this->assertJson( $json );
160
161 $st = FormatJson::parse( $json );
162 $this->assertType( 'Status', $st );
163 $this->assertTrue( $st->isGood() );
164 $this->assertEquals( $expected, $st->getValue() );
165
166 $st = FormatJson::parse( $json, FormatJson::FORCE_ASSOC );
167 $this->assertType( 'Status', $st );
168 $this->assertTrue( $st->isGood() );
169 $this->assertEquals( $value, $st->getValue() );
170 }
171
172 public static function provideParseTryFixing() {
173 return array(
174 array( "[,]", '[]' ),
175 array( "[ , ]", '[]' ),
176 array( "[ , }", false ),
177 array( '[1],', false ),
178 array( "[1,]", '[1]' ),
179 array( "[1\n,]", '[1]' ),
180 array( "[1,\n]", '[1]' ),
181 array( "[1,]\n", '[1]' ),
182 array( "[1\n,\n]\n", '[1]' ),
183 array( '["a,",]', '["a,"]' ),
184 array( "[[1,]\n,[2,\n],[3\n,]]", '[[1],[2],[3]]' ),
185 array( '[[1,],[2,],[3,]]', false ), // I wish we could parse this, but would need quote parsing
186 array( '[1,,]', false ),
187 );
188 }
189
190 /**
191 * @dataProvider provideParseTryFixing
192 * @param string $value
193 * @param string|bool $expected
194 */
195 public function testParseTryFixing( $value, $expected ) {
196 $st = FormatJson::parse( $value, FormatJson::TRY_FIXING );
197 $this->assertType( 'Status', $st );
198 if ( $expected === false ) {
199 $this->assertFalse( $st->isOK() );
200 } else {
201 $this->assertFalse( $st->isGood() );
202 $this->assertTrue( $st->isOK() );
203 $val = FormatJson::encode( $st->getValue(), false, FormatJson::ALL_OK );
204 $this->assertEquals( $expected, $val );
205 }
206 }
207
208 public static function provideParseErrors() {
209 return array(
210 array( 'aaa' ),
211 array( '{"j": 1 ] }' ),
212 );
213 }
214
215 /**
216 * @dataProvider provideParseErrors
217 * @param mixed $value
218 */
219 public function testParseErrors( $value ) {
220 $st = FormatJson::parse( $value );
221 $this->assertType( 'Status', $st );
222 $this->assertFalse( $st->isOK() );
223 }
224
225 public function provideStripComments() {
226 return array(
227 array( '{"a":"b"}', '{"a":"b"}' ),
228 array( "{\"a\":\"b\"}\n", "{\"a\":\"b\"}\n" ),
229 array( '/*c*/{"c":"b"}', '{"c":"b"}' ),
230 array( '{"a":"c"}/*c*/', '{"a":"c"}' ),
231 array( '/*c//d*/{"c":"b"}', '{"c":"b"}' ),
232 array( '{/*c*/"c":"b"}', '{"c":"b"}' ),
233 array( "/*\nc\r\n*/{\"c\":\"b\"}", '{"c":"b"}' ),
234 array( "//c\n{\"c\":\"b\"}", '{"c":"b"}' ),
235 array( "//c\r\n{\"c\":\"b\"}", '{"c":"b"}' ),
236 array( '{"a":"c"}//c', '{"a":"c"}' ),
237 array( "{\"a-c\"://c\n\"b\"}", '{"a-c":"b"}' ),
238 array( '{"/*a":"b"}', '{"/*a":"b"}' ),
239 array( '{"a":"//b"}', '{"a":"//b"}' ),
240 array( '{"a":"b/*c*/"}', '{"a":"b/*c*/"}' ),
241 array( "{\"\\\"/*a\":\"b\"}", "{\"\\\"/*a\":\"b\"}" ),
242 array( '', '' ),
243 array( '/*c', '' ),
244 array( '//c', '' ),
245 array( '"http://example.com"', '"http://example.com"' ),
246 array( "\0", "\0" ),
247 array( '"Blåbærsyltetøy"', '"Blåbærsyltetøy"' ),
248 );
249 }
250
251 /**
252 * @covers FormatJson::stripComments
253 * @dataProvider provideStripComments
254 * @param string $json
255 * @param string $expect
256 */
257 public function testStripComments( $json, $expect ) {
258 $this->assertSame( $expect, FormatJson::stripComments( $json ) );
259 }
260
261 public function provideParseStripComments() {
262 return array(
263 array( '/* blah */true', true ),
264 array( "// blah \ntrue", true ),
265 array( '[ "a" , /* blah */ "b" ]', array( 'a', 'b' ) ),
266 );
267 }
268
269 /**
270 * @covers FormatJson::parse
271 * @covers FormatJson::stripComments
272 * @dataProvider provideParseStripComments
273 * @param string $json
274 * @param mixed $expect
275 */
276 public function testParseStripComments( $json, $expect ) {
277 $st = FormatJson::parse( $json, FormatJson::STRIP_COMMENTS );
278 $this->assertType( 'Status', $st );
279 $this->assertTrue( $st->isGood() );
280 $this->assertEquals( $expect, $st->getValue() );
281 }
282
283 /**
284 * Generate a set of test cases for a particular combination of encoder options.
285 *
286 * @param array $unescapedGroups List of character groups to leave unescaped
287 * @return array Arrays of unencoded strings and corresponding encoded strings
288 */
289 private static function getEncodeTestCases( array $unescapedGroups ) {
290 $groups = array(
291 'always' => array(
292 // Forward slash (always unescaped)
293 '/' => '/',
294
295 // Control characters
296 "\0" => '\u0000',
297 "\x08" => '\b',
298 "\t" => '\t',
299 "\n" => '\n',
300 "\r" => '\r',
301 "\f" => '\f',
302 "\x1f" => '\u001f', // representative example
303
304 // Double quotes
305 '"' => '\"',
306
307 // Backslashes
308 '\\' => '\\\\',
309 '\\\\' => '\\\\\\\\',
310 '\\u00e9' => '\\\u00e9', // security check for Unicode unescaping
311
312 // Line terminators
313 "\xe2\x80\xa8" => '\u2028',
314 "\xe2\x80\xa9" => '\u2029',
315 ),
316 'unicode' => array(
317 "\xc3\xa9" => '\u00e9',
318 "\xf0\x9d\x92\x9e" => '\ud835\udc9e', // U+1D49E, outside the BMP
319 ),
320 'xmlmeta' => array(
321 '<' => '\u003C', // JSON_HEX_TAG uses uppercase hex digits
322 '>' => '\u003E',
323 '&' => '\u0026',
324 ),
325 );
326
327 $cases = array();
328 foreach ( $groups as $name => $rules ) {
329 $leaveUnescaped = in_array( $name, $unescapedGroups );
330 foreach ( $rules as $from => $to ) {
331 $cases[] = array( $from, '"' . ( $leaveUnescaped ? $from : $to ) . '"' );
332 }
333 }
334
335 return $cases;
336 }
337 }