3 class FormatJsonTest
extends MediaWikiTestCase
{
5 public function testEncoderPrettyPrinting() {
7 'emptyObject' => new stdClass
,
8 'emptyArray' => array(),
9 'string' => 'foobar\\',
10 'filledArray' => array(
15 '"7":["8",{"9":"10"}]',
19 // 4 space indent, no trailing whitespace, no trailing linefeed
27 "string": "foobar\\\\",
33 "\"7\":[\"8\",{\"9\":\"10\"}]"
37 $json = str_replace( "\r", '', $json ); // Windows compat
38 $this->assertSame( $json, FormatJson
::encode( $obj, true ) );
41 public static function provideEncodeDefault() {
42 return self
::getEncodeTestCases( array() );
46 * @dataProvider provideEncodeDefault
48 public function testEncodeDefault( $from, $to ) {
49 $this->assertSame( $to, FormatJson
::encode( $from ) );
52 public static function provideEncodeUtf8() {
53 return self
::getEncodeTestCases( array( 'unicode' ) );
57 * @dataProvider provideEncodeUtf8
59 public function testEncodeUtf8( $from, $to ) {
60 $this->assertSame( $to, FormatJson
::encode( $from, false, FormatJson
::UTF8_OK
) );
63 public static function provideEncodeXmlMeta() {
64 return self
::getEncodeTestCases( array( 'xmlmeta' ) );
68 * @dataProvider provideEncodeXmlMeta
70 public function testEncodeXmlMeta( $from, $to ) {
71 $this->assertSame( $to, FormatJson
::encode( $from, false, FormatJson
::XMLMETA_OK
) );
74 public static function provideEncodeAllOk() {
75 return self
::getEncodeTestCases( array( 'unicode', 'xmlmeta' ) );
79 * @dataProvider provideEncodeAllOk
81 public function testEncodeAllOk( $from, $to ) {
82 $this->assertSame( $to, FormatJson
::encode( $from, false, FormatJson
::ALL_OK
) );
85 public function testEncodePhpBug46944() {
86 $this->assertNotEquals(
88 strtolower( FormatJson
::encode( "\xf0\xa0\x80\x80" ) ),
89 'Test encoding an broken json_encode character (U+20000)'
94 public function testDecodeReturnType() {
95 $this->assertInternalType(
97 FormatJson
::decode( '{"Name": "Cheeso", "Rank": 7}' ),
101 $this->assertInternalType(
103 FormatJson
::decode( '{"Name": "Cheeso", "Rank": 7}', true ),
109 * Generate a set of test cases for a particular combination of encoder options.
111 * @param array $unescapedGroups List of character groups to leave unescaped
112 * @return array: Arrays of unencoded strings and corresponding encoded strings
114 private static function getEncodeTestCases( array $unescapedGroups ) {
117 // Forward slash (always unescaped)
120 // Control characters
127 "\x1f" => '\u001f', // representative example
134 '\\\\' => '\\\\\\\\',
135 '\\u00e9' => '\\\u00e9', // security check for Unicode unescaping
138 "\xe2\x80\xa8" => '\u2028',
139 "\xe2\x80\xa9" => '\u2029',
142 "\xc3\xa9" => '\u00e9',
143 "\xf0\x9d\x92\x9e" => '\ud835\udc9e', // U+1D49E, outside the BMP
146 '<' => '\u003C', // JSON_HEX_TAG uses uppercase hex digits
153 foreach ( $groups as $name => $rules ) {
154 $leaveUnescaped = in_array( $name, $unescapedGroups );
155 foreach ( $rules as $from => $to ) {
156 $cases[] = array( $from, '"' . ( $leaveUnescaped ?
$from : $to ) . '"' );