From 85a5f0eb8541736b9ccc84fce1a3bbf1c4893bb5 Mon Sep 17 00:00:00 2001 From: Chad Horohoe Date: Fri, 1 Jul 2011 16:34:02 +0000 Subject: [PATCH] Merge ApiTestSetup into ApiTestCase and update all subclasses. The amount of duplication here was nasty, and also lets us get rid of a bunch of useless require_once()s. ./phpunit.php --filter Api currently gives me: Tests: 24, Assertions: 107, Incomplete: 1, Skipped: 2. --- tests/TestsAutoLoader.php | 4 +- tests/phpunit/includes/api/ApiBlockTest.php | 6 +- tests/phpunit/includes/api/ApiPurgeTest.php | 4 +- tests/phpunit/includes/api/ApiQueryTest.php | 4 +- tests/phpunit/includes/api/ApiSetup.php | 96 ------------------- tests/phpunit/includes/api/ApiTest.php | 58 +++++------ tests/phpunit/includes/api/ApiTestCase.php | 79 ++++++++++++++- tests/phpunit/includes/api/ApiWatchTest.php | 6 +- .../includes/api/format/ApiFormatPhpTest.php | 9 -- .../includes/api/format/ApiFormatTestBase.php | 13 +-- .../includes/upload/UploadFromUrlTest.php | 4 +- 11 files changed, 113 insertions(+), 170 deletions(-) delete mode 100644 tests/phpunit/includes/api/ApiSetup.php diff --git a/tests/TestsAutoLoader.php b/tests/TestsAutoLoader.php index 4f47c98642..cccf7bf25b 100644 --- a/tests/TestsAutoLoader.php +++ b/tests/TestsAutoLoader.php @@ -14,10 +14,12 @@ $wgAutoloadClasses += array( 'BlockTest' => "$testFolder/phpunit/includes/BlockTest.php", //API - 'ApiTestSetup' => "$testFolder/phpunit/includes/api/ApiSetup.php", + 'ApiFormatTestBase' => "$testFolder/phpunit/includes/api/format/ApiFormatTestBase.php", 'ApiTestCase' => "$testFolder/phpunit/includes/api/ApiTestCase.php", 'ApiTestUser' => "$testFolder/phpunit/includes/api/ApiTestUser.php", + 'MockApi' => "$testFolder/phpunit/includes/api/ApiTestCase.php", 'RandomImageGenerator' => "$testFolder/phpunit/includes/api/RandomImageGenerator.php", + 'UserWrapper' => "$testFolder/phpunit/includes/api/ApiTestCase.php", //Parser 'ParserTestFileIterator' => "$testFolder/phpunit/includes/parser/NewParserHelpers.php", diff --git a/tests/phpunit/includes/api/ApiBlockTest.php b/tests/phpunit/includes/api/ApiBlockTest.php index 11953d7faa..325a30f1fb 100644 --- a/tests/phpunit/includes/api/ApiBlockTest.php +++ b/tests/phpunit/includes/api/ApiBlockTest.php @@ -1,12 +1,10 @@ getTokenList( $this->sysopUser ); + return $this->getTokenList( self::$users['sysop'] ); } function addDBData() { diff --git a/tests/phpunit/includes/api/ApiPurgeTest.php b/tests/phpunit/includes/api/ApiPurgeTest.php index 578f6d85a4..db1563e9a9 100644 --- a/tests/phpunit/includes/api/ApiPurgeTest.php +++ b/tests/phpunit/includes/api/ApiPurgeTest.php @@ -1,11 +1,9 @@ setupUser(); - } - - protected function doApiRequest( $params, $data = null, $appendModule = false ) { - $_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; - - if( $appendModule ) $data[3] = $module; - - return $data; - } - - function setupUser() { - if ( $this->user == null || $this->sysopUser == null ) { - $this->user = new UserWrapper( 'User for MediaWiki automated tests', User::randomPassword() ); - $this->sysopUser = new UserWrapper( 'Sysop for MediaWiki automated tests', User::randomPassword(), 'sysop' ); - } - - $GLOBALS['wgUser'] = $this->sysopUser->user; - } - - function doLogin() { - $data = $this->doApiRequest( array( - 'action' => 'login', - 'lgname' => $this->sysopUser->userName, - 'lgpassword' => $this->sysopUser->password ) ); - - $token = $data[0]['login']['token']; - - $data = $this->doApiRequest( array( - 'action' => 'login', - "lgtoken" => $token, - "lgname" => $this->sysopUser->userName, - "lgpassword" => $this->sysopUser->password ), $data ); - - return $data; - } - - function getTokenList( $user ) { - $GLOBALS['wgUser'] = $user->user; - $data = $this->doApiRequest( array( - 'action' => 'query', - 'titles' => 'Main Page', - 'intoken' => 'edit|delete|protect|move|block|unblock', - 'prop' => 'info' ) ); - return $data; - } - -} - -class UserWrapper { - public $userName, $password, $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(); - } -} - diff --git a/tests/phpunit/includes/api/ApiTest.php b/tests/phpunit/includes/api/ApiTest.php index d7133da30d..e2215ac447 100644 --- a/tests/phpunit/includes/api/ApiTest.php +++ b/tests/phpunit/includes/api/ApiTest.php @@ -1,25 +1,10 @@ null, - 'enablechunks' => false, - 'sessionkey' => null, - ); - } -} - /** * @group Database * @group Destructive */ -class ApiTest extends ApiTestSetup { +class ApiTest extends ApiTestCase { function testRequireOnlyOneParameterDefault() { $mock = new MockApi(); @@ -78,7 +63,7 @@ class ApiTest extends ApiTestSetup { */ function testApiLoginNoName() { $data = $this->doApiRequest( array( 'action' => 'login', - 'lgname' => '', 'lgpassword' => $this->user->password, + 'lgname' => '', 'lgpassword' => self::$users['sysop']->password, ) ); $this->assertEquals( 'NoName', $data[0]['login']['result'] ); } @@ -86,14 +71,15 @@ class ApiTest extends ApiTestSetup { function testApiLoginBadPass() { global $wgServer; - $user = $this->user; + $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, + "lgname" => $user->username, "lgpassword" => "bad", ) ); @@ -109,8 +95,8 @@ class ApiTest extends ApiTestSetup { $ret = $this->doApiRequest( array( "action" => "login", "lgtoken" => $token, - "lgname" => $user->userName, - "lgpassword" => "bad", + "lgname" => $user->username, + "lgpassword" => "badnowayinhell", ) ); @@ -129,11 +115,12 @@ class ApiTest extends ApiTestSetup { $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' ); } - $user = $this->user; + $user = self::$users['sysop']; + $user->user->logOut(); $ret = $this->doApiRequest( array( "action" => "login", - "lgname" => $user->userName, + "lgname" => $user->username, "lgpassword" => $user->password, ) ); @@ -149,7 +136,7 @@ class ApiTest extends ApiTestSetup { $ret = $this->doApiRequest( array( "action" => "login", "lgtoken" => $token, - "lgname" => $user->userName, + "lgname" => $user->username, "lgpassword" => $user->password, ) ); @@ -170,11 +157,13 @@ class ApiTest extends ApiTestSetup { 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" => $this->user->userName, - "lgpassword" => $this->user->password ) ) ); + "lgname" => $user->username, + "lgpassword" => $user->password ) ) ); $req->execute(); libxml_use_internal_errors( true ); @@ -189,8 +178,8 @@ class ApiTest extends ApiTestSetup { $req->setData( array( "lgtoken" => $token, - "lgname" => $this->user->userName, - "lgpassword" => $this->user->password ) ); + "lgname" => $user->username, + "lgpassword" => $user->password ) ); $req->execute(); $cj = $req->getCookieJar(); @@ -198,7 +187,7 @@ class ApiTest extends ApiTestSetup { $this->assertNotEquals( false, $serverName ); $serializedCookie = $cj->serializeToHttpRequest( $wgScriptPath, $serverName ); $this->assertNotEquals( '', $serializedCookie ); - $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . $this->user->userName . '; .*Token=/', $serializedCookie ); + $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . $user->userName . '; .*Token=/', $serializedCookie ); return $cj; } @@ -225,10 +214,11 @@ class ApiTest extends ApiTestSetup { } function testRunLogin() { + $sysopUser = self::$users['sysop']; $data = $this->doApiRequest( array( 'action' => 'login', - 'lgname' => $this->sysopUser->userName, - 'lgpassword' => $this->sysopUser->password ) ); + 'lgname' => $sysopUser->username, + 'lgpassword' => $sysopUser->password ) ); $this->assertArrayHasKey( "login", $data[0] ); $this->assertArrayHasKey( "result", $data[0]['login'] ); @@ -238,8 +228,8 @@ class ApiTest extends ApiTestSetup { $data = $this->doApiRequest( array( 'action' => 'login', "lgtoken" => $token, - "lgname" => $this->sysopUser->userName, - "lgpassword" => $this->sysopUser->password ), $data ); + "lgname" => $sysopUser->username, + "lgpassword" => $sysopUser->password ), $data ); $this->assertArrayHasKey( "login", $data[0] ); $this->assertArrayHasKey( "result", $data[0]['login'] ); @@ -250,7 +240,7 @@ class ApiTest extends ApiTestSetup { } function testGettingToken() { - foreach ( array( $this->user, $this->sysopUser ) as $user ) { + foreach ( self::$users as $user ) { $this->runTokenTest( $user ); } } diff --git a/tests/phpunit/includes/api/ApiTestCase.php b/tests/phpunit/includes/api/ApiTestCase.php index 54da6904b1..fab4187241 100644 --- a/tests/phpunit/includes/api/ApiTestCase.php +++ b/tests/phpunit/includes/api/ApiTestCase.php @@ -1,12 +1,17 @@ execute(); - return array( $module->getResultData(), $request, $request->getSessionArray() ); + $results = array( $module->getResultData(), $request, $request->getSessionArray() ); + if( $appendModule ) { + $results[] = $module; + } + + return $results; } /** @@ -62,4 +73,68 @@ abstract class ApiTestCase extends MediaWikiLangTestCase { } } + protected function doLogin() { + $data = $this->doApiRequest( array( + 'action' => 'login', + 'lgname' => self::$users['sysop']->username, + 'lgpassword' => self::$users['sysop']->password ) ); + + $token = $data[0]['login']['token']; + + $data = $this->doApiRequest( array( + 'action' => 'login', + 'lgtoken' => $token, + 'lgname' => self::$users['sysop']->username, + 'lgpassword' => self::$users['sysop']->password + ), $data ); + + return $data; + } + + protected function getTokenList( $user ) { + $GLOBALS['wgUser'] = $user->user; + $data = $this->doApiRequest( array( + 'action' => 'query', + 'titles' => 'Main Page', + 'intoken' => 'edit|delete|protect|move|block|unblock', + 'prop' => 'info' ) ); + return $data; + } +} + +class UserWrapper { + public $userName, $password, $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, + ); + } } diff --git a/tests/phpunit/includes/api/ApiWatchTest.php b/tests/phpunit/includes/api/ApiWatchTest.php index cd35a5b856..7fca538698 100644 --- a/tests/phpunit/includes/api/ApiWatchTest.php +++ b/tests/phpunit/includes/api/ApiWatchTest.php @@ -1,13 +1,11 @@ getTokenList( $this->sysopUser ); + return $this->getTokenList( self::$users['sysop'] ); } /** diff --git a/tests/phpunit/includes/api/format/ApiFormatPhpTest.php b/tests/phpunit/includes/api/format/ApiFormatPhpTest.php index 761857a3ba..f83ef184f2 100644 --- a/tests/phpunit/includes/api/format/ApiFormatPhpTest.php +++ b/tests/phpunit/includes/api/format/ApiFormatPhpTest.php @@ -1,19 +1,10 @@ doLogin(); - }*/ - - function testValidPHPSyntax() { $data = $this->apiRequest( 'php', array( 'action' => 'query', 'meta' => 'siteinfo' ) ); diff --git a/tests/phpunit/includes/api/format/ApiFormatTestBase.php b/tests/phpunit/includes/api/format/ApiFormatTestBase.php index 1ddbfcbe02..a0b7b02065 100644 --- a/tests/phpunit/includes/api/format/ApiFormatTestBase.php +++ b/tests/phpunit/includes/api/format/ApiFormatTestBase.php @@ -1,13 +1,9 @@ createPrinterByName( $format ); @@ -23,9 +19,4 @@ abstract class ApiFormatTestBase extends ApiTestSetup { return $out; } - - function setupUser() { - /* Do not setup a user here */ - } } - diff --git a/tests/phpunit/includes/upload/UploadFromUrlTest.php b/tests/phpunit/includes/upload/UploadFromUrlTest.php index cf4df4b86c..4722d40846 100644 --- a/tests/phpunit/includes/upload/UploadFromUrlTest.php +++ b/tests/phpunit/includes/upload/UploadFromUrlTest.php @@ -1,12 +1,10 @@