From 8edbab99b07e16f3d92995789b218426660913fe Mon Sep 17 00:00:00 2001 From: Ori Livneh Date: Mon, 9 May 2016 00:15:13 -0700 Subject: [PATCH] Permit use of User::getDefaultOptions() in-process cache in unit tests 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 | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/includes/user/User.php b/includes/user/User.php index 40b5b4008d..4a92f6586e 100644 --- a/includes/user/User.php +++ b/includes/user/User.php @@ -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; } -- 2.20.1