| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Api\Validator; |
| 4 |
|
| 5 |
use MediaWiki\Api\ApiBase; |
| 6 |
use MediaWiki\Api\ApiMain; |
| 7 |
use MediaWiki\Api\ApiMessage; |
| 8 |
use MediaWiki\Api\ApiUsageException; |
| 9 |
use MediaWiki\Api\Validator\ApiParamValidator; |
| 10 |
use MediaWiki\Message\Message; |
| 11 |
use MediaWiki\Request\FauxRequest; |
| 12 |
use MediaWiki\Tests\Api\ApiTestCase; |
| 13 |
use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait; |
| 14 |
use Wikimedia\Message\DataMessageValue; |
| 15 |
use Wikimedia\Message\MessageValue; |
| 16 |
use Wikimedia\ParamValidator\ParamValidator; |
| 17 |
use Wikimedia\ParamValidator\TypeDef\EnumDef; |
| 18 |
use Wikimedia\ParamValidator\TypeDef\IntegerDef; |
| 19 |
use Wikimedia\TestingAccessWrapper; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
class ApiParamValidatorTest extends ApiTestCase { |
| 27 |
use MockAuthorityTrait; |
| 28 |
|
| 29 |
private function getValidator( FauxRequest $request ): array { |
| 30 |
$context = $this->apiContext->newTestContext( $request, $this->mockRegisteredUltimateAuthority() ); |
| 31 |
$main = new ApiMain( $context ); |
| 32 |
return [ |
| 33 |
new ApiParamValidator( $main, $this->getServiceContainer()->getObjectFactory() ), |
| 34 |
$main |
| 35 |
]; |
| 36 |
} |
| 37 |
|
| 38 |
public function testKnownTypes(): void { |
| 39 |
[ $validator ] = $this->getValidator( new FauxRequest( [] ) ); |
| 40 |
$this->assertSame( |
| 41 |
[ |
| 42 |
'boolean', 'enum', 'expiry', 'integer', 'limit', 'namespace', 'NULL', 'password', |
| 43 |
'raw', 'string', 'submodule', 'tags', 'text', 'timestamp', 'title', 'user', 'upload', |
| 44 |
], |
| 45 |
$validator->knownTypes() |
| 46 |
); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public function testNormalizeSettings( $settings, array $expect ): void { |
| 55 |
[ $validator ] = $this->getValidator( new FauxRequest( [] ) ); |
| 56 |
$this->assertEquals( $expect, $validator->normalizeSettings( $settings ) ); |
| 57 |
} |
| 58 |
|
| 59 |
public static function provideNormalizeSettings(): array { |
| 60 |
return [ |
| 61 |
'Basic test' => [ |
| 62 |
[], |
| 63 |
[ |
| 64 |
ParamValidator::PARAM_IGNORE_UNRECOGNIZED_VALUES => true, |
| 65 |
IntegerDef::PARAM_IGNORE_RANGE => true, |
| 66 |
ParamValidator::PARAM_TYPE => 'NULL', |
| 67 |
], |
| 68 |
], |
| 69 |
'Explicit ParamValidator::PARAM_IGNORE_UNRECOGNIZED_VALUES' => [ |
| 70 |
[ |
| 71 |
ParamValidator::PARAM_IGNORE_UNRECOGNIZED_VALUES => false, |
| 72 |
], |
| 73 |
[ |
| 74 |
ParamValidator::PARAM_IGNORE_UNRECOGNIZED_VALUES => false, |
| 75 |
IntegerDef::PARAM_IGNORE_RANGE => true, |
| 76 |
ParamValidator::PARAM_TYPE => 'NULL', |
| 77 |
], |
| 78 |
], |
| 79 |
'Explicit IntegerDef::PARAM_IGNORE_RANGE' => [ |
| 80 |
[ |
| 81 |
IntegerDef::PARAM_IGNORE_RANGE => false, |
| 82 |
], |
| 83 |
[ |
| 84 |
ParamValidator::PARAM_IGNORE_UNRECOGNIZED_VALUES => true, |
| 85 |
IntegerDef::PARAM_IGNORE_RANGE => false, |
| 86 |
ParamValidator::PARAM_TYPE => 'NULL', |
| 87 |
], |
| 88 |
], |
| 89 |
'Handle ApiBase::PARAM_RANGE_ENFORCE' => [ |
| 90 |
[ |
| 91 |
ApiBase::PARAM_RANGE_ENFORCE => true, |
| 92 |
], |
| 93 |
[ |
| 94 |
ApiBase::PARAM_RANGE_ENFORCE => true, |
| 95 |
ParamValidator::PARAM_IGNORE_UNRECOGNIZED_VALUES => true, |
| 96 |
IntegerDef::PARAM_IGNORE_RANGE => false, |
| 97 |
ParamValidator::PARAM_TYPE => 'NULL', |
| 98 |
], |
| 99 |
], |
| 100 |
'Handle EnumDef::PARAM_DEPRECATED_VALUES, null' => [ |
| 101 |
[ |
| 102 |
EnumDef::PARAM_DEPRECATED_VALUES => [ |
| 103 |
'null' => null, |
| 104 |
'true' => true, |
| 105 |
'string' => 'some-message', |
| 106 |
'array' => [ 'some-message', 'with', 'params' ], |
| 107 |
'Message' => ApiMessage::create( |
| 108 |
[ 'api-message', 'with', 'params' ], 'somecode', [ 'some-data' ] |
| 109 |
), |
| 110 |
'MessageValue' => MessageValue::new( 'message-value', [ 'with', 'params' ] ), |
| 111 |
'DataMessageValue' => DataMessageValue::new( |
| 112 |
'data-message-value', [ 'with', 'params' ], 'somecode', [ 'some-data' ] |
| 113 |
), |
| 114 |
], |
| 115 |
], |
| 116 |
[ |
| 117 |
ParamValidator::PARAM_IGNORE_UNRECOGNIZED_VALUES => true, |
| 118 |
IntegerDef::PARAM_IGNORE_RANGE => true, |
| 119 |
EnumDef::PARAM_DEPRECATED_VALUES => [ |
| 120 |
'null' => null, |
| 121 |
'true' => true, |
| 122 |
'string' => DataMessageValue::new( 'some-message', [], 'bogus', [ '💩' => 'back-compat' ] ), |
| 123 |
'array' => DataMessageValue::new( |
| 124 |
'some-message', [ 'with', 'params' ], 'bogus', [ '💩' => 'back-compat' ] |
| 125 |
), |
| 126 |
'Message' => DataMessageValue::new( |
| 127 |
'api-message', [ 'with', 'params' ], 'bogus', [ '💩' => 'back-compat' ] |
| 128 |
), |
| 129 |
'MessageValue' => MessageValue::new( 'message-value', [ 'with', 'params' ] ), |
| 130 |
'DataMessageValue' => DataMessageValue::new( |
| 131 |
'data-message-value', [ 'with', 'params' ], 'somecode', [ 'some-data' ] |
| 132 |
), |
| 133 |
], |
| 134 |
ParamValidator::PARAM_TYPE => 'NULL', |
| 135 |
], |
| 136 |
], |
| 137 |
]; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public function testCheckSettings( array $params, string $name, array $expect ): void { |
| 147 |
[ $validator, $main ] = $this->getValidator( new FauxRequest( [] ) ); |
| 148 |
$module = $main->getModuleFromPath( 'query+allpages' ); |
| 149 |
|
| 150 |
$mock = $this->getMockBuilder( ParamValidator::class ) |
| 151 |
->disableOriginalConstructor() |
| 152 |
->onlyMethods( [ 'checkSettings' ] ) |
| 153 |
->getMock(); |
| 154 |
$mock->expects( $this->once() )->method( 'checkSettings' ) |
| 155 |
->willReturnCallback( function ( $n, $settings, $options ) use ( $name, $module ) { |
| 156 |
$this->assertSame( "ap$name", $n ); |
| 157 |
$this->assertSame( [ 'module' => $module ], $options ); |
| 158 |
|
| 159 |
$ret = [ 'issues' => [ 'X' ], 'allowedKeys' => [ 'Y' ], 'messages' => [] ]; |
| 160 |
$stack = is_array( $settings ) ? [ &$settings ] : []; |
| 161 |
while ( $stack ) { |
| 162 |
foreach ( $stack[0] as $k => $v ) { |
| 163 |
if ( $v instanceof MessageValue ) { |
| 164 |
$ret['messages'][] = $v; |
| 165 |
} elseif ( is_array( $v ) ) { |
| 166 |
$stack[] = &$stack[0][$k]; |
| 167 |
} |
| 168 |
} |
| 169 |
array_shift( $stack ); |
| 170 |
} |
| 171 |
return $ret; |
| 172 |
} ); |
| 173 |
TestingAccessWrapper::newFromObject( $validator )->paramValidator = $mock; |
| 174 |
|
| 175 |
$this->assertEquals( $expect, $validator->checkSettings( $module, $params, $name, [] ) ); |
| 176 |
} |
| 177 |
|
| 178 |
public static function provideCheckSettings() { |
| 179 |
$keys = [ |
| 180 |
'Y', ApiBase::PARAM_RANGE_ENFORCE, ApiBase::PARAM_HELP_MSG, ApiBase::PARAM_HELP_MSG_APPEND, |
| 181 |
ApiBase::PARAM_HELP_MSG_INFO, ApiBase::PARAM_HELP_MSG_PER_VALUE, ApiBase::PARAM_TEMPLATE_VARS, |
| 182 |
]; |
| 183 |
|
| 184 |
return [ |
| 185 |
'Basic test' => [ |
| 186 |
[ 'test' => null ], |
| 187 |
'test', |
| 188 |
[ |
| 189 |
'issues' => [ 'X' ], |
| 190 |
'allowedKeys' => $keys, |
| 191 |
'messages' => [ |
| 192 |
MessageValue::new( 'apihelp-query+allpages-param-test' ), |
| 193 |
], |
| 194 |
] |
| 195 |
], |
| 196 |
'Message mapping' => [ |
| 197 |
[ 'test' => [ |
| 198 |
EnumDef::PARAM_DEPRECATED_VALUES => [ |
| 199 |
'a' => true, |
| 200 |
'b' => 'bbb', |
| 201 |
'c' => [ 'ccc', 'p1', 'p2' ], |
| 202 |
'd' => Message::newFromKey( 'ddd' )->plaintextParams( 'p1', 'p2' ), |
| 203 |
], |
| 204 |
] ], |
| 205 |
'test', |
| 206 |
[ |
| 207 |
'issues' => [ 'X' ], |
| 208 |
'allowedKeys' => $keys, |
| 209 |
'messages' => [ |
| 210 |
DataMessageValue::new( 'bbb', [], 'bogus', [ '💩' => 'back-compat' ] ), |
| 211 |
DataMessageValue::new( 'ccc', [], 'bogus', [ '💩' => 'back-compat' ] ) |
| 212 |
->params( 'p1', 'p2' ), |
| 213 |
DataMessageValue::new( 'ddd', [], 'bogus', [ '💩' => 'back-compat' ] ) |
| 214 |
->plaintextParams( 'p1', 'p2' ), |
| 215 |
MessageValue::new( 'apihelp-query+allpages-param-test' ), |
| 216 |
], |
| 217 |
] |
| 218 |
], |
| 219 |
'Test everything' => [ |
| 220 |
[ |
| 221 |
'xxx' => [ |
| 222 |
ParamValidator::PARAM_TYPE => 'not tested here', |
| 223 |
ParamValidator::PARAM_ISMULTI => true |
| 224 |
], |
| 225 |
'test-{x}' => [ |
| 226 |
ParamValidator::PARAM_TYPE => [], |
| 227 |
ApiBase::PARAM_RANGE_ENFORCE => true, |
| 228 |
ApiBase::PARAM_HELP_MSG => 'foo', |
| 229 |
ApiBase::PARAM_HELP_MSG_APPEND => [], |
| 230 |
ApiBase::PARAM_HELP_MSG_INFO => [], |
| 231 |
ApiBase::PARAM_HELP_MSG_PER_VALUE => [], |
| 232 |
ApiBase::PARAM_TEMPLATE_VARS => [ |
| 233 |
'x' => 'xxx', |
| 234 |
] |
| 235 |
], |
| 236 |
], |
| 237 |
'test-{x}', |
| 238 |
[ |
| 239 |
'issues' => [ 'X' ], |
| 240 |
'allowedKeys' => $keys, |
| 241 |
'messages' => [ |
| 242 |
MessageValue::new( 'foo' ), |
| 243 |
], |
| 244 |
] |
| 245 |
], |
| 246 |
'Bad types' => [ |
| 247 |
[ 'test' => [ |
| 248 |
ApiBase::PARAM_RANGE_ENFORCE => 1, |
| 249 |
ApiBase::PARAM_HELP_MSG => false, |
| 250 |
ApiBase::PARAM_HELP_MSG_APPEND => 'foo', |
| 251 |
ApiBase::PARAM_HELP_MSG_INFO => 'bar', |
| 252 |
ApiBase::PARAM_HELP_MSG_PER_VALUE => true, |
| 253 |
ApiBase::PARAM_TEMPLATE_VARS => false, |
| 254 |
] ], |
| 255 |
'test', |
| 256 |
[ |
| 257 |
'issues' => [ |
| 258 |
'X', |
| 259 |
ApiBase::PARAM_RANGE_ENFORCE => 'PARAM_RANGE_ENFORCE must be boolean, got integer', |
| 260 |
'Message specification for PARAM_HELP_MSG is not valid', |
| 261 |
ApiBase::PARAM_HELP_MSG_APPEND => 'PARAM_HELP_MSG_APPEND must be an array, got string', |
| 262 |
ApiBase::PARAM_HELP_MSG_INFO => 'PARAM_HELP_MSG_INFO must be an array, got string', |
| 263 |
ApiBase::PARAM_HELP_MSG_PER_VALUE => 'PARAM_HELP_MSG_PER_VALUE must be an array, got boolean', |
| 264 |
ApiBase::PARAM_TEMPLATE_VARS => 'PARAM_TEMPLATE_VARS must be an array, got boolean', |
| 265 |
], |
| 266 |
'allowedKeys' => $keys, |
| 267 |
'messages' => [], |
| 268 |
] |
| 269 |
], |
| 270 |
'PARAM_HELP_MSG (string)' => [ |
| 271 |
[ 'test' => [ |
| 272 |
ApiBase::PARAM_HELP_MSG => 'foo', |
| 273 |
] ], |
| 274 |
'test', |
| 275 |
[ |
| 276 |
'issues' => [ 'X' ], |
| 277 |
'allowedKeys' => $keys, |
| 278 |
'messages' => [ |
| 279 |
MessageValue::new( 'foo' ), |
| 280 |
], |
| 281 |
] |
| 282 |
], |
| 283 |
'PARAM_HELP_MSG (array)' => [ |
| 284 |
[ 'test' => [ |
| 285 |
ApiBase::PARAM_HELP_MSG => [ 'foo', 'bar' ], |
| 286 |
] ], |
| 287 |
'test', |
| 288 |
[ |
| 289 |
'issues' => [ 'X' ], |
| 290 |
'allowedKeys' => $keys, |
| 291 |
'messages' => [ |
| 292 |
MessageValue::new( 'foo', [ 'bar' ] ), |
| 293 |
], |
| 294 |
] |
| 295 |
], |
| 296 |
'PARAM_HELP_MSG (Message)' => [ |
| 297 |
[ 'test' => [ |
| 298 |
ApiBase::PARAM_HELP_MSG => Message::newFromKey( 'foo' )->numParams( 123 ), |
| 299 |
] ], |
| 300 |
'test', |
| 301 |
[ |
| 302 |
'issues' => [ 'X' ], |
| 303 |
'allowedKeys' => $keys, |
| 304 |
'messages' => [ |
| 305 |
MessageValue::new( 'foo' )->numParams( 123 ), |
| 306 |
], |
| 307 |
] |
| 308 |
], |
| 309 |
'PARAM_HELP_MSG_APPEND' => [ |
| 310 |
[ 'test' => [ ApiBase::PARAM_HELP_MSG_APPEND => [ |
| 311 |
'foo', |
| 312 |
false, |
| 313 |
[ 'bar', 'p1', 'p2' ], |
| 314 |
Message::newFromKey( 'baz' )->numParams( 123 ), |
| 315 |
] ] ], |
| 316 |
'test', |
| 317 |
[ |
| 318 |
'issues' => [ |
| 319 |
'X', |
| 320 |
'Message specification for PARAM_HELP_MSG_APPEND[1] is not valid', |
| 321 |
], |
| 322 |
'allowedKeys' => $keys, |
| 323 |
'messages' => [ |
| 324 |
MessageValue::new( 'apihelp-query+allpages-param-test' ), |
| 325 |
MessageValue::new( 'foo' ), |
| 326 |
MessageValue::new( 'bar', [ 'p1', 'p2' ] ), |
| 327 |
MessageValue::new( 'baz' )->numParams( 123 ), |
| 328 |
], |
| 329 |
] |
| 330 |
], |
| 331 |
'PARAM_HELP_MSG_INFO' => [ |
| 332 |
[ 'test' => [ ApiBase::PARAM_HELP_MSG_INFO => [ |
| 333 |
'foo', |
| 334 |
[ false ], |
| 335 |
[ 'foo' ], |
| 336 |
[ 'bar', 'p1', 'p2' ], |
| 337 |
] ] ], |
| 338 |
'test', |
| 339 |
[ |
| 340 |
'issues' => [ |
| 341 |
'X', |
| 342 |
'PARAM_HELP_MSG_INFO[0] must be an array, got string', |
| 343 |
'PARAM_HELP_MSG_INFO[1][0] must be a string, got boolean', |
| 344 |
], |
| 345 |
'allowedKeys' => $keys, |
| 346 |
'messages' => [ |
| 347 |
MessageValue::new( 'apihelp-query+allpages-param-test' ), |
| 348 |
MessageValue::new( 'apihelp-query+allpages-paraminfo-foo' ), |
| 349 |
MessageValue::new( 'apihelp-query+allpages-paraminfo-bar', [ 'p1', 'p2' ] ), |
| 350 |
], |
| 351 |
] |
| 352 |
], |
| 353 |
'PARAM_HELP_MSG_PER_VALUE for non-array type' => [ |
| 354 |
[ 'test' => [ |
| 355 |
ParamValidator::PARAM_TYPE => 'namespace', |
| 356 |
ApiBase::PARAM_HELP_MSG_PER_VALUE => [], |
| 357 |
] ], |
| 358 |
'test', |
| 359 |
[ |
| 360 |
'issues' => [ |
| 361 |
'X', |
| 362 |
ApiBase::PARAM_HELP_MSG_PER_VALUE |
| 363 |
=> 'PARAM_HELP_MSG_PER_VALUE can only be used with PARAM_TYPE as an array', |
| 364 |
], |
| 365 |
'allowedKeys' => $keys, |
| 366 |
'messages' => [ |
| 367 |
MessageValue::new( 'apihelp-query+allpages-param-test' ), |
| 368 |
], |
| 369 |
] |
| 370 |
], |
| 371 |
'PARAM_HELP_MSG_PER_VALUE' => [ |
| 372 |
[ 'test' => [ |
| 373 |
ParamValidator::PARAM_TYPE => [ 'a', 'b', 'c', 'd' ], |
| 374 |
ApiBase::PARAM_HELP_MSG_PER_VALUE => [ |
| 375 |
'a' => null, |
| 376 |
'b' => 'bbb', |
| 377 |
'c' => [ 'ccc', 'p1', 'p2' ], |
| 378 |
'd' => Message::newFromKey( 'ddd' )->numParams( 123 ), |
| 379 |
'e' => 'eee', |
| 380 |
], |
| 381 |
] ], |
| 382 |
'test', |
| 383 |
[ |
| 384 |
'issues' => [ |
| 385 |
'X', |
| 386 |
'Message specification for PARAM_HELP_MSG_PER_VALUE[a] is not valid', |
| 387 |
'PARAM_HELP_MSG_PER_VALUE contains "e", which is not in PARAM_TYPE.', |
| 388 |
], |
| 389 |
'allowedKeys' => $keys, |
| 390 |
'messages' => [ |
| 391 |
MessageValue::new( 'apihelp-query+allpages-param-test' ), |
| 392 |
MessageValue::new( 'bbb' ), |
| 393 |
MessageValue::new( 'ccc', [ 'p1', 'p2' ] ), |
| 394 |
MessageValue::new( 'ddd' )->numParams( 123 ), |
| 395 |
MessageValue::new( 'eee' ), |
| 396 |
], |
| 397 |
] |
| 398 |
], |
| 399 |
'Template-style parameter name without PARAM_TEMPLATE_VARS' => [ |
| 400 |
[ 'test{x}' => null ], |
| 401 |
'test{x}', |
| 402 |
[ |
| 403 |
'issues' => [ |
| 404 |
'X', |
| 405 |
"Parameter name may not contain '{' or '}' without PARAM_TEMPLATE_VARS", |
| 406 |
], |
| 407 |
'allowedKeys' => $keys, |
| 408 |
'messages' => [ |
| 409 |
MessageValue::new( 'apihelp-query+allpages-param-test{x}' ), |
| 410 |
], |
| 411 |
] |
| 412 |
], |
| 413 |
'PARAM_TEMPLATE_VARS cannot be empty' => [ |
| 414 |
[ 'test{x}' => [ |
| 415 |
ApiBase::PARAM_TEMPLATE_VARS => [], |
| 416 |
] ], |
| 417 |
'test{x}', |
| 418 |
[ |
| 419 |
'issues' => [ |
| 420 |
'X', |
| 421 |
ApiBase::PARAM_TEMPLATE_VARS => 'PARAM_TEMPLATE_VARS cannot be the empty array', |
| 422 |
], |
| 423 |
'allowedKeys' => $keys, |
| 424 |
'messages' => [ |
| 425 |
MessageValue::new( 'apihelp-query+allpages-param-test{x}' ), |
| 426 |
], |
| 427 |
] |
| 428 |
], |
| 429 |
'PARAM_TEMPLATE_VARS, ok' => [ |
| 430 |
[ |
| 431 |
'ok' => [ |
| 432 |
ParamValidator::PARAM_ISMULTI => true, |
| 433 |
], |
| 434 |
'ok-templated-{x}' => [ |
| 435 |
ParamValidator::PARAM_ISMULTI => true, |
| 436 |
ApiBase::PARAM_TEMPLATE_VARS => [ |
| 437 |
'x' => 'ok', |
| 438 |
], |
| 439 |
], |
| 440 |
'test-{a}-{b}' => [ |
| 441 |
ApiBase::PARAM_TEMPLATE_VARS => [ |
| 442 |
'a' => 'ok', |
| 443 |
'b' => 'ok-templated-{x}', |
| 444 |
], |
| 445 |
], |
| 446 |
], |
| 447 |
'test-{a}-{b}', |
| 448 |
[ |
| 449 |
'issues' => [ 'X' ], |
| 450 |
'allowedKeys' => $keys, |
| 451 |
'messages' => [ |
| 452 |
MessageValue::new( 'apihelp-query+allpages-param-test-{a}-{b}' ), |
| 453 |
], |
| 454 |
] |
| 455 |
], |
| 456 |
'PARAM_TEMPLATE_VARS simple errors' => [ |
| 457 |
[ |
| 458 |
'ok' => [ |
| 459 |
ParamValidator::PARAM_ISMULTI => true, |
| 460 |
], |
| 461 |
'not-multi' => false, |
| 462 |
'test-{a}-{b}-{c}' => [ |
| 463 |
ApiBase::PARAM_TEMPLATE_VARS => [ |
| 464 |
'{x}' => 'ok', |
| 465 |
'not-in-name' => 'ok', |
| 466 |
'a' => false, |
| 467 |
'b' => 'missing', |
| 468 |
'c' => 'not-multi', |
| 469 |
], |
| 470 |
], |
| 471 |
], |
| 472 |
'test-{a}-{b}-{c}', |
| 473 |
[ |
| 474 |
'issues' => [ |
| 475 |
'X', |
| 476 |
"PARAM_TEMPLATE_VARS keys may not contain '{' or '}', got \"{x}\"", |
| 477 |
'Parameter name must contain PARAM_TEMPLATE_VARS key {not-in-name}', |
| 478 |
'PARAM_TEMPLATE_VARS[a] has invalid target type boolean', |
| 479 |
'PARAM_TEMPLATE_VARS[b] target parameter "missing" does not exist', |
| 480 |
'PARAM_TEMPLATE_VARS[c] target parameter "not-multi" must have PARAM_ISMULTI = true', |
| 481 |
], |
| 482 |
'allowedKeys' => $keys, |
| 483 |
'messages' => [ |
| 484 |
MessageValue::new( 'apihelp-query+allpages-param-test-{a}-{b}-{c}' ), |
| 485 |
], |
| 486 |
] |
| 487 |
], |
| 488 |
'PARAM_TEMPLATE_VARS no recursion' => [ |
| 489 |
[ |
| 490 |
'test-{a}' => [ |
| 491 |
ParamValidator::PARAM_ISMULTI => true, |
| 492 |
ApiBase::PARAM_TEMPLATE_VARS => [ |
| 493 |
'a' => 'test-{a}', |
| 494 |
], |
| 495 |
], |
| 496 |
], |
| 497 |
'test-{a}', |
| 498 |
[ |
| 499 |
'issues' => [ |
| 500 |
'X', |
| 501 |
'PARAM_TEMPLATE_VARS[a] cannot target the parameter itself' |
| 502 |
], |
| 503 |
'allowedKeys' => $keys, |
| 504 |
'messages' => [ |
| 505 |
MessageValue::new( 'apihelp-query+allpages-param-test-{a}' ), |
| 506 |
], |
| 507 |
] |
| 508 |
], |
| 509 |
'PARAM_TEMPLATE_VARS targeting another template, target must be a subset' => [ |
| 510 |
[ |
| 511 |
'ok1' => [ ParamValidator::PARAM_ISMULTI => true ], |
| 512 |
'ok2' => [ ParamValidator::PARAM_ISMULTI => true ], |
| 513 |
'test1-{a}' => [ |
| 514 |
ApiBase::PARAM_TEMPLATE_VARS => [ |
| 515 |
'a' => 'test2-{a}', |
| 516 |
], |
| 517 |
], |
| 518 |
'test2-{a}' => [ |
| 519 |
ParamValidator::PARAM_ISMULTI => true, |
| 520 |
ApiBase::PARAM_TEMPLATE_VARS => [ |
| 521 |
'a' => 'ok2', |
| 522 |
], |
| 523 |
], |
| 524 |
], |
| 525 |
'test1-{a}', |
| 526 |
[ |
| 527 |
'issues' => [ |
| 528 |
'X', |
| 529 |
'PARAM_TEMPLATE_VARS[a]: Target\'s PARAM_TEMPLATE_VARS must be a subset of the original', |
| 530 |
], |
| 531 |
'allowedKeys' => $keys, |
| 532 |
'messages' => [ |
| 533 |
MessageValue::new( 'apihelp-query+allpages-param-test1-{a}' ), |
| 534 |
], |
| 535 |
] |
| 536 |
], |
| 537 |
]; |
| 538 |
} |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
public function testGetValue( ?string $data, $settings, $expect ): void { |
| 544 |
[ $validator, $main ] = $this->getValidator( new FauxRequest( [ 'aptest' => $data ] ) ); |
| 545 |
$module = $main->getModuleFromPath( 'query+allpages' ); |
| 546 |
$options = [ |
| 547 |
'parse-limit' => false, |
| 548 |
'raw' => ( $settings[ParamValidator::PARAM_TYPE] ?? '' ) === 'raw', |
| 549 |
]; |
| 550 |
|
| 551 |
if ( $expect instanceof ApiUsageException ) { |
| 552 |
try { |
| 553 |
$validator->getValue( $module, 'test', $settings, $options ); |
| 554 |
$this->fail( 'Expected exception not thrown' ); |
| 555 |
} catch ( ApiUsageException $e ) { |
| 556 |
$this->assertSame( $module->getModulePath(), $e->getModulePath() ); |
| 557 |
$this->assertEquals( $expect->getStatusValue(), $e->getStatusValue() ); |
| 558 |
} |
| 559 |
} else { |
| 560 |
$this->assertEquals( $expect, $validator->getValue( $module, 'test', $settings, $options ) ); |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
public static function provideGetValue(): array { |
| 565 |
return [ |
| 566 |
'Basic test' => [ |
| 567 |
'1234', |
| 568 |
[ |
| 569 |
ParamValidator::PARAM_TYPE => 'integer', |
| 570 |
], |
| 571 |
1234 |
| 572 |
], |
| 573 |
'Test for default' => [ |
| 574 |
null, |
| 575 |
1234, |
| 576 |
1234 |
| 577 |
], |
| 578 |
'Test no value' => [ |
| 579 |
null, |
| 580 |
[ |
| 581 |
ParamValidator::PARAM_TYPE => 'integer', |
| 582 |
], |
| 583 |
null, |
| 584 |
], |
| 585 |
'Test boolean (false)' => [ |
| 586 |
null, |
| 587 |
false, |
| 588 |
null, |
| 589 |
], |
| 590 |
'Test boolean (true)' => [ |
| 591 |
'', |
| 592 |
false, |
| 593 |
true, |
| 594 |
], |
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
'Test string (Unicode NFC)' => [ |
| 599 |
"\u{2001}", |
| 600 |
[ |
| 601 |
ParamValidator::PARAM_TYPE => 'string', |
| 602 |
], |
| 603 |
"\u{2003}", |
| 604 |
], |
| 605 |
|
| 606 |
'Test string (raw)' => [ |
| 607 |
"\u{2001}", |
| 608 |
[ |
| 609 |
ParamValidator::PARAM_TYPE => 'raw', |
| 610 |
], |
| 611 |
"\u{2001}", |
| 612 |
], |
| 613 |
'Validation failure' => [ |
| 614 |
'xyz', |
| 615 |
[ |
| 616 |
ParamValidator::PARAM_TYPE => 'integer', |
| 617 |
], |
| 618 |
ApiUsageException::newWithMessage( null, [ |
| 619 |
'paramvalidator-badinteger', |
| 620 |
Message::plaintextParam( 'aptest' ), |
| 621 |
Message::plaintextParam( 'xyz' ), |
| 622 |
], 'badinteger' ), |
| 623 |
], |
| 624 |
]; |
| 625 |
} |
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
public function testValidateValue( $value, $settings, $expect ): void { |
| 631 |
[ $validator, $main ] = $this->getValidator( new FauxRequest() ); |
| 632 |
$module = $main->getModuleFromPath( 'query+allpages' ); |
| 633 |
|
| 634 |
if ( $expect instanceof ApiUsageException ) { |
| 635 |
try { |
| 636 |
$validator->validateValue( $module, 'test', $value, $settings, [] ); |
| 637 |
$this->fail( 'Expected exception not thrown' ); |
| 638 |
} catch ( ApiUsageException $e ) { |
| 639 |
$this->assertSame( $module->getModulePath(), $e->getModulePath() ); |
| 640 |
$this->assertEquals( $expect->getStatusValue(), $e->getStatusValue() ); |
| 641 |
} |
| 642 |
} else { |
| 643 |
$this->assertEquals( |
| 644 |
$expect, |
| 645 |
$validator->validateValue( $module, 'test', $value, $settings, [] ) |
| 646 |
); |
| 647 |
} |
| 648 |
} |
| 649 |
|
| 650 |
public static function provideValidateValue(): array { |
| 651 |
return [ |
| 652 |
'Basic test' => [ |
| 653 |
1234, |
| 654 |
[ |
| 655 |
ParamValidator::PARAM_TYPE => 'integer', |
| 656 |
], |
| 657 |
1234 |
| 658 |
], |
| 659 |
'Validation failure' => [ |
| 660 |
1234, |
| 661 |
[ |
| 662 |
ParamValidator::PARAM_TYPE => 'integer', |
| 663 |
IntegerDef::PARAM_IGNORE_RANGE => false, |
| 664 |
IntegerDef::PARAM_MAX => 10, |
| 665 |
], |
| 666 |
ApiUsageException::newWithMessage( null, [ |
| 667 |
'paramvalidator-outofrange-max', |
| 668 |
Message::plaintextParam( 'aptest' ), |
| 669 |
Message::plaintextParam( 1234 ), |
| 670 |
Message::numParam( '' ), |
| 671 |
Message::numParam( 10 ), |
| 672 |
], 'outofrange', [ 'min' => null, 'curmax' => 10, 'max' => 10, 'highmax' => 10 ] ), |
| 673 |
], |
| 674 |
]; |
| 675 |
} |
| 676 |
|
| 677 |
public function testGetParamInfo() { |
| 678 |
[ $validator, $main ] = $this->getValidator( new FauxRequest() ); |
| 679 |
$module = $main->getModuleFromPath( 'query+allpages' ); |
| 680 |
$dummy = (object)[]; |
| 681 |
|
| 682 |
$settings = [ |
| 683 |
'foo' => (object)[], |
| 684 |
]; |
| 685 |
$options = [ |
| 686 |
'bar' => (object)[], |
| 687 |
]; |
| 688 |
|
| 689 |
$mock = $this->getMockBuilder( ParamValidator::class ) |
| 690 |
->disableOriginalConstructor() |
| 691 |
->onlyMethods( [ 'getParamInfo' ] ) |
| 692 |
->getMock(); |
| 693 |
$mock->expects( $this->once() )->method( 'getParamInfo' ) |
| 694 |
->with( |
| 695 |
$this->identicalTo( 'aptest' ), |
| 696 |
$this->identicalTo( $settings ), |
| 697 |
$this->identicalTo( $options + [ 'module' => $module ] ) |
| 698 |
) |
| 699 |
->willReturn( [ $dummy ] ); |
| 700 |
|
| 701 |
TestingAccessWrapper::newFromObject( $validator )->paramValidator = $mock; |
| 702 |
$this->assertSame( [ $dummy ], $validator->getParamInfo( $module, 'test', $settings, $options ) ); |
| 703 |
} |
| 704 |
|
| 705 |
public function testGetHelpInfo() { |
| 706 |
[ $validator, $main ] = $this->getValidator( new FauxRequest() ); |
| 707 |
$module = $main->getModuleFromPath( 'query+allpages' ); |
| 708 |
|
| 709 |
$settings = [ |
| 710 |
'foo' => (object)[], |
| 711 |
]; |
| 712 |
$options = [ |
| 713 |
'bar' => (object)[], |
| 714 |
]; |
| 715 |
|
| 716 |
$mock = $this->getMockBuilder( ParamValidator::class ) |
| 717 |
->disableOriginalConstructor() |
| 718 |
->onlyMethods( [ 'getHelpInfo' ] ) |
| 719 |
->getMock(); |
| 720 |
$mock->expects( $this->once() )->method( 'getHelpInfo' ) |
| 721 |
->with( |
| 722 |
$this->identicalTo( 'aptest' ), |
| 723 |
$this->identicalTo( $settings ), |
| 724 |
$this->identicalTo( $options + [ 'module' => $module ] ) |
| 725 |
) |
| 726 |
->willReturn( [ |
| 727 |
'mv1' => MessageValue::new( 'parentheses', [ 'foobar' ] ), |
| 728 |
'mv2' => MessageValue::new( 'paramvalidator-help-continue' ), |
| 729 |
] ); |
| 730 |
|
| 731 |
TestingAccessWrapper::newFromObject( $validator )->paramValidator = $mock; |
| 732 |
$ret = $validator->getHelpInfo( $module, 'test', $settings, $options ); |
| 733 |
$this->assertArrayHasKey( 'mv1', $ret ); |
| 734 |
$this->assertInstanceOf( Message::class, $ret['mv1'] ); |
| 735 |
$this->assertEquals( '(parentheses: foobar)', $ret['mv1']->inLanguage( 'qqx' )->plain() ); |
| 736 |
$this->assertArrayHasKey( 'mv2', $ret ); |
| 737 |
$this->assertInstanceOf( Message::class, $ret['mv2'] ); |
| 738 |
$this->assertEquals( |
| 739 |
[ 'api-help-param-continue', 'paramvalidator-help-continue' ], |
| 740 |
$ret['mv2']->getKeysToTry() |
| 741 |
); |
| 742 |
$this->assertCount( 2, $ret ); |
| 743 |
} |
| 744 |
} |