Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / tests / phpunit / includes / api / ApiBaseTest.php
blob7327e8547163f51cbda2f9f59cbfe482f24c899c
1 <?php
3 /**
4 * @group API
5 * @group Database
6 * @group medium
7 */
8 class ApiBaseTest extends ApiTestCase {
10 /**
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 );
22 /**
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"
34 /**
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"
46 /**
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 ) {
62 try {
63 $wrapper->getParameterFromSettings( 'foo', $paramSettings, true );
64 } catch ( ApiUsageException $ex ) {
65 $this->assertEquals( $expected, $ex );
67 } else {
68 $result = $wrapper->getParameterFromSettings( 'foo', $paramSettings, true );
69 $this->assertSame( $expected, $result );
70 $this->assertSame( $warnings, $mock->warnings );
74 public static function provideGetParameterFromSettings() {
75 $warnings = [
76 [ 'apiwarn-badutf8', 'foo' ],
79 $c0 = '';
80 $enc = '';
81 for ( $i = 0; $i < 32; $i++ ) {
82 $c0 .= chr( $i );
83 $enc .= ( $i === 9 || $i === 10 || $i === 13 )
84 ? chr( $i )
85 : '�';
88 return [
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' => [
95 '',
96 [ ApiBase::PARAM_DFLT => 'default', ApiBase::PARAM_REQUIRED => true ],
97 ApiUsageException::newWithMessage( null, [ 'apierror-missingparam', 'foo' ] ),
100 'Multi-valued parameter' => [
101 'a|b|c',
102 [ ApiBase::PARAM_ISMULTI => true ],
103 [ 'a', 'b', 'c' ],
106 'Multi-valued parameter, alternative separator' => [
107 "\x1fa|b\x1fc|d",
108 [ ApiBase::PARAM_ISMULTI => true ],
109 [ 'a|b', 'c|d' ],
112 'Multi-valued parameter, other C0 controls' => [
113 $c0,
114 [ ApiBase::PARAM_ISMULTI => true ],
115 [ $enc ],
116 $warnings
118 'Multi-valued parameter, other C0 controls (2)' => [
119 "\x1f" . $c0,
120 [ ApiBase::PARAM_ISMULTI => true ],
121 [ substr( $enc, 0, -3 ), '' ],
122 $warnings
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( [
142 [ 'blockedtext' ],
143 [ 'autoblockedtext' ],
144 [ 'systemblockedtext' ],
145 'mainpage',
146 [ 'parentheses', 'foobar' ],
147 ] ) );
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,
156 ] );
157 $block->insert();
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( [
167 [ 'blockedtext' ],
168 [ 'autoblockedtext' ],
169 [ 'systemblockedtext' ],
170 'mainpage',
171 [ 'parentheses', 'foobar' ],
172 ], $user ) );