Get rid of ApiTestCase::doLogin
authorAryeh Gregor <ayg@aryeh.name>
Wed, 28 Mar 2018 14:01:46 +0000 (17:01 +0300)
committerAryeh Gregor <ayg@aryeh.name>
Wed, 11 Apr 2018 17:17:52 +0000 (20:17 +0300)
The function is entirely unnecessary.

Change-Id: I805520e5355119e872e602d0bfc93be26f227128

14 files changed:
tests/phpunit/includes/api/ApiBlockTest.php
tests/phpunit/includes/api/ApiDeleteTest.php
tests/phpunit/includes/api/ApiEditPageTest.php
tests/phpunit/includes/api/ApiPurgeTest.php
tests/phpunit/includes/api/ApiQueryAllPagesTest.php
tests/phpunit/includes/api/ApiQueryRecentChangesIntegrationTest.php
tests/phpunit/includes/api/ApiQueryWatchlistIntegrationTest.php
tests/phpunit/includes/api/ApiQueryWatchlistRawIntegrationTest.php
tests/phpunit/includes/api/ApiSetNotificationTimestampIntegrationTest.php
tests/phpunit/includes/api/ApiStashEditTest.php
tests/phpunit/includes/api/ApiTestCase.php
tests/phpunit/includes/api/ApiUnblockTest.php
tests/phpunit/includes/api/ApiWatchTest.php
tests/phpunit/includes/api/query/ApiQueryTest.php

index c456e9a..374ea3c 100644 (file)
@@ -12,7 +12,6 @@ class ApiBlockTest extends ApiTestCase {
 
        protected function setUp() {
                parent::setUp();
-               $this->doLogin();
 
                $this->mUser = $this->getMutableTestUser()->getUser();
        }
index c9ce28e..0f2bcc6 100644 (file)
  * @covers ApiDelete
  */
 class ApiDeleteTest extends ApiTestCase {
-
-       protected function setUp() {
-               parent::setUp();
-
-               $this->doLogin();
-       }
-
        public function testDelete() {
                $name = 'Help:' . ucfirst( __FUNCTION__ );
 
index 4790f6b..c196338 100644 (file)
@@ -40,8 +40,6 @@ class ApiEditPageTest extends ApiTestCase {
 
                MWNamespace::clearCaches();
                $wgContLang->resetNamespaces(); # reset namespace cache
-
-               $this->doLogin();
        }
 
        protected function tearDown() {
index 9e1d3a1..96d9a38 100644 (file)
@@ -9,11 +9,6 @@
  */
 class ApiPurgeTest extends ApiTestCase {
 
-       protected function setUp() {
-               parent::setUp();
-               $this->doLogin();
-       }
-
        /**
         * @group Broken
         */
index b482c31..2d89aa5 100644 (file)
@@ -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' );
index 24b7500..5b43dd1 100644 (file)
@@ -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()
                );
        }
 
index f973281..5f59d6f 100644 (file)
@@ -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()
                );
        }
 
index 0f01664..2af63c4 100644 (file)
@@ -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 ) {
index ef4f513..dacd48f 100644 (file)
@@ -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() {
index e2462c6..60cda09 100644 (file)
@@ -9,7 +9,6 @@
 class ApiStashEditTest extends ApiTestCase {
 
        public function testBasicEdit() {
-               $this->doLogin();
                $apiResult = $this->doApiRequestWithToken(
                        [
                                'action' => 'stashedit',
index d5078bf..31c8136 100644 (file)
@@ -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 ) {
index 971b63c..d20de0d 100644 (file)
@@ -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()
+                       ]
                );
        }
 }
index b663dc7..6d64a17 100644 (file)
@@ -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'] );
        }
index 88a2e62..de8d815 100644 (file)
@@ -9,7 +9,6 @@
 class ApiQueryTest extends ApiTestCase {
        protected function setUp() {
                parent::setUp();
-               $this->doLogin();
 
                // Setup apiquerytestiw: as interwiki prefix
                $this->setMwGlobals( 'wgHooks', [