Cleanup Api phpunit Tests
authoraddshore <addshorewiki@gmail.com>
Wed, 23 Oct 2013 16:01:33 +0000 (17:01 +0100)
committeraddshore <addshorewiki@gmail.com>
Thu, 24 Oct 2013 18:17:01 +0000 (19:17 +0100)
- Splits multiple classes into individual files
- Adds @covers tags
- Fixes scope

Change-Id: I7d2816d3574fa53a2aaa8e2a84b7a7ecdd245252

14 files changed:
tests/TestsAutoLoader.php
tests/phpunit/includes/api/ApiAccountCreationTest.php [deleted file]
tests/phpunit/includes/api/ApiBlockTest.php
tests/phpunit/includes/api/ApiCreateAccountTest.php [new file with mode: 0644]
tests/phpunit/includes/api/ApiEditPageTest.php
tests/phpunit/includes/api/ApiOptionsTest.php
tests/phpunit/includes/api/ApiParseTest.php
tests/phpunit/includes/api/ApiPurgeTest.php
tests/phpunit/includes/api/ApiTestCase.php
tests/phpunit/includes/api/ApiTestContext.php [new file with mode: 0644]
tests/phpunit/includes/api/ApiUnblockTest.php [new file with mode: 0644]
tests/phpunit/includes/api/MockApi.php [new file with mode: 0644]
tests/phpunit/includes/api/PrefixUniquenessTest.php
tests/phpunit/includes/api/UserWrapper.php [new file with mode: 0644]

index 00ce13c..dec8e22 100644 (file)
@@ -55,10 +55,10 @@ $wgAutoloadClasses += array(
        # tests/phpunit/includes/api
        'ApiFormatTestBase' => "$testDir/phpunit/includes/api/format/ApiFormatTestBase.php",
        'ApiTestCase' => "$testDir/phpunit/includes/api/ApiTestCase.php",
-       'ApiTestContext' => "$testDir/phpunit/includes/api/ApiTestCase.php",
-       'MockApi' => "$testDir/phpunit/includes/api/ApiTestCase.php",
+       'ApiTestContext' => "$testDir/phpunit/includes/api/ApiTestContext.php",
+       'MockApi' => "$testDir/phpunit/includes/api/MockApi.php",
+       'UserWrapper' => "$testDir/phpunit/includes/api/UserWrapper.php",
        'RandomImageGenerator' => "$testDir/phpunit/includes/api/RandomImageGenerator.php",
-       'UserWrapper' => "$testDir/phpunit/includes/api/ApiTestCase.php",
 
        # tests/phpunit/includes/content
        'DummyContentHandlerForTesting' => "$testDir/phpunit/includes/content/ContentHandlerTest.php",
diff --git a/tests/phpunit/includes/api/ApiAccountCreationTest.php b/tests/phpunit/includes/api/ApiAccountCreationTest.php
deleted file mode 100644 (file)
index 68f80ac..0000000
+++ /dev/null
@@ -1,159 +0,0 @@
-<?php
-
-/**
- * @group Database
- * @group API
- * @group medium
- */
-class ApiCreateAccountTest extends ApiTestCase {
-       function setUp() {
-               parent::setUp();
-               LoginForm::setCreateaccountToken();
-               $this->setMwGlobals( array( 'wgEnableEmail' => true ) );
-       }
-
-       /**
-        * Test the account creation API with a valid request. Also
-        * make sure the new account can log in and is valid.
-        *
-        * This test does multiple API requests so it might end up being
-        * a bit slow. Raise the default timeout.
-        * @group medium
-        */
-       public function testValid() {
-               global $wgServer;
-
-               if ( !isset( $wgServer ) ) {
-                       $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
-               }
-
-               $password = User::randomPassword();
-
-               $ret = $this->doApiRequest( array(
-                       'action' => 'createaccount',
-                       'name' => 'Apitestnew',
-                       'password' => $password,
-                       'email' => 'test@domain.test',
-                       'realname' => 'Test Name'
-               ) );
-
-               $result = $ret[0];
-               $this->assertNotInternalType( 'bool', $result );
-               $this->assertNotInternalType( 'null', $result['createaccount'] );
-
-               // Should first ask for token.
-               $a = $result['createaccount'];
-               $this->assertEquals( 'needtoken', $a['result'] );
-               $token = $a['token'];
-
-               // Finally create the account
-               $ret = $this->doApiRequest(
-                       array(
-                               'action' => 'createaccount',
-                               'name' => 'Apitestnew',
-                               'password' => $password,
-                               'token' => $token,
-                               'email' => 'test@domain.test',
-                               'realname' => 'Test Name'
-                       ),
-                       $ret[2]
-               );
-
-               $result = $ret[0];
-               $this->assertNotInternalType( 'bool', $result );
-               $this->assertEquals( 'success', $result['createaccount']['result'] );
-
-               // Try logging in with the new user.
-               $ret = $this->doApiRequest( array(
-                       'action' => 'login',
-                       'lgname' => 'Apitestnew',
-                       'lgpassword' => $password,
-               ) );
-
-               $result = $ret[0];
-               $this->assertNotInternalType( 'bool', $result );
-               $this->assertNotInternalType( 'null', $result['login'] );
-
-               $a = $result['login']['result'];
-               $this->assertEquals( 'NeedToken', $a );
-               $token = $result['login']['token'];
-
-               $ret = $this->doApiRequest(
-                       array(
-                               'action' => 'login',
-                               'lgtoken' => $token,
-                               'lgname' => 'Apitestnew',
-                               'lgpassword' => $password,
-                       ),
-                       $ret[2]
-               );
-
-               $result = $ret[0];
-
-               $this->assertNotInternalType( 'bool', $result );
-               $a = $result['login']['result'];
-
-               $this->assertEquals( 'Success', $a );
-
-               // log out to destroy the session
-               $ret = $this->doApiRequest(
-                       array(
-                               'action' => 'logout',
-                       ),
-                       $ret[2]
-               );
-               $this->assertEquals( array(), $ret[0] );
-       }
-
-       /**
-        * Make sure requests with no names are invalid.
-        * @expectedException UsageException
-        */
-       public function testNoName() {
-               $this->doApiRequest( array(
-                       'action' => 'createaccount',
-                       'token' => LoginForm::getCreateaccountToken(),
-                       'password' => 'password',
-               ) );
-       }
-
-       /**
-        * Make sure requests with no password are invalid.
-        * @expectedException UsageException
-        */
-       public function testNoPassword() {
-               $this->doApiRequest( array(
-                       'action' => 'createaccount',
-                       'name' => 'testName',
-                       'token' => LoginForm::getCreateaccountToken(),
-               ) );
-       }
-
-       /**
-        * Make sure requests with existing users are invalid.
-        * @expectedException UsageException
-        */
-       public function testExistingUser() {
-               $this->doApiRequest( array(
-                       'action' => 'createaccount',
-                       'name' => 'Apitestsysop',
-                       'token' => LoginForm::getCreateaccountToken(),
-                       'password' => 'password',
-                       'email' => 'test@domain.test',
-               ) );
-       }
-
-       /**
-        * Make sure requests with invalid emails are invalid.
-        * @expectedException UsageException
-        */
-       public function testInvalidEmail() {
-               $this->doApiRequest( array(
-                       'action' => 'createaccount',
-                       'name' => 'Test User',
-                       'token' => LoginForm::getCreateaccountToken(),
-                       'password' => 'password',
-                       'email' => 'invalid',
-               ) );
-       }
-}
index 8afb748..d98eec6 100644 (file)
@@ -4,6 +4,8 @@
  * @group API
  * @group Database
  * @group medium
+ *
+ * @covers ApiBlock
  */
 class ApiBlockTest extends ApiTestCase {
        protected function setUp() {
@@ -11,7 +13,7 @@ class ApiBlockTest extends ApiTestCase {
                $this->doLogin();
        }
 
-       function getTokens() {
+       protected function getTokens() {
                return $this->getTokenList( self::$users['sysop'] );
        }
 
@@ -63,17 +65,13 @@ class ApiBlockTest extends ApiTestCase {
        }
 
        /**
-        * Attempting to block without a token should give a UsageException with
-        * error message:
-        *   "The token parameter must be set"
-        *
-        * @dataProvider provideBlockUnblockAction
         * @expectedException UsageException
+        * @expectedExceptionMessage The token parameter must be set
         */
-       public function testBlockingActionWithNoToken( $action ) {
+       public function testBlockingActionWithNoToken( ) {
                $this->doApiRequest(
                        array(
-                               'action' => $action,
+                               'action' => 'block',
                                'user' => 'UTApiBlockee',
                                'reason' => 'Some reason',
                        ),
@@ -82,14 +80,4 @@ class ApiBlockTest extends ApiTestCase {
                        self::$users['sysop']->user
                );
        }
-
-       /**
-        * Just provide the 'block' and 'unblock' action to test both API calls
-        */
-       public static function provideBlockUnblockAction() {
-               return array(
-                       array( 'block' ),
-                       array( 'unblock' ),
-               );
-       }
 }
diff --git a/tests/phpunit/includes/api/ApiCreateAccountTest.php b/tests/phpunit/includes/api/ApiCreateAccountTest.php
new file mode 100644 (file)
index 0000000..a723245
--- /dev/null
@@ -0,0 +1,161 @@
+<?php
+
+/**
+ * @group Database
+ * @group API
+ * @group medium
+ *
+ * @covers ApiCreateAccount
+ */
+class ApiCreateAccountTest extends ApiTestCase {
+       protected function setUp() {
+               parent::setUp();
+               LoginForm::setCreateaccountToken();
+               $this->setMwGlobals( array( 'wgEnableEmail' => true ) );
+       }
+
+       /**
+        * Test the account creation API with a valid request. Also
+        * make sure the new account can log in and is valid.
+        *
+        * This test does multiple API requests so it might end up being
+        * a bit slow. Raise the default timeout.
+        * @group medium
+        */
+       public function testValid() {
+               global $wgServer;
+
+               if ( !isset( $wgServer ) ) {
+                       $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
+               }
+
+               $password = User::randomPassword();
+
+               $ret = $this->doApiRequest( array(
+                       'action' => 'createaccount',
+                       'name' => 'Apitestnew',
+                       'password' => $password,
+                       'email' => 'test@domain.test',
+                       'realname' => 'Test Name'
+               ) );
+
+               $result = $ret[0];
+               $this->assertNotInternalType( 'bool', $result );
+               $this->assertNotInternalType( 'null', $result['createaccount'] );
+
+               // Should first ask for token.
+               $a = $result['createaccount'];
+               $this->assertEquals( 'needtoken', $a['result'] );
+               $token = $a['token'];
+
+               // Finally create the account
+               $ret = $this->doApiRequest(
+                       array(
+                               'action' => 'createaccount',
+                               'name' => 'Apitestnew',
+                               'password' => $password,
+                               'token' => $token,
+                               'email' => 'test@domain.test',
+                               'realname' => 'Test Name'
+                       ),
+                       $ret[2]
+               );
+
+               $result = $ret[0];
+               $this->assertNotInternalType( 'bool', $result );
+               $this->assertEquals( 'success', $result['createaccount']['result'] );
+
+               // Try logging in with the new user.
+               $ret = $this->doApiRequest( array(
+                       'action' => 'login',
+                       'lgname' => 'Apitestnew',
+                       'lgpassword' => $password,
+               ) );
+
+               $result = $ret[0];
+               $this->assertNotInternalType( 'bool', $result );
+               $this->assertNotInternalType( 'null', $result['login'] );
+
+               $a = $result['login']['result'];
+               $this->assertEquals( 'NeedToken', $a );
+               $token = $result['login']['token'];
+
+               $ret = $this->doApiRequest(
+                       array(
+                               'action' => 'login',
+                               'lgtoken' => $token,
+                               'lgname' => 'Apitestnew',
+                               'lgpassword' => $password,
+                       ),
+                       $ret[2]
+               );
+
+               $result = $ret[0];
+
+               $this->assertNotInternalType( 'bool', $result );
+               $a = $result['login']['result'];
+
+               $this->assertEquals( 'Success', $a );
+
+               // log out to destroy the session
+               $ret = $this->doApiRequest(
+                       array(
+                               'action' => 'logout',
+                       ),
+                       $ret[2]
+               );
+               $this->assertEquals( array(), $ret[0] );
+       }
+
+       /**
+        * Make sure requests with no names are invalid.
+        * @expectedException UsageException
+        */
+       public function testNoName() {
+               $this->doApiRequest( array(
+                       'action' => 'createaccount',
+                       'token' => LoginForm::getCreateaccountToken(),
+                       'password' => 'password',
+               ) );
+       }
+
+       /**
+        * Make sure requests with no password are invalid.
+        * @expectedException UsageException
+        */
+       public function testNoPassword() {
+               $this->doApiRequest( array(
+                       'action' => 'createaccount',
+                       'name' => 'testName',
+                       'token' => LoginForm::getCreateaccountToken(),
+               ) );
+       }
+
+       /**
+        * Make sure requests with existing users are invalid.
+        * @expectedException UsageException
+        */
+       public function testExistingUser() {
+               $this->doApiRequest( array(
+                       'action' => 'createaccount',
+                       'name' => 'Apitestsysop',
+                       'token' => LoginForm::getCreateaccountToken(),
+                       'password' => 'password',
+                       'email' => 'test@domain.test',
+               ) );
+       }
+
+       /**
+        * Make sure requests with invalid emails are invalid.
+        * @expectedException UsageException
+        */
+       public function testInvalidEmail() {
+               $this->doApiRequest( array(
+                       'action' => 'createaccount',
+                       'name' => 'Test User',
+                       'token' => LoginForm::getCreateaccountToken(),
+                       'password' => 'password',
+                       'email' => 'invalid',
+               ) );
+       }
+}
index 0c49b12..7521dcf 100644 (file)
@@ -8,6 +8,8 @@
  * @group API
  * @group Database
  * @group medium
+ *
+ * @covers ApiEditPage
  */
 class ApiEditPageTest extends ApiTestCase {
 
index ad1e73a..3168f32 100644 (file)
@@ -4,10 +4,18 @@
  * @group API
  * @group Database
  * @group medium
+ *
+ * @covers ApiOptions
  */
 class ApiOptionsTest extends MediaWikiLangTestCase {
 
-       private $mTested, $mUserMock, $mContext, $mSession;
+       /** @var PHPUnit_Framework_MockObject_MockObject */
+       private $mUserMock ;
+       /** @var ApiOptions */
+       private $mTested;
+       private $mSession;
+       /** @var DerivativeContext */
+       private $mContext;
 
        private $mOldGetPreferencesHooks = false;
 
index 2d714e6..d303d4b 100644 (file)
@@ -4,6 +4,8 @@
  * @group API
  * @group Database
  * @group medium
+ *
+ * @covers ApiParse
  */
 class ApiParseTest extends ApiTestCase {
 
index 28b5ff4..d25a4c1 100644 (file)
@@ -4,6 +4,8 @@
  * @group API
  * @group Database
  * @group medium
+ *
+ * @covers ApiPurge
  */
 class ApiPurgeTest extends ApiTestCase {
 
index 94ef9c6..ad297dd 100644 (file)
@@ -117,7 +117,7 @@ abstract class ApiTestCase extends MediaWikiLangTestCase {
         * @param $params Array: key-value API params
         * @param $session Array|null: session array
         * @param $user User|null A User object for the context
-        * @return result of the API call
+        * @return mixed result of the API call
         * @throws Exception in case wsToken is not set in the session
         */
        protected function doApiRequestWithToken( array $params, array $session = null, User $user = null ) {
@@ -188,66 +188,3 @@ abstract class ApiTestCase extends MediaWikiLangTestCase {
                );
        }
 }
-
-class UserWrapper {
-       public $userName;
-       public $password;
-       public $user;
-
-       public function __construct( $userName, $password, $group = '' ) {
-               $this->userName = $userName;
-               $this->password = $password;
-
-               $this->user = User::newFromName( $this->userName );
-               if ( !$this->user->getID() ) {
-                       $this->user = User::createNew( $this->userName, array(
-                               "email" => "test@example.com",
-                               "real_name" => "Test User" ) );
-               }
-               $this->user->setPassword( $this->password );
-
-               if ( $group !== '' ) {
-                       $this->user->addGroup( $group );
-               }
-               $this->user->saveSettings();
-       }
-}
-
-class MockApi extends ApiBase {
-       public function execute() {
-       }
-
-       public function getVersion() {
-       }
-
-       public function __construct() {
-       }
-
-       public function getAllowedParams() {
-               return array(
-                       'filename' => null,
-                       'enablechunks' => false,
-                       'sessionkey' => null,
-               );
-       }
-}
-
-class ApiTestContext extends RequestContext {
-
-       /**
-        * Returns a DerivativeContext with the request variables in place
-        *
-        * @param $request WebRequest request object including parameters and session
-        * @param $user User or null
-        * @return DerivativeContext
-        */
-       public function newTestContext( WebRequest $request, User $user = null ) {
-               $context = new DerivativeContext( $this );
-               $context->setRequest( $request );
-               if ( $user !== null ) {
-                       $context->setUser( $user );
-               }
-
-               return $context;
-       }
-}
diff --git a/tests/phpunit/includes/api/ApiTestContext.php b/tests/phpunit/includes/api/ApiTestContext.php
new file mode 100644 (file)
index 0000000..43637c2
--- /dev/null
@@ -0,0 +1,21 @@
+<?php
+
+class ApiTestContext extends RequestContext {
+
+       /**
+        * Returns a DerivativeContext with the request variables in place
+        *
+        * @param $request WebRequest request object including parameters and session
+        * @param $user User or null
+        * @return DerivativeContext
+        */
+       public function newTestContext( WebRequest $request, User $user = null ) {
+               $context = new DerivativeContext( $this );
+               $context->setRequest( $request );
+               if ( $user !== null ) {
+                       $context->setUser( $user );
+               }
+
+               return $context;
+       }
+}
\ No newline at end of file
diff --git a/tests/phpunit/includes/api/ApiUnblockTest.php b/tests/phpunit/includes/api/ApiUnblockTest.php
new file mode 100644 (file)
index 0000000..2c2370a
--- /dev/null
@@ -0,0 +1,31 @@
+<?php
+
+/**
+ * @group API
+ * @group Database
+ * @group medium
+ *
+ * @covers ApiUnblock
+ */
+class ApiUnblockTest extends ApiTestCase {
+       protected function setUp() {
+               parent::setUp();
+               $this->doLogin();
+       }
+
+       /**
+        * @expectedException UsageException
+        */
+       public function testWithNoToken( ) {
+               $this->doApiRequest(
+                       array(
+                               'action' => 'unblock',
+                               'user' => 'UTApiBlockee',
+                               'reason' => 'Some reason',
+                       ),
+                       null,
+                       false,
+                       self::$users['sysop']->user
+               );
+       }
+}
diff --git a/tests/phpunit/includes/api/MockApi.php b/tests/phpunit/includes/api/MockApi.php
new file mode 100644 (file)
index 0000000..3686048
--- /dev/null
@@ -0,0 +1,20 @@
+<?php
+
+class MockApi extends ApiBase {
+       public function execute() {
+       }
+
+       public function getVersion() {
+       }
+
+       public function __construct() {
+       }
+
+       public function getAllowedParams() {
+               return array(
+                       'filename' => null,
+                       'enablechunks' => false,
+                       'sessionkey' => null,
+               );
+       }
+}
\ No newline at end of file
index d9be85e..a1fc2c2 100644 (file)
@@ -6,6 +6,7 @@
  * @group API
  */
 class PrefixUniquenessTest extends MediaWikiTestCase {
+
        public function testPrefixes() {
                $main = new ApiMain( new FauxRequest() );
                $query = new ApiQuery( $main, 'foo', 'bar' );
@@ -13,6 +14,7 @@ class PrefixUniquenessTest extends MediaWikiTestCase {
                $prefixes = array();
 
                foreach ( $modules as $name => $class ) {
+                       /** @var ApiMain $module */
                        $module = new $class( $main, $name );
                        $prefix = $module->getModulePrefix();
                        if ( isset( $prefixes[$prefix] ) ) {
diff --git a/tests/phpunit/includes/api/UserWrapper.php b/tests/phpunit/includes/api/UserWrapper.php
new file mode 100644 (file)
index 0000000..3262e6c
--- /dev/null
@@ -0,0 +1,25 @@
+<?php
+
+class UserWrapper {
+       public $userName;
+       public $password;
+       public $user;
+
+       public function __construct( $userName, $password, $group = '' ) {
+               $this->userName = $userName;
+               $this->password = $password;
+
+               $this->user = User::newFromName( $this->userName );
+               if ( !$this->user->getID() ) {
+                       $this->user = User::createNew( $this->userName, array(
+                               "email" => "test@example.com",
+                               "real_name" => "Test User" ) );
+               }
+               $this->user->setPassword( $this->password );
+
+               if ( $group !== '' ) {
+                       $this->user->addGroup( $group );
+               }
+               $this->user->saveSettings();
+       }
+}
\ No newline at end of file