From ed40e8ec5312481438b7369f0053219e51e0eb5e Mon Sep 17 00:00:00 2001 From: Aryeh Gregor Date: Wed, 28 Mar 2018 17:01:46 +0300 Subject: [PATCH] Get rid of ApiTestCase::doLogin The function is entirely unnecessary. Change-Id: I805520e5355119e872e602d0bfc93be26f227128 --- tests/phpunit/includes/api/ApiBlockTest.php | 1 - tests/phpunit/includes/api/ApiDeleteTest.php | 7 --- .../phpunit/includes/api/ApiEditPageTest.php | 2 - tests/phpunit/includes/api/ApiPurgeTest.php | 5 --- .../includes/api/ApiQueryAllPagesTest.php | 12 ++--- .../ApiQueryRecentChangesIntegrationTest.php | 9 ++-- .../api/ApiQueryWatchlistIntegrationTest.php | 6 ++- .../ApiQueryWatchlistRawIntegrationTest.php | 5 +-- ...etNotificationTimestampIntegrationTest.php | 1 - .../phpunit/includes/api/ApiStashEditTest.php | 1 - tests/phpunit/includes/api/ApiTestCase.php | 45 +++++++------------ tests/phpunit/includes/api/ApiUnblockTest.php | 10 +---- tests/phpunit/includes/api/ApiWatchTest.php | 5 --- .../includes/api/query/ApiQueryTest.php | 1 - 14 files changed, 33 insertions(+), 77 deletions(-) diff --git a/tests/phpunit/includes/api/ApiBlockTest.php b/tests/phpunit/includes/api/ApiBlockTest.php index c456e9abbb..374ea3cd6f 100644 --- a/tests/phpunit/includes/api/ApiBlockTest.php +++ b/tests/phpunit/includes/api/ApiBlockTest.php @@ -12,7 +12,6 @@ class ApiBlockTest extends ApiTestCase { protected function setUp() { parent::setUp(); - $this->doLogin(); $this->mUser = $this->getMutableTestUser()->getUser(); } diff --git a/tests/phpunit/includes/api/ApiDeleteTest.php b/tests/phpunit/includes/api/ApiDeleteTest.php index c9ce28e73a..0f2bcc6145 100644 --- a/tests/phpunit/includes/api/ApiDeleteTest.php +++ b/tests/phpunit/includes/api/ApiDeleteTest.php @@ -12,13 +12,6 @@ * @covers ApiDelete */ class ApiDeleteTest extends ApiTestCase { - - protected function setUp() { - parent::setUp(); - - $this->doLogin(); - } - public function testDelete() { $name = 'Help:' . ucfirst( __FUNCTION__ ); diff --git a/tests/phpunit/includes/api/ApiEditPageTest.php b/tests/phpunit/includes/api/ApiEditPageTest.php index 4790f6b727..c196338915 100644 --- a/tests/phpunit/includes/api/ApiEditPageTest.php +++ b/tests/phpunit/includes/api/ApiEditPageTest.php @@ -40,8 +40,6 @@ class ApiEditPageTest extends ApiTestCase { MWNamespace::clearCaches(); $wgContLang->resetNamespaces(); # reset namespace cache - - $this->doLogin(); } protected function tearDown() { diff --git a/tests/phpunit/includes/api/ApiPurgeTest.php b/tests/phpunit/includes/api/ApiPurgeTest.php index 9e1d3a189b..96d9a38488 100644 --- a/tests/phpunit/includes/api/ApiPurgeTest.php +++ b/tests/phpunit/includes/api/ApiPurgeTest.php @@ -9,11 +9,6 @@ */ class ApiPurgeTest extends ApiTestCase { - protected function setUp() { - parent::setUp(); - $this->doLogin(); - } - /** * @group Broken */ diff --git a/tests/phpunit/includes/api/ApiQueryAllPagesTest.php b/tests/phpunit/includes/api/ApiQueryAllPagesTest.php index b482c3189a..2d89aa5433 100644 --- a/tests/phpunit/includes/api/ApiQueryAllPagesTest.php +++ b/tests/phpunit/includes/api/ApiQueryAllPagesTest.php @@ -8,16 +8,10 @@ * @covers ApiQueryAllPages */ class ApiQueryAllPagesTest extends ApiTestCase { - - protected function setUp() { - parent::setUp(); - $this->doLogin(); - } - /** - *Test T27702 - *Prefixes of API search requests are not handled with case sensitivity and may result - *in wrong search results + * Test T27702 + * Prefixes of API search requests are not handled with case sensitivity and may result + * in wrong search results */ public function testPrefixNormalizationSearchBug() { $title = Title::newFromText( 'Category:Template:xyz' ); diff --git a/tests/phpunit/includes/api/ApiQueryRecentChangesIntegrationTest.php b/tests/phpunit/includes/api/ApiQueryRecentChangesIntegrationTest.php index 24b7500983..5b43dd1b83 100644 --- a/tests/phpunit/includes/api/ApiQueryRecentChangesIntegrationTest.php +++ b/tests/phpunit/includes/api/ApiQueryRecentChangesIntegrationTest.php @@ -23,7 +23,6 @@ class ApiQueryRecentChangesIntegrationTest extends ApiTestCase { parent::setUp(); self::$users['ApiQueryRecentChangesIntegrationTestUser'] = $this->getMutableTestUser(); - $this->doLogin( 'ApiQueryRecentChangesIntegrationTestUser' ); wfGetDB( DB_MASTER )->delete( 'recentchanges', '*', __METHOD__ ); } @@ -129,7 +128,8 @@ class ApiQueryRecentChangesIntegrationTest extends ApiTestCase { $params ), null, - false + false, + $this->getLoggedInTestUser() ); } @@ -138,7 +138,10 @@ class ApiQueryRecentChangesIntegrationTest extends ApiTestCase { array_merge( [ 'action' => 'query', 'generator' => 'recentchanges' ], $params - ) + ), + null, + false, + $this->getLoggedInTestUser() ); } diff --git a/tests/phpunit/includes/api/ApiQueryWatchlistIntegrationTest.php b/tests/phpunit/includes/api/ApiQueryWatchlistIntegrationTest.php index f973281c79..5f59d6fba4 100644 --- a/tests/phpunit/includes/api/ApiQueryWatchlistIntegrationTest.php +++ b/tests/phpunit/includes/api/ApiQueryWatchlistIntegrationTest.php @@ -23,7 +23,6 @@ class ApiQueryWatchlistIntegrationTest extends ApiTestCase { parent::setUp(); self::$users['ApiQueryWatchlistIntegrationTestUser'] = $this->getMutableTestUser(); self::$users['ApiQueryWatchlistIntegrationTestUser2'] = $this->getMutableTestUser(); - $this->doLogin( 'ApiQueryWatchlistIntegrationTestUser' ); } private function getLoggedInTestUser() { @@ -163,6 +162,9 @@ class ApiQueryWatchlistIntegrationTest extends ApiTestCase { } private function doListWatchlistRequest( array $params = [], $user = null ) { + if ( $user === null ) { + $user = $this->getLoggedInTestUser(); + } return $this->doApiRequest( array_merge( [ 'action' => 'query', 'list' => 'watchlist' ], @@ -176,7 +178,7 @@ class ApiQueryWatchlistIntegrationTest extends ApiTestCase { array_merge( [ 'action' => 'query', 'generator' => 'watchlist' ], $params - ) + ), null, false, $this->getLoggedInTestUser() ); } diff --git a/tests/phpunit/includes/api/ApiQueryWatchlistRawIntegrationTest.php b/tests/phpunit/includes/api/ApiQueryWatchlistRawIntegrationTest.php index 0f01664e72..2af63c4983 100644 --- a/tests/phpunit/includes/api/ApiQueryWatchlistRawIntegrationTest.php +++ b/tests/phpunit/includes/api/ApiQueryWatchlistRawIntegrationTest.php @@ -17,7 +17,6 @@ class ApiQueryWatchlistRawIntegrationTest extends ApiTestCase { = $this->getMutableTestUser(); self::$users['ApiQueryWatchlistRawIntegrationTestUser2'] = $this->getMutableTestUser(); - $this->doLogin( 'ApiQueryWatchlistRawIntegrationTestUser' ); } private function getLoggedInTestUser() { @@ -36,14 +35,14 @@ class ApiQueryWatchlistRawIntegrationTest extends ApiTestCase { return $this->doApiRequest( array_merge( [ 'action' => 'query', 'list' => 'watchlistraw' ], $params - ) ); + ), null, false, $this->getLoggedInTestUser() ); } private function doGeneratorWatchlistRawRequest( array $params = [] ) { return $this->doApiRequest( array_merge( [ 'action' => 'query', 'generator' => 'watchlistraw' ], $params - ) ); + ), null, false, $this->getLoggedInTestUser() ); } private function getItemsFromApiResponse( array $response ) { diff --git a/tests/phpunit/includes/api/ApiSetNotificationTimestampIntegrationTest.php b/tests/phpunit/includes/api/ApiSetNotificationTimestampIntegrationTest.php index ef4f5139e4..dacd48f623 100644 --- a/tests/phpunit/includes/api/ApiSetNotificationTimestampIntegrationTest.php +++ b/tests/phpunit/includes/api/ApiSetNotificationTimestampIntegrationTest.php @@ -13,7 +13,6 @@ class ApiSetNotificationTimestampIntegrationTest extends ApiTestCase { protected function setUp() { parent::setUp(); self::$users[__CLASS__] = new TestUser( __CLASS__ ); - $this->doLogin( __CLASS__ ); } public function testStuff() { diff --git a/tests/phpunit/includes/api/ApiStashEditTest.php b/tests/phpunit/includes/api/ApiStashEditTest.php index e2462c6138..60cda09030 100644 --- a/tests/phpunit/includes/api/ApiStashEditTest.php +++ b/tests/phpunit/includes/api/ApiStashEditTest.php @@ -9,7 +9,6 @@ class ApiStashEditTest extends ApiTestCase { public function testBasicEdit() { - $this->doLogin(); $apiResult = $this->doApiRequestWithToken( [ 'action' => 'stashedit', diff --git a/tests/phpunit/includes/api/ApiTestCase.php b/tests/phpunit/includes/api/ApiTestCase.php index d5078bff2e..31c8136bab 100644 --- a/tests/phpunit/includes/api/ApiTestCase.php +++ b/tests/phpunit/includes/api/ApiTestCase.php @@ -147,40 +147,29 @@ abstract class ApiTestCase extends MediaWikiLangTestCase { return $this->doApiRequest( $params, $session, false, $user, $tokenType ); } - protected function doLogin( $testUser = 'sysop' ) { + /** + * Previously this would do API requests to log in, as well as setting $wgUser and the request + * context's user. The API requests are unnecessary, and the global-setting is unwanted, so + * this method should not be called. Instead, pass appropriate User values directly to + * functions that need them. For functions that still rely on $wgUser, set that directly. If + * you just want to log in the test sysop user, don't do anything -- that's the default. + * + * @param TestUser|string $testUser Object, or key to self::$users such as 'sysop' or 'uploader' + * @deprecated since 1.31 + */ + protected function doLogin( $testUser = null ) { + global $wgUser; + if ( $testUser === null ) { $testUser = static::getTestSysop(); } elseif ( is_string( $testUser ) && array_key_exists( $testUser, self::$users ) ) { - $testUser = self::$users[ $testUser ]; + $testUser = self::$users[$testUser]; } elseif ( !$testUser instanceof TestUser ) { - throw new MWException( "Can not log in to undefined user $testUser" ); - } - - $data = $this->doApiRequest( [ - 'action' => 'login', - 'lgname' => $testUser->getUser()->getName(), - 'lgpassword' => $testUser->getPassword() ] ); - - $token = $data[0]['login']['token']; - - $data = $this->doApiRequest( - [ - 'action' => 'login', - 'lgtoken' => $token, - 'lgname' => $testUser->getUser()->getName(), - 'lgpassword' => $testUser->getPassword(), - ], - $data[2] - ); - - if ( $data[0]['login']['result'] === 'Success' ) { - // DWIM - global $wgUser; - $wgUser = $testUser->getUser(); - RequestContext::getMain()->setUser( $wgUser ); + throw new MWException( "Can't log in to undefined user $testUser" ); } - return $data; + $wgUser = $testUser->getUser(); + RequestContext::getMain()->setUser( $wgUser ); } protected function getTokenList( TestUser $user, $session = null ) { diff --git a/tests/phpunit/includes/api/ApiUnblockTest.php b/tests/phpunit/includes/api/ApiUnblockTest.php index 971b63c3d4..d20de0dc22 100644 --- a/tests/phpunit/includes/api/ApiUnblockTest.php +++ b/tests/phpunit/includes/api/ApiUnblockTest.php @@ -8,11 +8,6 @@ * @covers ApiUnblock */ class ApiUnblockTest extends ApiTestCase { - protected function setUp() { - parent::setUp(); - $this->doLogin(); - } - /** * @expectedException ApiUsageException */ @@ -22,10 +17,7 @@ class ApiUnblockTest extends ApiTestCase { 'action' => 'unblock', 'user' => 'UTApiBlockee', 'reason' => 'Some reason', - ], - null, - false, - self::$users['sysop']->getUser() + ] ); } } diff --git a/tests/phpunit/includes/api/ApiWatchTest.php b/tests/phpunit/includes/api/ApiWatchTest.php index b663dc7324..6d64a178df 100644 --- a/tests/phpunit/includes/api/ApiWatchTest.php +++ b/tests/phpunit/includes/api/ApiWatchTest.php @@ -9,11 +9,6 @@ * @covers ApiWatch */ class ApiWatchTest extends ApiTestCase { - protected function setUp() { - parent::setUp(); - $this->doLogin(); - } - function getTokens() { return $this->getTokenList( self::$users['sysop'] ); } diff --git a/tests/phpunit/includes/api/query/ApiQueryTest.php b/tests/phpunit/includes/api/query/ApiQueryTest.php index 88a2e62f46..de8d8156f3 100644 --- a/tests/phpunit/includes/api/query/ApiQueryTest.php +++ b/tests/phpunit/includes/api/query/ApiQueryTest.php @@ -9,7 +9,6 @@ class ApiQueryTest extends ApiTestCase { protected function setUp() { parent::setUp(); - $this->doLogin(); // Setup apiquerytestiw: as interwiki prefix $this->setMwGlobals( 'wgHooks', [ -- 2.20.1