Fix method/function names case mismatch in core files
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiBaseTest.php
index 121820a..3adf1b6 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 
+use MediaWiki\Block\DatabaseBlock;
+use MediaWiki\MediaWikiServices;
 use Wikimedia\TestingAccessWrapper;
 
 /**
@@ -529,12 +531,36 @@ class ApiBaseTest extends ApiTestCase {
                                'foo',
                                [ [ 'apiwarn-deprecation-parameter', 'myParam' ] ],
                        ],
+                       'Deprecated parameter with default, unspecified' => [
+                               null,
+                               [ ApiBase::PARAM_DEPRECATED => true, ApiBase::PARAM_DFLT => 'foo' ],
+                               'foo',
+                               [],
+                       ],
+                       'Deprecated parameter with default, specified' => [
+                               'foo',
+                               [ ApiBase::PARAM_DEPRECATED => true, ApiBase::PARAM_DFLT => 'foo' ],
+                               'foo',
+                               [ [ 'apiwarn-deprecation-parameter', 'myParam' ] ],
+                       ],
                        'Deprecated parameter value' => [
                                'a',
                                [ ApiBase::PARAM_DEPRECATED_VALUES => [ 'a' => true ] ],
                                'a',
                                [ [ 'apiwarn-deprecation-parameter', 'myParam=a' ] ],
                        ],
+                       'Deprecated parameter value as default, unspecified' => [
+                               null,
+                               [ ApiBase::PARAM_DEPRECATED_VALUES => [ 'a' => true ], ApiBase::PARAM_DFLT => 'a' ],
+                               'a',
+                               [],
+                       ],
+                       'Deprecated parameter value as default, specified' => [
+                               'a',
+                               [ ApiBase::PARAM_DEPRECATED_VALUES => [ 'a' => true ], ApiBase::PARAM_DFLT => 'a' ],
+                               'a',
+                               [ [ 'apiwarn-deprecation-parameter', 'myParam=a' ] ],
+                       ],
                        'Multiple deprecated parameter values' => [
                                'a|b|c|d',
                                [ ApiBase::PARAM_DEPRECATED_VALUES =>
@@ -619,7 +645,7 @@ class ApiBaseTest extends ApiTestCase {
                                        ApiBase::PARAM_ISMULTI => true,
                                        ApiBase::PARAM_TYPE => 'namespace',
                                ],
-                               MWNamespace::getValidNamespaces(),
+                               MediaWikiServices::getInstance()->getNamespaceInfo()->getValidNamespaces(),
                                [],
                        ],
                        // PARAM_ALL is ignored with namespace types.
@@ -630,7 +656,7 @@ class ApiBaseTest extends ApiTestCase {
                                        ApiBase::PARAM_TYPE => 'namespace',
                                        ApiBase::PARAM_ALL => false,
                                ],
-                               MWNamespace::getValidNamespaces(),
+                               MediaWikiServices::getInstance()->getNamespaceInfo()->getValidNamespaces(),
                                [],
                        ],
                        'Namespace with wildcard "x"' => [
@@ -1273,6 +1299,8 @@ class ApiBaseTest extends ApiTestCase {
        public function testErrorArrayToStatus() {
                $mock = new MockApi();
 
+               $msg = new Message( 'mainpage' );
+
                // Sanity check empty array
                $expect = Status::newGood();
                $this->assertEquals( $expect, $mock->errorArrayToStatus( [] ) );
@@ -1283,18 +1311,22 @@ class ApiBaseTest extends ApiTestCase {
                $expect->fatal( 'autoblockedtext' );
                $expect->fatal( 'systemblockedtext' );
                $expect->fatal( 'mainpage' );
+               $expect->fatal( $msg );
+               $expect->fatal( $msg, 'foobar' );
                $expect->fatal( 'parentheses', 'foobar' );
                $this->assertEquals( $expect, $mock->errorArrayToStatus( [
                        [ 'blockedtext' ],
                        [ 'autoblockedtext' ],
                        [ 'systemblockedtext' ],
                        'mainpage',
+                       $msg,
+                       [ $msg, 'foobar' ],
                        [ 'parentheses', 'foobar' ],
                ] ) );
 
                // Has a blocked $user, so special block handling
                $user = $this->getMutableTestUser()->getUser();
-               $block = new \Block( [
+               $block = new DatabaseBlock( [
                        'address' => $user->getName(),
                        'user' => $user->getID(),
                        'by' => $this->getTestSysop()->getUser()->getId(),
@@ -1302,23 +1334,89 @@ class ApiBaseTest extends ApiTestCase {
                        'expiry' => time() + 100500,
                ] );
                $block->insert();
-               $blockinfo = [ 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $block ) ];
+               $userInfoTrait = TestingAccessWrapper::newFromObject(
+                       $this->getMockForTrait( ApiBlockInfoTrait::class )
+               );
+               $blockinfo = [ 'blockinfo' => $userInfoTrait->getBlockDetails( $block ) ];
 
                $expect = Status::newGood();
                $expect->fatal( ApiMessage::create( 'apierror-blocked', 'blocked', $blockinfo ) );
                $expect->fatal( ApiMessage::create( 'apierror-autoblocked', 'autoblocked', $blockinfo ) );
                $expect->fatal( ApiMessage::create( 'apierror-systemblocked', 'blocked', $blockinfo ) );
                $expect->fatal( 'mainpage' );
+               $expect->fatal( $msg );
+               $expect->fatal( $msg, 'foobar' );
                $expect->fatal( 'parentheses', 'foobar' );
                $this->assertEquals( $expect, $mock->errorArrayToStatus( [
                        [ 'blockedtext' ],
                        [ 'autoblockedtext' ],
                        [ 'systemblockedtext' ],
                        'mainpage',
+                       $msg,
+                       [ $msg, 'foobar' ],
                        [ 'parentheses', 'foobar' ],
                ], $user ) );
        }
 
+       public function testAddBlockInfoToStatus() {
+               $mock = new MockApi();
+
+               $msg = new Message( 'mainpage' );
+
+               // Sanity check empty array
+               $expect = Status::newGood();
+               $test = Status::newGood();
+               $mock->addBlockInfoToStatus( $test );
+               $this->assertEquals( $expect, $test );
+
+               // No blocked $user, so no special block handling
+               $expect = Status::newGood();
+               $expect->fatal( 'blockedtext' );
+               $expect->fatal( 'autoblockedtext' );
+               $expect->fatal( 'systemblockedtext' );
+               $expect->fatal( 'mainpage' );
+               $expect->fatal( $msg );
+               $expect->fatal( $msg, 'foobar' );
+               $expect->fatal( 'parentheses', 'foobar' );
+               $test = clone $expect;
+               $mock->addBlockInfoToStatus( $test );
+               $this->assertEquals( $expect, $test );
+
+               // Has a blocked $user, so special block handling
+               $user = $this->getMutableTestUser()->getUser();
+               $block = new DatabaseBlock( [
+                       'address' => $user->getName(),
+                       'user' => $user->getID(),
+                       'by' => $this->getTestSysop()->getUser()->getId(),
+                       'reason' => __METHOD__,
+                       'expiry' => time() + 100500,
+               ] );
+               $block->insert();
+               $userInfoTrait = TestingAccessWrapper::newFromObject(
+                       $this->getObjectForTrait( ApiBlockInfoTrait::class )
+               );
+               $blockinfo = [ 'blockinfo' => $userInfoTrait->getBlockDetails( $block ) ];
+
+               $expect = Status::newGood();
+               $expect->fatal( ApiMessage::create( 'apierror-blocked', 'blocked', $blockinfo ) );
+               $expect->fatal( ApiMessage::create( 'apierror-autoblocked', 'autoblocked', $blockinfo ) );
+               $expect->fatal( ApiMessage::create( 'apierror-systemblocked', 'blocked', $blockinfo ) );
+               $expect->fatal( 'mainpage' );
+               $expect->fatal( $msg );
+               $expect->fatal( $msg, 'foobar' );
+               $expect->fatal( 'parentheses', 'foobar' );
+               $test = Status::newGood();
+               $test->fatal( 'blockedtext' );
+               $test->fatal( 'autoblockedtext' );
+               $test->fatal( 'systemblockedtext' );
+               $test->fatal( 'mainpage' );
+               $test->fatal( $msg );
+               $test->fatal( $msg, 'foobar' );
+               $test->fatal( 'parentheses', 'foobar' );
+               $mock->addBlockInfoToStatus( $test, $user );
+               $this->assertEquals( $expect, $test );
+       }
+
        public function testDieStatus() {
                $mock = new MockApi();
 
@@ -1345,7 +1443,7 @@ class ApiBaseTest extends ApiTestCase {
                }
 
                $status = StatusValue::newGood();
-               $status->setOk( false );
+               $status->setOK( false );
                try {
                        $mock->dieStatus( $status );
                        $this->fail( 'Expected exception not thrown' );