Merge "Add semantic tags to license info text"
[lhc/web/wiklou.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 * @covers ApiBase::getParameterFromSettings
55 */
56 public function testGetParameterFromSettings( $input, $paramSettings, $expected, $warnings ) {
57 $mock = new MockApi();
58 $wrapper = TestingAccessWrapper::newFromObject( $mock );
59
60 $context = new DerivativeContext( $mock );
61 $context->setRequest( new FauxRequest( $input !== null ? [ 'foo' => $input ] : [] ) );
62 $wrapper->mMainModule = new ApiMain( $context );
63
64 if ( $expected instanceof ApiUsageException ) {
65 try {
66 $wrapper->getParameterFromSettings( 'foo', $paramSettings, true );
67 } catch ( ApiUsageException $ex ) {
68 $this->assertEquals( $expected, $ex );
69 }
70 } else {
71 $result = $wrapper->getParameterFromSettings( 'foo', $paramSettings, true );
72 $this->assertSame( $expected, $result );
73 $this->assertSame( $warnings, $mock->warnings );
74 }
75 }
76
77 public static function provideGetParameterFromSettings() {
78 $warnings = [
79 [ 'apiwarn-badutf8', 'foo' ],
80 ];
81
82 $c0 = '';
83 $enc = '';
84 for ( $i = 0; $i < 32; $i++ ) {
85 $c0 .= chr( $i );
86 $enc .= ( $i === 9 || $i === 10 || $i === 13 )
87 ? chr( $i )
88 : '�';
89 }
90
91 return [
92 'Basic param' => [ 'bar', null, 'bar', [] ],
93 'Basic param, C0 controls' => [ $c0, null, $enc, $warnings ],
94 'String param' => [ 'bar', '', 'bar', [] ],
95 'String param, defaulted' => [ null, '', '', [] ],
96 'String param, empty' => [ '', 'default', '', [] ],
97 'String param, required, empty' => [
98 '',
99 [ ApiBase::PARAM_DFLT => 'default', ApiBase::PARAM_REQUIRED => true ],
100 ApiUsageException::newWithMessage( null, [ 'apierror-missingparam', 'foo' ] ),
101 []
102 ],
103 'Multi-valued parameter' => [
104 'a|b|c',
105 [ ApiBase::PARAM_ISMULTI => true ],
106 [ 'a', 'b', 'c' ],
107 []
108 ],
109 'Multi-valued parameter, alternative separator' => [
110 "\x1fa|b\x1fc|d",
111 [ ApiBase::PARAM_ISMULTI => true ],
112 [ 'a|b', 'c|d' ],
113 []
114 ],
115 'Multi-valued parameter, other C0 controls' => [
116 $c0,
117 [ ApiBase::PARAM_ISMULTI => true ],
118 [ $enc ],
119 $warnings
120 ],
121 'Multi-valued parameter, other C0 controls (2)' => [
122 "\x1f" . $c0,
123 [ ApiBase::PARAM_ISMULTI => true ],
124 [ substr( $enc, 0, -3 ), '' ],
125 $warnings
126 ],
127 ];
128 }
129
130 /**
131 * @covers ApiBase::errorArrayToStatus
132 */
133 public function testErrorArrayToStatus() {
134 $mock = new MockApi();
135
136 // Sanity check empty array
137 $expect = Status::newGood();
138 $this->assertEquals( $expect, $mock->errorArrayToStatus( [] ) );
139
140 // No blocked $user, so no special block handling
141 $expect = Status::newGood();
142 $expect->fatal( 'blockedtext' );
143 $expect->fatal( 'autoblockedtext' );
144 $expect->fatal( 'systemblockedtext' );
145 $expect->fatal( 'mainpage' );
146 $expect->fatal( 'parentheses', 'foobar' );
147 $this->assertEquals( $expect, $mock->errorArrayToStatus( [
148 [ 'blockedtext' ],
149 [ 'autoblockedtext' ],
150 [ 'systemblockedtext' ],
151 'mainpage',
152 [ 'parentheses', 'foobar' ],
153 ] ) );
154
155 // Has a blocked $user, so special block handling
156 $user = $this->getMutableTestUser()->getUser();
157 $block = new \Block( [
158 'address' => $user->getName(),
159 'user' => $user->getID(),
160 'reason' => __METHOD__,
161 'expiry' => time() + 100500,
162 ] );
163 $block->insert();
164 $blockinfo = [ 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $block ) ];
165
166 $expect = Status::newGood();
167 $expect->fatal( ApiMessage::create( 'apierror-blocked', 'blocked', $blockinfo ) );
168 $expect->fatal( ApiMessage::create( 'apierror-autoblocked', 'autoblocked', $blockinfo ) );
169 $expect->fatal( ApiMessage::create( 'apierror-systemblocked', 'blocked', $blockinfo ) );
170 $expect->fatal( 'mainpage' );
171 $expect->fatal( 'parentheses', 'foobar' );
172 $this->assertEquals( $expect, $mock->errorArrayToStatus( [
173 [ 'blockedtext' ],
174 [ 'autoblockedtext' ],
175 [ 'systemblockedtext' ],
176 'mainpage',
177 [ 'parentheses', 'foobar' ],
178 ], $user ) );
179 }
180
181 /**
182 * @covers ApiBase::dieStatus
183 */
184 public function testDieStatus() {
185 $mock = new MockApi();
186
187 $status = StatusValue::newGood();
188 $status->error( 'foo' );
189 $status->warning( 'bar' );
190 try {
191 $mock->dieStatus( $status );
192 $this->fail( 'Expected exception not thrown' );
193 } catch ( ApiUsageException $ex ) {
194 $this->assertTrue( ApiTestCase::apiExceptionHasCode( $ex, 'foo' ), 'Exception has "foo"' );
195 $this->assertFalse( ApiTestCase::apiExceptionHasCode( $ex, 'bar' ), 'Exception has "bar"' );
196 }
197
198 $status = StatusValue::newGood();
199 $status->warning( 'foo' );
200 $status->warning( 'bar' );
201 try {
202 $mock->dieStatus( $status );
203 $this->fail( 'Expected exception not thrown' );
204 } catch ( ApiUsageException $ex ) {
205 $this->assertTrue( ApiTestCase::apiExceptionHasCode( $ex, 'foo' ), 'Exception has "foo"' );
206 $this->assertTrue( ApiTestCase::apiExceptionHasCode( $ex, 'bar' ), 'Exception has "bar"' );
207 }
208
209 $status = StatusValue::newGood();
210 $status->setOk( false );
211 try {
212 $mock->dieStatus( $status );
213 $this->fail( 'Expected exception not thrown' );
214 } catch ( ApiUsageException $ex ) {
215 $this->assertTrue( ApiTestCase::apiExceptionHasCode( $ex, 'unknownerror-nocode' ),
216 'Exception has "unknownerror-nocode"' );
217 }
218 }
219
220 }