3 use Wikimedia\TestingAccessWrapper
;
10 class ApiBaseTest
extends ApiTestCase
{
13 * @covers ApiBase::requireOnlyOneParameter
15 public function testRequireOnlyOneParameterDefault() {
16 $mock = new MockApi();
17 $mock->requireOnlyOneParameter(
18 [ "filename" => "foo.txt", "enablechunks" => false ],
19 "filename", "enablechunks"
21 $this->assertTrue( true );
25 * @expectedException ApiUsageException
26 * @covers ApiBase::requireOnlyOneParameter
28 public function testRequireOnlyOneParameterZero() {
29 $mock = new MockApi();
30 $mock->requireOnlyOneParameter(
31 [ "filename" => "foo.txt", "enablechunks" => 0 ],
32 "filename", "enablechunks"
37 * @expectedException ApiUsageException
38 * @covers ApiBase::requireOnlyOneParameter
40 public function testRequireOnlyOneParameterTrue() {
41 $mock = new MockApi();
42 $mock->requireOnlyOneParameter(
43 [ "filename" => "foo.txt", "enablechunks" => true ],
44 "filename", "enablechunks"
49 * @dataProvider provideGetParameterFromSettings
50 * @param string|null $input
51 * @param array $paramSettings
52 * @param mixed $expected
53 * @param string[] $warnings
55 public function testGetParameterFromSettings( $input, $paramSettings, $expected, $warnings ) {
56 $mock = new MockApi();
57 $wrapper = TestingAccessWrapper
::newFromObject( $mock );
59 $context = new DerivativeContext( $mock );
60 $context->setRequest( new FauxRequest( $input !== null ?
[ 'foo' => $input ] : [] ) );
61 $wrapper->mMainModule
= new ApiMain( $context );
63 if ( $expected instanceof ApiUsageException
) {
65 $wrapper->getParameterFromSettings( 'foo', $paramSettings, true );
66 } catch ( ApiUsageException
$ex ) {
67 $this->assertEquals( $expected, $ex );
70 $result = $wrapper->getParameterFromSettings( 'foo', $paramSettings, true );
71 $this->assertSame( $expected, $result );
72 $this->assertSame( $warnings, $mock->warnings
);
76 public static function provideGetParameterFromSettings() {
78 [ 'apiwarn-badutf8', 'foo' ],
83 for ( $i = 0; $i < 32; $i++
) {
85 $enc .= ( $i === 9 ||
$i === 10 ||
$i === 13 )
91 'Basic param' => [ 'bar', null, 'bar', [] ],
92 'Basic param, C0 controls' => [ $c0, null, $enc, $warnings ],
93 'String param' => [ 'bar', '', 'bar', [] ],
94 'String param, defaulted' => [ null, '', '', [] ],
95 'String param, empty' => [ '', 'default', '', [] ],
96 'String param, required, empty' => [
98 [ ApiBase
::PARAM_DFLT
=> 'default', ApiBase
::PARAM_REQUIRED
=> true ],
99 ApiUsageException
::newWithMessage( null, [ 'apierror-missingparam', 'foo' ] ),
102 'Multi-valued parameter' => [
104 [ ApiBase
::PARAM_ISMULTI
=> true ],
108 'Multi-valued parameter, alternative separator' => [
110 [ ApiBase
::PARAM_ISMULTI
=> true ],
114 'Multi-valued parameter, other C0 controls' => [
116 [ ApiBase
::PARAM_ISMULTI
=> true ],
120 'Multi-valued parameter, other C0 controls (2)' => [
122 [ ApiBase
::PARAM_ISMULTI
=> true ],
123 [ substr( $enc, 0, -3 ), '' ],
129 public function testErrorArrayToStatus() {
130 $mock = new MockApi();
132 // Sanity check empty array
133 $expect = Status
::newGood();
134 $this->assertEquals( $expect, $mock->errorArrayToStatus( [] ) );
136 // No blocked $user, so no special block handling
137 $expect = Status
::newGood();
138 $expect->fatal( 'blockedtext' );
139 $expect->fatal( 'autoblockedtext' );
140 $expect->fatal( 'systemblockedtext' );
141 $expect->fatal( 'mainpage' );
142 $expect->fatal( 'parentheses', 'foobar' );
143 $this->assertEquals( $expect, $mock->errorArrayToStatus( [
145 [ 'autoblockedtext' ],
146 [ 'systemblockedtext' ],
148 [ 'parentheses', 'foobar' ],
151 // Has a blocked $user, so special block handling
152 $user = $this->getMutableTestUser()->getUser();
153 $block = new \
Block( [
154 'address' => $user->getName(),
155 'user' => $user->getID(),
156 'reason' => __METHOD__
,
157 'expiry' => time() +
100500,
160 $blockinfo = [ 'blockinfo' => ApiQueryUserInfo
::getBlockInfo( $block ) ];
162 $expect = Status
::newGood();
163 $expect->fatal( ApiMessage
::create( 'apierror-blocked', 'blocked', $blockinfo ) );
164 $expect->fatal( ApiMessage
::create( 'apierror-autoblocked', 'autoblocked', $blockinfo ) );
165 $expect->fatal( ApiMessage
::create( 'apierror-systemblocked', 'blocked', $blockinfo ) );
166 $expect->fatal( 'mainpage' );
167 $expect->fatal( 'parentheses', 'foobar' );
168 $this->assertEquals( $expect, $mock->errorArrayToStatus( [
170 [ 'autoblockedtext' ],
171 [ 'systemblockedtext' ],
173 [ 'parentheses', 'foobar' ],
178 * @covers ApiBase::dieStatus
180 public function testDieStatus() {
181 $mock = new MockApi();
183 $status = StatusValue
::newGood();
184 $status->error( 'foo' );
185 $status->warning( 'bar' );
187 $mock->dieStatus( $status );
188 $this->fail( 'Expected exception not thrown' );
189 } catch ( ApiUsageException
$ex ) {
190 $this->assertTrue( ApiTestCase
::apiExceptionHasCode( $ex, 'foo' ), 'Exception has "foo"' );
191 $this->assertFalse( ApiTestCase
::apiExceptionHasCode( $ex, 'bar' ), 'Exception has "bar"' );
194 $status = StatusValue
::newGood();
195 $status->warning( 'foo' );
196 $status->warning( 'bar' );
198 $mock->dieStatus( $status );
199 $this->fail( 'Expected exception not thrown' );
200 } catch ( ApiUsageException
$ex ) {
201 $this->assertTrue( ApiTestCase
::apiExceptionHasCode( $ex, 'foo' ), 'Exception has "foo"' );
202 $this->assertTrue( ApiTestCase
::apiExceptionHasCode( $ex, 'bar' ), 'Exception has "bar"' );
205 $status = StatusValue
::newGood();
206 $status->setOk( false );
208 $mock->dieStatus( $status );
209 $this->fail( 'Expected exception not thrown' );
210 } catch ( ApiUsageException
$ex ) {
211 $this->assertTrue( ApiTestCase
::apiExceptionHasCode( $ex, 'unknownerror-nocode' ),
212 'Exception has "unknownerror-nocode"' );