Permit use of User::getDefaultOptions() in-process cache in unit tests
authorOri Livneh <ori@wikimedia.org>
Mon, 9 May 2016 07:15:13 +0000 (00:15 -0700)
committerKrinkle <krinklemail@gmail.com>
Thu, 7 Jul 2016 19:50:52 +0000 (19:50 +0000)
MediaWiki uses an in-process cache to speed up repeat calls to
User::getDefaultOptions() -- except when the unit tests are running, in which
case the process cache is disabled, because otherwise it would be at risk of
becoming stale due to unit tests manipulating $wgContLang. Well, there's a less
aggressive option, which is to keep the cache enabled but use it only if
$wgContLang hasn't changed. Since MediaWiki's test setup code creates default
users for the unit tests, User::getDefaultOptions() ends up getting called
quite a lot, so enabling the process cache is worth the trouble.

Change-Id: I81f3ae42d356939c81e59ab12d7a9e7d1206cb40

includes/user/User.php

index 40b5b40..4a92f65 100644 (file)
@@ -1524,16 +1524,19 @@ class User implements IDBAccessObject {
                global $wgNamespacesToBeSearchedDefault, $wgDefaultUserOptions, $wgContLang, $wgDefaultSkin;
 
                static $defOpt = null;
-               if ( !defined( 'MW_PHPUNIT_TEST' ) && $defOpt !== null ) {
-                       // Disabling this for the unit tests, as they rely on being able to change $wgContLang
-                       // mid-request and see that change reflected in the return value of this function.
-                       // Which is insane and would never happen during normal MW operation
+               static $defOptLang = null;
+
+               if ( $defOpt !== null && $defOptLang === $wgContLang->getCode() ) {
+                       // $wgContLang does not change (and should not change) mid-request,
+                       // but the unit tests change it anyway, and expect this method to
+                       // return values relevant to the current $wgContLang.
                        return $defOpt;
                }
 
                $defOpt = $wgDefaultUserOptions;
                // Default language setting
-               $defOpt['language'] = $wgContLang->getCode();
+               $defOptLang = $wgContLang->getCode();
+               $defOpt['language'] = $defOptLang;
                foreach ( LanguageConverter::$languagesWithVariants as $langCode ) {
                        $defOpt[$langCode == $wgContLang->getCode() ? 'variant' : "variant-$langCode"] = $langCode;
                }