5 * @covers ApiFormatJson
7 class ApiFormatJsonTest
extends ApiFormatTestBase
{
9 protected $printerName = 'json';
11 private static function addFormatVersion( $format, $arr ) {
12 foreach ( $arr as &$p ) {
13 if ( !isset( $p[2] ) ) {
14 $p[2] = [ 'formatversion' => $format ];
16 $p[2]['formatversion'] = $format;
22 public static function provideGeneralEncoding() {
24 self
::addFormatVersion( 1, [
26 [ [ null ], '[null]' ],
29 [ [ true, ApiResult
::META_BC_BOOLS
=> [ 0 ] ], '[true]' ],
30 [ [ false, ApiResult
::META_BC_BOOLS
=> [ 0 ] ], '[false]' ],
32 [ [ 42.5 ], '[42.5]' ],
33 [ [ 1e42
], '[1.0e+42]' ],
34 [ [ 'foo' ], '["foo"]' ],
35 [ [ 'fóo' ], '["f\u00f3o"]' ],
36 [ [ 'fóo' ], '["fóo"]', [ 'utf8' => 1 ] ],
40 [ [ [ 1 ] ], '[[1]]' ],
41 [ [ [ 'x' => 1 ] ], '[{"x":1}]' ],
42 [ [ [ 2 => 1 ] ], '[{"2":1}]' ],
43 [ [ (object)[] ], '[{}]' ],
44 [ [ [ 1, ApiResult
::META_TYPE
=> 'assoc' ] ], '[{"0":1}]' ],
45 [ [ [ 'x' => 1, ApiResult
::META_TYPE
=> 'array' ] ], '[[1]]' ],
46 [ [ [ 'x' => 1, ApiResult
::META_TYPE
=> 'kvp' ] ], '[{"x":1}]' ],
50 ApiResult
::META_TYPE
=> 'BCkvp',
51 ApiResult
::META_KVP_KEY_NAME
=> 'key'
53 '[[{"key":"x","*":1}]]'
55 [ [ [ 'x' => 1, ApiResult
::META_TYPE
=> 'BCarray' ] ], '[{"x":1}]' ],
56 [ [ [ 'a', 'b', ApiResult
::META_TYPE
=> 'BCassoc' ] ], '[["a","b"]]' ],
59 [ [ 'content' => 'foo', ApiResult
::META_CONTENT
=> 'content' ],
63 [ [ 'foo' => 'foo', ApiResult
::META_BC_SUBELEMENTS
=> [ 'foo' ] ],
64 '{"foo":{"*":"foo"}}' ],
67 [ [ 1 ], '/**/myCallback([1])', [ 'callback' => 'myCallback' ] ],
69 // Cross-domain mangling
70 [ [ '< Cross-Domain-Policy >' ], '["\u003C Cross-Domain-Policy >"]' ],
72 self
::addFormatVersion( 2, [
74 [ [ null ], '[null]' ],
75 [ [ true ], '[true]' ],
76 [ [ false ], '[false]' ],
77 [ [ true, ApiResult
::META_BC_BOOLS
=> [ 0 ] ], '[true]' ],
78 [ [ false, ApiResult
::META_BC_BOOLS
=> [ 0 ] ], '[false]' ],
80 [ [ 42.5 ], '[42.5]' ],
81 [ [ 1e42
], '[1.0e+42]' ],
82 [ [ 'foo' ], '["foo"]' ],
83 [ [ 'fóo' ], '["fóo"]' ],
84 [ [ 'fóo' ], '["f\u00f3o"]', [ 'ascii' => 1 ] ],
88 [ [ [ 'x' => 1 ] ], '[{"x":1}]' ],
89 [ [ [ 2 => 1 ] ], '[{"2":1}]' ],
90 [ [ (object)[] ], '[{}]' ],
91 [ [ [ 1, ApiResult
::META_TYPE
=> 'assoc' ] ], '[{"0":1}]' ],
92 [ [ [ 'x' => 1, ApiResult
::META_TYPE
=> 'array' ] ], '[[1]]' ],
93 [ [ [ 'x' => 1, ApiResult
::META_TYPE
=> 'kvp' ] ], '[{"x":1}]' ],
97 ApiResult
::META_TYPE
=> 'BCkvp',
98 ApiResult
::META_KVP_KEY_NAME
=> 'key'
102 [ [ [ 'x' => 1, ApiResult
::META_TYPE
=> 'BCarray' ] ], '[[1]]' ],
107 ApiResult
::META_TYPE
=> 'BCassoc'
109 '[{"0":"a","1":"b"}]'
113 [ [ 'content' => 'foo', ApiResult
::META_CONTENT
=> 'content' ],
114 '{"content":"foo"}' ],
117 [ [ 'foo' => 'foo', ApiResult
::META_BC_SUBELEMENTS
=> [ 'foo' ] ],
121 [ [ 1 ], '/**/myCallback([1])', [ 'callback' => 'myCallback' ] ],
123 // Cross-domain mangling
124 [ [ '< Cross-Domain-Policy >' ], '["\u003C Cross-Domain-Policy >"]' ],