Merge "Improve ApiLogin test coverage"
[lhc/web/wiklou.git] / tests / phpunit / includes / api / format / ApiFormatJsonTest.php
1 <?php
2
3 /**
4 * @group API
5 * @covers ApiFormatJson
6 */
7 class ApiFormatJsonTest extends ApiFormatTestBase {
8
9 protected $printerName = 'json';
10
11 private static function addFormatVersion( $format, $arr ) {
12 $ret = [];
13 foreach ( $arr as $val ) {
14 if ( !isset( $val[2] ) ) {
15 $val[2] = [];
16 }
17 $val[2]['formatversion'] = $format;
18 $ret[] = $val;
19 if ( $format === 2 ) {
20 // Add a test for 'latest' as well
21 $val[2]['formatversion'] = 'latest';
22 $ret[] = $val;
23 }
24 }
25 return $ret;
26 }
27
28 public static function provideGeneralEncoding() {
29 return array_merge(
30 self::addFormatVersion( 1, [
31 // Basic types
32 [ [ null ], '[null]' ],
33 [ [ true ], '[""]' ],
34 [ [ false ], '[]' ],
35 [ [ true, ApiResult::META_BC_BOOLS => [ 0 ] ], '[true]' ],
36 [ [ false, ApiResult::META_BC_BOOLS => [ 0 ] ], '[false]' ],
37 [ [ 42 ], '[42]' ],
38 [ [ 42.5 ], '[42.5]' ],
39 [ [ 1e42 ], '[1.0e+42]' ],
40 [ [ 'foo' ], '["foo"]' ],
41 [ [ 'fóo' ], '["f\u00f3o"]' ],
42 [ [ 'fóo' ], '["fóo"]', [ 'utf8' => 1 ] ],
43
44 // Arrays and objects
45 [ [ [] ], '[[]]' ],
46 [ [ [ 1 ] ], '[[1]]' ],
47 [ [ [ 'x' => 1 ] ], '[{"x":1}]' ],
48 [ [ [ 2 => 1 ] ], '[{"2":1}]' ],
49 [ [ (object)[] ], '[{}]' ],
50 [ [ [ 1, ApiResult::META_TYPE => 'assoc' ] ], '[{"0":1}]' ],
51 [ [ [ 'x' => 1, ApiResult::META_TYPE => 'array' ] ], '[[1]]' ],
52 [ [ [ 'x' => 1, ApiResult::META_TYPE => 'kvp' ] ], '[{"x":1}]' ],
53 [
54 [ [
55 'x' => 1,
56 ApiResult::META_TYPE => 'BCkvp',
57 ApiResult::META_KVP_KEY_NAME => 'key'
58 ] ],
59 '[[{"key":"x","*":1}]]'
60 ],
61 [ [ [ 'x' => 1, ApiResult::META_TYPE => 'BCarray' ] ], '[{"x":1}]' ],
62 [ [ [ 'a', 'b', ApiResult::META_TYPE => 'BCassoc' ] ], '[["a","b"]]' ],
63
64 // Content
65 [ [ 'content' => 'foo', ApiResult::META_CONTENT => 'content' ],
66 '{"*":"foo"}' ],
67
68 // BC Subelements
69 [ [ 'foo' => 'foo', ApiResult::META_BC_SUBELEMENTS => [ 'foo' ] ],
70 '{"foo":{"*":"foo"}}' ],
71
72 // Callbacks
73 [ [ 1 ], '/**/myCallback([1])', [ 'callback' => 'myCallback' ] ],
74
75 // Cross-domain mangling
76 [ [ '< Cross-Domain-Policy >' ], '["\u003C Cross-Domain-Policy >"]' ],
77 ] ),
78 self::addFormatVersion( 2, [
79 // Basic types
80 [ [ null ], '[null]' ],
81 [ [ true ], '[true]' ],
82 [ [ false ], '[false]' ],
83 [ [ true, ApiResult::META_BC_BOOLS => [ 0 ] ], '[true]' ],
84 [ [ false, ApiResult::META_BC_BOOLS => [ 0 ] ], '[false]' ],
85 [ [ 42 ], '[42]' ],
86 [ [ 42.5 ], '[42.5]' ],
87 [ [ 1e42 ], '[1.0e+42]' ],
88 [ [ 'foo' ], '["foo"]' ],
89 [ [ 'fóo' ], '["fóo"]' ],
90 [ [ 'fóo' ], '["f\u00f3o"]', [ 'ascii' => 1 ] ],
91
92 // Arrays and objects
93 [ [ [] ], '[[]]' ],
94 [ [ [ 'x' => 1 ] ], '[{"x":1}]' ],
95 [ [ [ 2 => 1 ] ], '[{"2":1}]' ],
96 [ [ (object)[] ], '[{}]' ],
97 [ [ [ 1, ApiResult::META_TYPE => 'assoc' ] ], '[{"0":1}]' ],
98 [ [ [ 'x' => 1, ApiResult::META_TYPE => 'array' ] ], '[[1]]' ],
99 [ [ [ 'x' => 1, ApiResult::META_TYPE => 'kvp' ] ], '[{"x":1}]' ],
100 [
101 [ [
102 'x' => 1,
103 ApiResult::META_TYPE => 'BCkvp',
104 ApiResult::META_KVP_KEY_NAME => 'key'
105 ] ],
106 '[{"x":1}]'
107 ],
108 [ [ [ 'x' => 1, ApiResult::META_TYPE => 'BCarray' ] ], '[[1]]' ],
109 [
110 [ [
111 'a',
112 'b',
113 ApiResult::META_TYPE => 'BCassoc'
114 ] ],
115 '[{"0":"a","1":"b"}]'
116 ],
117
118 // Content
119 [ [ 'content' => 'foo', ApiResult::META_CONTENT => 'content' ],
120 '{"content":"foo"}' ],
121
122 // BC Subelements
123 [ [ 'foo' => 'foo', ApiResult::META_BC_SUBELEMENTS => [ 'foo' ] ],
124 '{"foo":"foo"}' ],
125
126 // Callbacks
127 [ [ 1 ], '/**/myCallback([1])', [ 'callback' => 'myCallback' ] ],
128
129 // Cross-domain mangling
130 [ [ '< Cross-Domain-Policy >' ], '["\u003C Cross-Domain-Policy >"]' ],
131 ] )
132 // @todo Test rawfm
133 );
134 }
135
136 }