Merge "Remove inadequate render-blocking styles for jquery.tablesorter"
[lhc/web/wiklou.git] / tests / phpunit / MediaWikiTestCase.php
index b4707e6..4140c23 100644 (file)
@@ -178,6 +178,55 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
                return self::getTestUser( [ 'sysop', 'bureaucrat' ] );
        }
 
+       /**
+        * Returns a WikiPage representing an existing page.
+        *
+        * @since 1.32
+        *
+        * @param Title|string|null $title
+        * @return WikiPage
+        * @throws MWException
+        */
+       protected function getExistingTestPage( $title = null ) {
+               $title = ( $title === null ) ? 'UTPage' : $title;
+               $title = is_string( $title ) ? Title::newFromText( $title ) : $title;
+               $page = WikiPage::factory( $title );
+
+               if ( !$page->exists() ) {
+                       $user = self::getTestSysop()->getUser();
+                       $page->doEditContent(
+                               new WikitextContent( 'UTContent' ),
+                               'UTPageSummary',
+                               EDIT_NEW | EDIT_SUPPRESS_RC,
+                               false,
+                               $user
+                       );
+               }
+
+               return $page;
+       }
+
+       /**
+        * Returns a WikiPage representing a non-existing page.
+        *
+        * @since 1.32
+        *
+        * @param Title|string|null $title
+        * @return WikiPage
+        * @throws MWException
+        */
+       protected function getNonexistingTestPage( $title = null ) {
+               $title = ( $title === null ) ? 'UTPage-' . rand( 0, 100000 ) : $title;
+               $title = is_string( $title ) ? Title::newFromText( $title ) : $title;
+               $page = WikiPage::factory( $title );
+
+               if ( $page->exists() ) {
+                       $page->doDeleteArticle( 'Testing' );
+               }
+
+               return $page;
+       }
+
        /**
         * Prepare service configuration for unit testing.
         *
@@ -402,10 +451,6 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
                        if ( !self::$dbSetup ) {
                                $this->setupAllTestDBs();
                                $this->addCoreDBData();
-
-                               if ( ( $this->db->getType() == 'oracle' || !self::$useTemporaryTables ) && self::$reuseDB ) {
-                                       $this->resetDB( $this->db, $this->tablesUsed );
-                               }
                        }
 
                        // TODO: the DB setup should be done in setUpBeforeClass(), so the test DB
@@ -413,6 +458,7 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
                        // This would also remove the need for the HACK that is oncePerClass().
                        if ( $this->oncePerClass() ) {
                                $this->setUpSchema( $this->db );
+                               $this->resetDB( $this->db, $this->tablesUsed );
                                $this->addDBDataOnce();
                        }
 
@@ -1026,7 +1072,7 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
         * Should be called from addDBData().
         *
         * @since 1.25 ($namespace in 1.28)
-        * @param string|title $pageName Page name or title
+        * @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
@@ -1084,7 +1130,10 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
        public function addDBData() {
        }
 
-       private function addCoreDBData() {
+       /**
+        * @since 1.32
+        */
+       protected function addCoreDBData() {
                if ( $this->db->getType() == 'oracle' ) {
                        # Insert 0 user to prevent FK violations
                        # Anonymous user
@@ -1338,8 +1387,10 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
        /**
         * @throws LogicException if the given database connection is not a set up to use
         * mock tables.
+        *
+        * @since 1.31 this is no longer private.
         */
-       private function ensureMockDatabaseConnection( IDatabase $db ) {
+       protected function ensureMockDatabaseConnection( IDatabase $db ) {
                if ( $db->tablePrefix() !== $this->dbPrefix() ) {
                        throw new LogicException(
                                'Trying to delete mock tables, but table prefix does not indicate a mock database.'
@@ -1373,7 +1424,7 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
        }
 
        /**
-        * Undoes the dpecified schema overrides..
+        * Undoes the specified schema overrides..
         * Called once per test class, just before addDataOnce().
         *
         * @param IMaintainableDatabase $db
@@ -1383,7 +1434,7 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
                $this->ensureMockDatabaseConnection( $db );
 
                $oldOverrides = $oldOverrides + self::$schemaOverrideDefaults;
-               $originalTables = $this->listOriginalTables( $db );
+               $originalTables = $this->listOriginalTables( $db, 'unprefixed' );
 
                // Drop tables that need to be restored or removed.
                $tablesToDrop = array_merge( $oldOverrides['create'], $oldOverrides['alter'] );
@@ -1444,7 +1495,7 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
                $this->ensureMockDatabaseConnection( $db );
 
                // Drop the tables that will be created by the schema scripts.
-               $originalTables = $this->listOriginalTables( $db );
+               $originalTables = $this->listOriginalTables( $db, 'unprefixed' );
                $tablesToDrop = array_intersect( $originalTables, $overrides['create'] );
 
                if ( $tablesToDrop ) {
@@ -1489,7 +1540,7 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
                        if ( $tbl === 'page' ) {
                                // Forget about the pages since they don't
                                // exist in the DB.
-                               LinkCache::singleton()->clear();
+                               MediaWikiServices::getInstance()->getLinkCache()->clear();
                        }
                }
        }
@@ -1498,14 +1549,25 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
         * Lists all tables in the live database schema.
         *
         * @param IMaintainableDatabase $db
+        * @param string $prefix Either 'prefixed' or 'unprefixed'
         * @return array
         */
-       private function listOriginalTables( IMaintainableDatabase $db ) {
+       private function listOriginalTables( IMaintainableDatabase $db, $prefix = 'prefixed' ) {
                if ( !isset( $db->_originalTablePrefix ) ) {
                        throw new LogicException( 'No original table prefix know, cannot list tables!' );
                }
 
                $originalTables = $db->listTables( $db->_originalTablePrefix, __METHOD__ );
+               if ( $prefix === 'unprefixed' ) {
+                       $originalPrefixRegex = '/^' . preg_quote( $db->_originalTablePrefix ) . '/';
+                       $originalTables = array_map(
+                               function ( $pt ) use ( $originalPrefixRegex ) {
+                                       return preg_replace( $originalPrefixRegex, '', $pt );
+                               },
+                               $originalTables
+                       );
+               }
+
                return $originalTables;
        }
 
@@ -1524,7 +1586,7 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
                        throw new LogicException( 'No original table prefix know, cannot restore tables!' );
                }
 
-               $originalTables = $this->listOriginalTables( $db );
+               $originalTables = $this->listOriginalTables( $db, 'unprefixed' );
                $tables = array_intersect( $tables, $originalTables );
 
                $dbClone = new CloneDatabase( $db, $tables, $db->tablePrefix(), $db->_originalTablePrefix );
@@ -1542,8 +1604,10 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
        private function resetDB( $db, $tablesUsed ) {
                if ( $db ) {
                        $userTables = [ 'user', 'user_groups', 'user_properties', 'actor' ];
-                       $pageTables = [ 'page', 'revision', 'ip_changes', 'revision_comment_temp',
-                               'revision_actor_temp', 'comment', 'archive' ];
+                       $pageTables = [
+                               'page', 'revision', 'ip_changes', 'revision_comment_temp', 'comment', 'archive',
+                               'revision_actor_temp', 'slots', 'content', 'content_models', 'slot_roles',
+                       ];
                        $coreDBDataTables = array_merge( $userTables, $pageTables );
 
                        // If any of the user or page tables were marked as used, we should clear all of them.
@@ -1555,6 +1619,19 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
                                $tablesUsed = array_unique( array_merge( $tablesUsed, $pageTables ) );
                        }
 
+                       // Postgres, Oracle, and MSSQL all use mwuser/pagecontent
+                       // instead of user/text. But Postgres does not remap the
+                       // table name in tableExists(), so we mark the real table
+                       // names as being used.
+                       if ( $db->getType() === 'postgres' ) {
+                               if ( in_array( 'user', $tablesUsed ) ) {
+                                       $tablesUsed[] = 'mwuser';
+                               }
+                               if ( in_array( 'text', $tablesUsed ) ) {
+                                       $tablesUsed[] = 'pagecontent';
+                               }
+                       }
+
                        $truncate = in_array( $db->getType(), [ 'oracle', 'mysql' ] );
                        foreach ( $tablesUsed as $tbl ) {
                                // TODO: reset interwiki table to its original content.
@@ -1580,7 +1657,7 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
                                if ( $tbl === 'page' ) {
                                        // Forget about the pages since they don't
                                        // exist in the DB.
-                                       LinkCache::singleton()->clear();
+                                       MediaWikiServices::getInstance()->getLinkCache()->clear();
                                }
                        }