6 class FormatJsonTest
extends MediaWikiTestCase
{
8 public function testEncoderPrettyPrinting() {
10 'emptyObject' => new stdClass
,
11 'emptyArray' => array(),
12 'string' => 'foobar\\',
13 'filledArray' => array(
18 // Nested json works without problems
19 '"7":["8",{"9":"10"}]',
20 // Whitespace clean up doesn't touch strings that look alike
21 "{\n\t\"emptyObject\": {\n\t},\n\t\"emptyArray\": [ ]\n}",
25 // 4 space indent, no trailing whitespace, no trailing linefeed
29 "string": "foobar\\\\",
35 "\"7\":[\"8\",{\"9\":\"10\"}]",
36 "{\n\t\"emptyObject\": {\n\t},\n\t\"emptyArray\": [ ]\n}"
40 $json = str_replace( "\r", '', $json ); // Windows compat
41 $this->assertSame( $json, FormatJson
::encode( $obj, true ) );
44 public static function provideEncodeDefault() {
45 return self
::getEncodeTestCases( array() );
49 * @dataProvider provideEncodeDefault
51 public function testEncodeDefault( $from, $to ) {
52 $this->assertSame( $to, FormatJson
::encode( $from ) );
55 public static function provideEncodeUtf8() {
56 return self
::getEncodeTestCases( array( 'unicode' ) );
60 * @dataProvider provideEncodeUtf8
62 public function testEncodeUtf8( $from, $to ) {
63 $this->assertSame( $to, FormatJson
::encode( $from, false, FormatJson
::UTF8_OK
) );
66 public static function provideEncodeXmlMeta() {
67 return self
::getEncodeTestCases( array( 'xmlmeta' ) );
71 * @dataProvider provideEncodeXmlMeta
73 public function testEncodeXmlMeta( $from, $to ) {
74 $this->assertSame( $to, FormatJson
::encode( $from, false, FormatJson
::XMLMETA_OK
) );
77 public static function provideEncodeAllOk() {
78 return self
::getEncodeTestCases( array( 'unicode', 'xmlmeta' ) );
82 * @dataProvider provideEncodeAllOk
84 public function testEncodeAllOk( $from, $to ) {
85 $this->assertSame( $to, FormatJson
::encode( $from, false, FormatJson
::ALL_OK
) );
88 public function testEncodePhpBug46944() {
89 $this->assertNotEquals(
91 strtolower( FormatJson
::encode( "\xf0\xa0\x80\x80" ) ),
92 'Test encoding an broken json_encode character (U+20000)'
96 public function testDecodeReturnType() {
97 $this->assertInternalType(
99 FormatJson
::decode( '{"Name": "Cheeso", "Rank": 7}' ),
103 $this->assertInternalType(
105 FormatJson
::decode( '{"Name": "Cheeso", "Rank": 7}', true ),
111 * Generate a set of test cases for a particular combination of encoder options.
113 * @param array $unescapedGroups List of character groups to leave unescaped
114 * @return array: Arrays of unencoded strings and corresponding encoded strings
116 private static function getEncodeTestCases( array $unescapedGroups ) {
119 // Forward slash (always unescaped)
122 // Control characters
129 "\x1f" => '\u001f', // representative example
136 '\\\\' => '\\\\\\\\',
137 '\\u00e9' => '\\\u00e9', // security check for Unicode unescaping
140 "\xe2\x80\xa8" => '\u2028',
141 "\xe2\x80\xa9" => '\u2029',
144 "\xc3\xa9" => '\u00e9',
145 "\xf0\x9d\x92\x9e" => '\ud835\udc9e', // U+1D49E, outside the BMP
148 '<' => '\u003C', // JSON_HEX_TAG uses uppercase hex digits
155 foreach ( $groups as $name => $rules ) {
156 $leaveUnescaped = in_array( $name, $unescapedGroups );
157 foreach ( $rules as $from => $to ) {
158 $cases[] = array( $from, '"' . ( $leaveUnescaped ?
$from : $to ) . '"' );