X-Git-Url: https://git.cyclocoop.org/%242?a=blobdiff_plain;f=tests%2Fphpunit%2Fincludes%2Fjson%2FFormatJsonTest.php;h=a6adf343d5af32df976312461280b82eca0c4c60;hb=ca31b7f793df5ef6d1fc31b4a43c88313067b619;hp=a4ab879ffb772c70c10c04bd884e7cdb2b8d422b;hpb=3f59cb9f3a53ad28f8a95fe299c5de6abd24b453;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/phpunit/includes/json/FormatJsonTest.php b/tests/phpunit/includes/json/FormatJsonTest.php index a4ab879ffb..a6adf343d5 100644 --- a/tests/phpunit/includes/json/FormatJsonTest.php +++ b/tests/phpunit/includes/json/FormatJsonTest.php @@ -109,6 +109,15 @@ class FormatJsonTest extends MediaWikiTestCase { ); } + public function testEncodeFail() { + // Set up a recursive object that can't be encoded. + $a = new stdClass; + $b = new stdClass; + $a->b = $b; + $b->a = $a; + $this->assertFalse( FormatJson::encode( $a ) ); + } + public function testDecodeReturnType() { $this->assertInternalType( 'object', @@ -172,7 +181,7 @@ class FormatJsonTest extends MediaWikiTestCase { /** * Test data for testParseTryFixing. * - * Some PHP interpreters use json-c rather than the JSON.org cannonical + * Some PHP interpreters use json-c rather than the JSON.org canonical * parser to avoid being encumbered by the "shall be used for Good, not * Evil" clause of the JSON.org parser's license. By default, json-c * parses in a non-strict mode which allows trailing commas for array and @@ -372,4 +381,56 @@ class FormatJsonTest extends MediaWikiTestCase { return $cases; } + + public function provideEmptyJsonKeyStrings() { + return [ + [ + '{"":"foo"}', + '{"":"foo"}', + '' + ], + [ + '{"_empty_":"foo"}', + '{"_empty_":"foo"}', + '_empty_' ], + [ + '{"\u005F\u0065\u006D\u0070\u0074\u0079\u005F":"foo"}', + '{"_empty_":"foo"}', + '_empty_' + ], + [ + '{"_empty_":"bar","":"foo"}', + '{"_empty_":"bar","":"foo"}', + '' + ], + [ + '{"":"bar","_empty_":"foo"}', + '{"":"bar","_empty_":"foo"}', + '_empty_' + ] + ]; + } + + /** + * @covers FormatJson::encode + * @covers FormatJson::decode + * @dataProvider provideEmptyJsonKeyStrings + * @param string $json + * + * Decoding behavior with empty keys can be surprising. + * See https://phabricator.wikimedia.org/T206411 + */ + public function testEmptyJsonKeyArray( $json, $expect, $php71Name ) { + // Decoding to array is consistent across supported PHP versions + $this->assertSame( $expect, FormatJson::encode( + FormatJson::decode( $json, true ) ) ); + + // Decoding to object differs between supported PHP versions + $obj = FormatJson::decode( $json ); + if ( version_compare( PHP_VERSION, '7.1', '<' ) ) { + $this->assertEquals( 'foo', $obj->_empty_ ); + } else { + $this->assertEquals( 'foo', $obj->{$php71Name} ); + } + } }