Disable reportDupes for unit tests
[lhc/web/wiklou.git] / tests / phpunit / MediaWikiTestCase.php
index 920dbb3..9599016 100644 (file)
@@ -42,7 +42,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
        /**
         * Primary database
         *
-        * @var DatabaseBase
+        * @var Database
         * @since 1.18
         */
        protected $db;
@@ -56,7 +56,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
        private static $useTemporaryTables = true;
        private static $reuseDB = false;
        private static $dbSetup = false;
-       private static $oldTablePrefix = false;
+       private static $oldTablePrefix = '';
 
        /**
         * Original value of PHP's error_reporting setting.
@@ -242,7 +242,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                 * which we can't allow, as that would open a new connection for mysql.
                 * Replace with a HashBag. They would not be going to persist anyway.
                 */
-               $hashCache = [ 'class' => 'HashBagOStuff' ];
+               $hashCache = [ 'class' => 'HashBagOStuff', 'reportDupes' => false ];
                $objectCaches = [
                                CACHE_DB => $hashCache,
                                CACHE_ACCEL => $hashCache,
@@ -336,6 +336,10 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
 
                JobQueueGroup::destroySingletons();
                ObjectCache::clear();
+               $services = MediaWikiServices::getInstance();
+               $services->resetServiceForTesting( 'MainObjectStash' );
+               $services->resetServiceForTesting( 'LocalServerObjectCache' );
+               $services->getMainWANObjectCache()->clearProcessCache();
                FileBackendGroup::destroySingleton();
 
                // TODO: move global state into MediaWikiServices
@@ -920,13 +924,22 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
         *
         * Should be called from addDBData().
         *
-        * @since 1.25
-        * @param string $pageName Page name
+        * @since 1.25 ($namespace in 1.28)
+        * @param string|title $pageName Page name or title
         * @param string $text Page's content
+        * @param int $namespace Namespace id (name cannot already contain namespace)
         * @return array Title object and page id
         */
-       protected function insertPage( $pageName, $text = 'Sample page for unit test.' ) {
-               $title = Title::newFromText( $pageName, 0 );
+       protected function insertPage(
+               $pageName,
+               $text = 'Sample page for unit test.',
+               $namespace = null
+       ) {
+               if ( is_string( $pageName ) ) {
+                       $title = Title::newFromText( $pageName, $namespace );
+               } else {
+                       $title = $pageName;
+               }
 
                $user = static::getTestSysop()->getUser();
                $comment = __METHOD__ . ': Sample page for unit test.';
@@ -1061,11 +1074,11 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
         * Clones all tables in the given database (whatever database that connection has
         * open), to versions with the test prefix.
         *
-        * @param DatabaseBase $db Database to use
+        * @param Database $db Database to use
         * @param string $prefix Prefix to use for test tables
         * @return bool True if tables were cloned, false if only the prefix was changed
         */
-       protected static function setupDatabaseWithTestPrefix( DatabaseBase $db, $prefix ) {
+       protected static function setupDatabaseWithTestPrefix( Database $db, $prefix ) {
                $tablesCloned = self::listTables( $db );
                $dbClone = new CloneDatabase( $db, $tablesCloned, $prefix );
                $dbClone->useTemporaryTables( self::$useTemporaryTables );
@@ -1114,12 +1127,12 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
         * @note this method only works when first called. Subsequent calls have no effect,
         * even if using different parameters.
         *
-        * @param DatabaseBase $db The database connection
+        * @param Database $db The database connection
         * @param string $prefix The prefix to use for the new table set (aka schema).
         *
         * @throws MWException If the database table prefix is already $prefix
         */
-       public static function setupTestDB( DatabaseBase $db, $prefix ) {
+       public static function setupTestDB( Database $db, $prefix ) {
                if ( self::$dbSetup ) {
                        return;
                }
@@ -1130,7 +1143,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                }
 
                // TODO: the below should be re-written as soon as LBFactory, LoadBalancer,
-               // and DatabaseBase no longer use global state.
+               // and Database no longer use global state.
 
                self::$dbSetup = true;
 
@@ -1169,19 +1182,23 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
         * Gets master database connections for all of the ExternalStoreDB
         * stores configured in $wgDefaultExternalStore.
         *
-        * @return array Array of DatabaseBase master connections
+        * @return Database[] Array of Database master connections
         */
 
        protected static function getExternalStoreDatabaseConnections() {
                global $wgDefaultExternalStore;
 
+               /** @var ExternalStoreDB $externalStoreDB */
                $externalStoreDB = ExternalStore::getStoreObject( 'DB' );
                $defaultArray = (array) $wgDefaultExternalStore;
                $dbws = [];
                foreach ( $defaultArray as $url ) {
                        if ( strpos( $url, 'DB://' ) === 0 ) {
                                list( $proto, $cluster ) = explode( '://', $url, 2 );
-                               $dbw = $externalStoreDB->getMaster( $cluster );
+                               // Avoid getMaster() because setupDatabaseWithTestPrefix()
+                               // requires Database instead of plain DBConnRef/IDatabase
+                               $lb = $externalStoreDB->getLoadBalancer( $cluster );
+                               $dbw = $lb->getConnection( DB_MASTER );
                                $dbws[] = $dbw;
                        }
                }
@@ -1213,7 +1230,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
        /**
         * Empty all tables so they can be repopulated for tests
         *
-        * @param DatabaseBase $db|null Database to reset
+        * @param Database $db|null Database to reset
         * @param array $tablesUsed Tables to reset
         */
        private function resetDB( $db, $tablesUsed ) {
@@ -1296,18 +1313,21 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
        /**
         * @since 1.18
         *
-        * @param DatabaseBase $db
+        * @param Database $db
         *
         * @return array
         */
-       public static function listTables( $db ) {
+       public static function listTables( Database $db ) {
                $prefix = $db->tablePrefix();
                $tables = $db->listTables( $prefix, __METHOD__ );
 
                if ( $db->getType() === 'mysql' ) {
-                       # bug 43571: cannot clone VIEWs under MySQL
-                       $views = $db->listViews( $prefix, __METHOD__ );
-                       $tables = array_diff( $tables, $views );
+                       static $viewListCache = null;
+                       if ( $viewListCache === null ) {
+                               $viewListCache = $db->listViews( null, __METHOD__ );
+                       }
+                       // T45571: cannot clone VIEWs under MySQL
+                       $tables = array_diff( $tables, $viewListCache );
                }
                array_walk( $tables, [ __CLASS__, 'unprefixTable' ], $prefix );