]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - tests/phpunit/includes/api/ApiBaseTest.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / tests / phpunit / includes / api / ApiBaseTest.php
1 <?php
2
3 use Wikimedia\TestingAccessWrapper;
4
5 /**
6  * @group API
7  * @group Database
8  * @group medium
9  */
10 class ApiBaseTest extends ApiTestCase {
11
12         /**
13          * @covers ApiBase::requireOnlyOneParameter
14          */
15         public function testRequireOnlyOneParameterDefault() {
16                 $mock = new MockApi();
17                 $mock->requireOnlyOneParameter(
18                         [ "filename" => "foo.txt", "enablechunks" => false ],
19                         "filename", "enablechunks"
20                 );
21                 $this->assertTrue( true );
22         }
23
24         /**
25          * @expectedException ApiUsageException
26          * @covers ApiBase::requireOnlyOneParameter
27          */
28         public function testRequireOnlyOneParameterZero() {
29                 $mock = new MockApi();
30                 $mock->requireOnlyOneParameter(
31                         [ "filename" => "foo.txt", "enablechunks" => 0 ],
32                         "filename", "enablechunks"
33                 );
34         }
35
36         /**
37          * @expectedException ApiUsageException
38          * @covers ApiBase::requireOnlyOneParameter
39          */
40         public function testRequireOnlyOneParameterTrue() {
41                 $mock = new MockApi();
42                 $mock->requireOnlyOneParameter(
43                         [ "filename" => "foo.txt", "enablechunks" => true ],
44                         "filename", "enablechunks"
45                 );
46         }
47
48         /**
49          * @dataProvider provideGetParameterFromSettings
50          * @param string|null $input
51          * @param array $paramSettings
52          * @param mixed $expected
53          * @param string[] $warnings
54          */
55         public function testGetParameterFromSettings( $input, $paramSettings, $expected, $warnings ) {
56                 $mock = new MockApi();
57                 $wrapper = TestingAccessWrapper::newFromObject( $mock );
58
59                 $context = new DerivativeContext( $mock );
60                 $context->setRequest( new FauxRequest( $input !== null ? [ 'foo' => $input ] : [] ) );
61                 $wrapper->mMainModule = new ApiMain( $context );
62
63                 if ( $expected instanceof ApiUsageException ) {
64                         try {
65                                 $wrapper->getParameterFromSettings( 'foo', $paramSettings, true );
66                         } catch ( ApiUsageException $ex ) {
67                                 $this->assertEquals( $expected, $ex );
68                         }
69                 } else {
70                         $result = $wrapper->getParameterFromSettings( 'foo', $paramSettings, true );
71                         $this->assertSame( $expected, $result );
72                         $this->assertSame( $warnings, $mock->warnings );
73                 }
74         }
75
76         public static function provideGetParameterFromSettings() {
77                 $warnings = [
78                         [ 'apiwarn-badutf8', 'foo' ],
79                 ];
80
81                 $c0 = '';
82                 $enc = '';
83                 for ( $i = 0; $i < 32; $i++ ) {
84                         $c0 .= chr( $i );
85                         $enc .= ( $i === 9 || $i === 10 || $i === 13 )
86                                 ? chr( $i )
87                                 : '�';
88                 }
89
90                 return [
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' => [
97                                 '',
98                                 [ ApiBase::PARAM_DFLT => 'default', ApiBase::PARAM_REQUIRED => true ],
99                                 ApiUsageException::newWithMessage( null, [ 'apierror-missingparam', 'foo' ] ),
100                                 []
101                         ],
102                         'Multi-valued parameter' => [
103                                 'a|b|c',
104                                 [ ApiBase::PARAM_ISMULTI => true ],
105                                 [ 'a', 'b', 'c' ],
106                                 []
107                         ],
108                         'Multi-valued parameter, alternative separator' => [
109                                 "\x1fa|b\x1fc|d",
110                                 [ ApiBase::PARAM_ISMULTI => true ],
111                                 [ 'a|b', 'c|d' ],
112                                 []
113                         ],
114                         'Multi-valued parameter, other C0 controls' => [
115                                 $c0,
116                                 [ ApiBase::PARAM_ISMULTI => true ],
117                                 [ $enc ],
118                                 $warnings
119                         ],
120                         'Multi-valued parameter, other C0 controls (2)' => [
121                                 "\x1f" . $c0,
122                                 [ ApiBase::PARAM_ISMULTI => true ],
123                                 [ substr( $enc, 0, -3 ), '' ],
124                                 $warnings
125                         ],
126                 ];
127         }
128
129         public function testErrorArrayToStatus() {
130                 $mock = new MockApi();
131
132                 // Sanity check empty array
133                 $expect = Status::newGood();
134                 $this->assertEquals( $expect, $mock->errorArrayToStatus( [] ) );
135
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( [
144                         [ 'blockedtext' ],
145                         [ 'autoblockedtext' ],
146                         [ 'systemblockedtext' ],
147                         'mainpage',
148                         [ 'parentheses', 'foobar' ],
149                 ] ) );
150
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,
158                 ] );
159                 $block->insert();
160                 $blockinfo = [ 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $block ) ];
161
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( [
169                         [ 'blockedtext' ],
170                         [ 'autoblockedtext' ],
171                         [ 'systemblockedtext' ],
172                         'mainpage',
173                         [ 'parentheses', 'foobar' ],
174                 ], $user ) );
175         }
176
177         /**
178          * @covers ApiBase::dieStatus
179          */
180         public function testDieStatus() {
181                 $mock = new MockApi();
182
183                 $status = StatusValue::newGood();
184                 $status->error( 'foo' );
185                 $status->warning( 'bar' );
186                 try {
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"' );
192                 }
193
194                 $status = StatusValue::newGood();
195                 $status->warning( 'foo' );
196                 $status->warning( 'bar' );
197                 try {
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"' );
203                 }
204
205                 $status = StatusValue::newGood();
206                 $status->setOk( false );
207                 try {
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"' );
213                 }
214         }
215
216 }