3 use Wikimedia\TestingAccessWrapper
;
12 class ApiBaseTest
extends ApiTestCase
{
14 * This covers a variety of stub methods that return a fixed value.
16 * @param string|array $method Name of method, or [ name, params... ]
17 * @param string $value Expected value
19 * @dataProvider provideStubMethods
21 public function testStubMethods( $expected, $method, $args = [] ) {
22 // Some of these are protected
23 $mock = TestingAccessWrapper
::newFromObject( new MockApi() );
24 $result = call_user_func_array( [ $mock, $method ], $args );
25 $this->assertSame( $expected, $result );
28 public function provideStubMethods() {
30 [ null, 'getModuleManager' ],
31 [ null, 'getCustomPrinter' ],
32 [ [], 'getHelpUrls' ],
33 // @todo This is actually overriden by MockApi
34 // [ [], 'getAllowedParams' ],
35 [ true, 'shouldCheckMaxLag' ],
36 [ true, 'isReadMode' ],
37 [ false, 'isWriteMode' ],
38 [ false, 'mustBePosted' ],
39 [ false, 'isDeprecated' ],
40 [ false, 'isInternal' ],
41 [ false, 'needsToken' ],
42 [ null, 'getWebUITokenSalt', [ [] ] ],
43 [ null, 'getConditionalRequestData', [ 'etag' ] ],
44 [ null, 'dynamicParameterDocumentation' ],
48 public function testRequireOnlyOneParameterDefault() {
49 $mock = new MockApi();
50 $mock->requireOnlyOneParameter(
51 [ "filename" => "foo.txt", "enablechunks" => false ],
52 "filename", "enablechunks"
54 $this->assertTrue( true );
58 * @expectedException ApiUsageException
60 public function testRequireOnlyOneParameterZero() {
61 $mock = new MockApi();
62 $mock->requireOnlyOneParameter(
63 [ "filename" => "foo.txt", "enablechunks" => 0 ],
64 "filename", "enablechunks"
69 * @expectedException ApiUsageException
71 public function testRequireOnlyOneParameterTrue() {
72 $mock = new MockApi();
73 $mock->requireOnlyOneParameter(
74 [ "filename" => "foo.txt", "enablechunks" => true ],
75 "filename", "enablechunks"
79 public function testRequireOnlyOneParameterMissing() {
80 $this->setExpectedException( ApiUsageException
::class,
81 'One of the parameters "foo" and "bar" is required.' );
82 $mock = new MockApi();
83 $mock->requireOnlyOneParameter(
84 [ "filename" => "foo.txt", "enablechunks" => false ],
88 public function testRequireMaxOneParameterZero() {
89 $mock = new MockApi();
90 $mock->requireMaxOneParameter(
91 [ 'foo' => 'bar', 'baz' => 'quz' ],
93 $this->assertTrue( true );
96 public function testRequireMaxOneParameterOne() {
97 $mock = new MockApi();
98 $mock->requireMaxOneParameter(
99 [ 'foo' => 'bar', 'baz' => 'quz' ],
101 $this->assertTrue( true );
104 public function testRequireMaxOneParameterTwo() {
105 $this->setExpectedException( ApiUsageException
::class,
106 'The parameters "foo" and "baz" can not be used together.' );
107 $mock = new MockApi();
108 $mock->requireMaxOneParameter(
109 [ 'foo' => 'bar', 'baz' => 'quz' ],
113 public function testRequireAtLeastOneParameterZero() {
114 $this->setExpectedException( ApiUsageException
::class,
115 'At least one of the parameters "foo" and "bar" is required.' );
116 $mock = new MockApi();
117 $mock->requireAtLeastOneParameter(
118 [ 'a' => 'b', 'c' => 'd' ],
122 public function testRequireAtLeastOneParameterOne() {
123 $mock = new MockApi();
124 $mock->requireAtLeastOneParameter(
125 [ 'a' => 'b', 'c' => 'd' ],
127 $this->assertTrue( true );
130 public function testRequireAtLeastOneParameterTwo() {
131 $mock = new MockApi();
132 $mock->requireAtLeastOneParameter(
133 [ 'a' => 'b', 'c' => 'd' ],
135 $this->assertTrue( true );
138 public function testGetTitleOrPageIdBadParams() {
139 $this->setExpectedException( ApiUsageException
::class,
140 'The parameters "title" and "pageid" can not be used together.' );
141 $mock = new MockApi();
142 $mock->getTitleOrPageId( [ 'title' => 'a', 'pageid' => 7 ] );
145 public function testGetTitleOrPageIdTitle() {
146 $mock = new MockApi();
147 $result = $mock->getTitleOrPageId( [ 'title' => 'Foo' ] );
148 $this->assertInstanceOf( WikiPage
::class, $result );
149 $this->assertSame( 'Foo', $result->getTitle()->getPrefixedText() );
152 public function testGetTitleOrPageIdInvalidTitle() {
153 $this->setExpectedException( ApiUsageException
::class,
155 $mock = new MockApi();
156 $mock->getTitleOrPageId( [ 'title' => '|' ] );
159 public function testGetTitleOrPageIdSpecialTitle() {
160 $this->setExpectedException( ApiUsageException
::class,
161 "Namespace doesn't allow actual pages." );
162 $mock = new MockApi();
163 $mock->getTitleOrPageId( [ 'title' => 'Special:RandomPage' ] );
166 public function testGetTitleOrPageIdPageId() {
167 $page = $this->getExistingTestPage();
168 $result = ( new MockApi() )->getTitleOrPageId(
169 [ 'pageid' => $page->getId() ] );
170 $this->assertInstanceOf( WikiPage
::class, $result );
172 $page->getTitle()->getPrefixedText(),
173 $result->getTitle()->getPrefixedText()
177 public function testGetTitleOrPageIdInvalidPageId() {
178 // FIXME: fails under postgres
179 $this->markTestSkippedIfDbType( 'postgres' );
181 $this->setExpectedException( ApiUsageException
::class,
182 'There is no page with ID 2147483648.' );
183 $mock = new MockApi();
184 $mock->getTitleOrPageId( [ 'pageid' => 2147483648 ] );
187 public function testGetTitleFromTitleOrPageIdBadParams() {
188 $this->setExpectedException( ApiUsageException
::class,
189 'The parameters "title" and "pageid" can not be used together.' );
190 $mock = new MockApi();
191 $mock->getTitleFromTitleOrPageId( [ 'title' => 'a', 'pageid' => 7 ] );
194 public function testGetTitleFromTitleOrPageIdTitle() {
195 $mock = new MockApi();
196 $result = $mock->getTitleFromTitleOrPageId( [ 'title' => 'Foo' ] );
197 $this->assertInstanceOf( Title
::class, $result );
198 $this->assertSame( 'Foo', $result->getPrefixedText() );
201 public function testGetTitleFromTitleOrPageIdInvalidTitle() {
202 $this->setExpectedException( ApiUsageException
::class,
204 $mock = new MockApi();
205 $mock->getTitleFromTitleOrPageId( [ 'title' => '|' ] );
208 public function testGetTitleFromTitleOrPageIdPageId() {
209 $page = $this->getExistingTestPage();
210 $result = ( new MockApi() )->getTitleFromTitleOrPageId(
211 [ 'pageid' => $page->getId() ] );
212 $this->assertInstanceOf( Title
::class, $result );
213 $this->assertSame( $page->getTitle()->getPrefixedText(), $result->getPrefixedText() );
216 public function testGetTitleFromTitleOrPageIdInvalidPageId() {
217 $this->setExpectedException( ApiUsageException
::class,
218 'There is no page with ID 298401643.' );
219 $mock = new MockApi();
220 $mock->getTitleFromTitleOrPageId( [ 'pageid' => 298401643 ] );
223 public function testGetParameter() {
224 $mock = $this->getMockBuilder( MockApi
::class )
225 ->setMethods( [ 'getAllowedParams' ] )
227 $mock->method( 'getAllowedParams' )->willReturn( [
229 ApiBase
::PARAM_TYPE
=> [ 'value' ],
232 ApiBase
::PARAM_TYPE
=> [ 'value' ],
235 $wrapper = TestingAccessWrapper
::newFromObject( $mock );
237 $context = new DerivativeContext( $mock );
238 $context->setRequest( new FauxRequest( [ 'foo' => 'bad', 'bar' => 'value' ] ) );
239 $wrapper->mMainModule
= new ApiMain( $context );
241 // Even though 'foo' is bad, getParameter( 'bar' ) must not fail
242 $this->assertSame( 'value', $wrapper->getParameter( 'bar' ) );
244 // But getParameter( 'foo' ) must throw.
246 $wrapper->getParameter( 'foo' );
247 $this->fail( 'Expected exception not thrown' );
248 } catch ( ApiUsageException
$ex ) {
249 $this->assertTrue( $this->apiExceptionHasCode( $ex, 'unknown_foo' ) );
252 // And extractRequestParams() must throw too.
254 $mock->extractRequestParams();
255 $this->fail( 'Expected exception not thrown' );
256 } catch ( ApiUsageException
$ex ) {
257 $this->assertTrue( $this->apiExceptionHasCode( $ex, 'unknown_foo' ) );
262 * @param string|null $input
263 * @param array $paramSettings
264 * @param mixed $expected
265 * @param array $options Key-value pairs:
266 * 'parseLimits': true|false
267 * 'apihighlimits': true|false
268 * 'internalmode': true|false
269 * 'prefix': true|false
270 * @param string[] $warnings
272 private function doGetParameterFromSettings(
273 $input, $paramSettings, $expected, $warnings, $options = []
275 $mock = new MockApi();
276 $wrapper = TestingAccessWrapper
::newFromObject( $mock );
277 if ( $options['prefix'] ) {
278 $wrapper->mModulePrefix
= 'my';
279 $paramName = 'Param';
281 $paramName = 'myParam';
284 $context = new DerivativeContext( $mock );
285 $context->setRequest( new FauxRequest(
286 $input !== null ?
[ 'myParam' => $input ] : [] ) );
287 $wrapper->mMainModule
= new ApiMain( $context );
289 $parseLimits = $options['parseLimits'] ??
true;
291 if ( !empty( $options['apihighlimits'] ) ) {
292 $context->setUser( self
::$users['sysop']->getUser() );
295 if ( isset( $options['internalmode'] ) && !$options['internalmode'] ) {
296 $mainWrapper = TestingAccessWrapper
::newFromObject( $wrapper->mMainModule
);
297 $mainWrapper->mInternalMode
= false;
300 // If we're testing tags, set up some tags
301 if ( isset( $paramSettings[ApiBase
::PARAM_TYPE
] ) &&
302 $paramSettings[ApiBase
::PARAM_TYPE
] === 'tags'
304 ChangeTags
::defineTag( 'tag1' );
305 ChangeTags
::defineTag( 'tag2' );
308 if ( $expected instanceof Exception
) {
310 $wrapper->getParameterFromSettings( $paramName, $paramSettings,
312 $this->fail( 'No exception thrown' );
313 } catch ( Exception
$ex ) {
314 $this->assertEquals( $expected, $ex );
317 $result = $wrapper->getParameterFromSettings( $paramName,
318 $paramSettings, $parseLimits );
319 if ( isset( $paramSettings[ApiBase
::PARAM_TYPE
] ) &&
320 $paramSettings[ApiBase
::PARAM_TYPE
] === 'timestamp' &&
323 // Allow one second of fuzziness. Make sure the formats are
325 $this->assertRegExp( '/^\d{14}$/', $result );
326 $this->assertLessThanOrEqual( 1,
327 abs( wfTimestamp( TS_UNIX
, $result ) - time() ),
328 "Result $result differs from expected $expected by " .
329 'more than one second' );
331 $this->assertSame( $expected, $result );
333 $actualWarnings = array_map( function ( $warn ) {
334 return $warn instanceof Message
335 ?
array_merge( [ $warn->getKey() ], $warn->getParams() )
337 }, $mock->warnings
);
338 $this->assertSame( $warnings, $actualWarnings );
341 if ( !empty( $paramSettings[ApiBase
::PARAM_SENSITIVE
] ) ||
342 ( isset( $paramSettings[ApiBase
::PARAM_TYPE
] ) &&
343 $paramSettings[ApiBase
::PARAM_TYPE
] === 'password' )
345 $mainWrapper = TestingAccessWrapper
::newFromObject( $wrapper->getMain() );
346 $this->assertSame( [ 'myParam' ],
347 $mainWrapper->getSensitiveParams() );
352 * @dataProvider provideGetParameterFromSettings
353 * @see self::doGetParameterFromSettings()
355 public function testGetParameterFromSettings_noprefix(
356 $input, $paramSettings, $expected, $warnings, $options = []
358 $options['prefix'] = false;
359 $this->doGetParameterFromSettings( $input, $paramSettings, $expected, $warnings, $options );
363 * @dataProvider provideGetParameterFromSettings
364 * @see self::doGetParameterFromSettings()
366 public function testGetParameterFromSettings_prefix(
367 $input, $paramSettings, $expected, $warnings, $options = []
369 $options['prefix'] = true;
370 $this->doGetParameterFromSettings( $input, $paramSettings, $expected, $warnings, $options );
373 public static function provideGetParameterFromSettings() {
375 [ 'apiwarn-badutf8', 'myParam' ],
380 for ( $i = 0; $i < 32; $i++
) {
382 $enc .= ( $i === 9 ||
$i === 10 ||
$i === 13 )
388 'Basic param' => [ 'bar', null, 'bar', [] ],
389 'Basic param, C0 controls' => [ $c0, null, $enc, $warnings ],
390 'String param' => [ 'bar', '', 'bar', [] ],
391 'String param, defaulted' => [ null, '', '', [] ],
392 'String param, empty' => [ '', 'default', '', [] ],
393 'String param, required, empty' => [
395 [ ApiBase
::PARAM_DFLT
=> 'default', ApiBase
::PARAM_REQUIRED
=> true ],
396 ApiUsageException
::newWithMessage( null,
397 [ 'apierror-missingparam', 'myParam' ] ),
400 'Multi-valued parameter' => [
402 [ ApiBase
::PARAM_ISMULTI
=> true ],
406 'Multi-valued parameter, alternative separator' => [
408 [ ApiBase
::PARAM_ISMULTI
=> true ],
412 'Multi-valued parameter, other C0 controls' => [
414 [ ApiBase
::PARAM_ISMULTI
=> true ],
418 'Multi-valued parameter, other C0 controls (2)' => [
420 [ ApiBase
::PARAM_ISMULTI
=> true ],
421 [ substr( $enc, 0, -3 ), '' ],
424 'Multi-valued parameter with limits' => [
427 ApiBase
::PARAM_ISMULTI
=> true,
428 ApiBase
::PARAM_ISMULTI_LIMIT1
=> 3,
433 'Multi-valued parameter with exceeded limits' => [
436 ApiBase
::PARAM_ISMULTI
=> true,
437 ApiBase
::PARAM_ISMULTI_LIMIT1
=> 2,
439 ApiUsageException
::newWithMessage(
440 null, [ 'apierror-toomanyvalues', 'myParam', 2 ], 'too-many-myParam'
444 'Multi-valued parameter with exceeded limits for non-bot' => [
447 ApiBase
::PARAM_ISMULTI
=> true,
448 ApiBase
::PARAM_ISMULTI_LIMIT1
=> 2,
449 ApiBase
::PARAM_ISMULTI_LIMIT2
=> 3,
451 ApiUsageException
::newWithMessage(
452 null, [ 'apierror-toomanyvalues', 'myParam', 2 ], 'too-many-myParam'
456 'Multi-valued parameter with non-exceeded limits for bot' => [
459 ApiBase
::PARAM_ISMULTI
=> true,
460 ApiBase
::PARAM_ISMULTI_LIMIT1
=> 2,
461 ApiBase
::PARAM_ISMULTI_LIMIT2
=> 3,
465 [ 'apihighlimits' => true ],
467 'Multi-valued parameter with prohibited duplicates' => [
469 [ ApiBase
::PARAM_ISMULTI
=> true ],
470 // Note that the keys are not sequential! This matches
471 // array_unique, but might be unexpected.
472 [ 0 => 'a', 1 => 'b', 3 => 'c' ],
475 'Multi-valued parameter with allowed duplicates' => [
478 ApiBase
::PARAM_ISMULTI
=> true,
479 ApiBase
::PARAM_ALLOW_DUPLICATES
=> true,
484 'Empty boolean param' => [
486 [ ApiBase
::PARAM_TYPE
=> 'boolean' ],
490 'Boolean param 0' => [
492 [ ApiBase
::PARAM_TYPE
=> 'boolean' ],
496 'Boolean param false' => [
498 [ ApiBase
::PARAM_TYPE
=> 'boolean' ],
502 'Boolean multi-param' => [
505 ApiBase
::PARAM_TYPE
=> 'boolean',
506 ApiBase
::PARAM_ISMULTI
=> true,
509 'Internal error in ApiBase::getParameterFromSettings: ' .
510 'Multi-values not supported for myParam'
514 'Empty boolean param with non-false default' => [
517 ApiBase
::PARAM_TYPE
=> 'boolean',
518 ApiBase
::PARAM_DFLT
=> true,
521 'Internal error in ApiBase::getParameterFromSettings: ' .
522 "Boolean param myParam's default is set to '1'. " .
523 'Boolean parameters must default to false.' ),
526 'Deprecated parameter' => [
528 [ ApiBase
::PARAM_DEPRECATED
=> true ],
530 [ [ 'apiwarn-deprecation-parameter', 'myParam' ] ],
532 'Deprecated parameter value' => [
534 [ ApiBase
::PARAM_DEPRECATED_VALUES
=> [ 'a' => true ] ],
536 [ [ 'apiwarn-deprecation-parameter', 'myParam=a' ] ],
538 'Multiple deprecated parameter values' => [
540 [ ApiBase
::PARAM_DEPRECATED_VALUES
=>
541 [ 'b' => true, 'd' => true ],
542 ApiBase
::PARAM_ISMULTI
=> true ],
543 [ 'a', 'b', 'c', 'd' ],
545 [ 'apiwarn-deprecation-parameter', 'myParam=b' ],
546 [ 'apiwarn-deprecation-parameter', 'myParam=d' ],
549 'Deprecated parameter value with custom warning' => [
551 [ ApiBase
::PARAM_DEPRECATED_VALUES
=> [ 'a' => 'my-msg' ] ],
555 '"*" when wildcard not allowed' => [
557 [ ApiBase
::PARAM_ISMULTI
=> true,
558 ApiBase
::PARAM_TYPE
=> [ 'a', 'b', 'c' ] ],
560 [ [ 'apiwarn-unrecognizedvalues', 'myParam',
561 [ 'list' => [ '*' ], 'type' => 'comma' ], 1 ] ],
566 ApiBase
::PARAM_ISMULTI
=> true,
567 ApiBase
::PARAM_TYPE
=> [ 'a', 'b', 'c' ],
568 ApiBase
::PARAM_ALL
=> true,
573 'Wildcard "*" with multiples not allowed' => [
576 ApiBase
::PARAM_TYPE
=> [ 'a', 'b', 'c' ],
577 ApiBase
::PARAM_ALL
=> true,
579 ApiUsageException
::newWithMessage( null,
580 [ 'apierror-unrecognizedvalue', 'myParam', '*' ],
584 'Wildcard "*" with unrestricted type' => [
587 ApiBase
::PARAM_ISMULTI
=> true,
588 ApiBase
::PARAM_ALL
=> true,
596 ApiBase
::PARAM_ISMULTI
=> true,
597 ApiBase
::PARAM_TYPE
=> [ 'a', 'b', 'c' ],
598 ApiBase
::PARAM_ALL
=> 'x',
603 'Wildcard conflicting with allowed value' => [
606 ApiBase
::PARAM_ISMULTI
=> true,
607 ApiBase
::PARAM_TYPE
=> [ 'a', 'b', 'c' ],
608 ApiBase
::PARAM_ALL
=> 'a',
611 'Internal error in ApiBase::getParameterFromSettings: ' .
612 'For param myParam, PARAM_ALL collides with a possible ' .
616 'Namespace with wildcard' => [
619 ApiBase
::PARAM_ISMULTI
=> true,
620 ApiBase
::PARAM_TYPE
=> 'namespace',
622 MWNamespace
::getValidNamespaces(),
625 // PARAM_ALL is ignored with namespace types.
626 'Namespace with wildcard suppressed' => [
629 ApiBase
::PARAM_ISMULTI
=> true,
630 ApiBase
::PARAM_TYPE
=> 'namespace',
631 ApiBase
::PARAM_ALL
=> false,
633 MWNamespace
::getValidNamespaces(),
636 'Namespace with wildcard "x"' => [
639 ApiBase
::PARAM_ISMULTI
=> true,
640 ApiBase
::PARAM_TYPE
=> 'namespace',
641 ApiBase
::PARAM_ALL
=> 'x',
644 [ [ 'apiwarn-unrecognizedvalues', 'myParam',
645 [ 'list' => [ 'x' ], 'type' => 'comma' ], 1 ] ],
648 'dDy+G?e?txnr.1:(@[Ru',
649 [ ApiBase
::PARAM_TYPE
=> 'password' ],
650 'dDy+G?e?txnr.1:(@[Ru',
653 'Sensitive field' => [
654 'I am fond of pineapples',
655 [ ApiBase
::PARAM_SENSITIVE
=> true ],
656 'I am fond of pineapples',
659 'Upload with default' => [
662 ApiBase
::PARAM_TYPE
=> 'upload',
663 ApiBase
::PARAM_DFLT
=> '',
666 'Internal error in ApiBase::getParameterFromSettings: ' .
667 "File upload param myParam's default is set to ''. " .
668 'File upload parameters may not have a default.' ),
671 'Multiple upload' => [
674 ApiBase
::PARAM_TYPE
=> 'upload',
675 ApiBase
::PARAM_ISMULTI
=> true,
678 'Internal error in ApiBase::getParameterFromSettings: ' .
679 'Multi-values not supported for myParam' ),
682 // @todo Test actual upload
685 [ ApiBase
::PARAM_TYPE
=> 'namespace' ],
686 ApiUsageException
::newWithMessage( null,
687 [ 'apierror-unrecognizedvalue', 'myParam', '-1' ],
691 'Extra namespace -1' => [
694 ApiBase
::PARAM_TYPE
=> 'namespace',
695 ApiBase
::PARAM_EXTRA_NAMESPACES
=> [ '-1' ],
700 // @todo Test with PARAM_SUBMODULE_MAP unset, need
701 // getModuleManager() to return something real
702 'Nonexistent module' => [
705 ApiBase
::PARAM_TYPE
=> 'submodule',
706 ApiBase
::PARAM_SUBMODULE_MAP
=>
707 [ 'foo' => 'foo', 'bar' => 'foo+bar' ],
709 ApiUsageException
::newWithMessage(
712 'apierror-unrecognizedvalue',
720 '\\x1f with multiples not allowed' => [
723 ApiUsageException
::newWithMessage( null,
724 'apierror-badvalue-notmultivalue',
725 'badvalue_notmultivalue' ),
728 'Integer with unenforced min' => [
731 ApiBase
::PARAM_TYPE
=> 'integer',
732 ApiBase
::PARAM_MIN
=> -1,
735 [ [ 'apierror-integeroutofrange-belowminimum', 'myParam', -1,
738 'Integer with enforced min' => [
741 ApiBase
::PARAM_TYPE
=> 'integer',
742 ApiBase
::PARAM_MIN
=> -1,
743 ApiBase
::PARAM_RANGE_ENFORCE
=> true,
745 ApiUsageException
::newWithMessage( null,
746 [ 'apierror-integeroutofrange-belowminimum', 'myParam',
747 '-1', '-2' ], 'integeroutofrange',
748 [ 'min' => -1, 'max' => null, 'botMax' => null ] ),
751 'Integer with unenforced max (internal mode)' => [
754 ApiBase
::PARAM_TYPE
=> 'integer',
755 ApiBase
::PARAM_MAX
=> 7,
760 'Integer with enforced max (internal mode)' => [
763 ApiBase
::PARAM_TYPE
=> 'integer',
764 ApiBase
::PARAM_MAX
=> 7,
765 ApiBase
::PARAM_RANGE_ENFORCE
=> true,
770 'Integer with unenforced max (non-internal mode)' => [
773 ApiBase
::PARAM_TYPE
=> 'integer',
774 ApiBase
::PARAM_MAX
=> 7,
777 [ [ 'apierror-integeroutofrange-abovemax', 'myParam', 7, 8 ] ],
778 [ 'internalmode' => false ],
780 'Integer with enforced max (non-internal mode)' => [
783 ApiBase
::PARAM_TYPE
=> 'integer',
784 ApiBase
::PARAM_MAX
=> 7,
785 ApiBase
::PARAM_RANGE_ENFORCE
=> true,
787 ApiUsageException
::newWithMessage(
789 [ 'apierror-integeroutofrange-abovemax', 'myParam', '7', '8' ],
791 [ 'min' => null, 'max' => 7, 'botMax' => 7 ]
794 [ 'internalmode' => false ],
796 'Array of integers' => [
799 ApiBase
::PARAM_ISMULTI
=> true,
800 ApiBase
::PARAM_TYPE
=> 'integer',
805 'Array of integers with unenforced min/max (internal mode)' => [
808 ApiBase
::PARAM_ISMULTI
=> true,
809 ApiBase
::PARAM_TYPE
=> 'integer',
810 ApiBase
::PARAM_MIN
=> 0,
811 ApiBase
::PARAM_MAX
=> 100,
814 [ [ 'apierror-integeroutofrange-belowminimum', 'myParam', 0, -1 ] ],
816 'Array of integers with enforced min/max (internal mode)' => [
819 ApiBase
::PARAM_ISMULTI
=> true,
820 ApiBase
::PARAM_TYPE
=> 'integer',
821 ApiBase
::PARAM_MIN
=> 0,
822 ApiBase
::PARAM_MAX
=> 100,
823 ApiBase
::PARAM_RANGE_ENFORCE
=> true,
825 ApiUsageException
::newWithMessage(
827 [ 'apierror-integeroutofrange-belowminimum', 'myParam', 0, -1 ],
829 [ 'min' => 0, 'max' => 100, 'botMax' => 100 ]
833 'Array of integers with unenforced min/max (non-internal mode)' => [
836 ApiBase
::PARAM_ISMULTI
=> true,
837 ApiBase
::PARAM_TYPE
=> 'integer',
838 ApiBase
::PARAM_MIN
=> 0,
839 ApiBase
::PARAM_MAX
=> 100,
843 [ 'apierror-integeroutofrange-abovemax', 'myParam', 100, 966 ],
844 [ 'apierror-integeroutofrange-belowminimum', 'myParam', 0, -1 ]
846 [ 'internalmode' => false ],
848 'Array of integers with enforced min/max (non-internal mode)' => [
851 ApiBase
::PARAM_ISMULTI
=> true,
852 ApiBase
::PARAM_TYPE
=> 'integer',
853 ApiBase
::PARAM_MIN
=> 0,
854 ApiBase
::PARAM_MAX
=> 100,
855 ApiBase
::PARAM_RANGE_ENFORCE
=> true,
857 ApiUsageException
::newWithMessage(
859 [ 'apierror-integeroutofrange-abovemax', 'myParam', 100, 966 ],
861 [ 'min' => 0, 'max' => 100, 'botMax' => 100 ]
864 [ 'internalmode' => false ],
866 'Limit with parseLimits false' => [
868 [ ApiBase
::PARAM_TYPE
=> 'limit' ],
871 [ 'parseLimits' => false ],
873 'Limit with no max' => [
876 ApiBase
::PARAM_TYPE
=> 'limit',
877 ApiBase
::PARAM_MAX2
=> 10,
878 ApiBase
::PARAM_ISMULTI
=> true,
881 'Internal error in ApiBase::getParameterFromSettings: ' .
882 'MAX1 or MAX2 are not defined for the limit myParam' ),
885 'Limit with no max2' => [
888 ApiBase
::PARAM_TYPE
=> 'limit',
889 ApiBase
::PARAM_MAX
=> 10,
890 ApiBase
::PARAM_ISMULTI
=> true,
893 'Internal error in ApiBase::getParameterFromSettings: ' .
894 'MAX1 or MAX2 are not defined for the limit myParam' ),
897 'Limit with multi-value' => [
900 ApiBase
::PARAM_TYPE
=> 'limit',
901 ApiBase
::PARAM_MAX
=> 10,
902 ApiBase
::PARAM_MAX2
=> 10,
903 ApiBase
::PARAM_ISMULTI
=> true,
906 'Internal error in ApiBase::getParameterFromSettings: ' .
907 'Multi-values not supported for myParam' ),
913 ApiBase
::PARAM_TYPE
=> 'limit',
914 ApiBase
::PARAM_MAX
=> 100,
915 ApiBase
::PARAM_MAX2
=> 100,
923 ApiBase
::PARAM_TYPE
=> 'limit',
924 ApiBase
::PARAM_MAX
=> 100,
925 ApiBase
::PARAM_MAX2
=> 101,
930 'Limit max for apihighlimits' => [
933 ApiBase
::PARAM_TYPE
=> 'limit',
934 ApiBase
::PARAM_MAX
=> 100,
935 ApiBase
::PARAM_MAX2
=> 101,
939 [ 'apihighlimits' => true ],
941 'Limit too large (internal mode)' => [
944 ApiBase
::PARAM_TYPE
=> 'limit',
945 ApiBase
::PARAM_MAX
=> 100,
946 ApiBase
::PARAM_MAX2
=> 101,
951 'Limit okay for apihighlimits (internal mode)' => [
954 ApiBase
::PARAM_TYPE
=> 'limit',
955 ApiBase
::PARAM_MAX
=> 100,
956 ApiBase
::PARAM_MAX2
=> 101,
960 [ 'apihighlimits' => true ],
962 'Limit too large for apihighlimits (internal mode)' => [
965 ApiBase
::PARAM_TYPE
=> 'limit',
966 ApiBase
::PARAM_MAX
=> 100,
967 ApiBase
::PARAM_MAX2
=> 101,
971 [ 'apihighlimits' => true ],
973 'Limit too large (non-internal mode)' => [
976 ApiBase
::PARAM_TYPE
=> 'limit',
977 ApiBase
::PARAM_MAX
=> 100,
978 ApiBase
::PARAM_MAX2
=> 101,
981 [ [ 'apierror-integeroutofrange-abovemax', 'myParam', 100, 101 ] ],
982 [ 'internalmode' => false ],
984 'Limit okay for apihighlimits (non-internal mode)' => [
987 ApiBase
::PARAM_TYPE
=> 'limit',
988 ApiBase
::PARAM_MAX
=> 100,
989 ApiBase
::PARAM_MAX2
=> 101,
993 [ 'internalmode' => false, 'apihighlimits' => true ],
995 'Limit too large for apihighlimits (non-internal mode)' => [
998 ApiBase
::PARAM_TYPE
=> 'limit',
999 ApiBase
::PARAM_MAX
=> 100,
1000 ApiBase
::PARAM_MAX2
=> 101,
1003 [ [ 'apierror-integeroutofrange-abovebotmax', 'myParam', 101, 102 ] ],
1004 [ 'internalmode' => false, 'apihighlimits' => true ],
1006 'Limit too small' => [
1009 ApiBase
::PARAM_TYPE
=> 'limit',
1010 ApiBase
::PARAM_MIN
=> -1,
1011 ApiBase
::PARAM_MAX
=> 100,
1012 ApiBase
::PARAM_MAX2
=> 100,
1015 [ [ 'apierror-integeroutofrange-belowminimum', 'myParam', -1,
1019 wfTimestamp( TS_UNIX
, '20211221122112' ),
1020 [ ApiBase
::PARAM_TYPE
=> 'timestamp' ],
1026 [ ApiBase
::PARAM_TYPE
=> 'timestamp' ],
1029 [ [ 'apiwarn-unclearnowtimestamp', 'myParam', '0' ] ],
1031 'Timestamp empty' => [
1033 [ ApiBase
::PARAM_TYPE
=> 'timestamp' ],
1035 [ [ 'apiwarn-unclearnowtimestamp', 'myParam', '' ] ],
1037 // wfTimestamp() interprets this as Unix time
1040 [ ApiBase
::PARAM_TYPE
=> 'timestamp' ],
1044 'Timestamp now' => [
1046 [ ApiBase
::PARAM_TYPE
=> 'timestamp' ],
1050 'Invalid timestamp' => [
1052 [ ApiBase
::PARAM_TYPE
=> 'timestamp' ],
1053 ApiUsageException
::newWithMessage(
1055 [ 'apierror-badtimestamp', 'myParam', 'a potato' ],
1056 'badtimestamp_myParam'
1060 'Timestamp array' => [
1063 ApiBase
::PARAM_TYPE
=> 'timestamp',
1064 ApiBase
::PARAM_ISMULTI
=> 1,
1066 [ wfTimestamp( TS_MW
, 100 ), wfTimestamp( TS_MW
, 101 ) ],
1071 [ ApiBase
::PARAM_TYPE
=> 'user' ],
1075 'User prefixed with "User:"' => [
1077 [ ApiBase
::PARAM_TYPE
=> 'user' ],
1081 'Invalid username "|"' => [
1083 [ ApiBase
::PARAM_TYPE
=> 'user' ],
1084 ApiUsageException
::newWithMessage( null,
1085 [ 'apierror-baduser', 'myParam', '|' ],
1086 'baduser_myParam' ),
1089 'Invalid username "300.300.300.300"' => [
1091 [ ApiBase
::PARAM_TYPE
=> 'user' ],
1092 ApiUsageException
::newWithMessage( null,
1093 [ 'apierror-baduser', 'myParam', '300.300.300.300' ],
1094 'baduser_myParam' ),
1097 'IP range as username' => [
1099 [ ApiBase
::PARAM_TYPE
=> 'user' ],
1103 'IPv6 as username' => [
1105 [ ApiBase
::PARAM_TYPE
=> 'user' ],
1109 'Obsolete cloaked usemod IP address as username' => [
1111 [ ApiBase
::PARAM_TYPE
=> 'user' ],
1115 'Invalid username containing IP address' => [
1116 'This is [not] valid 1.2.3.xxx, ha!',
1117 [ ApiBase
::PARAM_TYPE
=> 'user' ],
1118 ApiUsageException
::newWithMessage(
1120 [ 'apierror-baduser', 'myParam', 'This is [not] valid 1.2.3.xxx, ha!' ],
1125 'External username' => [
1127 [ ApiBase
::PARAM_TYPE
=> 'user' ],
1131 'Array of usernames' => [
1134 ApiBase
::PARAM_TYPE
=> 'user',
1135 ApiBase
::PARAM_ISMULTI
=> true,
1142 [ ApiBase
::PARAM_TYPE
=> 'tags' ],
1146 'Array of one tag' => [
1149 ApiBase
::PARAM_TYPE
=> 'tags',
1150 ApiBase
::PARAM_ISMULTI
=> true,
1155 'Array of tags' => [
1158 ApiBase
::PARAM_TYPE
=> 'tags',
1159 ApiBase
::PARAM_ISMULTI
=> true,
1166 [ ApiBase
::PARAM_TYPE
=> 'tags' ],
1167 new ApiUsageException( null,
1168 Status
::newFatal( 'tags-apply-not-allowed-one',
1169 'invalid tag', 1 ) ),
1172 'Unrecognized type' => [
1174 [ ApiBase
::PARAM_TYPE
=> 'nonexistenttype' ],
1176 'Internal error in ApiBase::getParameterFromSettings: ' .
1177 "Param myParam's type is unknown - nonexistenttype" ),
1180 'Too many bytes' => [
1183 ApiBase
::PARAM_MAX_BYTES
=> 0,
1184 ApiBase
::PARAM_MAX_CHARS
=> 0,
1186 ApiUsageException
::newWithMessage( null,
1187 [ 'apierror-maxbytes', 'myParam', 0 ] ),
1190 'Too many chars' => [
1193 ApiBase
::PARAM_MAX_BYTES
=> 4,
1194 ApiBase
::PARAM_MAX_CHARS
=> 1,
1196 ApiUsageException
::newWithMessage( null,
1197 [ 'apierror-maxchars', 'myParam', 1 ] ),
1200 'Omitted required param' => [
1202 [ ApiBase
::PARAM_REQUIRED
=> true ],
1203 ApiUsageException
::newWithMessage( null,
1204 [ 'apierror-missingparam', 'myParam' ] ),
1207 'Empty multi-value' => [
1209 [ ApiBase
::PARAM_ISMULTI
=> true ],
1213 'Multi-value \x1f' => [
1215 [ ApiBase
::PARAM_ISMULTI
=> true ],
1219 'Allowed non-multi-value with "|"' => [
1221 [ ApiBase
::PARAM_TYPE
=> [ 'a|b' ] ],
1225 'Prohibited multi-value' => [
1227 [ ApiBase
::PARAM_TYPE
=> [ 'a', 'b' ] ],
1228 ApiUsageException
::newWithMessage( null,
1230 'apierror-multival-only-one-of',
1232 Message
::listParam( [ '<kbd>a</kbd>', '<kbd>b</kbd>' ] ),
1241 // The following really just test PHP's string-to-int conversion.
1249 [ "\t1", 1, '\t1' ],
1250 [ "\r1", 1, '\r1' ],
1251 [ "\f1", 0, '\f1', 'badutf-8' ],
1252 [ "\n1", 1, '\n1' ],
1253 [ "\v1", 0, '\v1', 'badutf-8' ],
1254 [ "\e1", 0, '\e1', 'badutf-8' ],
1255 [ "\x001", 0, '\x001', 'badutf-8' ],
1258 foreach ( $integerTests as $test ) {
1259 $desc = $test[2] ??
$test[0];
1260 $warnings = isset( $test[3] ) ?
1261 [ [ 'apiwarn-badutf8', 'myParam' ] ] : [];
1262 $returnArray["\"$desc\" as integer"] = [
1264 [ ApiBase
::PARAM_TYPE
=> 'integer' ],
1270 return $returnArray;
1273 public function testErrorArrayToStatus() {
1274 $mock = new MockApi();
1276 // Sanity check empty array
1277 $expect = Status
::newGood();
1278 $this->assertEquals( $expect, $mock->errorArrayToStatus( [] ) );
1280 // No blocked $user, so no special block handling
1281 $expect = Status
::newGood();
1282 $expect->fatal( 'blockedtext' );
1283 $expect->fatal( 'autoblockedtext' );
1284 $expect->fatal( 'systemblockedtext' );
1285 $expect->fatal( 'mainpage' );
1286 $expect->fatal( 'parentheses', 'foobar' );
1287 $this->assertEquals( $expect, $mock->errorArrayToStatus( [
1289 [ 'autoblockedtext' ],
1290 [ 'systemblockedtext' ],
1292 [ 'parentheses', 'foobar' ],
1295 // Has a blocked $user, so special block handling
1296 $user = $this->getMutableTestUser()->getUser();
1297 $block = new \
Block( [
1298 'address' => $user->getName(),
1299 'user' => $user->getID(),
1300 'by' => $this->getTestSysop()->getUser()->getId(),
1301 'reason' => __METHOD__
,
1302 'expiry' => time() +
100500,
1305 $blockinfo = [ 'blockinfo' => ApiQueryUserInfo
::getBlockInfo( $block ) ];
1307 $expect = Status
::newGood();
1308 $expect->fatal( ApiMessage
::create( 'apierror-blocked', 'blocked', $blockinfo ) );
1309 $expect->fatal( ApiMessage
::create( 'apierror-autoblocked', 'autoblocked', $blockinfo ) );
1310 $expect->fatal( ApiMessage
::create( 'apierror-systemblocked', 'blocked', $blockinfo ) );
1311 $expect->fatal( 'mainpage' );
1312 $expect->fatal( 'parentheses', 'foobar' );
1313 $this->assertEquals( $expect, $mock->errorArrayToStatus( [
1315 [ 'autoblockedtext' ],
1316 [ 'systemblockedtext' ],
1318 [ 'parentheses', 'foobar' ],
1322 public function testDieStatus() {
1323 $mock = new MockApi();
1325 $status = StatusValue
::newGood();
1326 $status->error( 'foo' );
1327 $status->warning( 'bar' );
1329 $mock->dieStatus( $status );
1330 $this->fail( 'Expected exception not thrown' );
1331 } catch ( ApiUsageException
$ex ) {
1332 $this->assertTrue( ApiTestCase
::apiExceptionHasCode( $ex, 'foo' ), 'Exception has "foo"' );
1333 $this->assertFalse( ApiTestCase
::apiExceptionHasCode( $ex, 'bar' ), 'Exception has "bar"' );
1336 $status = StatusValue
::newGood();
1337 $status->warning( 'foo' );
1338 $status->warning( 'bar' );
1340 $mock->dieStatus( $status );
1341 $this->fail( 'Expected exception not thrown' );
1342 } catch ( ApiUsageException
$ex ) {
1343 $this->assertTrue( ApiTestCase
::apiExceptionHasCode( $ex, 'foo' ), 'Exception has "foo"' );
1344 $this->assertTrue( ApiTestCase
::apiExceptionHasCode( $ex, 'bar' ), 'Exception has "bar"' );
1347 $status = StatusValue
::newGood();
1348 $status->setOk( false );
1350 $mock->dieStatus( $status );
1351 $this->fail( 'Expected exception not thrown' );
1352 } catch ( ApiUsageException
$ex ) {
1353 $this->assertTrue( ApiTestCase
::apiExceptionHasCode( $ex, 'unknownerror-nocode' ),
1354 'Exception has "unknownerror-nocode"' );
1359 * @covers ApiBase::extractRequestParams
1361 public function testExtractRequestParams() {
1362 $request = new FauxRequest( [
1363 'xxexists' => 'exists!',
1364 'xxmulti' => 'a|b|c|d|{bad}',
1366 'xxtemplate-a' => 'A!',
1367 'xxtemplate-b' => 'B1|B2|B3',
1368 'xxtemplate-c' => '',
1369 'xxrecursivetemplate-b-B1' => 'X',
1370 'xxrecursivetemplate-b-B3' => 'Y',
1371 'xxrecursivetemplate-b-B4' => '?',
1372 'xxemptytemplate-' => 'nope',
1375 'errorformat' => 'raw',
1377 $context = new DerivativeContext( RequestContext
::getMain() );
1378 $context->setRequest( $request );
1379 $main = new ApiMain( $context );
1381 $mock = $this->getMockBuilder( ApiBase
::class )
1382 ->setConstructorArgs( [ $main, 'test', 'xx' ] )
1383 ->setMethods( [ 'getAllowedParams' ] )
1384 ->getMockForAbstractClass();
1385 $mock->method( 'getAllowedParams' )->willReturn( [
1386 'notexists' => null,
1389 ApiBase
::PARAM_ISMULTI
=> true,
1392 ApiBase
::PARAM_ISMULTI
=> true,
1395 ApiBase
::PARAM_ISMULTI
=> true,
1396 ApiBase
::PARAM_TEMPLATE_VARS
=> [ 'm' => 'multi' ],
1398 'recursivetemplate-{m}-{t}' => [
1399 ApiBase
::PARAM_TEMPLATE_VARS
=> [ 't' => 'template-{m}', 'm' => 'multi' ],
1401 'emptytemplate-{m}' => [
1402 ApiBase
::PARAM_ISMULTI
=> true,
1403 ApiBase
::PARAM_TEMPLATE_VARS
=> [ 'm' => 'empty' ],
1405 'badtemplate-{e}' => [
1406 ApiBase
::PARAM_TEMPLATE_VARS
=> [ 'e' => 'exists' ],
1408 'badtemplate2-{e}' => [
1409 ApiBase
::PARAM_TEMPLATE_VARS
=> [ 'e' => 'badtemplate2-{e}' ],
1411 'badtemplate3-{x}' => [
1412 ApiBase
::PARAM_TEMPLATE_VARS
=> [ 'x' => 'foo' ],
1416 $this->assertEquals( [
1417 'notexists' => null,
1418 'exists' => 'exists!',
1419 'multi' => [ 'a', 'b', 'c', 'd', '{bad}' ],
1421 'template-a' => [ 'A!' ],
1422 'template-b' => [ 'B1', 'B2', 'B3' ],
1424 'template-d' => null,
1425 'recursivetemplate-a-A!' => null,
1426 'recursivetemplate-b-B1' => 'X',
1427 'recursivetemplate-b-B2' => null,
1428 'recursivetemplate-b-B3' => 'Y',
1429 ], $mock->extractRequestParams() );
1431 $used = TestingAccessWrapper
::newFromObject( $main )->getParamsUsed();
1433 $this->assertEquals( [
1438 'xxrecursivetemplate-a-A!',
1439 'xxrecursivetemplate-b-B1',
1440 'xxrecursivetemplate-b-B2',
1441 'xxrecursivetemplate-b-B3',
1448 $warnings = $mock->getResult()->getResultData( 'warnings', [ 'Strip' => 'all' ] );
1449 $this->assertCount( 1, $warnings );
1450 $this->assertSame( 'ignoring-invalid-templated-value', $warnings[0]['code'] );