Merge "Drop cache interwiki"
[mediawiki.git] / tests / phpunit / includes / specials / SpecialWatchlistTest.php
blob3166e865f48bb6a4e126a84a016bdfb501ab00da
1 <?php
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;
10 /**
11 * @author Addshore
13 * @group Database
15 * @covers \MediaWiki\Specials\SpecialWatchlist
17 class SpecialWatchlistTest extends SpecialPageTestBase {
18 protected function setUp(): void {
19 parent::setUp();
21 $this->setTemporaryHook(
22 'ChangesListSpecialPageQuery',
23 HookContainer::NOOP
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
43 ] );
46 /**
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 );
74 /**
75 * @dataProvider provideFetchOptionsFromRequest
77 public function testFetchOptionsFromRequest(
78 $expectedValuesDefaults, $expectedValues, $preferences, $inputParams
79 ) {
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 ) {
94 case 'allFalse':
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;
108 break;
109 case 'wikiDefaults':
110 $expectedValues += $wikiDefaults;
111 break;
112 default:
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(
139 $expectedValues,
140 $formOptions->getAllValues(),
141 /* $ordered= */ false,
142 /* $named= */ true
146 public static function provideFetchOptionsFromRequest() {
147 return [
148 'ignores casing' => [
149 'expectedValuesDefaults' => 'wikiDefaults',
150 'expectedValues' => [
151 'hideminor' => true,
153 'preferences' => [],
154 'inputParams' => [
155 'hideMinor' => 1,
159 'first two same as prefs, second two overridden' => [
160 'expectedValuesDefaults' => 'wikiDefaults',
161 'expectedValues' => [
162 // First two same as prefs
163 'hideminor' => true,
164 'hidebots' => false,
166 // Second two overridden
167 'hideanons' => false,
168 'hideliu' => true,
169 'userExpLevel' => 'registered'
171 'preferences' => [
172 'watchlisthideminor' => 1,
173 'watchlisthidebots' => 0,
175 'watchlisthideanons' => 1,
176 'watchlisthideliu' => 0,
178 'inputParams' => [
179 'hideanons' => 0,
180 'hideliu' => 1,
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,
189 'hidebots' => true,
190 'hideanons' => false,
191 'hideliu' => true,
192 'userExpLevel' => 'unregistered'
194 'preferences' => [
195 'watchlisthideminor' => 0,
196 'watchlisthidebots' => 1,
198 'watchlisthideanons' => 0,
199 'watchlisthideliu' => 1,
201 'inputParams' => [
202 'hidebots' => 1,
203 'hideliu' => 1,
204 'action' => 'submit',