From ad6875383b4be396fb4ffd51e7a5771de49c61f8 Mon Sep 17 00:00:00 2001 From: X! Date: Sun, 2 Jan 2011 05:52:00 +0000 Subject: [PATCH] Refactor much of the API testing code. Old: ApiWatchTest not only tested the watch module, but also tested the login module, and the login module test was also used to log in to the API for the other tests. New: ApiWatchTest only contains watch-specific methods. The login testing has been moved to ApiTest. The code used to log in and get tokens have been moved to ApiSetup. Nice, organized, and much nicer than before. ApiUploadTest is still pretty fugly, though. --- tests/phpunit/includes/api/ApiSetup.php | 28 ++++ tests/phpunit/includes/api/ApiTest.php | 63 +++++++++ tests/phpunit/includes/api/ApiUploadTest.php | 2 + tests/phpunit/includes/api/ApiWatchTest.php | 129 +++++-------------- 4 files changed, 127 insertions(+), 95 deletions(-) diff --git a/tests/phpunit/includes/api/ApiSetup.php b/tests/phpunit/includes/api/ApiSetup.php index aa5901ed2c..f7b6f3fe1e 100644 --- a/tests/phpunit/includes/api/ApiSetup.php +++ b/tests/phpunit/includes/api/ApiSetup.php @@ -39,6 +39,34 @@ abstract class ApiTestSetup extends MediaWikiTestCase { $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 { diff --git a/tests/phpunit/includes/api/ApiTest.php b/tests/phpunit/includes/api/ApiTest.php index 9d38852e4a..d7133da30d 100644 --- a/tests/phpunit/includes/api/ApiTest.php +++ b/tests/phpunit/includes/api/ApiTest.php @@ -58,6 +58,7 @@ class ApiTest extends ApiTestSetup { * @expectedException UsageException */ function testApi() { + $api = new ApiMain( new FauxRequest( array( 'action' => 'help', 'format' => 'xml' ) ) ); @@ -222,4 +223,66 @@ class ApiTest extends ApiTestSetup { $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) ); $a = $sxe->query[0]->pages[0]->page[0]->attributes(); } + + function testRunLogin() { + $data = $this->doApiRequest( array( + 'action' => 'login', + 'lgname' => $this->sysopUser->userName, + 'lgpassword' => $this->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" => $this->sysopUser->userName, + "lgpassword" => $this->sysopUser->password ), $data ); + + $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; + } + + function testGettingToken() { + foreach ( array( $this->user, $this->sysopUser ) as $user ) { + $this->runTokenTest( $user ); + } + } + + function runTokenTest( $user ) { + + $data = $this->getTokenList( $user ); + + $this->assertArrayHasKey( 'query', $data[0] ); + $this->assertArrayHasKey( 'pages', $data[0]['query'] ); + $keys = array_keys( $data[0]['query']['pages'] ); + $key = array_pop( $keys ); + + $rights = $user->user->getRights(); + + $this->assertArrayHasKey( $key, $data[0]['query']['pages'] ); + $this->assertArrayHasKey( 'edittoken', $data[0]['query']['pages'][$key] ); + $this->assertArrayHasKey( 'movetoken', $data[0]['query']['pages'][$key] ); + + if ( isset( $rights['delete'] ) ) { + $this->assertArrayHasKey( 'deletetoken', $data[0]['query']['pages'][$key] ); + } + + if ( isset( $rights['block'] ) ) { + $this->assertArrayHasKey( 'blocktoken', $data[0]['query']['pages'][$key] ); + $this->assertArrayHasKey( 'unblocktoken', $data[0]['query']['pages'][$key] ); + } + + if ( isset( $rights['protect'] ) ) { + $this->assertArrayHasKey( 'protecttoken', $data[0]['query']['pages'][$key] ); + } + + return $data; + } } diff --git a/tests/phpunit/includes/api/ApiUploadTest.php b/tests/phpunit/includes/api/ApiUploadTest.php index b33a5d9c83..f537a10c4a 100644 --- a/tests/phpunit/includes/api/ApiUploadTest.php +++ b/tests/phpunit/includes/api/ApiUploadTest.php @@ -139,6 +139,8 @@ abstract class ApiTestCase extends MediaWikiTestCase { /** * @group Database * @group Destructive + * + * This is pretty sucky... needs to be prettified. */ class ApiUploadTest extends ApiTestCase { /** diff --git a/tests/phpunit/includes/api/ApiWatchTest.php b/tests/phpunit/includes/api/ApiWatchTest.php index 32e490811a..ea8c43816f 100644 --- a/tests/phpunit/includes/api/ApiWatchTest.php +++ b/tests/phpunit/includes/api/ApiWatchTest.php @@ -10,82 +10,17 @@ class ApiWatchTest extends ApiTestSetup { function setUp() { parent::setUp(); + $this->doLogin(); } - - function testLogin() { - $data = $this->doApiRequest( array( - 'action' => 'login', - 'lgname' => $this->sysopUser->userName, - 'lgpassword' => $this->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" => $this->sysopUser->userName, - "lgpassword" => $this->sysopUser->password ), $data ); - - $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; - } - - function testGettingToken() { - foreach ( array( $this->user, $this->sysopUser ) as $user ) { - $this->getUserTokens( $user ); - } - } - - function getUserTokens( $user ) { - $GLOBALS['wgUser'] = $user->user; - $data = $this->doApiRequest( array( - 'action' => 'query', - 'titles' => 'Main Page', - 'intoken' => 'edit|delete|protect|move|block|unblock', - 'prop' => 'info' ) ); - - $this->assertArrayHasKey( 'query', $data[0] ); - $this->assertArrayHasKey( 'pages', $data[0]['query'] ); - $keys = array_keys( $data[0]['query']['pages'] ); - $key = array_pop( $keys ); - - $rights = $user->user->getRights(); - - $this->assertArrayHasKey( $key, $data[0]['query']['pages'] ); - $this->assertArrayHasKey( 'edittoken', $data[0]['query']['pages'][$key] ); - $this->assertArrayHasKey( 'movetoken', $data[0]['query']['pages'][$key] ); - - if ( isset( $rights['delete'] ) ) { - $this->assertArrayHasKey( 'deletetoken', $data[0]['query']['pages'][$key] ); - } - - if ( isset( $rights['block'] ) ) { - $this->assertArrayHasKey( 'blocktoken', $data[0]['query']['pages'][$key] ); - $this->assertArrayHasKey( 'unblocktoken', $data[0]['query']['pages'][$key] ); - } - - if ( isset( $rights['protect'] ) ) { - $this->assertArrayHasKey( 'protecttoken', $data[0]['query']['pages'][$key] ); - } - - return $data; + + function getTokens() { + return $this->getTokenList( $this->sysopUser ); } - function testGetToken() { - return $this->getUserTokens( $this->sysopUser ); - } - - /** - * @depends testGetToken - */ - function testWatchEdit( $data ) { + function testWatchEdit() { + + $data = $this->getTokens(); + $this->markTestIncomplete( "Broken" ); $keys = array_keys( $data[0]['query']['pages'] ); $key = array_pop( $keys ); @@ -93,7 +28,7 @@ class ApiWatchTest extends ApiTestSetup { $data = $this->doApiRequest( array( 'action' => 'edit', - 'title' => 'Main Page', + 'title' => 'UTPage', 'text' => 'new text', 'token' => $pageinfo['edittoken'], 'watchlist' => 'watch' ), $data ); @@ -107,7 +42,8 @@ class ApiWatchTest extends ApiTestSetup { /** * @depends testWatchEdit */ - function testWatchClear( $data ) { + function testWatchClear() { + $data = $this->doApiRequest( array( 'action' => 'query', 'list' => 'watchlist' ), $data ); @@ -132,10 +68,11 @@ class ApiWatchTest extends ApiTestSetup { return $data; } - /** - * @depends testGetToken - */ - function testWatchProtect( $data ) { + + function testWatchProtect() { + + $data = $this->getTokens(); + $this->markTestIncomplete( "Broken" ); $keys = array_keys( $data[0]['query']['pages'] ); $key = array_pop( $keys ); @@ -144,7 +81,7 @@ class ApiWatchTest extends ApiTestSetup { $data = $this->doApiRequest( array( 'action' => 'protect', 'token' => $pageinfo['protecttoken'], - 'title' => 'Main Page', + 'title' => 'UTPage', 'protections' => 'edit=sysop', 'watchlist' => 'unwatch' ), $data ); @@ -154,18 +91,19 @@ class ApiWatchTest extends ApiTestSetup { $this->assertArrayHasKey( 'edit', $data[0]['protect']['protections'][0] ); } - /** - * @depends testGetToken - */ - function testGetRollbackToken( $data ) { - if ( !Title::newFromText( 'Main Page' )->exists() ) { - $this->markTestIncomplete( "The article [[Main Page]] does not exist" ); + + function testGetRollbackToken() { + + $data = $this->getTokens(); + + if ( !Title::newFromText( 'UTPage' )->exists() ) { + $this->markTestIncomplete( "The article [[UTPage]] does not exist" ); } $data = $this->doApiRequest( array( 'action' => 'query', 'prop' => 'revisions', - 'titles' => 'Main Page', + 'titles' => 'UTPage', 'rvtoken' => 'rollback' ), $data ); $this->assertArrayHasKey( 'query', $data[0] ); @@ -174,7 +112,7 @@ class ApiWatchTest extends ApiTestSetup { $key = array_pop( $keys ); if ( isset( $data[0]['query']['pages'][$key]['missing'] ) ) { - $this->markTestIncomplete( "Target page (Main Page) doesn't exist" ); + $this->markTestIncomplete( "Target page (UTPage) doesn't exist" ); } $this->assertArrayHasKey( 'pageid', $data[0]['query']['pages'][$key] ); @@ -196,13 +134,13 @@ class ApiWatchTest extends ApiTestSetup { try { $data = $this->doApiRequest( array( 'action' => 'rollback', - 'title' => 'Main Page', + 'title' => 'UTPage', 'user' => $pageinfo['user'], 'token' => $pageinfo['rollbacktoken'], 'watchlist' => 'watch' ), $data ); } catch( UsageException $ue ) { if( $ue->getCodeString() == 'onlyauthor' ) { - $this->markTestIncomplete( "Only one author to 'Main Page', cannot test rollback" ); + $this->markTestIncomplete( "Only one author to 'UTPage', cannot test rollback" ); } else { $this->fail( "Received error " . $ue->getCodeString() ); } @@ -212,10 +150,11 @@ class ApiWatchTest extends ApiTestSetup { $this->assertArrayHasKey( 'title', $data[0]['rollback'] ); } - /** - * @depends testGetToken - */ - function testWatchDelete( $data ) { + + function testWatchDelete() { + + $data = $this->getTokens(); + $this->markTestIncomplete( "Broken" ); $keys = array_keys( $data[0]['query']['pages'] ); $key = array_pop( $keys ); @@ -224,7 +163,7 @@ class ApiWatchTest extends ApiTestSetup { $data = $this->doApiRequest( array( 'action' => 'delete', 'token' => $pageinfo['deletetoken'], - 'title' => 'Main Page' ), $data ); + 'title' => 'UTPage' ), $data ); $this->assertArrayHasKey( 'delete', $data[0] ); $this->assertArrayHasKey( 'title', $data[0]['delete'] ); -- 2.20.1