From cd1044a35d49aea86a031afe16a5bcc059d31e65 Mon Sep 17 00:00:00 2001 From: Chad Horohoe Date: Wed, 29 Sep 2010 18:19:03 +0000 Subject: [PATCH] Move doApiRequest() up a level so ApiTest can use it as well. Fix first two tests to do internal FauxRequests rather than relying on an external HTTP connection --- .../phpunit/includes/UploadFromUrlTest.php | 2 +- .../tests/phpunit/includes/api/ApiSetup.php | 14 +++++++ .../tests/phpunit/includes/api/ApiTest.php | 42 +++++++++---------- .../phpunit/includes/api/ApiWatchTest.php | 14 ------- 4 files changed, 35 insertions(+), 37 deletions(-) diff --git a/maintenance/tests/phpunit/includes/UploadFromUrlTest.php b/maintenance/tests/phpunit/includes/UploadFromUrlTest.php index 7bcb5fb5b2..c636010345 100644 --- a/maintenance/tests/phpunit/includes/UploadFromUrlTest.php +++ b/maintenance/tests/phpunit/includes/UploadFromUrlTest.php @@ -25,7 +25,7 @@ class UploadFromUrlTest extends ApiTestSetup { } } - protected function doApiRequest( $params ) { + protected function doApiRequest( $params, $unused = null ) { $sessionId = session_id(); session_write_close(); diff --git a/maintenance/tests/phpunit/includes/api/ApiSetup.php b/maintenance/tests/phpunit/includes/api/ApiSetup.php index 5e108d2410..88960dba57 100644 --- a/maintenance/tests/phpunit/includes/api/ApiSetup.php +++ b/maintenance/tests/phpunit/includes/api/ApiSetup.php @@ -18,6 +18,20 @@ abstract class ApiTestSetup extends PHPUnit_Framework_TestCase { self::setupUser(); } + protected function doApiRequest( $params, $data = null ) { + $_SESSION = isset( $data[2] ) ? $data[2] : array(); + + $req = new FauxRequest( $params, true, $_SESSION ); + $module = new ApiMain( $req, true ); + $module->execute(); + + $data[0] = $module->getResultData(); + $data[1] = $req; + $data[2] = $_SESSION; + + return $data; + } + static function setupUser() { if ( self::$user == NULL ) { self::$userName = "Useruser"; diff --git a/maintenance/tests/phpunit/includes/api/ApiTest.php b/maintenance/tests/phpunit/includes/api/ApiTest.php index 9ee931f9cb..b520032f99 100644 --- a/maintenance/tests/phpunit/includes/api/ApiTest.php +++ b/maintenance/tests/phpunit/includes/api/ApiTest.php @@ -53,14 +53,20 @@ class ApiTest extends ApiTestSetup { "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 + */ function testApi() { - global $wgServer; - - if ( !isset( $wgServer ) ) { - $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' ); - } - /* Haven't thought about test ordering yet -- but this depends on HttpTest.php */ - $resp = Http::get( self::$apiUrl . "?format=xml" ); + $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 ); @@ -68,22 +74,14 @@ class ApiTest extends ApiTestSetup { $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) ); } + /** + * Test result of attempted login with an empty username + */ function testApiLoginNoName() { - global $wgServer; - - if ( !isset( $wgServer ) ) { - $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' ); - } - $resp = Http::post( self::$apiUrl . "?action=login&format=xml", - array( "postData" => array( - "lgname" => "", - "lgpassword" => self::$passWord ) ) ); - libxml_use_internal_errors( true ); - $sxe = simplexml_load_string( $resp ); - $this->assertNotType( "bool", $sxe ); - $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) ); - $a = $sxe->login[0]->attributes()->result; - $this->assertEquals( ' result="NoName"', $a->asXML() ); + $data = $this->doApiRequest( array( 'action' => 'login', + 'lgname' => '', 'lgpassword' => self::$passWord + ) ); + $this->assertEquals( 'NoName', $data[0]['login']['result'] ); } function testApiLoginBadPass() { diff --git a/maintenance/tests/phpunit/includes/api/ApiWatchTest.php b/maintenance/tests/phpunit/includes/api/ApiWatchTest.php index 7477a8944e..032e24400c 100644 --- a/maintenance/tests/phpunit/includes/api/ApiWatchTest.php +++ b/maintenance/tests/phpunit/includes/api/ApiWatchTest.php @@ -8,20 +8,6 @@ class ApiWatchTest extends ApiTestSetup { parent::setUp(); } - function doApiRequest( $params, $data = null ) { - $_SESSION = isset( $data[2] ) ? $data[2] : array(); - - $req = new FauxRequest( $params, true, $_SESSION ); - $module = new ApiMain( $req, true ); - $module->execute(); - - $data[0] = $module->getResultData(); - $data[1] = $req; - $data[2] = $_SESSION; - - return $data; - } - function testLogin() { $data = $this->doApiRequest( array( 'action' => 'login', -- 2.20.1