Merge "Fix sessionfailure i18n message during authentication"
[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
16 $this->setTemporaryHook(
17 'ChangesListSpecialPageFilters',
18 null
19 );
20
21 $this->setTemporaryHook(
22 'SpecialWatchlistQuery',
23 null
24 );
25
26 $this->setTemporaryHook(
27 'ChangesListSpecialPageQuery',
28 null
29 );
30
31 $this->setMwGlobals(
32 'wgDefaultUserOptions',
33 [
34 'extendwatchlist' => 1,
35 'watchlistdays' => 3.0,
36 'watchlisthideanons' => 0,
37 'watchlisthidebots' => 0,
38 'watchlisthideliu' => 0,
39 'watchlisthideminor' => 0,
40 'watchlisthideown' => 0,
41 'watchlisthidepatrolled' => 0,
42 'watchlisthidecategorization' => 1,
43 'watchlistreloadautomatically' => 0,
44 'watchlistunwatchlinks' => 0,
45 ]
46 );
47 }
48
49 /**
50 * Returns a new instance of the special page under test.
51 *
52 * @return SpecialPage
53 */
54 protected function newSpecialPage() {
55 return new SpecialWatchlist();
56 }
57
58 public function testNotLoggedIn_throwsException() {
59 $this->setExpectedException( UserNotLoggedIn::class );
60 $this->executeSpecialPage();
61 }
62
63 public function testUserWithNoWatchedItems_displaysNoWatchlistMessage() {
64 $user = new TestUser( __METHOD__ );
65 list( $html, ) = $this->executeSpecialPage( '', null, 'qqx', $user->getUser() );
66 $this->assertContains( '(nowatchlist)', $html );
67 }
68
69 /**
70 * @dataProvider provideFetchOptionsFromRequest
71 */
72 public function testFetchOptionsFromRequest( $expectedValues, $preferences, $inputParams ) {
73 $page = TestingAccessWrapper::newFromObject(
74 $this->newSpecialPage()
75 );
76
77 $context = new DerivativeContext( $page->getContext() );
78
79 $fauxRequest = new FauxRequest( $inputParams, /* $wasPosted= */ false );
80 $user = $this->getTestUser()->getUser();
81
82 foreach ( $preferences as $key => $value ) {
83 $user->setOption( $key, $value );
84 }
85
86 $context->setRequest( $fauxRequest );
87 $context->setUser( $user );
88 $page->setContext( $context );
89
90 $page->registerFilters();
91 $formOptions = $page->getDefaultOptions();
92 $page->fetchOptionsFromRequest( $formOptions );
93
94 $this->assertArrayEquals(
95 $expectedValues,
96 $formOptions->getAllValues(),
97 /* $ordered= */ false,
98 /* $named= */ true
99 );
100 }
101
102 public function provideFetchOptionsFromRequest() {
103 // $defaults and $allFalse are just to make the expected values below
104 // shorter by hiding the background.
105
106 $page = TestingAccessWrapper::newFromObject(
107 $this->newSpecialPage()
108 );
109
110 $this->setTemporaryHook(
111 'ChangesListSpecialPageFilters',
112 null
113 );
114
115 $page->registerFilters();
116
117 // Does not consider $preferences, just wiki's defaults
118 $wikiDefaults = $page->getDefaultOptions()->getAllValues();
119
120 $allFalse = $wikiDefaults;
121
122 foreach ( $allFalse as $key => &$value ) {
123 if ( $value === true ) {
124 $value = false;
125 }
126 }
127
128 // This is not exposed on the form (only in preferences) so it
129 // respects the preference.
130 $allFalse['extended'] = true;
131
132 return [
133 [
134 [
135 'hideminor' => true,
136 ] + $wikiDefaults,
137 [],
138 [
139 'hideMinor' => 1,
140 ],
141 ],
142
143 [
144 [
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 ] + $wikiDefaults,
154 [
155 'watchlisthideminor' => 1,
156 'watchlisthidebots' => 0,
157
158 'watchlisthideanons' => 1,
159 'watchlisthideliu' => 0,
160 ],
161 [
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 [
170 [
171 'hideminor' => false,
172 'hidebots' => true,
173 'hideanons' => false,
174 'hideliu' => true,
175 'userExpLevel' => 'unregistered'
176 ] + $allFalse,
177 [
178 'watchlisthideminor' => 0,
179 'watchlisthidebots' => 1,
180
181 'watchlisthideanons' => 0,
182 'watchlisthideliu' => 1,
183 ],
184 [
185 'hidebots' => 1,
186 'hideliu' => 1,
187 'action' => 'submit',
188 ],
189 ],
190 ];
191 }
192 }