clientpool: refactor Redis authentication error handling
[lhc/web/wiklou.git] / tests / phpunit / MediaWikiTestCase.php
index 47b6218..a27e9f9 100644 (file)
@@ -360,15 +360,20 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
         * @see resetGlobalServices()
         */
        private function doLightweightServiceReset() {
-               global $wgRequest;
+               global $wgRequest, $wgJobClasses;
 
+               foreach ( $wgJobClasses as $type => $class ) {
+                       JobQueueGroup::singleton()->get( $type )->delete();
+               }
                JobQueueGroup::destroySingletons();
+
                ObjectCache::clear();
                $services = MediaWikiServices::getInstance();
                $services->resetServiceForTesting( 'MainObjectStash' );
                $services->resetServiceForTesting( 'LocalServerObjectCache' );
                $services->getMainWANObjectCache()->clearProcessCache();
                FileBackendGroup::destroySingleton();
+               DeferredUpdates::clearPendingUpdates();
 
                // TODO: move global state into MediaWikiServices
                RequestContext::resetMain();
@@ -382,9 +387,6 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
        }
 
        public function run( PHPUnit_Framework_TestResult $result = null ) {
-               // Reset all caches between tests.
-               $this->doLightweightServiceReset();
-
                $needsResetDB = false;
 
                if ( !self::$dbSetup || $this->needsDB() ) {
@@ -515,8 +517,8 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
                        }
                }
 
-               DeferredUpdates::clearPendingUpdates();
-               ObjectCache::getMainWANInstance()->clearProcessCache();
+               // Reset all caches between tests.
+               $this->doLightweightServiceReset();
 
                // XXX: reset maintenance triggers
                // Hook into period lag checks which often happen in long-running scripts
@@ -903,6 +905,36 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
                ] );
        }
 
+       /**
+        * Alters $wgGroupPermissions for the duration of the test.  Can be called
+        * with an array, like
+        *   [ '*' => [ 'read' => false ], 'user' => [ 'read' => false ] ]
+        * or three values to set a single permission, like
+        *   $this->setGroupPermissions( '*', 'read', false );
+        *
+        * @since 1.31
+        * @param array|string $newPerms Either an array of permissions to change,
+        *   in which case the next two parameters are ignored; or a single string
+        *   identifying a group, to use with the next two parameters.
+        * @param string|null $newKey
+        * @param mixed $newValue
+        */
+       public function setGroupPermissions( $newPerms, $newKey = null, $newValue = null ) {
+               global $wgGroupPermissions;
+
+               $this->stashMwGlobals( 'wgGroupPermissions' );
+
+               if ( is_string( $newPerms ) ) {
+                       $newPerms = [ $newPerms => [ $newKey => $newValue ] ];
+               }
+
+               foreach ( $newPerms as $group => $permissions ) {
+                       foreach ( $permissions as $key => $value ) {
+                               $wgGroupPermissions[$group][$key] = $value;
+                       }
+               }
+       }
+
        /**
         * Sets the logger for a specified channel, for the duration of the test.
         * @since 1.27
@@ -1117,6 +1149,8 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
         * setupTestDB() was called. Useful if we need to perform database operations
         * after the test run has finished (such as saving logs or profiling info).
         *
+        * This is called by phpunit/bootstrap.php after the last test.
+        *
         * @since 1.21
         */
        public static function teardownTestDB() {
@@ -1540,7 +1574,7 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
                                        $db->delete( $tbl, '*', __METHOD__ );
                                }
 
-                               if ( $db->getType() === 'postgres' ) {
+                               if ( in_array( $db->getType(), [ 'postgres', 'sqlite' ], true ) ) {
                                        // Reset the table's sequence too.
                                        $db->resetSequenceForTable( $tbl, __METHOD__ );
                                }
@@ -1965,61 +1999,6 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
                return $loaded;
        }
 
-       /**
-        * Asserts that the given string is a valid HTML snippet.
-        * Wraps the given string in the required top level tags and
-        * then calls assertValidHtmlDocument().
-        * The snippet is expected to be HTML 5.
-        *
-        * @since 1.23
-        *
-        * @note Will mark the test as skipped if the "tidy" module is not installed.
-        * @note This ignores $wgUseTidy, so we can check for valid HTML even (and especially)
-        *        when automatic tidying is disabled.
-        *
-        * @param string $html An HTML snippet (treated as the contents of the body tag).
-        */
-       protected function assertValidHtmlSnippet( $html ) {
-               $html = '<!DOCTYPE html><html><head><title>test</title></head><body>' . $html . '</body></html>';
-               $this->assertValidHtmlDocument( $html );
-       }
-
-       /**
-        * Asserts that the given string is valid HTML document.
-        *
-        * @since 1.23
-        *
-        * @note Will mark the test as skipped if the "tidy" module is not installed.
-        * @note This ignores $wgUseTidy, so we can check for valid HTML even (and especially)
-        *        when automatic tidying is disabled.
-        *
-        * @param string $html A complete HTML document
-        */
-       protected function assertValidHtmlDocument( $html ) {
-               // Note: we only validate if the tidy PHP extension is available.
-               // In case wgTidyInternal is false, MWTidy would fall back to the command line version
-               // of tidy. In that case however, we can not reliably detect whether a failing validation
-               // is due to malformed HTML, or caused by tidy not being installed as a command line tool.
-               // That would cause all HTML assertions to fail on a system that has no tidy installed.
-               if ( !$GLOBALS['wgTidyInternal'] || !MWTidy::isEnabled() ) {
-                       $this->markTestSkipped( 'Tidy extension not installed' );
-               }
-
-               $errorBuffer = '';
-               MWTidy::checkErrors( $html, $errorBuffer );
-               $allErrors = preg_split( '/[\r\n]+/', $errorBuffer );
-
-               // Filter Tidy warnings which aren't useful for us.
-               // Tidy eg. often cries about parameters missing which have actually
-               // been deprecated since HTML4, thus we should not care about them.
-               $errors = preg_grep(
-                       '/^(.*Warning: (trimming empty|.* lacks ".*?" attribute).*|\s*)$/m',
-                       $allErrors, PREG_GREP_INVERT
-               );
-
-               $this->assertEmpty( $errors, implode( "\n", $errors ) );
-       }
-
        /**
         * Used as a marker to prevent wfResetOutputBuffers from breaking PHPUnit.
         * @param string $buffer