MediaWiki  1.33.0
ApiFormatJsonTest.php
Go to the documentation of this file.
1 <?php
2 
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',
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',
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  // Invalid UTF-8: bytes 192, 193, and 245-255 are off-limits
133  [
134  [ 'foo' => "\xFF" ],
135  "{\"foo\":\"\u{FFFD}\"}", // Mangled when validated (T210548)
136  ],
137  [
138  [ 'foo' => "\xFF" ],
139  new MWException(
140  'Internal error in ApiFormatJson::execute: ' .
141  'Unable to encode API result as JSON'
142  ),
143  [],
144  [ 'flags' => ApiResult::NO_VALIDATE ],
145  ],
146  // NaN is also not allowed
147  [
148  [ 'foo' => NAN ],
149  new InvalidArgumentException(
150  'Cannot add non-finite floats to ApiResult'
151  ),
152  ],
153  [
154  [ 'foo' => NAN ],
155  new MWException(
156  'Internal error in ApiFormatJson::execute: ' .
157  'Unable to encode API result as JSON'
158  ),
159  [],
160  [ 'flags' => ApiResult::NO_VALIDATE ],
161  ],
162  ] )
163  // @todo Test rawfm
164  );
165  }
166 
167 }
ApiResult\META_TYPE
const META_TYPE
Key for the 'type' metadata item.
Definition: ApiResult.php:110
ApiResult\META_BC_SUBELEMENTS
const META_BC_SUBELEMENTS
Key for the 'BC subelements' metadata item.
Definition: ApiResult.php:143
ApiResult\META_KVP_KEY_NAME
const META_KVP_KEY_NAME
Key for the metadata item whose value specifies the name used for the kvp key in the alternative outp...
Definition: ApiResult.php:119
ApiResult\NO_VALIDATE
const NO_VALIDATE
For addValue(), setValue() and similar functions, do not validate data.
Definition: ApiResult.php:66
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
ApiFormatJsonTest
API ApiFormatJson.
Definition: ApiFormatJsonTest.php:7
ApiFormatTestBase
Definition: ApiFormatTestBase.php:3
MWException
MediaWiki exception.
Definition: MWException.php:26
ApiResult\META_BC_BOOLS
const META_BC_BOOLS
Key for the 'BC bools' metadata item.
Definition: ApiResult.php:136
$ret
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition: hooks.txt:1985
ApiFormatJsonTest\$printerName
$printerName
Definition: ApiFormatJsonTest.php:9
ApiFormatJsonTest\provideGeneralEncoding
static provideGeneralEncoding()
Return general data to be encoded for testing.
Definition: ApiFormatJsonTest.php:28
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
ApiFormatJsonTest\addFormatVersion
static addFormatVersion( $format, $arr)
Definition: ApiFormatJsonTest.php:11
ApiResult\META_CONTENT
const META_CONTENT
Key for the 'content' metadata item.
Definition: ApiResult.php:90