Merge "Fix use of GenderCache in ApiPageSet::processTitlesArray"
[lhc/web/wiklou.git] / tests / phpunit / includes / specials / SpecialWatchlistTest.php
1 <?php
2
3 use Wikimedia\TestingAccessWrapper;
4
5 /**
6 * @author Addshore
7 *
8 * @group Database
9 *
10 * @covers SpecialWatchlist
11 */
12 class SpecialWatchlistTest extends SpecialPageTestBase {
13 public function setUp() {
14 parent::setUp();
15 $this->tablesUsed = [ 'watchlist' ];
16 $this->setTemporaryHook(
17 'ChangesListSpecialPageQuery',
18 null
19 );
20
21 $this->setMwGlobals(
22 'wgDefaultUserOptions',
23 [
24 'extendwatchlist' => 1,
25 'watchlistdays' => 3.0,
26 'watchlisthideanons' => 0,
27 'watchlisthidebots' => 0,
28 'watchlisthideliu' => 0,
29 'watchlisthideminor' => 0,
30 'watchlisthideown' => 0,
31 'watchlisthidepatrolled' => 0,
32 'watchlisthidecategorization' => 1,
33 'watchlistreloadautomatically' => 0,
34 'watchlistunwatchlinks' => 0,
35 ]
36 );
37 }
38
39 /**
40 * Returns a new instance of the special page under test.
41 *
42 * @return SpecialPage
43 */
44 protected function newSpecialPage() {
45 return new SpecialWatchlist();
46 }
47
48 public function testNotLoggedIn_throwsException() {
49 $this->setExpectedException( UserNotLoggedIn::class );
50 $this->executeSpecialPage();
51 }
52
53 public function testUserWithNoWatchedItems_displaysNoWatchlistMessage() {
54 $user = new TestUser( __METHOD__ );
55 list( $html, ) = $this->executeSpecialPage( '', null, 'qqx', $user->getUser() );
56 $this->assertContains( '(nowatchlist)', $html );
57 }
58
59 /**
60 * @dataProvider provideFetchOptionsFromRequest
61 */
62 public function testFetchOptionsFromRequest(
63 $expectedValuesDefaults, $expectedValues, $preferences, $inputParams
64 ) {
65 // $defaults and $allFalse are just to make the expected values below
66 // shorter by hiding the background.
67
68 $page = TestingAccessWrapper::newFromObject(
69 $this->newSpecialPage()
70 );
71
72 $page->registerFilters();
73
74 // Does not consider $preferences, just wiki's defaults
75 $wikiDefaults = $page->getDefaultOptions()->getAllValues();
76
77 switch ( $expectedValuesDefaults ) {
78 case 'allFalse':
79 $allFalse = $wikiDefaults;
80
81 foreach ( $allFalse as $key => $value ) {
82 if ( $value === true ) {
83 $allFalse[$key] = false;
84 }
85 }
86
87 // This is not exposed on the form (only in preferences) so it
88 // respects the preference.
89 $allFalse['extended'] = true;
90
91 $expectedValues += $allFalse;
92 break;
93 case 'wikiDefaults':
94 $expectedValues += $wikiDefaults;
95 break;
96 default:
97 $this->fail( "Unknown \$expectedValuesDefaults: $expectedValuesDefaults" );
98 }
99
100 $page = TestingAccessWrapper::newFromObject(
101 $this->newSpecialPage()
102 );
103
104 $context = new DerivativeContext( $page->getContext() );
105
106 $fauxRequest = new FauxRequest( $inputParams, /* $wasPosted= */ false );
107 $user = $this->getTestUser()->getUser();
108
109 foreach ( $preferences as $key => $value ) {
110 $user->setOption( $key, $value );
111 }
112
113 $context->setRequest( $fauxRequest );
114 $context->setUser( $user );
115 $page->setContext( $context );
116
117 $page->registerFilters();
118 $formOptions = $page->getDefaultOptions();
119 $page->fetchOptionsFromRequest( $formOptions );
120
121 $this->assertArrayEquals(
122 $expectedValues,
123 $formOptions->getAllValues(),
124 /* $ordered= */ false,
125 /* $named= */ true
126 );
127 }
128
129 public function provideFetchOptionsFromRequest() {
130 return [
131 'ignores casing' => [
132 'expectedValuesDefaults' => 'wikiDefaults',
133 'expectedValues' => [
134 'hideminor' => true,
135 ],
136 'preferences' => [],
137 'inputParams' => [
138 'hideMinor' => 1,
139 ],
140 ],
141
142 'first two same as prefs, second two overriden' => [
143 'expectedValuesDefaults' => 'wikiDefaults',
144 'expectedValues' => [
145 // First two same as prefs
146 'hideminor' => true,
147 'hidebots' => false,
148
149 // Second two overriden
150 'hideanons' => false,
151 'hideliu' => true,
152 'userExpLevel' => 'registered'
153 ],
154 'preferences' => [
155 'watchlisthideminor' => 1,
156 'watchlisthidebots' => 0,
157
158 'watchlisthideanons' => 1,
159 'watchlisthideliu' => 0,
160 ],
161 'inputParams' => [
162 'hideanons' => 0,
163 'hideliu' => 1,
164 ],
165 ],
166
167 'Defaults/preferences for form elements are entirely ignored for '
168 . 'action=submit and omitted elements become false' => [
169 'expectedValuesDefaults' => 'allFalse',
170 'expectedValues' => [
171 'hideminor' => false,
172 'hidebots' => true,
173 'hideanons' => false,
174 'hideliu' => true,
175 'userExpLevel' => 'unregistered'
176 ],
177 'preferences' => [
178 'watchlisthideminor' => 0,
179 'watchlisthidebots' => 1,
180
181 'watchlisthideanons' => 0,
182 'watchlisthideliu' => 1,
183 ],
184 'inputParams' => [
185 'hidebots' => 1,
186 'hideliu' => 1,
187 'action' => 'submit',
188 ],
189 ],
190 ];
191 }
192 }