Allow DefaultOptionsLookup to be used with registered users in tests

Some tests use fake registered users which end up being passed to
DefaultOptionsLookup when storage is disabled. This should not be
considered a bug, and using the default preferences in this scenario
makes sense.

Add a switch to DefaultOptionsLookup to disable the precondition check
in that scenario.

Change-Id: I5987c4f1c3c502b23113d6688bbff7f1abe65879
This commit is contained in:
Daimona Eaytoy 2023-08-06 02:36:52 +02:00
parent 59b93e9e4f
commit c98aeff078
3 changed files with 21 additions and 4 deletions

View file

@ -2413,7 +2413,8 @@ return [
new ServiceOptions( DefaultOptionsLookup::CONSTRUCTOR_OPTIONS, $services->getMainConfig() ),
$services->getContentLanguage(),
$services->getHookContainer(),
$services->getNamespaceInfo()
$services->getNamespaceInfo(),
defined( 'MW_PHPUNIT_TEST' ) && $services->isStorageDisabled()
);
},

View file

@ -60,23 +60,31 @@ class DefaultOptionsLookup extends UserOptionsLookup {
/** @var HookRunner */
private $hookRunner;
/**
* @var bool Whether a database-less test is being executed.
*/
private bool $isDatabaselessTest;
/**
* @param ServiceOptions $options
* @param Language $contentLang
* @param HookContainer $hookContainer
* @param NamespaceInfo $nsInfo
* @param bool $isDatabaselessTest
*/
public function __construct(
ServiceOptions $options,
Language $contentLang,
HookContainer $hookContainer,
NamespaceInfo $nsInfo
NamespaceInfo $nsInfo,
bool $isDatabaselessTest
) {
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
$this->serviceOptions = $options;
$this->contentLang = $contentLang;
$this->hookRunner = new HookRunner( $hookContainer );
$this->nsInfo = $nsInfo;
$this->isDatabaselessTest = $isDatabaselessTest;
}
/**
@ -151,10 +159,17 @@ class DefaultOptionsLookup extends UserOptionsLookup {
* It only makes sense in an installer context when UserOptionsManager cannot be yet instantiated
* as the database is not available. Thus, this can only be called for an anon user,
* calling under different circumstances indicates a bug.
* The only exception to this is database-less PHPUnit tests, where sometimes fake registered users are
* used and end up being passed to this class. This should not be considered a bug, and using the default
* preferences in this scenario is probably the intended behaviour.
*
* @param UserIdentity $user
* @param string $fname
*/
private function verifyUsable( UserIdentity $user, string $fname ) {
Assert::precondition( !$user->isRegistered(), "$fname called on a registered user " );
Assert::precondition(
$this->isDatabaselessTest || !$user->isRegistered(),
"$fname called on a registered user "
);
}
}

View file

@ -50,7 +50,8 @@ abstract class UserOptionsLookupTest extends MediaWikiIntegrationTestCase {
),
$lang,
$this->getServiceContainer()->getHookContainer(),
$this->getServiceContainer()->getNamespaceInfo()
$this->getServiceContainer()->getNamespaceInfo(),
!$this->needsDB()
);
}