Refactor ApiTestCase to get token from ApiQueryTokens
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiBaseTest.php
index 96f3e44..3f6cac9 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 
+use Wikimedia\TestingAccessWrapper;
+
 /**
  * @group API
  * @group Database
@@ -49,6 +51,7 @@ class ApiBaseTest extends ApiTestCase {
         * @param array $paramSettings
         * @param mixed $expected
         * @param string[] $warnings
+        * @covers ApiBase::getParameterFromSettings
         */
        public function testGetParameterFromSettings( $input, $paramSettings, $expected, $warnings ) {
                $mock = new MockApi();
@@ -124,6 +127,9 @@ class ApiBaseTest extends ApiTestCase {
                ];
        }
 
+       /**
+        * @covers ApiBase::errorArrayToStatus
+        */
        public function testErrorArrayToStatus() {
                $mock = new MockApi();
 
@@ -135,11 +141,13 @@ class ApiBaseTest extends ApiTestCase {
                $expect = Status::newGood();
                $expect->fatal( 'blockedtext' );
                $expect->fatal( 'autoblockedtext' );
+               $expect->fatal( 'systemblockedtext' );
                $expect->fatal( 'mainpage' );
                $expect->fatal( 'parentheses', 'foobar' );
                $this->assertEquals( $expect, $mock->errorArrayToStatus( [
                        [ 'blockedtext' ],
                        [ 'autoblockedtext' ],
+                       [ 'systemblockedtext' ],
                        'mainpage',
                        [ 'parentheses', 'foobar' ],
                ] ) );
@@ -149,6 +157,7 @@ class ApiBaseTest extends ApiTestCase {
                $block = new \Block( [
                        'address' => $user->getName(),
                        'user' => $user->getID(),
+                       'by' => $this->getTestSysop()->getUser()->getId(),
                        'reason' => __METHOD__,
                        'expiry' => time() + 100500,
                ] );
@@ -158,14 +167,55 @@ class ApiBaseTest extends ApiTestCase {
                $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( 'parentheses', 'foobar' );
                $this->assertEquals( $expect, $mock->errorArrayToStatus( [
                        [ 'blockedtext' ],
                        [ 'autoblockedtext' ],
+                       [ 'systemblockedtext' ],
                        'mainpage',
                        [ 'parentheses', 'foobar' ],
                ], $user ) );
        }
 
+       /**
+        * @covers ApiBase::dieStatus
+        */
+       public function testDieStatus() {
+               $mock = new MockApi();
+
+               $status = StatusValue::newGood();
+               $status->error( 'foo' );
+               $status->warning( 'bar' );
+               try {
+                       $mock->dieStatus( $status );
+                       $this->fail( 'Expected exception not thrown' );
+               } catch ( ApiUsageException $ex ) {
+                       $this->assertTrue( ApiTestCase::apiExceptionHasCode( $ex, 'foo' ), 'Exception has "foo"' );
+                       $this->assertFalse( ApiTestCase::apiExceptionHasCode( $ex, 'bar' ), 'Exception has "bar"' );
+               }
+
+               $status = StatusValue::newGood();
+               $status->warning( 'foo' );
+               $status->warning( 'bar' );
+               try {
+                       $mock->dieStatus( $status );
+                       $this->fail( 'Expected exception not thrown' );
+               } catch ( ApiUsageException $ex ) {
+                       $this->assertTrue( ApiTestCase::apiExceptionHasCode( $ex, 'foo' ), 'Exception has "foo"' );
+                       $this->assertTrue( ApiTestCase::apiExceptionHasCode( $ex, 'bar' ), 'Exception has "bar"' );
+               }
+
+               $status = StatusValue::newGood();
+               $status->setOk( false );
+               try {
+                       $mock->dieStatus( $status );
+                       $this->fail( 'Expected exception not thrown' );
+               } catch ( ApiUsageException $ex ) {
+                       $this->assertTrue( ApiTestCase::apiExceptionHasCode( $ex, 'unknownerror-nocode' ),
+                               'Exception has "unknownerror-nocode"' );
+               }
+       }
+
 }