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)'
93 public function testDecodeReturnType() {
94 $this->assertInternalType(
96 FormatJson
::decode( '{"Name": "Cheeso", "Rank": 7}' ),
100 $this->assertInternalType(
102 FormatJson
::decode( '{"Name": "Cheeso", "Rank": 7}', true ),
108 * Generate a set of test cases for a particular combination of encoder options.
110 * @param array $unescapedGroups List of character groups to leave unescaped
111 * @return array: Arrays of unencoded strings and corresponding encoded strings
113 private static function getEncodeTestCases( array $unescapedGroups ) {
116 // Forward slash (always unescaped)
119 // Control characters
126 "\x1f" => '\u001f', // representative example
133 '\\\\' => '\\\\\\\\',
134 '\\u00e9' => '\\\u00e9', // security check for Unicode unescaping
137 "\xe2\x80\xa8" => '\u2028',
138 "\xe2\x80\xa9" => '\u2029',
141 "\xc3\xa9" => '\u00e9',
142 "\xf0\x9d\x92\x9e" => '\ud835\udc9e', // U+1D49E, outside the BMP
145 '<' => '\u003C', // JSON_HEX_TAG uses uppercase hex digits
152 foreach ( $groups as $name => $rules ) {
153 $leaveUnescaped = in_array( $name, $unescapedGroups );
154 foreach ( $rules as $from => $to ) {
155 $cases[] = array( $from, '"' . ( $leaveUnescaped ?
$from : $to ) . '"' );