X-Git-Url: http://git.heureux-cyclage.org/?a=blobdiff_plain;f=tests%2Fphpunit%2Fincludes%2Fapi%2FApiBaseTest.php;h=253ac959ff80a29bb946be29cc5a77a086fd25d6;hb=525bfbc8df855aa12e01868d92532cd64482dc7d;hp=5d1ead0c2f5252c66d65d58402e79eaadfababf8;hpb=e3bd13db0c285f312e31bb1b7271af4628cca80c;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/phpunit/includes/api/ApiBaseTest.php b/tests/phpunit/includes/api/ApiBaseTest.php index 5d1ead0c2f..253ac959ff 100644 --- a/tests/phpunit/includes/api/ApiBaseTest.php +++ b/tests/phpunit/includes/api/ApiBaseTest.php @@ -1,5 +1,7 @@ setRequest( new FauxRequest( $input !== null ? [ 'foo' => $input ] : [] ) ); + $wrapper->mMainModule = new ApiMain( $context ); + + if ( $expected instanceof ApiUsageException ) { + try { + $wrapper->getParameterFromSettings( 'foo', $paramSettings, true ); + } catch ( ApiUsageException $ex ) { + $this->assertEquals( $expected, $ex ); + } + } else { + $result = $wrapper->getParameterFromSettings( 'foo', $paramSettings, true ); + $this->assertSame( $expected, $result ); + $this->assertSame( $warnings, $mock->warnings ); + } + } + + public static function provideGetParameterFromSettings() { + $warnings = [ + [ 'apiwarn-badutf8', 'foo' ], + ]; + + $c0 = ''; + $enc = ''; + for ( $i = 0; $i < 32; $i++ ) { + $c0 .= chr( $i ); + $enc .= ( $i === 9 || $i === 10 || $i === 13 ) + ? chr( $i ) + : '�'; + } + + return [ + 'Basic param' => [ 'bar', null, 'bar', [] ], + 'Basic param, C0 controls' => [ $c0, null, $enc, $warnings ], + 'String param' => [ 'bar', '', 'bar', [] ], + 'String param, defaulted' => [ null, '', '', [] ], + 'String param, empty' => [ '', 'default', '', [] ], + 'String param, required, empty' => [ + '', + [ ApiBase::PARAM_DFLT => 'default', ApiBase::PARAM_REQUIRED => true ], + ApiUsageException::newWithMessage( null, [ 'apierror-missingparam', 'foo' ] ), + [] + ], + 'Multi-valued parameter' => [ + 'a|b|c', + [ ApiBase::PARAM_ISMULTI => true ], + [ 'a', 'b', 'c' ], + [] + ], + 'Multi-valued parameter, alternative separator' => [ + "\x1fa|b\x1fc|d", + [ ApiBase::PARAM_ISMULTI => true ], + [ 'a|b', 'c|d' ], + [] + ], + 'Multi-valued parameter, other C0 controls' => [ + $c0, + [ ApiBase::PARAM_ISMULTI => true ], + [ $enc ], + $warnings + ], + 'Multi-valued parameter, other C0 controls (2)' => [ + "\x1f" . $c0, + [ ApiBase::PARAM_ISMULTI => true ], + [ substr( $enc, 0, -3 ), '' ], + $warnings + ], + ]; + } + + public function testErrorArrayToStatus() { + $mock = new MockApi(); + + // Sanity check empty array + $expect = Status::newGood(); + $this->assertEquals( $expect, $mock->errorArrayToStatus( [] ) ); + + // No blocked $user, so no special block handling + $expect = Status::newGood(); + $expect->fatal( 'blockedtext' ); + $expect->fatal( 'autoblockedtext' ); + $expect->fatal( 'systemblockedtext' ); + $expect->fatal( 'mainpage' ); + $expect->fatal( 'parentheses', 'foobar' ); + $this->assertEquals( $expect, $mock->errorArrayToStatus( [ + [ 'blockedtext' ], + [ 'autoblockedtext' ], + [ 'systemblockedtext' ], + 'mainpage', + [ 'parentheses', 'foobar' ], + ] ) ); + + // Has a blocked $user, so special block handling + $user = $this->getMutableTestUser()->getUser(); + $block = new \Block( [ + 'address' => $user->getName(), + 'user' => $user->getID(), + 'reason' => __METHOD__, + 'expiry' => time() + 100500, + ] ); + $block->insert(); + $blockinfo = [ 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $block ) ]; + + $expect = Status::newGood(); + $expect->fatal( ApiMessage::create( 'apierror-blocked', 'blocked', $blockinfo ) ); + $expect->fatal( ApiMessage::create( 'apierror-autoblocked', 'autoblocked', $blockinfo ) ); + $expect->fatal( ApiMessage::create( 'apierror-systemblocked', 'blocked', $blockinfo ) ); + $expect->fatal( 'mainpage' ); + $expect->fatal( 'parentheses', 'foobar' ); + $this->assertEquals( $expect, $mock->errorArrayToStatus( [ + [ 'blockedtext' ], + [ 'autoblockedtext' ], + [ 'systemblockedtext' ], + 'mainpage', + [ 'parentheses', 'foobar' ], + ], $user ) ); + } + }