8 class ApiBaseTest
extends ApiTestCase
{
11 * @covers ApiBase::requireOnlyOneParameter
13 public function testRequireOnlyOneParameterDefault() {
14 $mock = new MockApi();
15 $mock->requireOnlyOneParameter(
16 [ "filename" => "foo.txt", "enablechunks" => false ],
17 "filename", "enablechunks"
19 $this->assertTrue( true );
23 * @expectedException ApiUsageException
24 * @covers ApiBase::requireOnlyOneParameter
26 public function testRequireOnlyOneParameterZero() {
27 $mock = new MockApi();
28 $mock->requireOnlyOneParameter(
29 [ "filename" => "foo.txt", "enablechunks" => 0 ],
30 "filename", "enablechunks"
35 * @expectedException ApiUsageException
36 * @covers ApiBase::requireOnlyOneParameter
38 public function testRequireOnlyOneParameterTrue() {
39 $mock = new MockApi();
40 $mock->requireOnlyOneParameter(
41 [ "filename" => "foo.txt", "enablechunks" => true ],
42 "filename", "enablechunks"
47 * @dataProvider provideGetParameterFromSettings
48 * @param string|null $input
49 * @param array $paramSettings
50 * @param mixed $expected
51 * @param string[] $warnings
53 public function testGetParameterFromSettings( $input, $paramSettings, $expected, $warnings ) {
54 $mock = new MockApi();
55 $wrapper = TestingAccessWrapper
::newFromObject( $mock );
57 $context = new DerivativeContext( $mock );
58 $context->setRequest( new FauxRequest( $input !== null ?
[ 'foo' => $input ] : [] ) );
59 $wrapper->mMainModule
= new ApiMain( $context );
61 if ( $expected instanceof ApiUsageException
) {
63 $wrapper->getParameterFromSettings( 'foo', $paramSettings, true );
64 } catch ( ApiUsageException
$ex ) {
65 $this->assertEquals( $expected, $ex );
68 $result = $wrapper->getParameterFromSettings( 'foo', $paramSettings, true );
69 $this->assertSame( $expected, $result );
70 $this->assertSame( $warnings, $mock->warnings
);
74 public static function provideGetParameterFromSettings() {
76 [ 'apiwarn-badutf8', 'foo' ],
81 for ( $i = 0; $i < 32; $i++
) {
83 $enc .= ( $i === 9 ||
$i === 10 ||
$i === 13 )
89 'Basic param' => [ 'bar', null, 'bar', [] ],
90 'Basic param, C0 controls' => [ $c0, null, $enc, $warnings ],
91 'String param' => [ 'bar', '', 'bar', [] ],
92 'String param, defaulted' => [ null, '', '', [] ],
93 'String param, empty' => [ '', 'default', '', [] ],
94 'String param, required, empty' => [
96 [ ApiBase
::PARAM_DFLT
=> 'default', ApiBase
::PARAM_REQUIRED
=> true ],
97 ApiUsageException
::newWithMessage( null, [ 'apierror-missingparam', 'foo' ] ),
100 'Multi-valued parameter' => [
102 [ ApiBase
::PARAM_ISMULTI
=> true ],
106 'Multi-valued parameter, alternative separator' => [
108 [ ApiBase
::PARAM_ISMULTI
=> true ],
112 'Multi-valued parameter, other C0 controls' => [
114 [ ApiBase
::PARAM_ISMULTI
=> true ],
118 'Multi-valued parameter, other C0 controls (2)' => [
120 [ ApiBase
::PARAM_ISMULTI
=> true ],
121 [ substr( $enc, 0, -3 ), '' ],
127 public function testErrorArrayToStatus() {
128 $mock = new MockApi();
130 // Sanity check empty array
131 $expect = Status
::newGood();
132 $this->assertEquals( $expect, $mock->errorArrayToStatus( [] ) );
134 // No blocked $user, so no special block handling
135 $expect = Status
::newGood();
136 $expect->fatal( 'blockedtext' );
137 $expect->fatal( 'autoblockedtext' );
138 $expect->fatal( 'systemblockedtext' );
139 $expect->fatal( 'mainpage' );
140 $expect->fatal( 'parentheses', 'foobar' );
141 $this->assertEquals( $expect, $mock->errorArrayToStatus( [
143 [ 'autoblockedtext' ],
144 [ 'systemblockedtext' ],
146 [ 'parentheses', 'foobar' ],
149 // Has a blocked $user, so special block handling
150 $user = $this->getMutableTestUser()->getUser();
151 $block = new \
Block( [
152 'address' => $user->getName(),
153 'user' => $user->getID(),
154 'reason' => __METHOD__
,
155 'expiry' => time() +
100500,
158 $blockinfo = [ 'blockinfo' => ApiQueryUserInfo
::getBlockInfo( $block ) ];
160 $expect = Status
::newGood();
161 $expect->fatal( ApiMessage
::create( 'apierror-blocked', 'blocked', $blockinfo ) );
162 $expect->fatal( ApiMessage
::create( 'apierror-autoblocked', 'autoblocked', $blockinfo ) );
163 $expect->fatal( ApiMessage
::create( 'apierror-systemblocked', 'blocked', $blockinfo ) );
164 $expect->fatal( 'mainpage' );
165 $expect->fatal( 'parentheses', 'foobar' );
166 $this->assertEquals( $expect, $mock->errorArrayToStatus( [
168 [ 'autoblockedtext' ],
169 [ 'systemblockedtext' ],
171 [ 'parentheses', 'foobar' ],