This converts user options management to a separate service for use in DI context. User options are accessed quite early on in installation process and full-on options management depends on the database. Prior we have protected from accessing the DB by setting a hacky $wgUser with 0 id, and relying on the implementation that it doesn't go into the database to get the default user options. Now we can't really do that since DBLoadBalancer is required to instantiate the options manager. Instead, we redefine the options manager with a DefaultOptionsManager, that only provides access to default options and doesn't require DB access. UserOptionsManager uses PreferencesFactory, however injecting it will produce a cyclic dependency. The problem is that we separate options to different kinds, which are inferred from the PreferencesFactory declaration for those options (e.g. if it's a radio button in the UI declaration, the option is of multiselect kind). This is plain wrong, the dependency should be wise versa. This will be addressed separately, since it's requires larger refactoring. For now the PreferencesFactory is obtained on demand. This will be addressed in a followup. Bug: T248527 Change-Id: I74917c5eaec184d188911a319895b941ed55ee87
151 lines
3.6 KiB
PHP
151 lines
3.6 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Abstract base class for shared logic when testing ChangesListSpecialPage
|
|
* and subclasses
|
|
*
|
|
* @group Database
|
|
*/
|
|
abstract class AbstractChangesListSpecialPageTestCase extends MediaWikiTestCase {
|
|
// Must be initialized by subclass
|
|
/**
|
|
* @var ChangesListSpecialPage
|
|
*/
|
|
protected $changesListSpecialPage;
|
|
|
|
protected $oldPatrollersGroup;
|
|
|
|
protected function setUp() : void {
|
|
global $wgGroupPermissions;
|
|
|
|
parent::setUp();
|
|
$this->setMwGlobals( [
|
|
'wgRCWatchCategoryMembership' => true,
|
|
'wgUseRCPatrol' => true,
|
|
] );
|
|
|
|
if ( isset( $wgGroupPermissions['patrollers'] ) ) {
|
|
$this->oldPatrollersGroup = $wgGroupPermissions['patrollers'];
|
|
}
|
|
|
|
$wgGroupPermissions['patrollers'] = [
|
|
'patrol' => true,
|
|
];
|
|
|
|
# setup the ChangesListSpecialPage (or subclass) object
|
|
$this->changesListSpecialPage = $this->getPage();
|
|
$context = $this->changesListSpecialPage->getContext();
|
|
$context = new DerivativeContext( $context );
|
|
$context->setUser( $this->getTestUser( [ 'patrollers' ] )->getUser() );
|
|
$this->changesListSpecialPage->setContext( $context );
|
|
$this->changesListSpecialPage->registerFilters();
|
|
}
|
|
|
|
abstract protected function getPage();
|
|
|
|
protected function tearDown() : void {
|
|
global $wgGroupPermissions;
|
|
|
|
parent::tearDown();
|
|
|
|
if ( $this->oldPatrollersGroup !== null ) {
|
|
$wgGroupPermissions['patrollers'] = $this->oldPatrollersGroup;
|
|
}
|
|
}
|
|
|
|
abstract public function provideParseParameters();
|
|
|
|
/**
|
|
* @dataProvider provideParseParameters
|
|
*/
|
|
public function testParseParameters( $params, $expected ) {
|
|
$opts = new FormOptions();
|
|
foreach ( $expected as $key => $value ) {
|
|
// Register it as null so sets aren't rejected.
|
|
$opts->add(
|
|
$key,
|
|
null,
|
|
FormOptions::guessType( $expected )
|
|
);
|
|
}
|
|
|
|
$this->changesListSpecialPage->parseParameters(
|
|
$params,
|
|
$opts
|
|
);
|
|
|
|
$this->assertArrayEquals(
|
|
$expected,
|
|
$opts->getAllValues(),
|
|
/** ordered= */ false,
|
|
/** named= */ true
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @dataProvider validateOptionsProvider
|
|
*/
|
|
public function testValidateOptions(
|
|
$optionsToSet,
|
|
$expectedRedirect,
|
|
$expectedRedirectOptions,
|
|
$rcfilters
|
|
) {
|
|
$redirectQuery = [];
|
|
$redirected = false;
|
|
$output = $this->getMockBuilder( OutputPage::class )
|
|
->disableProxyingToOriginalMethods()
|
|
->disableOriginalConstructor()
|
|
->getMock();
|
|
$output->method( 'redirect' )->willReturnCallback(
|
|
function ( $url ) use ( &$redirectQuery, &$redirected ) {
|
|
$urlParts = wfParseUrl( $url );
|
|
$query = $urlParts[ 'query' ] ?? '';
|
|
parse_str( $query, $redirectQuery );
|
|
$redirected = true;
|
|
}
|
|
);
|
|
|
|
// Disable this hook or it could break changeType
|
|
// depending on which other extensions are running.
|
|
$this->setTemporaryHook(
|
|
'ChangesListSpecialPageStructuredFilters',
|
|
null
|
|
);
|
|
|
|
// Give users patrol permissions so we can test that.
|
|
$user = $this->getTestSysop()->getUser();
|
|
$user->setOption( 'rcenhancedfilters-disable', $rcfilters ? 0 : 1 );
|
|
$ctx = new RequestContext();
|
|
$ctx->setUser( $user );
|
|
|
|
$ctx->setOutput( $output );
|
|
$clsp = $this->changesListSpecialPage;
|
|
$clsp->setContext( $ctx );
|
|
$opts = $clsp->getDefaultOptions();
|
|
|
|
foreach ( $optionsToSet as $option => $value ) {
|
|
$opts->setValue( $option, $value );
|
|
}
|
|
|
|
$clsp->validateOptions( $opts );
|
|
|
|
$this->assertEquals( $expectedRedirect, $redirected, 'redirection' );
|
|
|
|
if ( $expectedRedirect ) {
|
|
if ( count( $expectedRedirectOptions ) > 0 ) {
|
|
$expectedRedirectOptions += [
|
|
'title' => $clsp->getPageTitle()->getPrefixedText(),
|
|
];
|
|
}
|
|
|
|
$this->assertArrayEquals(
|
|
$expectedRedirectOptions,
|
|
$redirectQuery,
|
|
/* $ordered= */ false,
|
|
/* $named= */ true,
|
|
'redirection query'
|
|
);
|
|
}
|
|
}
|
|
}
|