Merge "maintenance: Script to rename titles for Unicode uppercasing changes"
[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 /**
9 * Test data for testParseTryFixing.
10 *
11 * Some PHP interpreters use json-c rather than the JSON.org canonical
12 * parser to avoid being encumbered by the "shall be used for Good, not
13 * Evil" clause of the JSON.org parser's license. By default, json-c
14 * parses in a non-strict mode which allows trailing commas for array and
15 * object delarations among other things, so our JSON_ERROR_SYNTAX rescue
16 * block is not always triggered. It however isn't lenient in exactly the
17 * same ways as our TRY_FIXING mode, so the assertions in this test are
18 * a bit more complicated than they ideally would be:
19 *
20 * Optional third argument: true if json-c parses the value without
21 * intervention, false otherwise. Defaults to true.
22 *
23 * Optional fourth argument: expected cannonical JSON serialization of
24 * json-c parsed result. Defaults to the second argument's value.
25 */
26 public static function provideParseTryFixing() {
27 return [
28 [ "[,]", '[]', false ],
29 [ "[ , ]", '[]', false ],
30 [ "[ , }", false ],
31 [ '[1],', false, true, '[1]' ],
32 [ "[1,]", '[1]' ],
33 [ "[1\n,]", '[1]' ],
34 [ "[1,\n]", '[1]' ],
35 [ "[1,]\n", '[1]' ],
36 [ "[1\n,\n]\n", '[1]' ],
37 [ '["a,",]', '["a,"]' ],
38 [ "[[1,]\n,[2,\n],[3\n,]]", '[[1],[2],[3]]' ],
39 // I wish we could parse this, but would need quote parsing
40 [ '[[1,],[2,],[3,]]', false, true, '[[1],[2],[3]]' ],
41 [ '[1,,]', false, false, '[1]' ],
42 ];
43 }
44
45 /**
46 * @dataProvider provideParseTryFixing
47 * @param string $value
48 * @param string|bool $expected Expected result with strict parser
49 * @param bool $jsoncParses Will json-c parse this value without TRY_FIXING?
50 * @param string|bool $expectedJsonc Expected result with lenient parser
51 * if different from the strict expectation
52 */
53 public function testParseTryFixing(
54 $value, $expected,
55 $jsoncParses = true, $expectedJsonc = null
56 ) {
57 // PHP5 results are always expected to have isGood() === false
58 $expectedGoodStatus = false;
59
60 // Check to see if json parser allows trailing commas
61 if ( json_decode( '[1,]' ) !== null ) {
62 // Use json-c specific expected result if provided
63 $expected = ( $expectedJsonc === null ) ? $expected : $expectedJsonc;
64 // If json-c parses the value natively, expect isGood() === true
65 $expectedGoodStatus = $jsoncParses;
66 }
67
68 $st = FormatJson::parse( $value, FormatJson::TRY_FIXING );
69 $this->assertInstanceOf( Status::class, $st );
70 if ( $expected === false ) {
71 $this->assertFalse( $st->isOK(), 'Expected isOK() == false' );
72 } else {
73 $this->assertSame( $expectedGoodStatus, $st->isGood(),
74 'Expected isGood() == ' . ( $expectedGoodStatus ? 'true' : 'false' )
75 );
76 $this->assertTrue( $st->isOK(), 'Expected isOK == true' );
77 $val = FormatJson::encode( $st->getValue(), false, FormatJson::ALL_OK );
78 $this->assertEquals( $expected, $val );
79 }
80 }
81
82 }