Merge "Fix and make some types in PHPDoc and JSDoc tags more specific"
[lhc/web/wiklou.git] / tests / phpunit / MediaWikiTestCase.php
index c1ada45..4afe710 100644 (file)
@@ -5,6 +5,8 @@ use MediaWiki\Logger\LoggerFactory;
 use MediaWiki\Logger\MonologSpi;
 use MediaWiki\MediaWikiServices;
 use Psr\Log\LoggerInterface;
+use Wikimedia\Rdbms\IMaintainableDatabase;
+use Wikimedia\Rdbms\Database;
 use Wikimedia\TestingAccessWrapper;
 
 /**
@@ -215,6 +217,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
        protected static function resetGlobalServices( Config $bootstrapConfig = null ) {
                $oldServices = MediaWikiServices::getInstance();
                $oldConfigFactory = $oldServices->getConfigFactory();
+               $oldLoadBalancerFactory = $oldServices->getDBLoadBalancerFactory();
 
                $testConfig = self::makeTestConfig( $bootstrapConfig );
 
@@ -223,6 +226,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                $serviceLocator = MediaWikiServices::getInstance();
                self::installTestServices(
                        $oldConfigFactory,
+                       $oldLoadBalancerFactory,
                        $serviceLocator
                );
                return $serviceLocator;
@@ -278,12 +282,14 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
 
        /**
         * @param ConfigFactory $oldConfigFactory
+        * @param LBFactory $oldLoadBalancerFactory
         * @param MediaWikiServices $newServices
         *
         * @throws MWException
         */
        private static function installTestServices(
                ConfigFactory $oldConfigFactory,
+               LBFactory $oldLoadBalancerFactory,
                MediaWikiServices $newServices
        ) {
                // Use bootstrap config for all configuration.
@@ -297,6 +303,13 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                                [ 'main' =>  $bootstrapConfig ]
                        )
                );
+               $newServices->resetServiceForTesting( 'DBLoadBalancerFactory' );
+               $newServices->redefineService(
+                       'DBLoadBalancerFactory',
+                       function ( MediaWikiServices $services ) use ( $oldLoadBalancerFactory ) {
+                               return $oldLoadBalancerFactory;
+                       }
+               );
        }
 
        /**
@@ -627,8 +640,8 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
         * The key is added to the array of globals that will be reset afterwards
         * in the tearDown().
         *
-        * @example
-        * <code>
+        * @par Example
+        * @code
         *     protected function setUp() {
         *         $this->setMwGlobals( 'wgRestrictStuff', true );
         *     }
@@ -643,7 +656,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
         *     }
         *
         *     function testQuux() {}
-        * </code>
+        * @endcode
         *
         * @param array|string $pairs Key to the global variable, or an array
         *  of key/value pairs.
@@ -829,6 +842,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
 
                $oldInstance = MediaWikiServices::getInstance();
                $oldConfigFactory = $oldInstance->getConfigFactory();
+               $oldLoadBalancerFactory = $oldInstance->getDBLoadBalancerFactory();
 
                $testConfig = self::makeTestConfig( null, $configOverrides );
                $newInstance = new MediaWikiServices( $testConfig );
@@ -847,6 +861,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
 
                self::installTestServices(
                        $oldConfigFactory,
+                       $oldLoadBalancerFactory,
                        $newInstance
                );
                MediaWikiServices::forceGlobalInstance( $newInstance );
@@ -1036,7 +1051,6 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
 
        private function addCoreDBData() {
                if ( $this->db->getType() == 'oracle' ) {
-
                        # Insert 0 user to prevent FK violations
                        # Anonymous user
                        if ( !$this->db->selectField( 'user', '1', [ 'user_id' => 0 ] ) ) {
@@ -1073,10 +1087,15 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                        $page->doEditContent(
                                new WikitextContent( 'UTContent' ),
                                'UTPageSummary',
-                               EDIT_NEW,
+                               EDIT_NEW | EDIT_SUPPRESS_RC,
                                false,
                                $user
                        );
+                       // an edit always attempt to purge backlink links such as history
+                       // pages. That is unneccessary.
+                       JobQueueGroup::singleton()->get( 'htmlCacheUpdate' )->delete();
+                       // WikiPages::doEditUpdates randomly adds RC purges
+                       JobQueueGroup::singleton()->get( 'recentChangesUpdate' )->delete();
 
                        // doEditContent() probably started the session via
                        // User::loadFromSession(). Close it now.
@@ -1101,6 +1120,8 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                        return;
                }
 
+               Hooks::run( 'UnitTestsBeforeDatabaseTeardown' );
+
                foreach ( $wgJobClasses as $type => $class ) {
                        // Delete any jobs under the clone DB (or old prefix in other stores)
                        JobQueueGroup::singleton()->get( $type )->delete();
@@ -1203,6 +1224,8 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                if ( $db->getType() == 'oracle' ) {
                        $db->query( 'BEGIN FILL_WIKI_INFO; END;' );
                }
+
+               Hooks::run( 'UnitTestsAfterDatabaseSetup', [ $db, $prefix ] );
        }
 
        /**
@@ -1467,7 +1490,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                                ' method should return true. Use @group Database or $this->tablesUsed.' );
                }
 
-               $db = wfGetDB( DB_SLAVE );
+               $db = wfGetDB( DB_REPLICA );
 
                $res = $db->select( $table, $fields, $condition, wfGetCaller(), [ 'ORDER BY' => $fields ] );
                $this->assertNotEmpty( $res, "query failed: " . $db->lastError() );
@@ -1700,7 +1723,6 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                        if ( !isset( $wgNamespaceContentModels[$ns] ) ||
                                $wgNamespaceContentModels[$ns] === CONTENT_MODEL_WIKITEXT
                        ) {
-
                                $wikitextNS = $ns;
 
                                return $wikitextNS;