jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/362098 )

Change subject: Unbreak tests
......................................................................


Unbreak tests

This moves dummy CA tables creation to before any tests are run, so
that none of the parts of code CA hooks into attempts to use them before
they're in place. Also add an empty title check to fix another failure
that was popping for me.

Depends-On: If7050513719833d4167a24283885d7c10a25856b
Bug: T168802
Change-Id: I618840fafd22d9b6471eb470ef0414e354aa17f5
---
M extension.json
M includes/CentralAuthHooks.php
M tests/phpunit/CentralAuthTestCaseUsingDatabase.php
3 files changed, 58 insertions(+), 64 deletions(-)

Approvals:
  Gergő Tisza: Looks good to me, but someone else must approve
  jenkins-bot: Verified
  Kaldari: Looks good to me, approved



diff --git a/extension.json b/extension.json
index f6c41cd..597a041 100644
--- a/extension.json
+++ b/extension.json
@@ -388,7 +388,9 @@
                "AbuseFilter-builder": "CentralAuthHooks::abuseFilterBuilder",
                "SecurePoll_GetUserParams": 
"CentralAuthHooks::onSecurePoll_GetUserParams",
                "DeleteAccount": "CentralAuthHooks::onDeleteAccount",
-               "SessionCheckInfo": "CentralAuthHooks::onSessionCheckInfo"
+               "SessionCheckInfo": "CentralAuthHooks::onSessionCheckInfo",
+               "UnitTestsAfterDatabaseSetup": 
"CentralAuthHooks::onUnitTestsAfterDatabaseSetup",
+               "UnitTestsBeforeDatabaseTeardown": 
"CentralAuthHooks::onUnitTestsBeforeDatabaseTeardown"
        },
        "config": {
                "@doc": "see CentralAuth.php",
diff --git a/includes/CentralAuthHooks.php b/includes/CentralAuthHooks.php
index c9b85ad..0236011 100644
--- a/includes/CentralAuthHooks.php
+++ b/includes/CentralAuthHooks.php
@@ -1,5 +1,7 @@
 <?php
 
+use Wikimedia\Rdbms\IMaintainableDatabase;
+
 class CentralAuthHooks {
 
        /**
@@ -472,7 +474,7 @@
 
                // Check that this is actually for a special login page view
                $title = $context->getTitle();
-               if ( $direct && ( $title->isSpecial( 'Userlogin' ) || 
$title->isSpecial( 'CreateAccount' ) ) ) {
+               if ( $direct && $title && ( $title->isSpecial( 'Userlogin' ) || 
$title->isSpecial( 'CreateAccount' ) ) ) {
                        // User will be redirected to 
Special:CentralLogin/start (central wiki),
                        // then redirected back to 
Special:CentralLogin/complete (this wiki).
                        // Sanity check that "returnto" is not one of the 
central login pages. If it
@@ -1425,4 +1427,54 @@
 
                return true;
        }
+
+       /**
+        * UnitTestsAfterDatabaseSetup hook handler
+        *
+        * Setup the centralauth tables in the current DB, so we don't have
+        * to worry about rights on another database. The first time it's called
+        * we have to set the DB prefix ourselves, and reset it back to the 
original
+        * so that CloneDatabase will work. On subsequent runs, the prefix is 
already
+        * set up for us.
+        *
+        * @param IMaintainableDatabase $db
+        * @param string $prefix
+        */
+
+       public static function onUnitTestsAfterDatabaseSetup( 
IMaintainableDatabase $db, $prefix ) {
+               global $wgCentralAuthDatabase;
+               $wgCentralAuthDatabase = false;
+
+               $originalPrefix = $db->tablePrefix();
+               $db->tablePrefix( $prefix );
+               if ( !$db->tableExists( 'globaluser' ) ) {
+                       $db->sourceFile( __DIR__ . '/../central-auth.sql' );
+               }
+               $db->tablePrefix( $originalPrefix );
+       }
+
+       public static $centralauthTables = [
+               'global_group_permissions',
+               'global_group_restrictions',
+               'global_user_groups',
+               'globalnames',
+               'globaluser',
+               'localnames',
+               'localuser',
+               'wikiset',
+               'renameuser_status',
+               'renameuser_queue',
+               'users_to_rename',
+       ];
+
+       /**
+        * UnitTestsBeforeDatabaseTeardown hook handler
+        * Cleans up tables created by onUnitTestsAfterDatabaseSetup() above
+        */
+       public static function onUnitTestsBeforeDatabaseTeardown() {
+               $db = wfGetDB( DB_MASTER );
+               foreach ( self::$centralauthTables as $table ) {
+                       $db->dropTable( $table );
+               }
+       }
 }
diff --git a/tests/phpunit/CentralAuthTestCaseUsingDatabase.php 
b/tests/phpunit/CentralAuthTestCaseUsingDatabase.php
index 82b4300..af356c2 100644
--- a/tests/phpunit/CentralAuthTestCaseUsingDatabase.php
+++ b/tests/phpunit/CentralAuthTestCaseUsingDatabase.php
@@ -1,73 +1,13 @@
 <?php
+
 /**
  * Setup database tests for centralauth.
  *
  * @group Database
  */
 abstract class CentralAuthTestCaseUsingDatabase extends MediaWikiTestCase {
-
-       public static $centralauthTables = [
-               'global_group_permissions',
-               'global_group_restrictions',
-               'global_user_groups',
-               'globalnames',
-               'globaluser',
-               'localnames',
-               'localuser',
-               'wikiset',
-               'renameuser_status',
-               'renameuser_queue',
-               'users_to_rename',
-       ];
-
-       // Keep track of the original db name
-       protected static $centralAuthDatabase = null;
-
-       /**
-        * Setup the centralauth tables in the current db, so we don't have
-        * to worry about rights on another database. The first time it's called
-        * we have to set the DB prefix ourselves, and reset it back to the 
original
-        * so that CloneDatabase will work. On subsequent runs, the prefix is 
already
-        * setup for us.
-        */
-       public static function setUpBeforeClass() {
-               global $wgCentralAuthDatabase;
-               parent::setUpBeforeClass();
-
-               if ( is_null( self::$centralAuthDatabase ) ) {
-                       self::$centralAuthDatabase = $wgCentralAuthDatabase;
-               }
-               $wgCentralAuthDatabase = false; // use the current wiki db
-
-               $db = wfGetDB( DB_MASTER );
-               if ( $db->tablePrefix() !== MediaWikiTestCase::DB_PREFIX ) {
-                       $originalPrefix = $db->tablePrefix();
-                       $db->tablePrefix( MediaWikiTestCase::DB_PREFIX );
-                       if ( !$db->tableExists( 'globaluser' ) ) {
-                               $db->sourceFile( __DIR__ . 
'/../../central-auth.sql' );
-                       }
-                       $db->tablePrefix( $originalPrefix );
-               } else {
-                       if ( !$db->tableExists( 'globaluser' ) ) {
-                               $db->sourceFile( __DIR__ . 
'/../../central-auth.sql' );
-                       }
-               }
-       }
-
-       public static function tearDownAfterClass() {
-               global $wgCentralAuthDatabase;
-               $db = wfGetDB( DB_MASTER );
-               foreach ( self::$centralauthTables as $table ) {
-                       $db->dropTable( $table );
-               }
-               if ( !is_null( self::$centralAuthDatabase ) ) {
-                       $wgCentralAuthDatabase = self::$centralAuthDatabase;
-               }
-               parent::tearDownAfterClass();
-       }
-
        public function __construct( $name = null, array $data = [], $dataName 
= '' ) {
-               $this->tablesUsed = array_merge( $this->tablesUsed, 
self::$centralauthTables );
+               $this->tablesUsed = array_merge( $this->tablesUsed, 
CentralAuthHooks::$centralauthTables );
                parent::__construct( $name, $data, $dataName );
        }
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/362098
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I618840fafd22d9b6471eb470ef0414e354aa17f5
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/CentralAuth
Gerrit-Branch: master
Gerrit-Owner: MaxSem <maxsem.w...@gmail.com>
Gerrit-Reviewer: Anomie <bjor...@wikimedia.org>
Gerrit-Reviewer: Gergő Tisza <gti...@wikimedia.org>
Gerrit-Reviewer: Kaldari <rkald...@wikimedia.org>
Gerrit-Reviewer: Krinkle <krinklem...@gmail.com>
Gerrit-Reviewer: Mattflaschen <mflasc...@wikimedia.org>
Gerrit-Reviewer: MaxSem <maxsem.w...@gmail.com>
Gerrit-Reviewer: Niharika29 <nko...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to