MediaWiki  1.23.13
FormatJsonTest.php
Go to the documentation of this file.
1 <?php
2 
7 
8  public static function provideEncoderPrettyPrinting() {
9  return array(
10  // Four spaces
11  array( true, ' ' ),
12  array( ' ', ' ' ),
13  // Two spaces
14  array( ' ', ' ' ),
15  // One tab
16  array( "\t", "\t" ),
17  );
18  }
19 
23  public function testEncoderPrettyPrinting( $pretty, $expectedIndent ) {
24  $obj = array(
25  'emptyObject' => new stdClass,
26  'emptyArray' => array(),
27  'string' => 'foobar\\',
28  'filledArray' => array(
29  array(
30  123,
31  456,
32  ),
33  // Nested json works without problems
34  '"7":["8",{"9":"10"}]',
35  // Whitespace clean up doesn't touch strings that look alike
36  "{\n\t\"emptyObject\": {\n\t},\n\t\"emptyArray\": [ ]\n}",
37  ),
38  );
39 
40  // No trailing whitespace, no trailing linefeed
41  $json = '{
42  "emptyObject": {},
43  "emptyArray": [],
44  "string": "foobar\\\\",
45  "filledArray": [
46  [
47  123,
48  456
49  ],
50  "\"7\":[\"8\",{\"9\":\"10\"}]",
51  "{\n\t\"emptyObject\": {\n\t},\n\t\"emptyArray\": [ ]\n}"
52  ]
53 }';
54 
55  $json = str_replace( "\r", '', $json ); // Windows compat
56  $json = str_replace( "\t", $expectedIndent, $json );
57  $this->assertSame( $json, FormatJson::encode( $obj, $pretty ) );
58  }
59 
60  public static function provideEncodeDefault() {
61  return self::getEncodeTestCases( array() );
62  }
63 
67  public function testEncodeDefault( $from, $to ) {
68  $this->assertSame( $to, FormatJson::encode( $from ) );
69  }
70 
71  public static function provideEncodeUtf8() {
72  return self::getEncodeTestCases( array( 'unicode' ) );
73  }
74 
78  public function testEncodeUtf8( $from, $to ) {
79  $this->assertSame( $to, FormatJson::encode( $from, false, FormatJson::UTF8_OK ) );
80  }
81 
82  public static function provideEncodeXmlMeta() {
83  return self::getEncodeTestCases( array( 'xmlmeta' ) );
84  }
85 
89  public function testEncodeXmlMeta( $from, $to ) {
90  $this->assertSame( $to, FormatJson::encode( $from, false, FormatJson::XMLMETA_OK ) );
91  }
92 
93  public static function provideEncodeAllOk() {
94  return self::getEncodeTestCases( array( 'unicode', 'xmlmeta' ) );
95  }
96 
100  public function testEncodeAllOk( $from, $to ) {
101  $this->assertSame( $to, FormatJson::encode( $from, false, FormatJson::ALL_OK ) );
102  }
103 
104  public function testEncodePhpBug46944() {
105  $this->assertNotEquals(
106  '\ud840\udc00',
107  strtolower( FormatJson::encode( "\xf0\xa0\x80\x80" ) ),
108  'Test encoding an broken json_encode character (U+20000)'
109  );
110  }
111 
112  public function testDecodeReturnType() {
113  $this->assertInternalType(
114  'object',
115  FormatJson::decode( '{"Name": "Cheeso", "Rank": 7}' ),
116  'Default to object'
117  );
118 
119  $this->assertInternalType(
120  'array',
121  FormatJson::decode( '{"Name": "Cheeso", "Rank": 7}', true ),
122  'Optional array'
123  );
124  }
125 
132  private static function getEncodeTestCases( array $unescapedGroups ) {
133  $groups = array(
134  'always' => array(
135  // Forward slash (always unescaped)
136  '/' => '/',
137 
138  // Control characters
139  "\0" => '\u0000',
140  "\x08" => '\b',
141  "\t" => '\t',
142  "\n" => '\n',
143  "\r" => '\r',
144  "\f" => '\f',
145  "\x1f" => '\u001f', // representative example
146 
147  // Double quotes
148  '"' => '\"',
149 
150  // Backslashes
151  '\\' => '\\\\',
152  '\\\\' => '\\\\\\\\',
153  '\\u00e9' => '\\\u00e9', // security check for Unicode unescaping
154 
155  // Line terminators
156  "\xe2\x80\xa8" => '\u2028',
157  "\xe2\x80\xa9" => '\u2029',
158  ),
159  'unicode' => array(
160  "\xc3\xa9" => '\u00e9',
161  "\xf0\x9d\x92\x9e" => '\ud835\udc9e', // U+1D49E, outside the BMP
162  ),
163  'xmlmeta' => array(
164  '<' => '\u003C', // JSON_HEX_TAG uses uppercase hex digits
165  '>' => '\u003E',
166  '&' => '\u0026',
167  ),
168  );
169 
170  $cases = array();
171  foreach ( $groups as $name => $rules ) {
172  $leaveUnescaped = in_array( $name, $unescapedGroups );
173  foreach ( $rules as $from => $to ) {
174  $cases[] = array( $from, '"' . ( $leaveUnescaped ? $from : $to ) . '"' );
175  }
176  }
177 
178  return $cases;
179  }
180 }
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
FormatJsonTest\testEncodeXmlMeta
testEncodeXmlMeta( $from, $to)
@dataProvider provideEncodeXmlMeta
Definition: FormatJsonTest.php:89
FormatJson\XMLMETA_OK
const XMLMETA_OK
Skip escaping the characters '<', '>', and '&', which have special meanings in HTML and XML.
Definition: FormatJson.php:46
FormatJsonTest\testEncodeDefault
testEncodeDefault( $from, $to)
@dataProvider provideEncodeDefault
Definition: FormatJsonTest.php:67
$from
$from
Definition: importImages.php:90
FormatJsonTest\testEncodeAllOk
testEncodeAllOk( $from, $to)
@dataProvider provideEncodeAllOk
Definition: FormatJsonTest.php:100
FormatJsonTest\provideEncodeAllOk
static provideEncodeAllOk()
Definition: FormatJsonTest.php:93
FormatJson\ALL_OK
const ALL_OK
Skip escaping as many characters as reasonably possible.
Definition: FormatJson.php:55
FormatJson\UTF8_OK
const UTF8_OK
Skip escaping most characters above U+007F for readability and compactness.
Definition: FormatJson.php:34
FormatJson\decode
static decode( $value, $assoc=false)
Decodes a JSON string.
Definition: FormatJson.php:126
FormatJson\encode
static encode( $value, $pretty=false, $escaping=0)
Returns the JSON representation of a value.
Definition: FormatJson.php:104
FormatJsonTest\provideEncodeUtf8
static provideEncodeUtf8()
Definition: FormatJsonTest.php:71
FormatJsonTest\testEncoderPrettyPrinting
testEncoderPrettyPrinting( $pretty, $expectedIndent)
@dataProvider provideEncoderPrettyPrinting
Definition: FormatJsonTest.php:23
FormatJsonTest\testEncodeUtf8
testEncodeUtf8( $from, $to)
@dataProvider provideEncodeUtf8
Definition: FormatJsonTest.php:78
MediaWikiTestCase
Definition: MediaWikiTestCase.php:6
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
FormatJsonTest\provideEncoderPrettyPrinting
static provideEncoderPrettyPrinting()
Definition: FormatJsonTest.php:8
FormatJsonTest\testEncodePhpBug46944
testEncodePhpBug46944()
Definition: FormatJsonTest.php:104
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:336
FormatJsonTest\getEncodeTestCases
static getEncodeTestCases(array $unescapedGroups)
Generate a set of test cases for a particular combination of encoder options.
Definition: FormatJsonTest.php:132
FormatJsonTest\provideEncodeDefault
static provideEncodeDefault()
Definition: FormatJsonTest.php:60
FormatJsonTest\testDecodeReturnType
testDecodeReturnType()
Definition: FormatJsonTest.php:112
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
FormatJsonTest
@covers FormatJson
Definition: FormatJsonTest.php:6
FormatJsonTest\provideEncodeXmlMeta
static provideEncodeXmlMeta()
Definition: FormatJsonTest.php:82