3 use MediaWiki\Context\DerivativeContext
;
4 use MediaWiki\HookContainer\HookContainer
;
5 use MediaWiki\MainConfigNames
;
6 use MediaWiki\Request\FauxRequest
;
7 use MediaWiki\Specials\SpecialWatchlist
;
8 use Wikimedia\TestingAccessWrapper
;
15 * @covers \MediaWiki\Specials\SpecialWatchlist
17 class SpecialWatchlistTest
extends SpecialPageTestBase
{
18 protected function setUp(): void
{
21 $this->setTemporaryHook(
22 'ChangesListSpecialPageQuery',
26 $this->overrideConfigValues( [
27 MainConfigNames
::DefaultUserOptions
=>
29 'extendwatchlist' => 1,
30 'watchlistdays' => 3.0,
31 'watchlisthideanons' => 0,
32 'watchlisthidebots' => 0,
33 'watchlisthideliu' => 0,
34 'watchlisthideminor' => 0,
35 'watchlisthideown' => 0,
36 'watchlisthidepatrolled' => 1,
37 'watchlisthidecategorization' => 0,
38 'watchlistreloadautomatically' => 0,
39 'watchlistunwatchlinks' => 0,
40 'timecorrection' => '0'
42 MainConfigNames
::WatchlistExpiry
=> true
47 * Returns a new instance of the special page under test.
49 * @return SpecialWatchlist
51 protected function newSpecialPage() {
52 $services = $this->getServiceContainer();
53 return new SpecialWatchlist(
54 $services->getWatchedItemStore(),
55 $services->getWatchlistManager(),
56 $services->getUserOptionsLookup(),
57 $services->getChangeTagsStore(),
58 $services->getUserIdentityUtils(),
59 $services->getTempUserConfig()
63 public function testNotLoggedIn_throwsException() {
64 $this->expectException( UserNotLoggedIn
::class );
65 $this->executeSpecialPage();
68 public function testUserWithNoWatchedItems_displaysNoWatchlistMessage() {
69 $user = new TestUser( __METHOD__
);
70 [ $html, ] = $this->executeSpecialPage( '', null, 'qqx', $user->getUser() );
71 $this->assertStringContainsString( '(nowatchlist)', $html );
75 * @dataProvider provideFetchOptionsFromRequest
77 public function testFetchOptionsFromRequest(
78 $expectedValuesDefaults, $expectedValues, $preferences, $inputParams
80 // $defaults and $allFalse are just to make the expected values below
81 // shorter by hiding the background.
83 /** @var SpecialWatchlist $page */
84 $page = TestingAccessWrapper
::newFromObject(
85 $this->newSpecialPage()
88 $page->registerFilters();
90 // Does not consider $preferences, just wiki's defaults
91 $wikiDefaults = $page->getDefaultOptions()->getAllValues();
93 switch ( $expectedValuesDefaults ) {
95 $allFalse = $wikiDefaults;
97 foreach ( $allFalse as $key => $value ) {
98 if ( $value === true ) {
99 $allFalse[$key] = false;
103 // This is not exposed on the form (only in preferences) so it
104 // respects the preference.
105 $allFalse['extended'] = true;
107 $expectedValues +
= $allFalse;
110 $expectedValues +
= $wikiDefaults;
113 $this->fail( "Unknown \$expectedValuesDefaults: $expectedValuesDefaults" );
116 $page = TestingAccessWrapper
::newFromObject(
117 $this->newSpecialPage()
120 $context = new DerivativeContext( $page->getContext() );
122 $fauxRequest = new FauxRequest( $inputParams, /* $wasPosted= */ false );
123 $user = $this->getTestUser()->getUser();
125 $userOptionsManager = $this->getServiceContainer()->getUserOptionsManager();
126 foreach ( $preferences as $key => $value ) {
127 $userOptionsManager->setOption( $user, $key, $value );
130 $context->setRequest( $fauxRequest );
131 $context->setUser( $user );
132 $page->setContext( $context );
134 $page->registerFilters();
135 $formOptions = $page->getDefaultOptions();
136 $page->fetchOptionsFromRequest( $formOptions );
138 $this->assertArrayEquals(
140 $formOptions->getAllValues(),
141 /* $ordered= */ false,
146 public static function provideFetchOptionsFromRequest() {
148 'ignores casing' => [
149 'expectedValuesDefaults' => 'wikiDefaults',
150 'expectedValues' => [
159 'first two same as prefs, second two overridden' => [
160 'expectedValuesDefaults' => 'wikiDefaults',
161 'expectedValues' => [
162 // First two same as prefs
166 // Second two overridden
167 'hideanons' => false,
169 'userExpLevel' => 'registered'
172 'watchlisthideminor' => 1,
173 'watchlisthidebots' => 0,
175 'watchlisthideanons' => 1,
176 'watchlisthideliu' => 0,
184 'Defaults/preferences for form elements are entirely ignored for '
185 . 'action=submit and omitted elements become false' => [
186 'expectedValuesDefaults' => 'allFalse',
187 'expectedValues' => [
188 'hideminor' => false,
190 'hideanons' => false,
192 'userExpLevel' => 'unregistered'
195 'watchlisthideminor' => 0,
196 'watchlisthidebots' => 1,
198 'watchlisthideanons' => 0,
199 'watchlisthideliu' => 1,
204 'action' => 'submit',