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 ] );
215 public function testGetParameter() {
216 $mock = $this->getMockBuilder( MockApi
::class )
217 ->setMethods( [ 'getAllowedParams' ] )
219 $mock->method( 'getAllowedParams' )->willReturn( [
221 ApiBase
::PARAM_TYPE
=> [ 'value' ],
224 ApiBase
::PARAM_TYPE
=> [ 'value' ],
227 $wrapper = TestingAccessWrapper
::newFromObject( $mock );
229 $context = new DerivativeContext( $mock );
230 $context->setRequest( new FauxRequest( [ 'foo' => 'bad', 'bar' => 'value' ] ) );
231 $wrapper->mMainModule
= new ApiMain( $context );
233 // Even though 'foo' is bad, getParameter( 'bar' ) must not fail
234 $this->assertSame( 'value', $wrapper->getParameter( 'bar' ) );
236 // But getParameter( 'foo' ) must throw.
238 $wrapper->getParameter( 'foo' );
239 $this->fail( 'Expected exception not thrown' );
240 } catch ( ApiUsageException
$ex ) {
241 $this->assertTrue( $this->apiExceptionHasCode( $ex, 'unknown_foo' ) );
244 // And extractRequestParams() must throw too.
246 $mock->extractRequestParams();
247 $this->fail( 'Expected exception not thrown' );
248 } catch ( ApiUsageException
$ex ) {
249 $this->assertTrue( $this->apiExceptionHasCode( $ex, 'unknown_foo' ) );
254 * @dataProvider provideGetParameterFromSettings
255 * @param string|null $input
256 * @param array $paramSettings
257 * @param mixed $expected
258 * @param array $options Key-value pairs:
259 * 'parseLimits': true|false
260 * 'apihighlimits': true|false
261 * 'internalmode': true|false
262 * @param string[] $warnings
264 public function testGetParameterFromSettings(
265 $input, $paramSettings, $expected, $warnings, $options = []
267 $mock = new MockApi();
268 $wrapper = TestingAccessWrapper
::newFromObject( $mock );
270 $context = new DerivativeContext( $mock );
271 $context->setRequest( new FauxRequest(
272 $input !== null ?
[ 'myParam' => $input ] : [] ) );
273 $wrapper->mMainModule
= new ApiMain( $context );
275 $parseLimits = $options['parseLimits'] ??
true;
277 if ( !empty( $options['apihighlimits'] ) ) {
278 $context->setUser( self
::$users['sysop']->getUser() );
281 if ( isset( $options['internalmode'] ) && !$options['internalmode'] ) {
282 $mainWrapper = TestingAccessWrapper
::newFromObject( $wrapper->mMainModule
);
283 $mainWrapper->mInternalMode
= false;
286 // If we're testing tags, set up some tags
287 if ( isset( $paramSettings[ApiBase
::PARAM_TYPE
] ) &&
288 $paramSettings[ApiBase
::PARAM_TYPE
] === 'tags'
290 ChangeTags
::defineTag( 'tag1' );
291 ChangeTags
::defineTag( 'tag2' );
294 if ( $expected instanceof Exception
) {
296 $wrapper->getParameterFromSettings( 'myParam', $paramSettings,
298 $this->fail( 'No exception thrown' );
299 } catch ( Exception
$ex ) {
300 $this->assertEquals( $expected, $ex );
303 $result = $wrapper->getParameterFromSettings( 'myParam',
304 $paramSettings, $parseLimits );
305 if ( isset( $paramSettings[ApiBase
::PARAM_TYPE
] ) &&
306 $paramSettings[ApiBase
::PARAM_TYPE
] === 'timestamp' &&
309 // Allow one second of fuzziness. Make sure the formats are
311 $this->assertRegExp( '/^\d{14}$/', $result );
312 $this->assertLessThanOrEqual( 1,
313 abs( wfTimestamp( TS_UNIX
, $result ) - time() ),
314 "Result $result differs from expected $expected by " .
315 'more than one second' );
317 $this->assertSame( $expected, $result );
319 $actualWarnings = array_map( function ( $warn ) {
320 return $warn instanceof Message
321 ?
array_merge( [ $warn->getKey() ], $warn->getParams() )
323 }, $mock->warnings
);
324 $this->assertSame( $warnings, $actualWarnings );
327 if ( !empty( $paramSettings[ApiBase
::PARAM_SENSITIVE
] ) ||
328 ( isset( $paramSettings[ApiBase
::PARAM_TYPE
] ) &&
329 $paramSettings[ApiBase
::PARAM_TYPE
] === 'password' )
331 $mainWrapper = TestingAccessWrapper
::newFromObject( $wrapper->getMain() );
332 $this->assertSame( [ 'myParam' ],
333 $mainWrapper->getSensitiveParams() );
337 public static function provideGetParameterFromSettings() {
339 [ 'apiwarn-badutf8', 'myParam' ],
344 for ( $i = 0; $i < 32; $i++
) {
346 $enc .= ( $i === 9 ||
$i === 10 ||
$i === 13 )
352 'Basic param' => [ 'bar', null, 'bar', [] ],
353 'Basic param, C0 controls' => [ $c0, null, $enc, $warnings ],
354 'String param' => [ 'bar', '', 'bar', [] ],
355 'String param, defaulted' => [ null, '', '', [] ],
356 'String param, empty' => [ '', 'default', '', [] ],
357 'String param, required, empty' => [
359 [ ApiBase
::PARAM_DFLT
=> 'default', ApiBase
::PARAM_REQUIRED
=> true ],
360 ApiUsageException
::newWithMessage( null,
361 [ 'apierror-missingparam', 'myParam' ] ),
364 'Multi-valued parameter' => [
366 [ ApiBase
::PARAM_ISMULTI
=> true ],
370 'Multi-valued parameter, alternative separator' => [
372 [ ApiBase
::PARAM_ISMULTI
=> true ],
376 'Multi-valued parameter, other C0 controls' => [
378 [ ApiBase
::PARAM_ISMULTI
=> true ],
382 'Multi-valued parameter, other C0 controls (2)' => [
384 [ ApiBase
::PARAM_ISMULTI
=> true ],
385 [ substr( $enc, 0, -3 ), '' ],
388 'Multi-valued parameter with limits' => [
391 ApiBase
::PARAM_ISMULTI
=> true,
392 ApiBase
::PARAM_ISMULTI_LIMIT1
=> 3,
397 'Multi-valued parameter with exceeded limits' => [
400 ApiBase
::PARAM_ISMULTI
=> true,
401 ApiBase
::PARAM_ISMULTI_LIMIT1
=> 2,
403 ApiUsageException
::newWithMessage(
404 null, [ 'apierror-toomanyvalues', 'myParam', 2 ], 'too-many-myParam'
408 'Multi-valued parameter with exceeded limits for non-bot' => [
411 ApiBase
::PARAM_ISMULTI
=> true,
412 ApiBase
::PARAM_ISMULTI_LIMIT1
=> 2,
413 ApiBase
::PARAM_ISMULTI_LIMIT2
=> 3,
415 ApiUsageException
::newWithMessage(
416 null, [ 'apierror-toomanyvalues', 'myParam', 2 ], 'too-many-myParam'
420 'Multi-valued parameter with non-exceeded limits for bot' => [
423 ApiBase
::PARAM_ISMULTI
=> true,
424 ApiBase
::PARAM_ISMULTI_LIMIT1
=> 2,
425 ApiBase
::PARAM_ISMULTI_LIMIT2
=> 3,
429 [ 'apihighlimits' => true ],
431 'Multi-valued parameter with prohibited duplicates' => [
433 [ ApiBase
::PARAM_ISMULTI
=> true ],
434 // Note that the keys are not sequential! This matches
435 // array_unique, but might be unexpected.
436 [ 0 => 'a', 1 => 'b', 3 => 'c' ],
439 'Multi-valued parameter with allowed duplicates' => [
442 ApiBase
::PARAM_ISMULTI
=> true,
443 ApiBase
::PARAM_ALLOW_DUPLICATES
=> true,
448 'Empty boolean param' => [
450 [ ApiBase
::PARAM_TYPE
=> 'boolean' ],
454 'Boolean param 0' => [
456 [ ApiBase
::PARAM_TYPE
=> 'boolean' ],
460 'Boolean param false' => [
462 [ ApiBase
::PARAM_TYPE
=> 'boolean' ],
466 'Boolean multi-param' => [
469 ApiBase
::PARAM_TYPE
=> 'boolean',
470 ApiBase
::PARAM_ISMULTI
=> true,
473 'Internal error in ApiBase::getParameterFromSettings: ' .
474 'Multi-values not supported for myParam'
478 'Empty boolean param with non-false default' => [
481 ApiBase
::PARAM_TYPE
=> 'boolean',
482 ApiBase
::PARAM_DFLT
=> true,
485 'Internal error in ApiBase::getParameterFromSettings: ' .
486 "Boolean param myParam's default is set to '1'. " .
487 'Boolean parameters must default to false.' ),
490 'Deprecated parameter' => [
492 [ ApiBase
::PARAM_DEPRECATED
=> true ],
494 [ [ 'apiwarn-deprecation-parameter', 'myParam' ] ],
496 'Deprecated parameter value' => [
498 [ ApiBase
::PARAM_DEPRECATED_VALUES
=> [ 'a' => true ] ],
500 [ [ 'apiwarn-deprecation-parameter', 'myParam=a' ] ],
502 'Multiple deprecated parameter values' => [
504 [ ApiBase
::PARAM_DEPRECATED_VALUES
=>
505 [ 'b' => true, 'd' => true ],
506 ApiBase
::PARAM_ISMULTI
=> true ],
507 [ 'a', 'b', 'c', 'd' ],
509 [ 'apiwarn-deprecation-parameter', 'myParam=b' ],
510 [ 'apiwarn-deprecation-parameter', 'myParam=d' ],
513 'Deprecated parameter value with custom warning' => [
515 [ ApiBase
::PARAM_DEPRECATED_VALUES
=> [ 'a' => 'my-msg' ] ],
519 '"*" when wildcard not allowed' => [
521 [ ApiBase
::PARAM_ISMULTI
=> true,
522 ApiBase
::PARAM_TYPE
=> [ 'a', 'b', 'c' ] ],
524 [ [ 'apiwarn-unrecognizedvalues', 'myParam',
525 [ 'list' => [ '*' ], 'type' => 'comma' ], 1 ] ],
530 ApiBase
::PARAM_ISMULTI
=> true,
531 ApiBase
::PARAM_TYPE
=> [ 'a', 'b', 'c' ],
532 ApiBase
::PARAM_ALL
=> true,
537 'Wildcard "*" with multiples not allowed' => [
540 ApiBase
::PARAM_TYPE
=> [ 'a', 'b', 'c' ],
541 ApiBase
::PARAM_ALL
=> true,
543 ApiUsageException
::newWithMessage( null,
544 [ 'apierror-unrecognizedvalue', 'myParam', '*' ],
548 'Wildcard "*" with unrestricted type' => [
551 ApiBase
::PARAM_ISMULTI
=> true,
552 ApiBase
::PARAM_ALL
=> true,
560 ApiBase
::PARAM_ISMULTI
=> true,
561 ApiBase
::PARAM_TYPE
=> [ 'a', 'b', 'c' ],
562 ApiBase
::PARAM_ALL
=> 'x',
567 'Wildcard conflicting with allowed value' => [
570 ApiBase
::PARAM_ISMULTI
=> true,
571 ApiBase
::PARAM_TYPE
=> [ 'a', 'b', 'c' ],
572 ApiBase
::PARAM_ALL
=> 'a',
575 'Internal error in ApiBase::getParameterFromSettings: ' .
576 'For param myParam, PARAM_ALL collides with a possible ' .
580 'Namespace with wildcard' => [
583 ApiBase
::PARAM_ISMULTI
=> true,
584 ApiBase
::PARAM_TYPE
=> 'namespace',
586 MWNamespace
::getValidNamespaces(),
589 // PARAM_ALL is ignored with namespace types.
590 'Namespace with wildcard suppressed' => [
593 ApiBase
::PARAM_ISMULTI
=> true,
594 ApiBase
::PARAM_TYPE
=> 'namespace',
595 ApiBase
::PARAM_ALL
=> false,
597 MWNamespace
::getValidNamespaces(),
600 'Namespace with wildcard "x"' => [
603 ApiBase
::PARAM_ISMULTI
=> true,
604 ApiBase
::PARAM_TYPE
=> 'namespace',
605 ApiBase
::PARAM_ALL
=> 'x',
608 [ [ 'apiwarn-unrecognizedvalues', 'myParam',
609 [ 'list' => [ 'x' ], 'type' => 'comma' ], 1 ] ],
612 'dDy+G?e?txnr.1:(@[Ru',
613 [ ApiBase
::PARAM_TYPE
=> 'password' ],
614 'dDy+G?e?txnr.1:(@[Ru',
617 'Sensitive field' => [
618 'I am fond of pineapples',
619 [ ApiBase
::PARAM_SENSITIVE
=> true ],
620 'I am fond of pineapples',
623 'Upload with default' => [
626 ApiBase
::PARAM_TYPE
=> 'upload',
627 ApiBase
::PARAM_DFLT
=> '',
630 'Internal error in ApiBase::getParameterFromSettings: ' .
631 "File upload param myParam's default is set to ''. " .
632 'File upload parameters may not have a default.' ),
635 'Multiple upload' => [
638 ApiBase
::PARAM_TYPE
=> 'upload',
639 ApiBase
::PARAM_ISMULTI
=> true,
642 'Internal error in ApiBase::getParameterFromSettings: ' .
643 'Multi-values not supported for myParam' ),
646 // @todo Test actual upload
649 [ ApiBase
::PARAM_TYPE
=> 'namespace' ],
650 ApiUsageException
::newWithMessage( null,
651 [ 'apierror-unrecognizedvalue', 'myParam', '-1' ],
655 'Extra namespace -1' => [
658 ApiBase
::PARAM_TYPE
=> 'namespace',
659 ApiBase
::PARAM_EXTRA_NAMESPACES
=> [ '-1' ],
664 // @todo Test with PARAM_SUBMODULE_MAP unset, need
665 // getModuleManager() to return something real
666 'Nonexistent module' => [
669 ApiBase
::PARAM_TYPE
=> 'submodule',
670 ApiBase
::PARAM_SUBMODULE_MAP
=>
671 [ 'foo' => 'foo', 'bar' => 'foo+bar' ],
673 ApiUsageException
::newWithMessage(
676 'apierror-unrecognizedvalue',
684 '\\x1f with multiples not allowed' => [
687 ApiUsageException
::newWithMessage( null,
688 'apierror-badvalue-notmultivalue',
689 'badvalue_notmultivalue' ),
692 'Integer with unenforced min' => [
695 ApiBase
::PARAM_TYPE
=> 'integer',
696 ApiBase
::PARAM_MIN
=> -1,
699 [ [ 'apierror-integeroutofrange-belowminimum', 'myParam', -1,
702 'Integer with enforced min' => [
705 ApiBase
::PARAM_TYPE
=> 'integer',
706 ApiBase
::PARAM_MIN
=> -1,
707 ApiBase
::PARAM_RANGE_ENFORCE
=> true,
709 ApiUsageException
::newWithMessage( null,
710 [ 'apierror-integeroutofrange-belowminimum', 'myParam',
711 '-1', '-2' ], 'integeroutofrange',
712 [ 'min' => -1, 'max' => null, 'botMax' => null ] ),
715 'Integer with unenforced max (internal mode)' => [
718 ApiBase
::PARAM_TYPE
=> 'integer',
719 ApiBase
::PARAM_MAX
=> 7,
724 'Integer with enforced max (internal mode)' => [
727 ApiBase
::PARAM_TYPE
=> 'integer',
728 ApiBase
::PARAM_MAX
=> 7,
729 ApiBase
::PARAM_RANGE_ENFORCE
=> true,
734 'Integer with unenforced max (non-internal mode)' => [
737 ApiBase
::PARAM_TYPE
=> 'integer',
738 ApiBase
::PARAM_MAX
=> 7,
741 [ [ 'apierror-integeroutofrange-abovemax', 'myParam', 7, 8 ] ],
742 [ 'internalmode' => false ],
744 'Integer with enforced max (non-internal mode)' => [
747 ApiBase
::PARAM_TYPE
=> 'integer',
748 ApiBase
::PARAM_MAX
=> 7,
749 ApiBase
::PARAM_RANGE_ENFORCE
=> true,
751 ApiUsageException
::newWithMessage(
753 [ 'apierror-integeroutofrange-abovemax', 'myParam', '7', '8' ],
755 [ 'min' => null, 'max' => 7, 'botMax' => 7 ]
758 [ 'internalmode' => false ],
760 'Array of integers' => [
763 ApiBase
::PARAM_ISMULTI
=> true,
764 ApiBase
::PARAM_TYPE
=> 'integer',
769 'Array of integers with unenforced min/max (internal mode)' => [
772 ApiBase
::PARAM_ISMULTI
=> true,
773 ApiBase
::PARAM_TYPE
=> 'integer',
774 ApiBase
::PARAM_MIN
=> 0,
775 ApiBase
::PARAM_MAX
=> 100,
778 [ [ 'apierror-integeroutofrange-belowminimum', 'myParam', 0, -1 ] ],
780 'Array of integers with enforced min/max (internal mode)' => [
783 ApiBase
::PARAM_ISMULTI
=> true,
784 ApiBase
::PARAM_TYPE
=> 'integer',
785 ApiBase
::PARAM_MIN
=> 0,
786 ApiBase
::PARAM_MAX
=> 100,
787 ApiBase
::PARAM_RANGE_ENFORCE
=> true,
789 ApiUsageException
::newWithMessage(
791 [ 'apierror-integeroutofrange-belowminimum', 'myParam', 0, -1 ],
793 [ 'min' => 0, 'max' => 100, 'botMax' => 100 ]
797 'Array of integers with unenforced min/max (non-internal mode)' => [
800 ApiBase
::PARAM_ISMULTI
=> true,
801 ApiBase
::PARAM_TYPE
=> 'integer',
802 ApiBase
::PARAM_MIN
=> 0,
803 ApiBase
::PARAM_MAX
=> 100,
807 [ 'apierror-integeroutofrange-abovemax', 'myParam', 100, 966 ],
808 [ 'apierror-integeroutofrange-belowminimum', 'myParam', 0, -1 ]
810 [ 'internalmode' => false ],
812 'Array of integers with enforced min/max (non-internal mode)' => [
815 ApiBase
::PARAM_ISMULTI
=> true,
816 ApiBase
::PARAM_TYPE
=> 'integer',
817 ApiBase
::PARAM_MIN
=> 0,
818 ApiBase
::PARAM_MAX
=> 100,
819 ApiBase
::PARAM_RANGE_ENFORCE
=> true,
821 ApiUsageException
::newWithMessage(
823 [ 'apierror-integeroutofrange-abovemax', 'myParam', 100, 966 ],
825 [ 'min' => 0, 'max' => 100, 'botMax' => 100 ]
828 [ 'internalmode' => false ],
830 'Limit with parseLimits false' => [
832 [ ApiBase
::PARAM_TYPE
=> 'limit' ],
835 [ 'parseLimits' => false ],
837 'Limit with no max' => [
840 ApiBase
::PARAM_TYPE
=> 'limit',
841 ApiBase
::PARAM_MAX2
=> 10,
842 ApiBase
::PARAM_ISMULTI
=> true,
845 'Internal error in ApiBase::getParameterFromSettings: ' .
846 'MAX1 or MAX2 are not defined for the limit myParam' ),
849 'Limit with no max2' => [
852 ApiBase
::PARAM_TYPE
=> 'limit',
853 ApiBase
::PARAM_MAX
=> 10,
854 ApiBase
::PARAM_ISMULTI
=> true,
857 'Internal error in ApiBase::getParameterFromSettings: ' .
858 'MAX1 or MAX2 are not defined for the limit myParam' ),
861 'Limit with multi-value' => [
864 ApiBase
::PARAM_TYPE
=> 'limit',
865 ApiBase
::PARAM_MAX
=> 10,
866 ApiBase
::PARAM_MAX2
=> 10,
867 ApiBase
::PARAM_ISMULTI
=> true,
870 'Internal error in ApiBase::getParameterFromSettings: ' .
871 'Multi-values not supported for myParam' ),
877 ApiBase
::PARAM_TYPE
=> 'limit',
878 ApiBase
::PARAM_MAX
=> 100,
879 ApiBase
::PARAM_MAX2
=> 100,
887 ApiBase
::PARAM_TYPE
=> 'limit',
888 ApiBase
::PARAM_MAX
=> 100,
889 ApiBase
::PARAM_MAX2
=> 101,
894 'Limit max for apihighlimits' => [
897 ApiBase
::PARAM_TYPE
=> 'limit',
898 ApiBase
::PARAM_MAX
=> 100,
899 ApiBase
::PARAM_MAX2
=> 101,
903 [ 'apihighlimits' => true ],
905 'Limit too large (internal mode)' => [
908 ApiBase
::PARAM_TYPE
=> 'limit',
909 ApiBase
::PARAM_MAX
=> 100,
910 ApiBase
::PARAM_MAX2
=> 101,
915 'Limit okay for apihighlimits (internal mode)' => [
918 ApiBase
::PARAM_TYPE
=> 'limit',
919 ApiBase
::PARAM_MAX
=> 100,
920 ApiBase
::PARAM_MAX2
=> 101,
924 [ 'apihighlimits' => true ],
926 'Limit too large for apihighlimits (internal mode)' => [
929 ApiBase
::PARAM_TYPE
=> 'limit',
930 ApiBase
::PARAM_MAX
=> 100,
931 ApiBase
::PARAM_MAX2
=> 101,
935 [ 'apihighlimits' => true ],
937 'Limit too large (non-internal mode)' => [
940 ApiBase
::PARAM_TYPE
=> 'limit',
941 ApiBase
::PARAM_MAX
=> 100,
942 ApiBase
::PARAM_MAX2
=> 101,
945 [ [ 'apierror-integeroutofrange-abovemax', 'myParam', 100, 101 ] ],
946 [ 'internalmode' => false ],
948 'Limit okay for apihighlimits (non-internal mode)' => [
951 ApiBase
::PARAM_TYPE
=> 'limit',
952 ApiBase
::PARAM_MAX
=> 100,
953 ApiBase
::PARAM_MAX2
=> 101,
957 [ 'internalmode' => false, 'apihighlimits' => true ],
959 'Limit too large for apihighlimits (non-internal mode)' => [
962 ApiBase
::PARAM_TYPE
=> 'limit',
963 ApiBase
::PARAM_MAX
=> 100,
964 ApiBase
::PARAM_MAX2
=> 101,
967 [ [ 'apierror-integeroutofrange-abovebotmax', 'myParam', 101, 102 ] ],
968 [ 'internalmode' => false, 'apihighlimits' => true ],
970 'Limit too small' => [
973 ApiBase
::PARAM_TYPE
=> 'limit',
974 ApiBase
::PARAM_MIN
=> -1,
975 ApiBase
::PARAM_MAX
=> 100,
976 ApiBase
::PARAM_MAX2
=> 100,
979 [ [ 'apierror-integeroutofrange-belowminimum', 'myParam', -1,
983 wfTimestamp( TS_UNIX
, '20211221122112' ),
984 [ ApiBase
::PARAM_TYPE
=> 'timestamp' ],
990 [ ApiBase
::PARAM_TYPE
=> 'timestamp' ],
993 [ [ 'apiwarn-unclearnowtimestamp', 'myParam', '0' ] ],
995 'Timestamp empty' => [
997 [ ApiBase
::PARAM_TYPE
=> 'timestamp' ],
999 [ [ 'apiwarn-unclearnowtimestamp', 'myParam', '' ] ],
1001 // wfTimestamp() interprets this as Unix time
1004 [ ApiBase
::PARAM_TYPE
=> 'timestamp' ],
1008 'Timestamp now' => [
1010 [ ApiBase
::PARAM_TYPE
=> 'timestamp' ],
1014 'Invalid timestamp' => [
1016 [ ApiBase
::PARAM_TYPE
=> 'timestamp' ],
1017 ApiUsageException
::newWithMessage(
1019 [ 'apierror-badtimestamp', 'myParam', 'a potato' ],
1020 'badtimestamp_myParam'
1024 'Timestamp array' => [
1027 ApiBase
::PARAM_TYPE
=> 'timestamp',
1028 ApiBase
::PARAM_ISMULTI
=> 1,
1030 [ wfTimestamp( TS_MW
, 100 ), wfTimestamp( TS_MW
, 101 ) ],
1035 [ ApiBase
::PARAM_TYPE
=> 'user' ],
1039 'User prefixed with "User:"' => [
1041 [ ApiBase
::PARAM_TYPE
=> 'user' ],
1045 'Invalid username "|"' => [
1047 [ ApiBase
::PARAM_TYPE
=> 'user' ],
1048 ApiUsageException
::newWithMessage( null,
1049 [ 'apierror-baduser', 'myParam', '|' ],
1050 'baduser_myParam' ),
1053 'Invalid username "300.300.300.300"' => [
1055 [ ApiBase
::PARAM_TYPE
=> 'user' ],
1056 ApiUsageException
::newWithMessage( null,
1057 [ 'apierror-baduser', 'myParam', '300.300.300.300' ],
1058 'baduser_myParam' ),
1061 'IP range as username' => [
1063 [ ApiBase
::PARAM_TYPE
=> 'user' ],
1067 'IPv6 as username' => [
1069 [ ApiBase
::PARAM_TYPE
=> 'user' ],
1073 'Obsolete cloaked usemod IP address as username' => [
1075 [ ApiBase
::PARAM_TYPE
=> 'user' ],
1079 'Invalid username containing IP address' => [
1080 'This is [not] valid 1.2.3.xxx, ha!',
1081 [ ApiBase
::PARAM_TYPE
=> 'user' ],
1082 ApiUsageException
::newWithMessage(
1084 [ 'apierror-baduser', 'myParam', 'This is [not] valid 1.2.3.xxx, ha!' ],
1089 'External username' => [
1091 [ ApiBase
::PARAM_TYPE
=> 'user' ],
1095 'Array of usernames' => [
1098 ApiBase
::PARAM_TYPE
=> 'user',
1099 ApiBase
::PARAM_ISMULTI
=> true,
1106 [ ApiBase
::PARAM_TYPE
=> 'tags' ],
1110 'Array of one tag' => [
1113 ApiBase
::PARAM_TYPE
=> 'tags',
1114 ApiBase
::PARAM_ISMULTI
=> true,
1119 'Array of tags' => [
1122 ApiBase
::PARAM_TYPE
=> 'tags',
1123 ApiBase
::PARAM_ISMULTI
=> true,
1130 [ ApiBase
::PARAM_TYPE
=> 'tags' ],
1131 new ApiUsageException( null,
1132 Status
::newFatal( 'tags-apply-not-allowed-one',
1133 'invalid tag', 1 ) ),
1136 'Unrecognized type' => [
1138 [ ApiBase
::PARAM_TYPE
=> 'nonexistenttype' ],
1140 'Internal error in ApiBase::getParameterFromSettings: ' .
1141 "Param myParam's type is unknown - nonexistenttype" ),
1144 'Too many bytes' => [
1147 ApiBase
::PARAM_MAX_BYTES
=> 0,
1148 ApiBase
::PARAM_MAX_CHARS
=> 0,
1150 ApiUsageException
::newWithMessage( null,
1151 [ 'apierror-maxbytes', 'myParam', 0 ] ),
1154 'Too many chars' => [
1157 ApiBase
::PARAM_MAX_BYTES
=> 4,
1158 ApiBase
::PARAM_MAX_CHARS
=> 1,
1160 ApiUsageException
::newWithMessage( null,
1161 [ 'apierror-maxchars', 'myParam', 1 ] ),
1164 'Omitted required param' => [
1166 [ ApiBase
::PARAM_REQUIRED
=> true ],
1167 ApiUsageException
::newWithMessage( null,
1168 [ 'apierror-missingparam', 'myParam' ] ),
1171 'Empty multi-value' => [
1173 [ ApiBase
::PARAM_ISMULTI
=> true ],
1177 'Multi-value \x1f' => [
1179 [ ApiBase
::PARAM_ISMULTI
=> true ],
1183 'Allowed non-multi-value with "|"' => [
1185 [ ApiBase
::PARAM_TYPE
=> [ 'a|b' ] ],
1189 'Prohibited multi-value' => [
1191 [ ApiBase
::PARAM_TYPE
=> [ 'a', 'b' ] ],
1192 ApiUsageException
::newWithMessage( null,
1194 'apierror-multival-only-one-of',
1196 Message
::listParam( [ '<kbd>a</kbd>', '<kbd>b</kbd>' ] ),
1205 // The following really just test PHP's string-to-int conversion.
1213 [ "\t1", 1, '\t1' ],
1214 [ "\r1", 1, '\r1' ],
1215 [ "\f1", 0, '\f1', 'badutf-8' ],
1216 [ "\n1", 1, '\n1' ],
1217 [ "\v1", 0, '\v1', 'badutf-8' ],
1218 [ "\e1", 0, '\e1', 'badutf-8' ],
1219 [ "\x001", 0, '\x001', 'badutf-8' ],
1222 foreach ( $integerTests as $test ) {
1223 $desc = $test[2] ??
$test[0];
1224 $warnings = isset( $test[3] ) ?
1225 [ [ 'apiwarn-badutf8', 'myParam' ] ] : [];
1226 $returnArray["\"$desc\" as integer"] = [
1228 [ ApiBase
::PARAM_TYPE
=> 'integer' ],
1234 return $returnArray;
1237 public function testErrorArrayToStatus() {
1238 $mock = new MockApi();
1240 // Sanity check empty array
1241 $expect = Status
::newGood();
1242 $this->assertEquals( $expect, $mock->errorArrayToStatus( [] ) );
1244 // No blocked $user, so no special block handling
1245 $expect = Status
::newGood();
1246 $expect->fatal( 'blockedtext' );
1247 $expect->fatal( 'autoblockedtext' );
1248 $expect->fatal( 'systemblockedtext' );
1249 $expect->fatal( 'mainpage' );
1250 $expect->fatal( 'parentheses', 'foobar' );
1251 $this->assertEquals( $expect, $mock->errorArrayToStatus( [
1253 [ 'autoblockedtext' ],
1254 [ 'systemblockedtext' ],
1256 [ 'parentheses', 'foobar' ],
1259 // Has a blocked $user, so special block handling
1260 $user = $this->getMutableTestUser()->getUser();
1261 $block = new \
Block( [
1262 'address' => $user->getName(),
1263 'user' => $user->getID(),
1264 'by' => $this->getTestSysop()->getUser()->getId(),
1265 'reason' => __METHOD__
,
1266 'expiry' => time() +
100500,
1269 $blockinfo = [ 'blockinfo' => ApiQueryUserInfo
::getBlockInfo( $block ) ];
1271 $expect = Status
::newGood();
1272 $expect->fatal( ApiMessage
::create( 'apierror-blocked', 'blocked', $blockinfo ) );
1273 $expect->fatal( ApiMessage
::create( 'apierror-autoblocked', 'autoblocked', $blockinfo ) );
1274 $expect->fatal( ApiMessage
::create( 'apierror-systemblocked', 'blocked', $blockinfo ) );
1275 $expect->fatal( 'mainpage' );
1276 $expect->fatal( 'parentheses', 'foobar' );
1277 $this->assertEquals( $expect, $mock->errorArrayToStatus( [
1279 [ 'autoblockedtext' ],
1280 [ 'systemblockedtext' ],
1282 [ 'parentheses', 'foobar' ],
1286 public function testDieStatus() {
1287 $mock = new MockApi();
1289 $status = StatusValue
::newGood();
1290 $status->error( 'foo' );
1291 $status->warning( 'bar' );
1293 $mock->dieStatus( $status );
1294 $this->fail( 'Expected exception not thrown' );
1295 } catch ( ApiUsageException
$ex ) {
1296 $this->assertTrue( ApiTestCase
::apiExceptionHasCode( $ex, 'foo' ), 'Exception has "foo"' );
1297 $this->assertFalse( ApiTestCase
::apiExceptionHasCode( $ex, 'bar' ), 'Exception has "bar"' );
1300 $status = StatusValue
::newGood();
1301 $status->warning( 'foo' );
1302 $status->warning( 'bar' );
1304 $mock->dieStatus( $status );
1305 $this->fail( 'Expected exception not thrown' );
1306 } catch ( ApiUsageException
$ex ) {
1307 $this->assertTrue( ApiTestCase
::apiExceptionHasCode( $ex, 'foo' ), 'Exception has "foo"' );
1308 $this->assertTrue( ApiTestCase
::apiExceptionHasCode( $ex, 'bar' ), 'Exception has "bar"' );
1311 $status = StatusValue
::newGood();
1312 $status->setOk( false );
1314 $mock->dieStatus( $status );
1315 $this->fail( 'Expected exception not thrown' );
1316 } catch ( ApiUsageException
$ex ) {
1317 $this->assertTrue( ApiTestCase
::apiExceptionHasCode( $ex, 'unknownerror-nocode' ),
1318 'Exception has "unknownerror-nocode"' );
1323 * @covers ApiBase::extractRequestParams
1325 public function testExtractRequestParams() {
1326 $request = new FauxRequest( [
1327 'xxexists' => 'exists!',
1328 'xxmulti' => 'a|b|c|d|{bad}',
1330 'xxtemplate-a' => 'A!',
1331 'xxtemplate-b' => 'B1|B2|B3',
1332 'xxtemplate-c' => '',
1333 'xxrecursivetemplate-b-B1' => 'X',
1334 'xxrecursivetemplate-b-B3' => 'Y',
1335 'xxrecursivetemplate-b-B4' => '?',
1336 'xxemptytemplate-' => 'nope',
1339 'errorformat' => 'raw',
1341 $context = new DerivativeContext( RequestContext
::getMain() );
1342 $context->setRequest( $request );
1343 $main = new ApiMain( $context );
1345 $mock = $this->getMockBuilder( ApiBase
::class )
1346 ->setConstructorArgs( [ $main, 'test', 'xx' ] )
1347 ->setMethods( [ 'getAllowedParams' ] )
1348 ->getMockForAbstractClass();
1349 $mock->method( 'getAllowedParams' )->willReturn( [
1350 'notexists' => null,
1353 ApiBase
::PARAM_ISMULTI
=> true,
1356 ApiBase
::PARAM_ISMULTI
=> true,
1359 ApiBase
::PARAM_ISMULTI
=> true,
1360 ApiBase
::PARAM_TEMPLATE_VARS
=> [ 'm' => 'multi' ],
1362 'recursivetemplate-{m}-{t}' => [
1363 ApiBase
::PARAM_TEMPLATE_VARS
=> [ 't' => 'template-{m}', 'm' => 'multi' ],
1365 'emptytemplate-{m}' => [
1366 ApiBase
::PARAM_ISMULTI
=> true,
1367 ApiBase
::PARAM_TEMPLATE_VARS
=> [ 'm' => 'empty' ],
1369 'badtemplate-{e}' => [
1370 ApiBase
::PARAM_TEMPLATE_VARS
=> [ 'e' => 'exists' ],
1372 'badtemplate2-{e}' => [
1373 ApiBase
::PARAM_TEMPLATE_VARS
=> [ 'e' => 'badtemplate2-{e}' ],
1375 'badtemplate3-{x}' => [
1376 ApiBase
::PARAM_TEMPLATE_VARS
=> [ 'x' => 'foo' ],
1380 $this->assertEquals( [
1381 'notexists' => null,
1382 'exists' => 'exists!',
1383 'multi' => [ 'a', 'b', 'c', 'd', '{bad}' ],
1385 'template-a' => [ 'A!' ],
1386 'template-b' => [ 'B1', 'B2', 'B3' ],
1388 'template-d' => null,
1389 'recursivetemplate-a-A!' => null,
1390 'recursivetemplate-b-B1' => 'X',
1391 'recursivetemplate-b-B2' => null,
1392 'recursivetemplate-b-B3' => 'Y',
1393 ], $mock->extractRequestParams() );
1395 $used = TestingAccessWrapper
::newFromObject( $main )->getParamsUsed();
1397 $this->assertEquals( [
1402 'xxrecursivetemplate-a-A!',
1403 'xxrecursivetemplate-b-B1',
1404 'xxrecursivetemplate-b-B2',
1405 'xxrecursivetemplate-b-B3',
1412 $warnings = $mock->getResult()->getResultData( 'warnings', [ 'Strip' => 'all' ] );
1413 $this->assertCount( 1, $warnings );
1414 $this->assertSame( 'ignoring-invalid-templated-value', $warnings[0]['code'] );