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 $result = ( new MockApi() )->getTitleOrPageId(
168 [ 'pageid' => Title
::newFromText( 'UTPage' )->getArticleId() ] );
169 $this->assertInstanceOf( WikiPage
::class, $result );
170 $this->assertSame( 'UTPage', $result->getTitle()->getPrefixedText() );
173 public function testGetTitleOrPageIdInvalidPageId() {
174 $this->setExpectedException( ApiUsageException
::class,
175 'There is no page with ID 2147483648.' );
176 $mock = new MockApi();
177 $mock->getTitleOrPageId( [ 'pageid' => 2147483648 ] );
180 public function testGetTitleFromTitleOrPageIdBadParams() {
181 $this->setExpectedException( ApiUsageException
::class,
182 'The parameters "title" and "pageid" can not be used together.' );
183 $mock = new MockApi();
184 $mock->getTitleFromTitleOrPageId( [ 'title' => 'a', 'pageid' => 7 ] );
187 public function testGetTitleFromTitleOrPageIdTitle() {
188 $mock = new MockApi();
189 $result = $mock->getTitleFromTitleOrPageId( [ 'title' => 'Foo' ] );
190 $this->assertInstanceOf( Title
::class, $result );
191 $this->assertSame( 'Foo', $result->getPrefixedText() );
194 public function testGetTitleFromTitleOrPageIdInvalidTitle() {
195 $this->setExpectedException( ApiUsageException
::class,
197 $mock = new MockApi();
198 $mock->getTitleFromTitleOrPageId( [ 'title' => '|' ] );
201 public function testGetTitleFromTitleOrPageIdPageId() {
202 $result = ( new MockApi() )->getTitleFromTitleOrPageId(
203 [ 'pageid' => Title
::newFromText( 'UTPage' )->getArticleId() ] );
204 $this->assertInstanceOf( Title
::class, $result );
205 $this->assertSame( 'UTPage', $result->getPrefixedText() );
208 public function testGetTitleFromTitleOrPageIdInvalidPageId() {
209 $this->setExpectedException( ApiUsageException
::class,
210 'There is no page with ID 298401643.' );
211 $mock = new MockApi();
212 $mock->getTitleFromTitleOrPageId( [ 'pageid' => 298401643 ] );
216 * @dataProvider provideGetParameterFromSettings
217 * @param string|null $input
218 * @param array $paramSettings
219 * @param mixed $expected
220 * @param array $options Key-value pairs:
221 * 'parseLimits': true|false
222 * 'apihighlimits': true|false
223 * 'internalmode': true|false
224 * @param string[] $warnings
226 public function testGetParameterFromSettings(
227 $input, $paramSettings, $expected, $warnings, $options = []
229 $mock = new MockApi();
230 $wrapper = TestingAccessWrapper
::newFromObject( $mock );
232 $context = new DerivativeContext( $mock );
233 $context->setRequest( new FauxRequest(
234 $input !== null ?
[ 'myParam' => $input ] : [] ) );
235 $wrapper->mMainModule
= new ApiMain( $context );
237 $parseLimits = isset( $options['parseLimits'] ) ?
238 $options['parseLimits'] : true;
240 if ( !empty( $options['apihighlimits'] ) ) {
241 $context->setUser( self
::$users['sysop']->getUser() );
244 if ( isset( $options['internalmode'] ) && !$options['internalmode'] ) {
245 $mainWrapper = TestingAccessWrapper
::newFromObject( $wrapper->mMainModule
);
246 $mainWrapper->mInternalMode
= false;
249 // If we're testing tags, set up some tags
250 if ( isset( $paramSettings[ApiBase
::PARAM_TYPE
] ) &&
251 $paramSettings[ApiBase
::PARAM_TYPE
] === 'tags'
253 ChangeTags
::defineTag( 'tag1' );
254 ChangeTags
::defineTag( 'tag2' );
257 if ( $expected instanceof Exception
) {
259 $wrapper->getParameterFromSettings( 'myParam', $paramSettings,
261 $this->fail( 'No exception thrown' );
262 } catch ( Exception
$ex ) {
263 $this->assertEquals( $expected, $ex );
266 $result = $wrapper->getParameterFromSettings( 'myParam',
267 $paramSettings, $parseLimits );
268 if ( isset( $paramSettings[ApiBase
::PARAM_TYPE
] ) &&
269 $paramSettings[ApiBase
::PARAM_TYPE
] === 'timestamp' &&
272 // Allow one second of fuzziness. Make sure the formats are
274 $this->assertRegExp( '/^\d{14}$/', $result );
275 $this->assertLessThanOrEqual( 1,
276 abs( wfTimestamp( TS_UNIX
, $result ) - time() ),
277 "Result $result differs from expected $expected by " .
278 'more than one second' );
280 $this->assertSame( $expected, $result );
282 $actualWarnings = array_map( function ( $warn ) {
283 return $warn instanceof Message
284 ?
array_merge( [ $warn->getKey() ], $warn->getParams() )
286 }, $mock->warnings
);
287 $this->assertSame( $warnings, $actualWarnings );
290 if ( !empty( $paramSettings[ApiBase
::PARAM_SENSITIVE
] ) ||
291 ( isset( $paramSettings[ApiBase
::PARAM_TYPE
] ) &&
292 $paramSettings[ApiBase
::PARAM_TYPE
] === 'password' )
294 $mainWrapper = TestingAccessWrapper
::newFromObject( $wrapper->getMain() );
295 $this->assertSame( [ 'myParam' ],
296 $mainWrapper->getSensitiveParams() );
300 public static function provideGetParameterFromSettings() {
302 [ 'apiwarn-badutf8', 'myParam' ],
307 for ( $i = 0; $i < 32; $i++
) {
309 $enc .= ( $i === 9 ||
$i === 10 ||
$i === 13 )
315 'Basic param' => [ 'bar', null, 'bar', [] ],
316 'Basic param, C0 controls' => [ $c0, null, $enc, $warnings ],
317 'String param' => [ 'bar', '', 'bar', [] ],
318 'String param, defaulted' => [ null, '', '', [] ],
319 'String param, empty' => [ '', 'default', '', [] ],
320 'String param, required, empty' => [
322 [ ApiBase
::PARAM_DFLT
=> 'default', ApiBase
::PARAM_REQUIRED
=> true ],
323 ApiUsageException
::newWithMessage( null,
324 [ 'apierror-missingparam', 'myParam' ] ),
327 'Multi-valued parameter' => [
329 [ ApiBase
::PARAM_ISMULTI
=> true ],
333 'Multi-valued parameter, alternative separator' => [
335 [ ApiBase
::PARAM_ISMULTI
=> true ],
339 'Multi-valued parameter, other C0 controls' => [
341 [ ApiBase
::PARAM_ISMULTI
=> true ],
345 'Multi-valued parameter, other C0 controls (2)' => [
347 [ ApiBase
::PARAM_ISMULTI
=> true ],
348 [ substr( $enc, 0, -3 ), '' ],
351 'Multi-valued parameter with limits' => [
354 ApiBase
::PARAM_ISMULTI
=> true,
355 ApiBase
::PARAM_ISMULTI_LIMIT1
=> 3,
360 'Multi-valued parameter with exceeded limits' => [
363 ApiBase
::PARAM_ISMULTI
=> true,
364 ApiBase
::PARAM_ISMULTI_LIMIT1
=> 2,
366 ApiUsageException
::newWithMessage(
367 null, [ 'apierror-toomanyvalues', 'myParam', 2 ], 'too-many-myParam'
371 'Multi-valued parameter with exceeded limits for non-bot' => [
374 ApiBase
::PARAM_ISMULTI
=> true,
375 ApiBase
::PARAM_ISMULTI_LIMIT1
=> 2,
376 ApiBase
::PARAM_ISMULTI_LIMIT2
=> 3,
378 ApiUsageException
::newWithMessage(
379 null, [ 'apierror-toomanyvalues', 'myParam', 2 ], 'too-many-myParam'
383 'Multi-valued parameter with non-exceeded limits for bot' => [
386 ApiBase
::PARAM_ISMULTI
=> true,
387 ApiBase
::PARAM_ISMULTI_LIMIT1
=> 2,
388 ApiBase
::PARAM_ISMULTI_LIMIT2
=> 3,
392 [ 'apihighlimits' => true ],
394 'Multi-valued parameter with prohibited duplicates' => [
396 [ ApiBase
::PARAM_ISMULTI
=> true ],
397 // Note that the keys are not sequential! This matches
398 // array_unique, but might be unexpected.
399 [ 0 => 'a', 1 => 'b', 3 => 'c' ],
402 'Multi-valued parameter with allowed duplicates' => [
405 ApiBase
::PARAM_ISMULTI
=> true,
406 ApiBase
::PARAM_ALLOW_DUPLICATES
=> true,
411 'Empty boolean param' => [
413 [ ApiBase
::PARAM_TYPE
=> 'boolean' ],
417 'Boolean param 0' => [
419 [ ApiBase
::PARAM_TYPE
=> 'boolean' ],
423 'Boolean param false' => [
425 [ ApiBase
::PARAM_TYPE
=> 'boolean' ],
429 'Boolean multi-param' => [
432 ApiBase
::PARAM_TYPE
=> 'boolean',
433 ApiBase
::PARAM_ISMULTI
=> true,
436 'Internal error in ApiBase::getParameterFromSettings: ' .
437 'Multi-values not supported for myParam'
441 'Empty boolean param with non-false default' => [
444 ApiBase
::PARAM_TYPE
=> 'boolean',
445 ApiBase
::PARAM_DFLT
=> true,
448 'Internal error in ApiBase::getParameterFromSettings: ' .
449 "Boolean param myParam's default is set to '1'. " .
450 'Boolean parameters must default to false.' ),
453 'Deprecated parameter' => [
455 [ ApiBase
::PARAM_DEPRECATED
=> true ],
457 [ [ 'apiwarn-deprecation-parameter', 'myParam' ] ],
459 'Deprecated parameter value' => [
461 [ ApiBase
::PARAM_DEPRECATED_VALUES
=> [ 'a' => true ] ],
463 [ [ 'apiwarn-deprecation-parameter', 'myParam=a' ] ],
465 'Multiple deprecated parameter values' => [
467 [ ApiBase
::PARAM_DEPRECATED_VALUES
=>
468 [ 'b' => true, 'd' => true ],
469 ApiBase
::PARAM_ISMULTI
=> true ],
470 [ 'a', 'b', 'c', 'd' ],
472 [ 'apiwarn-deprecation-parameter', 'myParam=b' ],
473 [ 'apiwarn-deprecation-parameter', 'myParam=d' ],
476 'Deprecated parameter value with custom warning' => [
478 [ ApiBase
::PARAM_DEPRECATED_VALUES
=> [ 'a' => 'my-msg' ] ],
482 '"*" when wildcard not allowed' => [
484 [ ApiBase
::PARAM_ISMULTI
=> true,
485 ApiBase
::PARAM_TYPE
=> [ 'a', 'b', 'c' ] ],
487 [ [ 'apiwarn-unrecognizedvalues', 'myParam',
488 [ 'list' => [ '*' ], 'type' => 'comma' ], 1 ] ],
493 ApiBase
::PARAM_ISMULTI
=> true,
494 ApiBase
::PARAM_TYPE
=> [ 'a', 'b', 'c' ],
495 ApiBase
::PARAM_ALL
=> true,
500 'Wildcard "*" with multiples not allowed' => [
503 ApiBase
::PARAM_TYPE
=> [ 'a', 'b', 'c' ],
504 ApiBase
::PARAM_ALL
=> true,
506 ApiUsageException
::newWithMessage( null,
507 [ 'apierror-unrecognizedvalue', 'myParam', '*' ],
511 'Wildcard "*" with unrestricted type' => [
514 ApiBase
::PARAM_ISMULTI
=> true,
515 ApiBase
::PARAM_ALL
=> true,
523 ApiBase
::PARAM_ISMULTI
=> true,
524 ApiBase
::PARAM_TYPE
=> [ 'a', 'b', 'c' ],
525 ApiBase
::PARAM_ALL
=> 'x',
530 'Wildcard conflicting with allowed value' => [
533 ApiBase
::PARAM_ISMULTI
=> true,
534 ApiBase
::PARAM_TYPE
=> [ 'a', 'b', 'c' ],
535 ApiBase
::PARAM_ALL
=> 'a',
538 'Internal error in ApiBase::getParameterFromSettings: ' .
539 'For param myParam, PARAM_ALL collides with a possible ' .
543 'Namespace with wildcard' => [
546 ApiBase
::PARAM_ISMULTI
=> true,
547 ApiBase
::PARAM_TYPE
=> 'namespace',
549 MWNamespace
::getValidNamespaces(),
552 // PARAM_ALL is ignored with namespace types.
553 'Namespace with wildcard suppressed' => [
556 ApiBase
::PARAM_ISMULTI
=> true,
557 ApiBase
::PARAM_TYPE
=> 'namespace',
558 ApiBase
::PARAM_ALL
=> false,
560 MWNamespace
::getValidNamespaces(),
563 'Namespace with wildcard "x"' => [
566 ApiBase
::PARAM_ISMULTI
=> true,
567 ApiBase
::PARAM_TYPE
=> 'namespace',
568 ApiBase
::PARAM_ALL
=> 'x',
571 [ [ 'apiwarn-unrecognizedvalues', 'myParam',
572 [ 'list' => [ 'x' ], 'type' => 'comma' ], 1 ] ],
575 'dDy+G?e?txnr.1:(@[Ru',
576 [ ApiBase
::PARAM_TYPE
=> 'password' ],
577 'dDy+G?e?txnr.1:(@[Ru',
580 'Sensitive field' => [
581 'I am fond of pineapples',
582 [ ApiBase
::PARAM_SENSITIVE
=> true ],
583 'I am fond of pineapples',
586 'Upload with default' => [
589 ApiBase
::PARAM_TYPE
=> 'upload',
590 ApiBase
::PARAM_DFLT
=> '',
593 'Internal error in ApiBase::getParameterFromSettings: ' .
594 "File upload param myParam's default is set to ''. " .
595 'File upload parameters may not have a default.' ),
598 'Multiple upload' => [
601 ApiBase
::PARAM_TYPE
=> 'upload',
602 ApiBase
::PARAM_ISMULTI
=> true,
605 'Internal error in ApiBase::getParameterFromSettings: ' .
606 'Multi-values not supported for myParam' ),
609 // @todo Test actual upload
612 [ ApiBase
::PARAM_TYPE
=> 'namespace' ],
613 ApiUsageException
::newWithMessage( null,
614 [ 'apierror-unrecognizedvalue', 'myParam', '-1' ],
618 'Extra namespace -1' => [
621 ApiBase
::PARAM_TYPE
=> 'namespace',
622 ApiBase
::PARAM_EXTRA_NAMESPACES
=> [ '-1' ],
627 // @todo Test with PARAM_SUBMODULE_MAP unset, need
628 // getModuleManager() to return something real
629 'Nonexistent module' => [
632 ApiBase
::PARAM_TYPE
=> 'submodule',
633 ApiBase
::PARAM_SUBMODULE_MAP
=>
634 [ 'foo' => 'foo', 'bar' => 'foo+bar' ],
636 ApiUsageException
::newWithMessage(
639 'apierror-unrecognizedvalue',
647 '\\x1f with multiples not allowed' => [
650 ApiUsageException
::newWithMessage( null,
651 'apierror-badvalue-notmultivalue',
652 'badvalue_notmultivalue' ),
655 'Integer with unenforced min' => [
658 ApiBase
::PARAM_TYPE
=> 'integer',
659 ApiBase
::PARAM_MIN
=> -1,
662 [ [ 'apierror-integeroutofrange-belowminimum', 'myParam', -1,
665 'Integer with enforced min' => [
668 ApiBase
::PARAM_TYPE
=> 'integer',
669 ApiBase
::PARAM_MIN
=> -1,
670 ApiBase
::PARAM_RANGE_ENFORCE
=> true,
672 ApiUsageException
::newWithMessage( null,
673 [ 'apierror-integeroutofrange-belowminimum', 'myParam',
674 '-1', '-2' ], 'integeroutofrange',
675 [ 'min' => -1, 'max' => null, 'botMax' => null ] ),
678 'Integer with unenforced max (internal mode)' => [
681 ApiBase
::PARAM_TYPE
=> 'integer',
682 ApiBase
::PARAM_MAX
=> 7,
687 'Integer with enforced max (internal mode)' => [
690 ApiBase
::PARAM_TYPE
=> 'integer',
691 ApiBase
::PARAM_MAX
=> 7,
692 ApiBase
::PARAM_RANGE_ENFORCE
=> true,
697 'Integer with unenforced max (non-internal mode)' => [
700 ApiBase
::PARAM_TYPE
=> 'integer',
701 ApiBase
::PARAM_MAX
=> 7,
704 [ [ 'apierror-integeroutofrange-abovemax', 'myParam', 7, 8 ] ],
705 [ 'internalmode' => false ],
707 'Integer with enforced max (non-internal mode)' => [
710 ApiBase
::PARAM_TYPE
=> 'integer',
711 ApiBase
::PARAM_MAX
=> 7,
712 ApiBase
::PARAM_RANGE_ENFORCE
=> true,
714 ApiUsageException
::newWithMessage(
716 [ 'apierror-integeroutofrange-abovemax', 'myParam', '7', '8' ],
718 [ 'min' => null, 'max' => 7, 'botMax' => 7 ]
721 [ 'internalmode' => false ],
723 'Array of integers' => [
726 ApiBase
::PARAM_ISMULTI
=> true,
727 ApiBase
::PARAM_TYPE
=> 'integer',
732 'Array of integers with unenforced min/max (internal mode)' => [
735 ApiBase
::PARAM_ISMULTI
=> true,
736 ApiBase
::PARAM_TYPE
=> 'integer',
737 ApiBase
::PARAM_MIN
=> 0,
738 ApiBase
::PARAM_MAX
=> 100,
741 [ [ 'apierror-integeroutofrange-belowminimum', 'myParam', 0, -1 ] ],
743 'Array of integers with enforced min/max (internal mode)' => [
746 ApiBase
::PARAM_ISMULTI
=> true,
747 ApiBase
::PARAM_TYPE
=> 'integer',
748 ApiBase
::PARAM_MIN
=> 0,
749 ApiBase
::PARAM_MAX
=> 100,
750 ApiBase
::PARAM_RANGE_ENFORCE
=> true,
752 ApiUsageException
::newWithMessage(
754 [ 'apierror-integeroutofrange-belowminimum', 'myParam', 0, -1 ],
756 [ 'min' => 0, 'max' => 100, 'botMax' => 100 ]
760 'Array of integers with unenforced min/max (non-internal mode)' => [
763 ApiBase
::PARAM_ISMULTI
=> true,
764 ApiBase
::PARAM_TYPE
=> 'integer',
765 ApiBase
::PARAM_MIN
=> 0,
766 ApiBase
::PARAM_MAX
=> 100,
770 [ 'apierror-integeroutofrange-abovemax', 'myParam', 100, 966 ],
771 [ 'apierror-integeroutofrange-belowminimum', 'myParam', 0, -1 ]
773 [ 'internalmode' => false ],
775 'Array of integers with enforced min/max (non-internal mode)' => [
778 ApiBase
::PARAM_ISMULTI
=> true,
779 ApiBase
::PARAM_TYPE
=> 'integer',
780 ApiBase
::PARAM_MIN
=> 0,
781 ApiBase
::PARAM_MAX
=> 100,
782 ApiBase
::PARAM_RANGE_ENFORCE
=> true,
784 ApiUsageException
::newWithMessage(
786 [ 'apierror-integeroutofrange-abovemax', 'myParam', 100, 966 ],
788 [ 'min' => 0, 'max' => 100, 'botMax' => 100 ]
791 [ 'internalmode' => false ],
793 'Limit with parseLimits false' => [
795 [ ApiBase
::PARAM_TYPE
=> 'limit' ],
798 [ 'parseLimits' => false ],
800 'Limit with no max' => [
803 ApiBase
::PARAM_TYPE
=> 'limit',
804 ApiBase
::PARAM_MAX2
=> 10,
805 ApiBase
::PARAM_ISMULTI
=> true,
808 'Internal error in ApiBase::getParameterFromSettings: ' .
809 'MAX1 or MAX2 are not defined for the limit myParam' ),
812 'Limit with no max2' => [
815 ApiBase
::PARAM_TYPE
=> 'limit',
816 ApiBase
::PARAM_MAX
=> 10,
817 ApiBase
::PARAM_ISMULTI
=> true,
820 'Internal error in ApiBase::getParameterFromSettings: ' .
821 'MAX1 or MAX2 are not defined for the limit myParam' ),
824 'Limit with multi-value' => [
827 ApiBase
::PARAM_TYPE
=> 'limit',
828 ApiBase
::PARAM_MAX
=> 10,
829 ApiBase
::PARAM_MAX2
=> 10,
830 ApiBase
::PARAM_ISMULTI
=> true,
833 'Internal error in ApiBase::getParameterFromSettings: ' .
834 'Multi-values not supported for myParam' ),
840 ApiBase
::PARAM_TYPE
=> 'limit',
841 ApiBase
::PARAM_MAX
=> 100,
842 ApiBase
::PARAM_MAX2
=> 100,
850 ApiBase
::PARAM_TYPE
=> 'limit',
851 ApiBase
::PARAM_MAX
=> 100,
852 ApiBase
::PARAM_MAX2
=> 101,
857 'Limit max for apihighlimits' => [
860 ApiBase
::PARAM_TYPE
=> 'limit',
861 ApiBase
::PARAM_MAX
=> 100,
862 ApiBase
::PARAM_MAX2
=> 101,
866 [ 'apihighlimits' => true ],
868 'Limit too large (internal mode)' => [
871 ApiBase
::PARAM_TYPE
=> 'limit',
872 ApiBase
::PARAM_MAX
=> 100,
873 ApiBase
::PARAM_MAX2
=> 101,
878 'Limit okay for apihighlimits (internal mode)' => [
881 ApiBase
::PARAM_TYPE
=> 'limit',
882 ApiBase
::PARAM_MAX
=> 100,
883 ApiBase
::PARAM_MAX2
=> 101,
887 [ 'apihighlimits' => true ],
889 'Limit too large for apihighlimits (internal mode)' => [
892 ApiBase
::PARAM_TYPE
=> 'limit',
893 ApiBase
::PARAM_MAX
=> 100,
894 ApiBase
::PARAM_MAX2
=> 101,
898 [ 'apihighlimits' => true ],
900 'Limit too large (non-internal mode)' => [
903 ApiBase
::PARAM_TYPE
=> 'limit',
904 ApiBase
::PARAM_MAX
=> 100,
905 ApiBase
::PARAM_MAX2
=> 101,
908 [ [ 'apierror-integeroutofrange-abovemax', 'myParam', 100, 101 ] ],
909 [ 'internalmode' => false ],
911 'Limit okay for apihighlimits (non-internal mode)' => [
914 ApiBase
::PARAM_TYPE
=> 'limit',
915 ApiBase
::PARAM_MAX
=> 100,
916 ApiBase
::PARAM_MAX2
=> 101,
920 [ 'internalmode' => false, 'apihighlimits' => true ],
922 'Limit too large for apihighlimits (non-internal mode)' => [
925 ApiBase
::PARAM_TYPE
=> 'limit',
926 ApiBase
::PARAM_MAX
=> 100,
927 ApiBase
::PARAM_MAX2
=> 101,
930 [ [ 'apierror-integeroutofrange-abovebotmax', 'myParam', 101, 102 ] ],
931 [ 'internalmode' => false, 'apihighlimits' => true ],
933 'Limit too small' => [
936 ApiBase
::PARAM_TYPE
=> 'limit',
937 ApiBase
::PARAM_MIN
=> -1,
938 ApiBase
::PARAM_MAX
=> 100,
939 ApiBase
::PARAM_MAX2
=> 100,
942 [ [ 'apierror-integeroutofrange-belowminimum', 'myParam', -1,
946 wfTimestamp( TS_UNIX
, '20211221122112' ),
947 [ ApiBase
::PARAM_TYPE
=> 'timestamp' ],
953 [ ApiBase
::PARAM_TYPE
=> 'timestamp' ],
956 [ [ 'apiwarn-unclearnowtimestamp', 'myParam', '0' ] ],
958 'Timestamp empty' => [
960 [ ApiBase
::PARAM_TYPE
=> 'timestamp' ],
962 [ [ 'apiwarn-unclearnowtimestamp', 'myParam', '' ] ],
964 // wfTimestamp() interprets this as Unix time
967 [ ApiBase
::PARAM_TYPE
=> 'timestamp' ],
973 [ ApiBase
::PARAM_TYPE
=> 'timestamp' ],
977 'Invalid timestamp' => [
979 [ ApiBase
::PARAM_TYPE
=> 'timestamp' ],
980 ApiUsageException
::newWithMessage(
982 [ 'apierror-badtimestamp', 'myParam', 'a potato' ],
983 'badtimestamp_myParam'
987 'Timestamp array' => [
990 ApiBase
::PARAM_TYPE
=> 'timestamp',
991 ApiBase
::PARAM_ISMULTI
=> 1,
993 [ wfTimestamp( TS_MW
, 100 ), wfTimestamp( TS_MW
, 101 ) ],
998 [ ApiBase
::PARAM_TYPE
=> 'user' ],
1002 'User prefixed with "User:"' => [
1004 [ ApiBase
::PARAM_TYPE
=> 'user' ],
1008 'Invalid username "|"' => [
1010 [ ApiBase
::PARAM_TYPE
=> 'user' ],
1011 ApiUsageException
::newWithMessage( null,
1012 [ 'apierror-baduser', 'myParam', '|' ],
1013 'baduser_myParam' ),
1016 'Invalid username "300.300.300.300"' => [
1018 [ ApiBase
::PARAM_TYPE
=> 'user' ],
1019 ApiUsageException
::newWithMessage( null,
1020 [ 'apierror-baduser', 'myParam', '300.300.300.300' ],
1021 'baduser_myParam' ),
1024 'IP range as username' => [
1026 [ ApiBase
::PARAM_TYPE
=> 'user' ],
1030 'IPv6 as username' => [
1032 [ ApiBase
::PARAM_TYPE
=> 'user' ],
1036 'Obsolete cloaked usemod IP address as username' => [
1038 [ ApiBase
::PARAM_TYPE
=> 'user' ],
1042 'Invalid username containing IP address' => [
1043 'This is [not] valid 1.2.3.xxx, ha!',
1044 [ ApiBase
::PARAM_TYPE
=> 'user' ],
1045 ApiUsageException
::newWithMessage(
1047 [ 'apierror-baduser', 'myParam', 'This is [not] valid 1.2.3.xxx, ha!' ],
1052 'External username' => [
1054 [ ApiBase
::PARAM_TYPE
=> 'user' ],
1058 'Array of usernames' => [
1061 ApiBase
::PARAM_TYPE
=> 'user',
1062 ApiBase
::PARAM_ISMULTI
=> true,
1069 [ ApiBase
::PARAM_TYPE
=> 'tags' ],
1073 'Array of one tag' => [
1076 ApiBase
::PARAM_TYPE
=> 'tags',
1077 ApiBase
::PARAM_ISMULTI
=> true,
1082 'Array of tags' => [
1085 ApiBase
::PARAM_TYPE
=> 'tags',
1086 ApiBase
::PARAM_ISMULTI
=> true,
1093 [ ApiBase
::PARAM_TYPE
=> 'tags' ],
1094 new ApiUsageException( null,
1095 Status
::newFatal( 'tags-apply-not-allowed-one',
1096 'invalid tag', 1 ) ),
1099 'Unrecognized type' => [
1101 [ ApiBase
::PARAM_TYPE
=> 'nonexistenttype' ],
1103 'Internal error in ApiBase::getParameterFromSettings: ' .
1104 "Param myParam's type is unknown - nonexistenttype" ),
1107 'Too many bytes' => [
1110 ApiBase
::PARAM_MAX_BYTES
=> 0,
1111 ApiBase
::PARAM_MAX_CHARS
=> 0,
1113 ApiUsageException
::newWithMessage( null,
1114 [ 'apierror-maxbytes', 'myParam', 0 ] ),
1117 'Too many chars' => [
1120 ApiBase
::PARAM_MAX_BYTES
=> 4,
1121 ApiBase
::PARAM_MAX_CHARS
=> 1,
1123 ApiUsageException
::newWithMessage( null,
1124 [ 'apierror-maxchars', 'myParam', 1 ] ),
1127 'Omitted required param' => [
1129 [ ApiBase
::PARAM_REQUIRED
=> true ],
1130 ApiUsageException
::newWithMessage( null,
1131 [ 'apierror-missingparam', 'myParam' ] ),
1134 'Empty multi-value' => [
1136 [ ApiBase
::PARAM_ISMULTI
=> true ],
1140 'Multi-value \x1f' => [
1142 [ ApiBase
::PARAM_ISMULTI
=> true ],
1146 'Allowed non-multi-value with "|"' => [
1148 [ ApiBase
::PARAM_TYPE
=> [ 'a|b' ] ],
1152 'Prohibited multi-value' => [
1154 [ ApiBase
::PARAM_TYPE
=> [ 'a', 'b' ] ],
1155 ApiUsageException
::newWithMessage( null,
1157 'apierror-multival-only-one-of',
1159 Message
::listParam( [ '<kbd>a</kbd>', '<kbd>b</kbd>' ] ),
1168 // The following really just test PHP's string-to-int conversion.
1176 [ "\t1", 1, '\t1' ],
1177 [ "\r1", 1, '\r1' ],
1178 [ "\f1", 0, '\f1', 'badutf-8' ],
1179 [ "\n1", 1, '\n1' ],
1180 [ "\v1", 0, '\v1', 'badutf-8' ],
1181 [ "\e1", 0, '\e1', 'badutf-8' ],
1182 [ "\x001", 0, '\x001', 'badutf-8' ],
1185 foreach ( $integerTests as $test ) {
1186 $desc = isset( $test[2] ) ?
$test[2] : $test[0];
1187 $warnings = isset( $test[3] ) ?
1188 [ [ 'apiwarn-badutf8', 'myParam' ] ] : [];
1189 $returnArray["\"$desc\" as integer"] = [
1191 [ ApiBase
::PARAM_TYPE
=> 'integer' ],
1197 return $returnArray;
1200 public function testErrorArrayToStatus() {
1201 $mock = new MockApi();
1203 // Sanity check empty array
1204 $expect = Status
::newGood();
1205 $this->assertEquals( $expect, $mock->errorArrayToStatus( [] ) );
1207 // No blocked $user, so no special block handling
1208 $expect = Status
::newGood();
1209 $expect->fatal( 'blockedtext' );
1210 $expect->fatal( 'autoblockedtext' );
1211 $expect->fatal( 'systemblockedtext' );
1212 $expect->fatal( 'mainpage' );
1213 $expect->fatal( 'parentheses', 'foobar' );
1214 $this->assertEquals( $expect, $mock->errorArrayToStatus( [
1216 [ 'autoblockedtext' ],
1217 [ 'systemblockedtext' ],
1219 [ 'parentheses', 'foobar' ],
1222 // Has a blocked $user, so special block handling
1223 $user = $this->getMutableTestUser()->getUser();
1224 $block = new \
Block( [
1225 'address' => $user->getName(),
1226 'user' => $user->getID(),
1227 'by' => $this->getTestSysop()->getUser()->getId(),
1228 'reason' => __METHOD__
,
1229 'expiry' => time() +
100500,
1232 $blockinfo = [ 'blockinfo' => ApiQueryUserInfo
::getBlockInfo( $block ) ];
1234 $expect = Status
::newGood();
1235 $expect->fatal( ApiMessage
::create( 'apierror-blocked', 'blocked', $blockinfo ) );
1236 $expect->fatal( ApiMessage
::create( 'apierror-autoblocked', 'autoblocked', $blockinfo ) );
1237 $expect->fatal( ApiMessage
::create( 'apierror-systemblocked', 'blocked', $blockinfo ) );
1238 $expect->fatal( 'mainpage' );
1239 $expect->fatal( 'parentheses', 'foobar' );
1240 $this->assertEquals( $expect, $mock->errorArrayToStatus( [
1242 [ 'autoblockedtext' ],
1243 [ 'systemblockedtext' ],
1245 [ 'parentheses', 'foobar' ],
1249 public function testDieStatus() {
1250 $mock = new MockApi();
1252 $status = StatusValue
::newGood();
1253 $status->error( 'foo' );
1254 $status->warning( 'bar' );
1256 $mock->dieStatus( $status );
1257 $this->fail( 'Expected exception not thrown' );
1258 } catch ( ApiUsageException
$ex ) {
1259 $this->assertTrue( ApiTestCase
::apiExceptionHasCode( $ex, 'foo' ), 'Exception has "foo"' );
1260 $this->assertFalse( ApiTestCase
::apiExceptionHasCode( $ex, 'bar' ), 'Exception has "bar"' );
1263 $status = StatusValue
::newGood();
1264 $status->warning( 'foo' );
1265 $status->warning( 'bar' );
1267 $mock->dieStatus( $status );
1268 $this->fail( 'Expected exception not thrown' );
1269 } catch ( ApiUsageException
$ex ) {
1270 $this->assertTrue( ApiTestCase
::apiExceptionHasCode( $ex, 'foo' ), 'Exception has "foo"' );
1271 $this->assertTrue( ApiTestCase
::apiExceptionHasCode( $ex, 'bar' ), 'Exception has "bar"' );
1274 $status = StatusValue
::newGood();
1275 $status->setOk( false );
1277 $mock->dieStatus( $status );
1278 $this->fail( 'Expected exception not thrown' );
1279 } catch ( ApiUsageException
$ex ) {
1280 $this->assertTrue( ApiTestCase
::apiExceptionHasCode( $ex, 'unknownerror-nocode' ),
1281 'Exception has "unknownerror-nocode"' );
1286 * @covers ApiBase::extractRequestParams
1288 public function testExtractRequestParams() {
1289 $request = new FauxRequest( [
1290 'xxexists' => 'exists!',
1291 'xxmulti' => 'a|b|c|d|{bad}',
1293 'xxtemplate-a' => 'A!',
1294 'xxtemplate-b' => 'B1|B2|B3',
1295 'xxtemplate-c' => '',
1296 'xxrecursivetemplate-b-B1' => 'X',
1297 'xxrecursivetemplate-b-B3' => 'Y',
1298 'xxrecursivetemplate-b-B4' => '?',
1299 'xxemptytemplate-' => 'nope',
1302 'errorformat' => 'raw',
1304 $context = new DerivativeContext( RequestContext
::getMain() );
1305 $context->setRequest( $request );
1306 $main = new ApiMain( $context );
1308 $mock = $this->getMockBuilder( ApiBase
::class )
1309 ->setConstructorArgs( [ $main, 'test', 'xx' ] )
1310 ->setMethods( [ 'getAllowedParams' ] )
1311 ->getMockForAbstractClass();
1312 $mock->method( 'getAllowedParams' )->willReturn( [
1313 'notexists' => null,
1316 ApiBase
::PARAM_ISMULTI
=> true,
1319 ApiBase
::PARAM_ISMULTI
=> true,
1322 ApiBase
::PARAM_ISMULTI
=> true,
1323 ApiBase
::PARAM_TEMPLATE_VARS
=> [ 'm' => 'multi' ],
1325 'recursivetemplate-{m}-{t}' => [
1326 ApiBase
::PARAM_TEMPLATE_VARS
=> [ 't' => 'template-{m}', 'm' => 'multi' ],
1328 'emptytemplate-{m}' => [
1329 ApiBase
::PARAM_ISMULTI
=> true,
1330 ApiBase
::PARAM_TEMPLATE_VARS
=> [ 'm' => 'empty' ],
1332 'badtemplate-{e}' => [
1333 ApiBase
::PARAM_TEMPLATE_VARS
=> [ 'e' => 'exists' ],
1335 'badtemplate2-{e}' => [
1336 ApiBase
::PARAM_TEMPLATE_VARS
=> [ 'e' => 'badtemplate2-{e}' ],
1338 'badtemplate3-{x}' => [
1339 ApiBase
::PARAM_TEMPLATE_VARS
=> [ 'x' => 'foo' ],
1343 $this->assertEquals( [
1344 'notexists' => null,
1345 'exists' => 'exists!',
1346 'multi' => [ 'a', 'b', 'c', 'd', '{bad}' ],
1348 'template-a' => [ 'A!' ],
1349 'template-b' => [ 'B1', 'B2', 'B3' ],
1351 'template-d' => null,
1352 'recursivetemplate-a-A!' => null,
1353 'recursivetemplate-b-B1' => 'X',
1354 'recursivetemplate-b-B2' => null,
1355 'recursivetemplate-b-B3' => 'Y',
1356 ], $mock->extractRequestParams() );
1358 $used = TestingAccessWrapper
::newFromObject( $main )->getParamsUsed();
1360 $this->assertEquals( [
1365 'xxrecursivetemplate-a-A!',
1366 'xxrecursivetemplate-b-B1',
1367 'xxrecursivetemplate-b-B2',
1368 'xxrecursivetemplate-b-B3',
1375 $warnings = $mock->getResult()->getResultData( 'warnings', [ 'Strip' => 'all' ] );
1376 $this->assertCount( 1, $warnings );
1377 $this->assertSame( 'ignoring-invalid-templated-value', $warnings[0]['code'] );