From 41c0ab2857918d9e1ace321f71fc5a06251d96fa Mon Sep 17 00:00:00 2001 From: addshore Date: Mon, 5 Aug 2013 21:31:45 +0200 Subject: [PATCH] Use action=tokens for api test tokens Change-Id: I5d517eca7529dd57e14629a1ea5dd4d50110f39f --- tests/phpunit/includes/api/ApiBlockTest.php | 10 +++----- tests/phpunit/includes/api/ApiTest.php | 22 +++++++---------- tests/phpunit/includes/api/ApiTestCase.php | 13 +++++++---- tests/phpunit/includes/api/ApiWatchTest.php | 26 +++++++-------------- 4 files changed, 28 insertions(+), 43 deletions(-) diff --git a/tests/phpunit/includes/api/ApiBlockTest.php b/tests/phpunit/includes/api/ApiBlockTest.php index a3d39dd712..3c189687f8 100644 --- a/tests/phpunit/includes/api/ApiBlockTest.php +++ b/tests/phpunit/includes/api/ApiBlockTest.php @@ -35,7 +35,7 @@ class ApiBlockTest extends ApiTestCase { * previously always considered valid (bug 34212). */ function testMakeNormalBlock() { - $data = $this->getTokens(); + $tokens = $this->getTokens(); $user = User::newFromName( 'UTApiBlockee' ); @@ -43,19 +43,15 @@ class ApiBlockTest extends ApiTestCase { $this->markTestIncomplete( "The user UTApiBlockee does not exist" ); } - if ( !isset( $data[0]['query']['pages'] ) ) { + if ( !array_key_exists( 'blocktoken', $tokens ) ) { $this->markTestIncomplete( "No block token found" ); } - $keys = array_keys( $data[0]['query']['pages'] ); - $key = array_pop( $keys ); - $pageinfo = $data[0]['query']['pages'][$key]; - $this->doApiRequest( array( 'action' => 'block', 'user' => 'UTApiBlockee', 'reason' => 'Some reason', - 'token' => $pageinfo['blocktoken'] ), null, false, self::$users['sysop']->user ); + 'token' => $tokens['blocktoken'] ), null, false, self::$users['sysop']->user ); $block = Block::newFromTarget( 'UTApiBlockee' ); diff --git a/tests/phpunit/includes/api/ApiTest.php b/tests/phpunit/includes/api/ApiTest.php index e278fd0e27..21c6247eb0 100644 --- a/tests/phpunit/includes/api/ApiTest.php +++ b/tests/phpunit/includes/api/ApiTest.php @@ -234,32 +234,26 @@ class ApiTest extends ApiTestCase { } 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 ); + $tokens = $this->getTokenList( $user ); $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] ); + $this->assertArrayHasKey( 'edittoken', $tokens ); + $this->assertArrayHasKey( 'movetoken', $tokens ); if ( isset( $rights['delete'] ) ) { - $this->assertArrayHasKey( 'deletetoken', $data[0]['query']['pages'][$key] ); + $this->assertArrayHasKey( 'deletetoken', $tokens ); } if ( isset( $rights['block'] ) ) { - $this->assertArrayHasKey( 'blocktoken', $data[0]['query']['pages'][$key] ); - $this->assertArrayHasKey( 'unblocktoken', $data[0]['query']['pages'][$key] ); + $this->assertArrayHasKey( 'blocktoken', $tokens ); + $this->assertArrayHasKey( 'unblocktoken', $tokens ); } if ( isset( $rights['protect'] ) ) { - $this->assertArrayHasKey( 'protecttoken', $data[0]['query']['pages'][$key] ); + $this->assertArrayHasKey( 'protecttoken', $tokens ); } - return $data; + return $tokens; } } diff --git a/tests/phpunit/includes/api/ApiTestCase.php b/tests/phpunit/includes/api/ApiTestCase.php index bf970da6fa..374eded7ea 100644 --- a/tests/phpunit/includes/api/ApiTestCase.php +++ b/tests/phpunit/includes/api/ApiTestCase.php @@ -166,12 +166,15 @@ abstract class ApiTestCase extends MediaWikiLangTestCase { protected function getTokenList( $user, $session = null ) { $data = $this->doApiRequest( array( - 'action' => 'query', - 'titles' => 'Main Page', - 'intoken' => 'edit|delete|protect|move|block|unblock|watch', - 'prop' => 'info' ), $session, false, $user->user ); + 'action' => 'tokens', + 'type' => 'edit|delete|protect|move|block|unblock|watch' + ), $session, false, $user->user ); - return $data; + if( !array_key_exists( 'tokens', $data[0] ) ){ + throw new MWException( 'Api failed to return a token list' ); + } + + return $data[0]['tokens']; } public function testApiTestGroup() { diff --git a/tests/phpunit/includes/api/ApiWatchTest.php b/tests/phpunit/includes/api/ApiWatchTest.php index a9bc43ae3b..78bb1515f8 100644 --- a/tests/phpunit/includes/api/ApiWatchTest.php +++ b/tests/phpunit/includes/api/ApiWatchTest.php @@ -13,25 +13,19 @@ class ApiWatchTest extends ApiTestCase { } function getTokens() { - $data = $this->getTokenList( self::$users['sysop'] ); - - $keys = array_keys( $data[0]['query']['pages'] ); - $key = array_pop( $keys ); - $pageinfo = $data[0]['query']['pages'][$key]; - - return $pageinfo; + return $this->getTokenList( self::$users['sysop'] ); } /** */ function testWatchEdit() { - $pageinfo = $this->getTokens(); + $tokens = $this->getTokens(); $data = $this->doApiRequest( array( 'action' => 'edit', 'title' => 'Help:UTPage', // Help namespace is hopefully wikitext 'text' => 'new text', - 'token' => $pageinfo['edittoken'], + 'token' => $tokens['edittoken'], 'watchlist' => 'watch' ) ); $this->assertArrayHasKey( 'edit', $data[0] ); $this->assertArrayHasKey( 'result', $data[0]['edit'] ); @@ -44,8 +38,7 @@ class ApiWatchTest extends ApiTestCase { * @depends testWatchEdit */ function testWatchClear() { - - $pageinfo = $this->getTokens(); + $tokens = $this->getTokens(); $data = $this->doApiRequest( array( 'action' => 'query', @@ -59,7 +52,7 @@ class ApiWatchTest extends ApiTestCase { 'action' => 'watch', 'title' => $page['title'], 'unwatch' => true, - 'token' => $pageinfo['watchtoken'] ) ); + 'token' => $tokens['watchtoken'] ) ); } } $data = $this->doApiRequest( array( @@ -75,12 +68,11 @@ class ApiWatchTest extends ApiTestCase { /** */ function testWatchProtect() { - - $pageinfo = $this->getTokens(); + $tokens = $this->getTokens(); $data = $this->doApiRequest( array( 'action' => 'protect', - 'token' => $pageinfo['protecttoken'], + 'token' => $tokens['protecttoken'], 'title' => 'Help:UTPage', 'protections' => 'edit=sysop', 'watchlist' => 'unwatch' ) ); @@ -157,11 +149,11 @@ class ApiWatchTest extends ApiTestCase { /** */ function testWatchDelete() { - $pageinfo = $this->getTokens(); + $tokens = $this->getTokens(); $data = $this->doApiRequest( array( 'action' => 'delete', - 'token' => $pageinfo['deletetoken'], + 'token' => $tokens['deletetoken'], 'title' => 'Help:UTPage' ) ); $this->assertArrayHasKey( 'delete', $data[0] ); $this->assertArrayHasKey( 'title', $data[0]['delete'] ); -- 2.20.1