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
25 "string": "foobar\\\\",
31 "\"7\":[\"8\",{\"9\":\"10\"}]"
35 $json = str_replace( "\r", '', $json ); // Windows compat
36 $this->assertSame( $json, str_replace("\n\n", "\n", FormatJson
::encode( $obj, true ) ));
39 public static function provideEncodeDefault() {
40 return self
::getEncodeTestCases( array() );
44 * @dataProvider provideEncodeDefault
46 public function testEncodeDefault( $from, $to ) {
47 $this->assertSame( $to, FormatJson
::encode( $from ) );
50 public static function provideEncodeUtf8() {
51 return self
::getEncodeTestCases( array( 'unicode' ) );
55 * @dataProvider provideEncodeUtf8
57 public function testEncodeUtf8( $from, $to ) {
58 $this->assertSame( $to, FormatJson
::encode( $from, false, FormatJson
::UTF8_OK
) );
61 public static function provideEncodeXmlMeta() {
62 return self
::getEncodeTestCases( array( 'xmlmeta' ) );
66 * @dataProvider provideEncodeXmlMeta
68 public function testEncodeXmlMeta( $from, $to ) {
69 $this->assertSame( $to, FormatJson
::encode( $from, false, FormatJson
::XMLMETA_OK
) );
72 public static function provideEncodeAllOk() {
73 return self
::getEncodeTestCases( array( 'unicode', 'xmlmeta' ) );
77 * @dataProvider provideEncodeAllOk
79 public function testEncodeAllOk( $from, $to ) {
80 $this->assertSame( $to, FormatJson
::encode( $from, false, FormatJson
::ALL_OK
) );
83 public function testEncodePhpBug46944() {
84 $this->assertNotEquals(
86 strtolower( FormatJson
::encode( "\xf0\xa0\x80\x80" ) ),
87 'Test encoding an broken json_encode character (U+20000)'
91 public function testDecodeReturnType() {
92 $this->assertInternalType(
94 FormatJson
::decode( '{"Name": "Cheeso", "Rank": 7}' ),
98 $this->assertInternalType(
100 FormatJson
::decode( '{"Name": "Cheeso", "Rank": 7}', true ),
106 * Generate a set of test cases for a particular combination of encoder options.
108 * @param array $unescapedGroups List of character groups to leave unescaped
109 * @return array: Arrays of unencoded strings and corresponding encoded strings
111 private static function getEncodeTestCases( array $unescapedGroups ) {
114 // Forward slash (always unescaped)
117 // Control characters
124 "\x1f" => '\u001f', // representative example
131 '\\\\' => '\\\\\\\\',
132 '\\u00e9' => '\\\u00e9', // security check for Unicode unescaping
135 "\xe2\x80\xa8" => '\u2028',
136 "\xe2\x80\xa9" => '\u2029',
139 "\xc3\xa9" => '\u00e9',
140 "\xf0\x9d\x92\x9e" => '\ud835\udc9e', // U+1D49E, outside the BMP
143 '<' => '\u003C', // JSON_HEX_TAG uses uppercase hex digits
150 foreach ( $groups as $name => $rules ) {
151 $leaveUnescaped = in_array( $name, $unescapedGroups );
152 foreach ( $rules as $from => $to ) {
153 $cases[] = array( $from, '"' . ( $leaveUnescaped ?
$from : $to ) . '"' );