Split ApiTest class into seperate module classes
authoraddshore <addshorewiki@gmail.com>
Wed, 23 Oct 2013 15:36:40 +0000 (16:36 +0100)
committerAntoine Musso <hashar@free.fr>
Tue, 5 Nov 2013 11:40:50 +0000 (12:40 +0100)
Also!
 - adds @covers tags

Change-Id: I6d4f98f75cd3c2a52c982ece6dd295a4bf84a6fa

tests/phpunit/includes/api/ApiBaseTest.php [new file with mode: 0644]
tests/phpunit/includes/api/ApiLoginTest.php [new file with mode: 0644]
tests/phpunit/includes/api/ApiMainTest.php [new file with mode: 0644]
tests/phpunit/includes/api/ApiTest.php [deleted file]
tests/phpunit/includes/api/ApiTokensTest.php [new file with mode: 0644]

diff --git a/tests/phpunit/includes/api/ApiBaseTest.php b/tests/phpunit/includes/api/ApiBaseTest.php
new file mode 100644 (file)
index 0000000..bfb75ef
--- /dev/null
@@ -0,0 +1,46 @@
+<?php
+
+/**
+ * @group API
+ * @group Database
+ * @group medium
+ */
+class ApiBaseTest extends ApiTestCase {
+
+       /**
+        * @covers ApiBase::requireOnlyOneParameter
+        */
+       public function testRequireOnlyOneParameterDefault() {
+               $mock = new MockApi();
+               $mock->requireOnlyOneParameter(
+                       array( "filename" => "foo.txt", "enablechunks" => false ),
+                       "filename", "enablechunks"
+               );
+               $this->assertTrue( true );
+       }
+
+       /**
+        * @expectedException UsageException
+        * @covers ApiBase::requireOnlyOneParameter
+        */
+       public function testRequireOnlyOneParameterZero() {
+               $mock = new MockApi();
+               $mock->requireOnlyOneParameter(
+                       array( "filename" => "foo.txt","enablechunks" => 0 ),
+                       "filename", "enablechunks"
+               );
+       }
+
+       /**
+        * @expectedException UsageException
+        * @covers ApiBase::requireOnlyOneParameter
+        */
+       public function testRequireOnlyOneParameterTrue() {
+               $mock = new MockApi();
+               $mock->requireOnlyOneParameter(
+                       array( "filename" => "foo.txt", "enablechunks" => true ),
+                       "filename", "enablechunks"
+               );
+       }
+
+}
diff --git a/tests/phpunit/includes/api/ApiLoginTest.php b/tests/phpunit/includes/api/ApiLoginTest.php
new file mode 100644 (file)
index 0000000..f1199e0
--- /dev/null
@@ -0,0 +1,177 @@
+<?php
+
+/**
+ * @group API
+ * @group Database
+ * @group medium
+ *
+ * @covers ApiLogin
+ */
+class ApiLoginTest extends ApiTestCase {
+
+       /**
+        * Test result of attempted login with an empty username
+        */
+       public function testApiLoginNoName() {
+               $data = $this->doApiRequest( array( 'action' => 'login',
+                       'lgname' => '', 'lgpassword' => self::$users['sysop']->password,
+               ) );
+               $this->assertEquals( 'NoName', $data[0]['login']['result'] );
+       }
+
+       public function testApiLoginBadPass() {
+               global $wgServer;
+
+               $user = self::$users['sysop'];
+               $user->user->logOut();
+
+               if ( !isset( $wgServer ) ) {
+                       $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
+               }
+               $ret = $this->doApiRequest( array(
+                       "action" => "login",
+                       "lgname" => $user->username,
+                       "lgpassword" => "bad",
+               ) );
+
+               $result = $ret[0];
+
+               $this->assertNotInternalType( "bool", $result );
+               $a = $result["login"]["result"];
+               $this->assertEquals( "NeedToken", $a );
+
+               $token = $result["login"]["token"];
+
+               $ret = $this->doApiRequest(
+                       array(
+                               "action" => "login",
+                               "lgtoken" => $token,
+                               "lgname" => $user->username,
+                               "lgpassword" => "badnowayinhell",
+                       ),
+                       $ret[2]
+               );
+
+               $result = $ret[0];
+
+               $this->assertNotInternalType( "bool", $result );
+               $a = $result["login"]["result"];
+
+               $this->assertEquals( "WrongPass", $a );
+       }
+
+       public function testApiLoginGoodPass() {
+               global $wgServer;
+
+               if ( !isset( $wgServer ) ) {
+                       $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
+               }
+
+               $user = self::$users['sysop'];
+               $user->user->logOut();
+
+               $ret = $this->doApiRequest( array(
+                               "action" => "login",
+                               "lgname" => $user->username,
+                               "lgpassword" => $user->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" => $user->username,
+                               "lgpassword" => $user->password,
+                       ),
+                       $ret[2]
+               );
+
+               $result = $ret[0];
+
+               $this->assertNotInternalType( "bool", $result );
+               $a = $result["login"]["result"];
+
+               $this->assertEquals( "Success", $a );
+       }
+
+       /**
+        * @group Broken
+        */
+       public function testApiLoginGotCookie() {
+               $this->markTestIncomplete( "The server can't do external HTTP requests, and the internal one won't give cookies" );
+
+               global $wgServer, $wgScriptPath;
+
+               if ( !isset( $wgServer ) ) {
+                       $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
+               }
+               $user = self::$users['sysop'];
+
+               $req = MWHttpRequest::factory( self::$apiUrl . "?action=login&format=xml",
+                       array( "method" => "POST",
+                               "postData" => array(
+                                       "lgname" => $user->username,
+                                       "lgpassword" => $user->password
+                               )
+                       )
+               );
+               $req->execute();
+
+               libxml_use_internal_errors( true );
+               $sxe = simplexml_load_string( $req->getContent() );
+               $this->assertNotInternalType( "bool", $sxe );
+               $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
+               $this->assertNotInternalType( "null", $sxe->login[0] );
+
+               $a = $sxe->login[0]->attributes()->result[0];
+               $this->assertEquals( ' result="NeedToken"', $a->asXML() );
+               $token = (string)$sxe->login[0]->attributes()->token;
+
+               $req->setData( array(
+                       "lgtoken" => $token,
+                       "lgname" => $user->username,
+                       "lgpassword" => $user->password ) );
+               $req->execute();
+
+               $cj = $req->getCookieJar();
+               $serverName = parse_url( $wgServer, PHP_URL_HOST );
+               $this->assertNotEquals( false, $serverName );
+               $serializedCookie = $cj->serializeToHttpRequest( $wgScriptPath, $serverName );
+               $this->assertNotEquals( '', $serializedCookie );
+               $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . $user->userName . '; .*Token=/', $serializedCookie );
+       }
+
+       public function testRunLogin() {
+               $sysopUser = self::$users['sysop'];
+               $data = $this->doApiRequest( array(
+                       'action' => 'login',
+                       'lgname' => $sysopUser->username,
+                       'lgpassword' => $sysopUser->password ) );
+
+               $this->assertArrayHasKey( "login", $data[0] );
+               $this->assertArrayHasKey( "result", $data[0]['login'] );
+               $this->assertEquals( "NeedToken", $data[0]['login']['result'] );
+               $token = $data[0]['login']['token'];
+
+               $data = $this->doApiRequest( array(
+                       'action' => 'login',
+                       "lgtoken" => $token,
+                       "lgname" => $sysopUser->username,
+                       "lgpassword" => $sysopUser->password ), $data[2] );
+
+               $this->assertArrayHasKey( "login", $data[0] );
+               $this->assertArrayHasKey( "result", $data[0]['login'] );
+               $this->assertEquals( "Success", $data[0]['login']['result'] );
+               $this->assertArrayHasKey( 'lgtoken', $data[0]['login'] );
+       }
+
+}
diff --git a/tests/phpunit/includes/api/ApiMainTest.php b/tests/phpunit/includes/api/ApiMainTest.php
new file mode 100644 (file)
index 0000000..4ed5aa9
--- /dev/null
@@ -0,0 +1,33 @@
+<?php
+
+/**
+ * @group API
+ * @group Database
+ * @group medium
+ *
+ * @covers ApiMain
+ */
+class ApiMainTest extends ApiTestCase {
+
+       /**
+        * Test that the API will accept a FauxRequest and execute. The help action
+        * (default) throws a UsageException. Just validate we're getting proper XML
+        *
+        * @expectedException UsageException
+        */
+       public function testApi() {
+               $api = new ApiMain(
+                       new FauxRequest( array( 'action' => 'help', 'format' => 'xml' ) )
+               );
+               $api->execute();
+               $api->getPrinter()->setBufferResult( true );
+               $api->printResult( false );
+               $resp = $api->getPrinter()->getBuffer();
+
+               libxml_use_internal_errors( true );
+               $sxe = simplexml_load_string( $resp );
+               $this->assertNotInternalType( "bool", $sxe );
+               $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
+       }
+
+}
diff --git a/tests/phpunit/includes/api/ApiTest.php b/tests/phpunit/includes/api/ApiTest.php
deleted file mode 100644 (file)
index 472f8c4..0000000
+++ /dev/null
@@ -1,259 +0,0 @@
-<?php
-
-/**
- * @group API
- * @group Database
- * @group medium
- */
-class ApiTest extends ApiTestCase {
-
-       public function testRequireOnlyOneParameterDefault() {
-               $mock = new MockApi();
-
-               $this->assertEquals(
-                       null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
-                       "enablechunks" => false ), "filename", "enablechunks" ) );
-       }
-
-       /**
-        * @expectedException UsageException
-        */
-       public function testRequireOnlyOneParameterZero() {
-               $mock = new MockApi();
-
-               $this->assertEquals(
-                       null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
-                       "enablechunks" => 0 ), "filename", "enablechunks" ) );
-       }
-
-       /**
-        * @expectedException UsageException
-        */
-       public function testRequireOnlyOneParameterTrue() {
-               $mock = new MockApi();
-
-               $this->assertEquals(
-                       null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
-                       "enablechunks" => true ), "filename", "enablechunks" ) );
-       }
-
-       /**
-        * Test that the API will accept a FauxRequest and execute. The help action
-        * (default) throws a UsageException. Just validate we're getting proper XML
-        *
-        * @expectedException UsageException
-        */
-       public function testApi() {
-               $api = new ApiMain(
-                       new FauxRequest( array( 'action' => 'help', 'format' => 'xml' ) )
-               );
-               $api->execute();
-               $api->getPrinter()->setBufferResult( true );
-               $api->printResult( false );
-               $resp = $api->getPrinter()->getBuffer();
-
-               libxml_use_internal_errors( true );
-               $sxe = simplexml_load_string( $resp );
-               $this->assertNotInternalType( "bool", $sxe );
-               $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
-       }
-
-       /**
-        * Test result of attempted login with an empty username
-        */
-       public function testApiLoginNoName() {
-               $data = $this->doApiRequest( array( 'action' => 'login',
-                       'lgname' => '', 'lgpassword' => self::$users['sysop']->password,
-               ) );
-               $this->assertEquals( 'NoName', $data[0]['login']['result'] );
-       }
-
-       public function testApiLoginBadPass() {
-               global $wgServer;
-
-               $user = self::$users['sysop'];
-               $user->user->logOut();
-
-               if ( !isset( $wgServer ) ) {
-                       $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
-               }
-               $ret = $this->doApiRequest( array(
-                       "action" => "login",
-                       "lgname" => $user->username,
-                       "lgpassword" => "bad",
-               ) );
-
-               $result = $ret[0];
-
-               $this->assertNotInternalType( "bool", $result );
-               $a = $result["login"]["result"];
-               $this->assertEquals( "NeedToken", $a );
-
-               $token = $result["login"]["token"];
-
-               $ret = $this->doApiRequest(
-                       array(
-                               "action" => "login",
-                               "lgtoken" => $token,
-                               "lgname" => $user->username,
-                               "lgpassword" => "badnowayinhell",
-                       ),
-                       $ret[2]
-               );
-
-               $result = $ret[0];
-
-               $this->assertNotInternalType( "bool", $result );
-               $a = $result["login"]["result"];
-
-               $this->assertEquals( "WrongPass", $a );
-       }
-
-       public function testApiLoginGoodPass() {
-               global $wgServer;
-
-               if ( !isset( $wgServer ) ) {
-                       $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
-               }
-
-               $user = self::$users['sysop'];
-               $user->user->logOut();
-
-               $ret = $this->doApiRequest( array(
-                               "action" => "login",
-                               "lgname" => $user->username,
-                               "lgpassword" => $user->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" => $user->username,
-                               "lgpassword" => $user->password,
-                       ),
-                       $ret[2]
-               );
-
-               $result = $ret[0];
-
-               $this->assertNotInternalType( "bool", $result );
-               $a = $result["login"]["result"];
-
-               $this->assertEquals( "Success", $a );
-       }
-
-       /**
-        * @group Broken
-        */
-       public function testApiGotCookie() {
-               $this->markTestIncomplete( "The server can't do external HTTP requests, and the internal one won't give cookies" );
-
-               global $wgServer, $wgScriptPath;
-
-               if ( !isset( $wgServer ) ) {
-                       $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
-               }
-               $user = self::$users['sysop'];
-
-               $req = MWHttpRequest::factory( self::$apiUrl . "?action=login&format=xml",
-                       array( "method" => "POST",
-                               "postData" => array(
-                                       "lgname" => $user->username,
-                                       "lgpassword" => $user->password
-                               )
-                       )
-               );
-               $req->execute();
-
-               libxml_use_internal_errors( true );
-               $sxe = simplexml_load_string( $req->getContent() );
-               $this->assertNotInternalType( "bool", $sxe );
-               $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
-               $this->assertNotInternalType( "null", $sxe->login[0] );
-
-               $a = $sxe->login[0]->attributes()->result[0];
-               $this->assertEquals( ' result="NeedToken"', $a->asXML() );
-               $token = (string)$sxe->login[0]->attributes()->token;
-
-               $req->setData( array(
-                       "lgtoken" => $token,
-                       "lgname" => $user->username,
-                       "lgpassword" => $user->password ) );
-               $req->execute();
-
-               $cj = $req->getCookieJar();
-               $serverName = parse_url( $wgServer, PHP_URL_HOST );
-               $this->assertNotEquals( false, $serverName );
-               $serializedCookie = $cj->serializeToHttpRequest( $wgScriptPath, $serverName );
-               $this->assertNotEquals( '', $serializedCookie );
-               $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . $user->userName . '; .*Token=/', $serializedCookie );
-
-               return $cj;
-       }
-
-       public function testRunLogin() {
-               $sysopUser = self::$users['sysop'];
-               $data = $this->doApiRequest( array(
-                       'action' => 'login',
-                       'lgname' => $sysopUser->username,
-                       'lgpassword' => $sysopUser->password ) );
-
-               $this->assertArrayHasKey( "login", $data[0] );
-               $this->assertArrayHasKey( "result", $data[0]['login'] );
-               $this->assertEquals( "NeedToken", $data[0]['login']['result'] );
-               $token = $data[0]['login']['token'];
-
-               $data = $this->doApiRequest( array(
-                       'action' => 'login',
-                       "lgtoken" => $token,
-                       "lgname" => $sysopUser->username,
-                       "lgpassword" => $sysopUser->password ), $data[2] );
-
-               $this->assertArrayHasKey( "login", $data[0] );
-               $this->assertArrayHasKey( "result", $data[0]['login'] );
-               $this->assertEquals( "Success", $data[0]['login']['result'] );
-               $this->assertArrayHasKey( 'lgtoken', $data[0]['login'] );
-
-               return $data;
-       }
-
-       public function testGettingToken() {
-               foreach ( self::$users as $user ) {
-                       $this->runTokenTest( $user );
-               }
-       }
-
-       function runTokenTest( $user ) {
-               $tokens = $this->getTokenList( $user );
-
-               $rights = $user->user->getRights();
-
-               $this->assertArrayHasKey( 'edittoken', $tokens );
-               $this->assertArrayHasKey( 'movetoken', $tokens );
-
-               if ( isset( $rights['delete'] ) ) {
-                       $this->assertArrayHasKey( 'deletetoken', $tokens );
-               }
-
-               if ( isset( $rights['block'] ) ) {
-                       $this->assertArrayHasKey( 'blocktoken', $tokens );
-                       $this->assertArrayHasKey( 'unblocktoken', $tokens );
-               }
-
-               if ( isset( $rights['protect'] ) ) {
-                       $this->assertArrayHasKey( 'protecttoken', $tokens );
-               }
-
-               return $tokens;
-       }
-}
diff --git a/tests/phpunit/includes/api/ApiTokensTest.php b/tests/phpunit/includes/api/ApiTokensTest.php
new file mode 100644 (file)
index 0000000..fbe9789
--- /dev/null
@@ -0,0 +1,40 @@
+<?php
+
+/**
+ * @group API
+ * @group Database
+ * @group medium
+ *
+ * @covers ApiTokens
+ */
+class ApiTokensTest extends ApiTestCase {
+
+       public function testGettingToken() {
+               foreach ( self::$users as $user ) {
+                       $this->runTokenTest( $user );
+               }
+       }
+
+       protected function runTokenTest( $user ) {
+               $tokens = $this->getTokenList( $user );
+
+               $rights = $user->user->getRights();
+
+               $this->assertArrayHasKey( 'edittoken', $tokens );
+               $this->assertArrayHasKey( 'movetoken', $tokens );
+
+               if ( isset( $rights['delete'] ) ) {
+                       $this->assertArrayHasKey( 'deletetoken', $tokens );
+               }
+
+               if ( isset( $rights['block'] ) ) {
+                       $this->assertArrayHasKey( 'blocktoken', $tokens );
+                       $this->assertArrayHasKey( 'unblocktoken', $tokens );
+               }
+
+               if ( isset( $rights['protect'] ) ) {
+                       $this->assertArrayHasKey( 'protecttoken', $tokens );
+               }
+       }
+
+}