Merge "Create API to allow content handlers to handle structured data definitions"
[lhc/web/wiklou.git] / tests / phpunit / MediaWikiTestCase.php
index 08785ea..8dfe628 100644 (file)
@@ -127,6 +127,42 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                self::prepareServices( new GlobalVarConfig() );
        }
 
+       /**
+        * Convenience method for getting an immutable test user
+        *
+        * @since 1.28
+        *
+        * @param string[] $groups Groups the test user should be in.
+        * @return TestUser
+        */
+       public static function getTestUser( $groups = [] ) {
+               return TestUserRegistry::getImmutableTestUser( $groups );
+       }
+
+       /**
+        * Convenience method for getting a mutable test user
+        *
+        * @since 1.28
+        *
+        * @param string[] $groups Groups the test user should be added in.
+        * @return TestUser
+        */
+       public static function getMutableTestUser( $groups = [] ) {
+               return TestUserRegistry::getMutableTestUser( __CLASS__, $groups );
+       }
+
+       /**
+        * Convenience method for getting an immutable admin test user
+        *
+        * @since 1.28
+        *
+        * @param string[] $groups Groups the test user should be added to.
+        * @return TestUser
+        */
+       public static function getTestSysop() {
+               return self::getTestUser( [ 'sysop', 'bureaucrat' ] );
+       }
+
        /**
         * Prepare service configuration for unit testing.
         *
@@ -161,7 +197,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
         * Reset global services, and install testing environment.
         * This is the testing equivalent of MediaWikiServices::resetGlobalInstance().
         * This should only be used to set up the testing environment, not when
-        * runnnig unit tests. Use overrideMwServices() for that.
+        * running unit tests. Use overrideMwServices() for that.
         *
         * @see MediaWikiServices::resetGlobalInstance()
         * @see prepareServices()
@@ -187,7 +223,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
 
        /**
         * Create a config suitable for testing, based on a base config, default overrides,
-        * and custom overrdies.
+        * and custom overrides.
         *
         * @param Config|null $baseConfig
         * @param Config|null $customOverrides
@@ -221,6 +257,9 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                $defaultOverrides->set( 'ObjectCaches', $objectCaches );
                $defaultOverrides->set( 'MainCacheType', CACHE_NONE );
 
+               // Use a fast hash algorithm to hash passwords.
+               $defaultOverrides->set( 'PasswordDefault', 'A' );
+
                $testConfig = $customOverrides
                        ? new MultiConfig( [ $customOverrides, $defaultOverrides, $baseConfig ] )
                        : new MultiConfig( [ $defaultOverrides, $baseConfig ] );
@@ -253,7 +292,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
 
        /**
         * @param ConfigFactory $oldFactory
-        * @param Config[] $config
+        * @param Config[] $configurations
         *
         * @return Closure
         */
@@ -296,8 +335,6 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
        private function doLightweightServiceReset() {
                global $wgRequest;
 
-               $services = MediaWikiServices::getInstance();
-
                JobQueueGroup::destroySingletons();
                ObjectCache::clear();
                FileBackendGroup::destroySingleton();
@@ -320,7 +357,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
 
                $needsResetDB = false;
 
-               if ( $this->needsDB() ) {
+               if ( !self::$dbSetup || $this->needsDB() ) {
                        // set up a DB connection for this test to use
 
                        self::$useTemporaryTables = !$this->getCliArg( 'use-normal-tables' );
@@ -500,6 +537,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                }
                $wgRequest = new FauxRequest();
                MediaWiki\Session\SessionManager::resetCache();
+               MediaWiki\Auth\AuthManager::resetCache();
 
                $phpErrorLevel = intval( ini_get( 'error_reporting' ) );
 
@@ -531,6 +569,8 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
         * Sets a service, maintaining a stashed version of the previous service to be
         * restored in tearDown
         *
+        * @since 1.27
+        *
         * @param string $name
         * @param object $object
         */
@@ -596,6 +636,29 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                }
        }
 
+       /**
+        * Check if we can back up a value by performing a shallow copy.
+        * Values which fail this test are copied recursively.
+        *
+        * @param mixed $value
+        * @return bool True if a shallow copy will do; false if a deep copy
+        *  is required.
+        */
+       private static function canShallowCopy( $value ) {
+               if ( is_scalar( $value ) || $value === null ) {
+                       return true;
+               }
+               if ( is_array( $value ) ) {
+                       foreach ( $value as $subValue ) {
+                               if ( !is_scalar( $subValue ) && $subValue !== null ) {
+                                       return false;
+                               }
+                       }
+                       return true;
+               }
+               return false;
+       }
+
        /**
         * Stashes the global, will be restored in tearDown()
         *
@@ -631,13 +694,22 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                                // NOTE: we serialize then unserialize the value in case it is an object
                                // this stops any objects being passed by reference. We could use clone
                                // and if is_object but this does account for objects within objects!
-                               try {
-                                       $this->mwGlobals[$globalKey] = unserialize( serialize( $GLOBALS[$globalKey] ) );
-                               }
-                                       // NOTE; some things such as Closures are not serializable
-                                       // in this case just set the value!
-                               catch ( Exception $e ) {
+                               if ( self::canShallowCopy( $GLOBALS[$globalKey] ) ) {
                                        $this->mwGlobals[$globalKey] = $GLOBALS[$globalKey];
+                               } elseif (
+                                       // Many MediaWiki types are safe to clone. These are the
+                                       // ones that are most commonly stashed.
+                                       $GLOBALS[$globalKey] instanceof Language ||
+                                       $GLOBALS[$globalKey] instanceof User ||
+                                       $GLOBALS[$globalKey] instanceof FauxRequest
+                               ) {
+                                       $this->mwGlobals[$globalKey] = clone $GLOBALS[$globalKey];
+                               } else {
+                                       try {
+                                               $this->mwGlobals[$globalKey] = unserialize( serialize( $GLOBALS[$globalKey] ) );
+                                       } catch ( Exception $e ) {
+                                               $this->mwGlobals[$globalKey] = $GLOBALS[$globalKey];
+                                       }
                                }
                        }
                }
@@ -847,7 +919,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
        protected function insertPage( $pageName, $text = 'Sample page for unit test.' ) {
                $title = Title::newFromText( $pageName, 0 );
 
-               $user = User::newFromName( 'UTSysop' );
+               $user = static::getTestSysop()->getUser();
                $comment = __METHOD__ . ': Sample page for unit test.';
 
                // Avoid memory leak...?
@@ -898,38 +970,33 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
 
                        # Insert 0 user to prevent FK violations
                        # Anonymous user
-                       $this->db->insert( 'user', [
-                               'user_id' => 0,
-                               'user_name' => 'Anonymous' ], __METHOD__, [ 'IGNORE' ] );
+                       if ( !$this->db->selectField( 'user', '1', [ 'user_id' => 0 ] ) ) {
+                               $this->db->insert( 'user', [
+                                       'user_id' => 0,
+                                       'user_name' => 'Anonymous' ], __METHOD__, [ 'IGNORE' ] );
+                       }
 
                        # Insert 0 page to prevent FK violations
                        # Blank page
-                       $this->db->insert( 'page', [
-                               'page_id' => 0,
-                               'page_namespace' => 0,
-                               'page_title' => ' ',
-                               'page_restrictions' => null,
-                               'page_is_redirect' => 0,
-                               'page_is_new' => 0,
-                               'page_random' => 0,
-                               'page_touched' => $this->db->timestamp(),
-                               'page_latest' => 0,
-                               'page_len' => 0 ], __METHOD__, [ 'IGNORE' ] );
+                       if ( !$this->db->selectField( 'page', '1', [ 'page_id' => 0 ] ) ) {
+                               $this->db->insert( 'page', [
+                                       'page_id' => 0,
+                                       'page_namespace' => 0,
+                                       'page_title' => ' ',
+                                       'page_restrictions' => null,
+                                       'page_is_redirect' => 0,
+                                       'page_is_new' => 0,
+                                       'page_random' => 0,
+                                       'page_touched' => $this->db->timestamp(),
+                                       'page_latest' => 0,
+                                       'page_len' => 0 ], __METHOD__, [ 'IGNORE' ] );
+                       }
                }
 
                User::resetIdByNameCache();
 
                // Make sysop user
-               $user = User::newFromName( 'UTSysop' );
-
-               if ( $user->idForName() == 0 ) {
-                       $user->addToDatabase();
-                       TestUser::setPasswordForUser( $user, 'UTSysopPassword' );
-               }
-
-               // Always set groups, because $this->resetDB() wipes them out
-               $user->addGroup( 'sysop' );
-               $user->addGroup( 'bureaucrat' );
+               $user = static::getTestSysop()->getUser();
 
                // Make 1 page with 1 revision
                $page = WikiPage::factory( Title::newFromText( 'UTPage' ) );
@@ -1142,17 +1209,25 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
         */
        private function resetDB( $db, $tablesUsed ) {
                if ( $db ) {
+                       $userTables = [ 'user', 'user_groups', 'user_properties' ];
+                       $coreDBDataTables = array_merge( $userTables, [ 'page', 'revision' ] );
+
+                       // If any of the user tables were marked as used, we should clear all of them.
+                       if ( array_intersect( $tablesUsed, $userTables ) ) {
+                               $tablesUsed = array_unique( array_merge( $tablesUsed, $userTables ) );
+                               TestUserRegistry::clear();
+                       }
+
                        $truncate = in_array( $db->getType(), [ 'oracle', 'mysql' ] );
                        foreach ( $tablesUsed as $tbl ) {
-                               // TODO: reset interwiki and user tables to their original content.
-                               if ( $tbl == 'interwiki' || $tbl == 'user' ) {
+                               // TODO: reset interwiki table to its original content.
+                               if ( $tbl == 'interwiki' ) {
                                        continue;
                                }
 
                                if ( $truncate ) {
                                        $db->query( 'TRUNCATE TABLE ' . $db->tableName( $tbl ), __METHOD__ );
                                } else {
-
                                        $db->delete( $tbl, '*', __METHOD__ );
                                }
 
@@ -1162,6 +1237,11 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                                        LinkCache::singleton()->clear();
                                }
                        }
+
+                       if ( array_intersect( $tablesUsed, $coreDBDataTables ) ) {
+                               // Re-add core DB data that was deleted
+                               $this->addCoreDBData();
+                       }
                }
        }
 
@@ -1570,32 +1650,6 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                }
        }
 
-       /**
-        * Check whether we have the 'gzip' commandline utility, will skip
-        * the test whenever "gzip -V" fails.
-        *
-        * Result is cached at the process level.
-        *
-        * @return bool
-        *
-        * @since 1.21
-        */
-       protected function checkHasGzip() {
-               static $haveGzip;
-
-               if ( $haveGzip === null ) {
-                       $retval = null;
-                       wfShellExec( 'gzip -V', $retval );
-                       $haveGzip = ( $retval === 0 );
-               }
-
-               if ( !$haveGzip ) {
-                       $this->markTestSkipped( "Skip test, requires the gzip utility in PATH" );
-               }
-
-               return $haveGzip;
-       }
-
        /**
         * Check if $extName is a loaded PHP extension, will skip the
         * test whenever it is not loaded.