$mock->getTitleFromTitleOrPageId( [ 'pageid' => 298401643 ] );
}
+ public function testGetParameter() {
+ $mock = $this->getMockBuilder( MockApi::class )
+ ->setMethods( [ 'getAllowedParams' ] )
+ ->getMock();
+ $mock->method( 'getAllowedParams' )->willReturn( [
+ 'foo' => [
+ ApiBase::PARAM_TYPE => [ 'value' ],
+ ],
+ 'bar' => [
+ ApiBase::PARAM_TYPE => [ 'value' ],
+ ],
+ ] );
+ $wrapper = TestingAccessWrapper::newFromObject( $mock );
+
+ $context = new DerivativeContext( $mock );
+ $context->setRequest( new FauxRequest( [ 'foo' => 'bad', 'bar' => 'value' ] ) );
+ $wrapper->mMainModule = new ApiMain( $context );
+
+ // Even though 'foo' is bad, getParameter( 'bar' ) must not fail
+ $this->assertSame( 'value', $wrapper->getParameter( 'bar' ) );
+
+ // But getParameter( 'foo' ) must throw.
+ try {
+ $wrapper->getParameter( 'foo' );
+ $this->fail( 'Expected exception not thrown' );
+ } catch ( ApiUsageException $ex ) {
+ $this->assertTrue( $this->apiExceptionHasCode( $ex, 'unknown_foo' ) );
+ }
+
+ // And extractRequestParams() must throw too.
+ try {
+ $mock->extractRequestParams();
+ $this->fail( 'Expected exception not thrown' );
+ } catch ( ApiUsageException $ex ) {
+ $this->assertTrue( $this->apiExceptionHasCode( $ex, 'unknown_foo' ) );
+ }
+ }
+
/**
* @dataProvider provideGetParameterFromSettings
* @param string|null $input
$input !== null ? [ 'myParam' => $input ] : [] ) );
$wrapper->mMainModule = new ApiMain( $context );
- $parseLimits = isset( $options['parseLimits'] ) ?
- $options['parseLimits'] : true;
+ $parseLimits = $options['parseLimits'] ?? true;
if ( !empty( $options['apihighlimits'] ) ) {
$context->setUser( self::$users['sysop']->getUser() );
ApiBase::PARAM_ISMULTI => true,
ApiBase::PARAM_ISMULTI_LIMIT1 => 2,
],
- [ 'a', 'b' ],
- [ [ 'apiwarn-toomanyvalues', 'myParam', 2 ] ],
+ ApiUsageException::newWithMessage(
+ null, [ 'apierror-toomanyvalues', 'myParam', 2 ], 'too-many-myParam'
+ ),
+ []
],
'Multi-valued parameter with exceeded limits for non-bot' => [
'a|b|c',
ApiBase::PARAM_ISMULTI_LIMIT1 => 2,
ApiBase::PARAM_ISMULTI_LIMIT2 => 3,
],
- [ 'a', 'b' ],
- [ [ 'apiwarn-toomanyvalues', 'myParam', 2 ] ],
+ ApiUsageException::newWithMessage(
+ null, [ 'apierror-toomanyvalues', 'myParam', 2 ], 'too-many-myParam'
+ ),
+ []
],
'Multi-valued parameter with non-exceeded limits for bot' => [
'a|b|c',
'Foo bar',
[],
],
+ 'User prefixed with "User:"' => [
+ 'User:foo_bar',
+ [ ApiBase::PARAM_TYPE => 'user' ],
+ 'Foo bar',
+ [],
+ ],
'Invalid username "|"' => [
'|',
[ ApiBase::PARAM_TYPE => 'user' ],
];
foreach ( $integerTests as $test ) {
- $desc = isset( $test[2] ) ? $test[2] : $test[0];
+ $desc = $test[2] ?? $test[0];
$warnings = isset( $test[3] ) ?
[ [ 'apiwarn-badutf8', 'myParam' ] ] : [];
$returnArray["\"$desc\" as integer"] = [