Add DROP INDEX support to DatabaseSqlite::replaceVars method
[lhc/web/wiklou.git] / tests / phpunit / includes / json / FormatJsonTest.php
1 <?php
2
3 class FormatJsonTest extends MediaWikiTestCase {
4
5 public function testEncoderPrettyPrinting() {
6 $obj = array(
7 'emptyObject' => new stdClass,
8 'emptyArray' => array(),
9 'string' => 'foobar\\',
10 'filledArray' => array(
11 array(
12 123,
13 456,
14 ),
15 '"7":["8",{"9":"10"}]',
16 ),
17 );
18
19 // 4 space indent, no trailing whitespace, no trailing linefeed
20 $json = '{
21 "emptyObject": {
22 },
23 "emptyArray": [
24 ],
25 "string": "foobar\\\\",
26 "filledArray": [
27 [
28 123,
29 456
30 ],
31 "\"7\":[\"8\",{\"9\":\"10\"}]"
32 ]
33 }';
34
35 $json = str_replace( "\r", '', $json ); // Windows compat
36 $this->assertSame( $json, str_replace("\n\n", "\n", FormatJson::encode( $obj, true ) ));
37 }
38
39 public static function provideEncodeDefault() {
40 return self::getEncodeTestCases( array() );
41 }
42
43 /**
44 * @dataProvider provideEncodeDefault
45 */
46 public function testEncodeDefault( $from, $to ) {
47 $this->assertSame( $to, FormatJson::encode( $from ) );
48 }
49
50 public static function provideEncodeUtf8() {
51 return self::getEncodeTestCases( array( 'unicode' ) );
52 }
53
54 /**
55 * @dataProvider provideEncodeUtf8
56 */
57 public function testEncodeUtf8( $from, $to ) {
58 $this->assertSame( $to, FormatJson::encode( $from, false, FormatJson::UTF8_OK ) );
59 }
60
61 public static function provideEncodeXmlMeta() {
62 return self::getEncodeTestCases( array( 'xmlmeta' ) );
63 }
64
65 /**
66 * @dataProvider provideEncodeXmlMeta
67 */
68 public function testEncodeXmlMeta( $from, $to ) {
69 $this->assertSame( $to, FormatJson::encode( $from, false, FormatJson::XMLMETA_OK ) );
70 }
71
72 public static function provideEncodeAllOk() {
73 return self::getEncodeTestCases( array( 'unicode', 'xmlmeta' ) );
74 }
75
76 /**
77 * @dataProvider provideEncodeAllOk
78 */
79 public function testEncodeAllOk( $from, $to ) {
80 $this->assertSame( $to, FormatJson::encode( $from, false, FormatJson::ALL_OK ) );
81 }
82
83 public function testEncodePhpBug46944() {
84 $this->assertNotEquals(
85 '\ud840\udc00',
86 strtolower( FormatJson::encode( "\xf0\xa0\x80\x80" ) ),
87 'Test encoding an broken json_encode character (U+20000)'
88 );
89 }
90
91 public function testDecodeReturnType() {
92 $this->assertInternalType(
93 'object',
94 FormatJson::decode( '{"Name": "Cheeso", "Rank": 7}' ),
95 'Default to object'
96 );
97
98 $this->assertInternalType(
99 'array',
100 FormatJson::decode( '{"Name": "Cheeso", "Rank": 7}', true ),
101 'Optional array'
102 );
103 }
104
105 /**
106 * Generate a set of test cases for a particular combination of encoder options.
107 *
108 * @param array $unescapedGroups List of character groups to leave unescaped
109 * @return array: Arrays of unencoded strings and corresponding encoded strings
110 */
111 private static function getEncodeTestCases( array $unescapedGroups ) {
112 $groups = array(
113 'always' => array(
114 // Forward slash (always unescaped)
115 '/' => '/',
116
117 // Control characters
118 "\0" => '\u0000',
119 "\x08" => '\b',
120 "\t" => '\t',
121 "\n" => '\n',
122 "\r" => '\r',
123 "\f" => '\f',
124 "\x1f" => '\u001f', // representative example
125
126 // Double quotes
127 '"' => '\"',
128
129 // Backslashes
130 '\\' => '\\\\',
131 '\\\\' => '\\\\\\\\',
132 '\\u00e9' => '\\\u00e9', // security check for Unicode unescaping
133
134 // Line terminators
135 "\xe2\x80\xa8" => '\u2028',
136 "\xe2\x80\xa9" => '\u2029',
137 ),
138 'unicode' => array(
139 "\xc3\xa9" => '\u00e9',
140 "\xf0\x9d\x92\x9e" => '\ud835\udc9e', // U+1D49E, outside the BMP
141 ),
142 'xmlmeta' => array(
143 '<' => '\u003C', // JSON_HEX_TAG uses uppercase hex digits
144 '>' => '\u003E',
145 '&' => '\u0026',
146 ),
147 );
148
149 $cases = array();
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 ) . '"' );
154 }
155 }
156
157 return $cases;
158 }
159 }