| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Api\Format; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use MediaWiki\Api\ApiFormatJson; |
| 7 |
use MediaWiki\Api\ApiResult; |
| 8 |
use MWException; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
class ApiFormatJsonTest extends ApiFormatTestBase { |
| 15 |
|
| 16 |
|
| 17 |
protected $printerName = 'json'; |
| 18 |
|
| 19 |
private static function addFormatVersion( $format, $arr ) { |
| 20 |
$ret = []; |
| 21 |
foreach ( $arr as $val ) { |
| 22 |
if ( !isset( $val[2] ) ) { |
| 23 |
$val[2] = []; |
| 24 |
} |
| 25 |
$val[2]['formatversion'] = $format; |
| 26 |
$ret[] = $val; |
| 27 |
if ( $format === 2 ) { |
| 28 |
|
| 29 |
$val[2]['formatversion'] = 'latest'; |
| 30 |
$ret[] = $val; |
| 31 |
} |
| 32 |
} |
| 33 |
return $ret; |
| 34 |
} |
| 35 |
|
| 36 |
public static function provideGeneralEncoding() { |
| 37 |
return array_merge( |
| 38 |
self::addFormatVersion( 1, [ |
| 39 |
|
| 40 |
[ [ null ], '[null]' ], |
| 41 |
[ [ true ], '[""]' ], |
| 42 |
[ [ false ], '[]' ], |
| 43 |
[ [ true, ApiResult::META_BC_BOOLS => [ 0 ] ], '[true]' ], |
| 44 |
[ [ false, ApiResult::META_BC_BOOLS => [ 0 ] ], '[false]' ], |
| 45 |
[ [ 42 ], '[42]' ], |
| 46 |
[ [ 42.5 ], '[42.5]' ], |
| 47 |
[ [ 1e42 ], '[1.0e+42]' ], |
| 48 |
[ [ 'foo' ], '["foo"]' ], |
| 49 |
[ [ 'fóo' ], '["f\u00f3o"]' ], |
| 50 |
[ [ 'fóo' ], '["fóo"]', [ 'utf8' => 1 ] ], |
| 51 |
|
| 52 |
|
| 53 |
[ [ [] ], '[[]]' ], |
| 54 |
[ [ [ 1 ] ], '[[1]]' ], |
| 55 |
[ [ [ 'x' => 1 ] ], '[{"x":1}]' ], |
| 56 |
[ [ [ 2 => 1 ] ], '[{"2":1}]' ], |
| 57 |
[ [ (object)[] ], '[{}]' ], |
| 58 |
[ [ [ 1, ApiResult::META_TYPE => 'assoc' ] ], '[{"0":1}]' ], |
| 59 |
[ [ [ 'x' => 1, ApiResult::META_TYPE => 'array' ] ], '[[1]]' ], |
| 60 |
[ [ [ 'x' => 1, ApiResult::META_TYPE => 'kvp' ] ], '[{"x":1}]' ], |
| 61 |
[ |
| 62 |
[ [ |
| 63 |
'x' => 1, |
| 64 |
ApiResult::META_TYPE => 'BCkvp', |
| 65 |
ApiResult::META_KVP_KEY_NAME => 'key' |
| 66 |
] ], |
| 67 |
'[[{"key":"x","*":1}]]' |
| 68 |
], |
| 69 |
[ [ [ 'x' => 1, ApiResult::META_TYPE => 'BCarray' ] ], '[{"x":1}]' ], |
| 70 |
[ [ [ 'a', 'b', ApiResult::META_TYPE => 'BCassoc' ] ], '[["a","b"]]' ], |
| 71 |
|
| 72 |
|
| 73 |
[ [ 'content' => 'foo', ApiResult::META_CONTENT => 'content' ], |
| 74 |
'{"*":"foo"}' ], |
| 75 |
|
| 76 |
|
| 77 |
[ [ 'foo' => 'foo', ApiResult::META_BC_SUBELEMENTS => [ 'foo' ] ], |
| 78 |
'{"foo":{"*":"foo"}}' ], |
| 79 |
|
| 80 |
|
| 81 |
[ [ 1 ], '/**/myCallback([1])', [ 'callback' => 'myCallback' ] ], |
| 82 |
] ), |
| 83 |
self::addFormatVersion( 2, [ |
| 84 |
|
| 85 |
[ [ null ], '[null]' ], |
| 86 |
[ [ true ], '[true]' ], |
| 87 |
[ [ false ], '[false]' ], |
| 88 |
[ [ true, ApiResult::META_BC_BOOLS => [ 0 ] ], '[true]' ], |
| 89 |
[ [ false, ApiResult::META_BC_BOOLS => [ 0 ] ], '[false]' ], |
| 90 |
[ [ 42 ], '[42]' ], |
| 91 |
[ [ 42.5 ], '[42.5]' ], |
| 92 |
[ [ 1e42 ], '[1.0e+42]' ], |
| 93 |
[ [ 'foo' ], '["foo"]' ], |
| 94 |
[ [ 'fóo' ], '["fóo"]' ], |
| 95 |
[ [ 'fóo' ], '["f\u00f3o"]', [ 'ascii' => 1 ] ], |
| 96 |
|
| 97 |
|
| 98 |
[ [ [] ], '[[]]' ], |
| 99 |
[ [ [ 'x' => 1 ] ], '[{"x":1}]' ], |
| 100 |
[ [ [ 2 => 1 ] ], '[{"2":1}]' ], |
| 101 |
[ [ (object)[] ], '[{}]' ], |
| 102 |
[ [ [ 1, ApiResult::META_TYPE => 'assoc' ] ], '[{"0":1}]' ], |
| 103 |
[ [ [ 'x' => 1, ApiResult::META_TYPE => 'array' ] ], '[[1]]' ], |
| 104 |
[ [ [ 'x' => 1, ApiResult::META_TYPE => 'kvp' ] ], '[{"x":1}]' ], |
| 105 |
[ |
| 106 |
[ [ |
| 107 |
'x' => 1, |
| 108 |
ApiResult::META_TYPE => 'BCkvp', |
| 109 |
ApiResult::META_KVP_KEY_NAME => 'key' |
| 110 |
] ], |
| 111 |
'[{"x":1}]' |
| 112 |
], |
| 113 |
[ [ [ 'x' => 1, ApiResult::META_TYPE => 'BCarray' ] ], '[[1]]' ], |
| 114 |
[ |
| 115 |
[ [ |
| 116 |
'a', |
| 117 |
'b', |
| 118 |
ApiResult::META_TYPE => 'BCassoc' |
| 119 |
] ], |
| 120 |
'[{"0":"a","1":"b"}]' |
| 121 |
], |
| 122 |
|
| 123 |
|
| 124 |
[ [ 'content' => 'foo', ApiResult::META_CONTENT => 'content' ], |
| 125 |
'{"content":"foo"}' ], |
| 126 |
|
| 127 |
|
| 128 |
[ [ 'foo' => 'foo', ApiResult::META_BC_SUBELEMENTS => [ 'foo' ] ], |
| 129 |
'{"foo":"foo"}' ], |
| 130 |
|
| 131 |
|
| 132 |
[ [ 1 ], '/**/myCallback([1])', [ 'callback' => 'myCallback' ] ], |
| 133 |
|
| 134 |
|
| 135 |
[ |
| 136 |
[ 'foo' => "\xFF" ], |
| 137 |
"{\"foo\":\"\u{FFFD}\"}", |
| 138 |
], |
| 139 |
[ |
| 140 |
[ 'foo' => "\xFF" ], |
| 141 |
new MWException( |
| 142 |
'Internal error in ' . ApiFormatJson::class . '::execute: ' . |
| 143 |
'Unable to encode API result as JSON' |
| 144 |
), |
| 145 |
[], |
| 146 |
[ 'flags' => ApiResult::NO_VALIDATE ], |
| 147 |
], |
| 148 |
|
| 149 |
[ |
| 150 |
[ 'foo' => NAN ], |
| 151 |
new InvalidArgumentException( |
| 152 |
'Cannot add non-finite floats to ApiResult' |
| 153 |
), |
| 154 |
], |
| 155 |
[ |
| 156 |
[ 'foo' => NAN ], |
| 157 |
new MWException( |
| 158 |
'Internal error in ' . ApiFormatJson::class . '::execute: ' . |
| 159 |
'Unable to encode API result as JSON' |
| 160 |
), |
| 161 |
[], |
| 162 |
[ 'flags' => ApiResult::NO_VALIDATE ], |
| 163 |
], |
| 164 |
] ) |
| 165 |
|
| 166 |
); |
| 167 |
} |
| 168 |
|
| 169 |
} |