Fix php code style
[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 ]
45 );
46 }
47
48 /**
49 * Returns a new instance of the special page under test.
50 *
51 * @return SpecialPage
52 */
53 protected function newSpecialPage() {
54 return new SpecialWatchlist();
55 }
56
57 public function testNotLoggedIn_throwsException() {
58 $this->setExpectedException( 'UserNotLoggedIn' );
59 $this->executeSpecialPage();
60 }
61
62 public function testUserWithNoWatchedItems_displaysNoWatchlistMessage() {
63 $user = new TestUser( __METHOD__ );
64 list( $html, ) = $this->executeSpecialPage( '', null, 'qqx', $user->getUser() );
65 $this->assertContains( '(nowatchlist)', $html );
66 }
67
68 /**
69 * @dataProvider provideFetchOptionsFromRequest
70 */
71 public function testFetchOptionsFromRequest( $expectedValues, $preferences, $inputParams ) {
72 $page = TestingAccessWrapper::newFromObject(
73 $this->newSpecialPage()
74 );
75
76 $context = new DerivativeContext( $page->getContext() );
77
78 $fauxRequest = new FauxRequest( $inputParams, /* $wasPosted= */ false );
79 $user = $this->getTestUser()->getUser();
80
81 foreach ( $preferences as $key => $value ) {
82 $user->setOption( $key, $value );
83 }
84
85 $context->setRequest( $fauxRequest );
86 $context->setUser( $user );
87 $page->setContext( $context );
88
89 $page->registerFilters();
90 $formOptions = $page->getDefaultOptions();
91 $page->fetchOptionsFromRequest( $formOptions );
92
93 $this->assertArrayEquals(
94 $expectedValues,
95 $formOptions->getAllValues(),
96 /* $ordered= */ false,
97 /* $named= */ true
98 );
99 }
100
101 public function provideFetchOptionsFromRequest() {
102 // $defaults and $allFalse are just to make the expected values below
103 // shorter by hiding the background.
104
105 $page = TestingAccessWrapper::newFromObject(
106 $this->newSpecialPage()
107 );
108
109 $this->setTemporaryHook(
110 'ChangesListSpecialPageFilters',
111 null
112 );
113
114 $page->registerFilters();
115
116 // Does not consider $preferences, just wiki's defaults
117 $wikiDefaults = $page->getDefaultOptions()->getAllValues();
118
119 $allFalse = $wikiDefaults;
120
121 foreach ( $allFalse as $key => &$value ) {
122 if ( $value === true ) {
123 $value = false;
124 }
125 }
126
127 // This is not exposed on the form (only in preferences) so it
128 // respects the preference.
129 $allFalse['extended'] = true;
130
131 return [
132 [
133 [
134 'hideminor' => true,
135 ] + $wikiDefaults,
136 [],
137 [
138 'hideMinor' => 1,
139 ],
140 ],
141
142 [
143 [
144 // First two same as prefs
145 'hideminor' => true,
146 'hidebots' => false,
147
148 // Second two overriden
149 'hideanons' => false,
150 'hideliu' => true,
151 ] + $wikiDefaults,
152 [
153 'watchlisthideminor' => 1,
154 'watchlisthidebots' => 0,
155
156 'watchlisthideanons' => 1,
157 'watchlisthideliu' => 0,
158 ],
159 [
160 'hideanons' => 0,
161 'hideliu' => 1,
162 ],
163 ],
164
165 // Defaults/preferences for form elements are entirely ignored for
166 // action=submit and omitted elements become false
167 [
168 [
169 'hideminor' => false,
170 'hidebots' => true,
171 'hideanons' => false,
172 'hideliu' => true,
173 ] + $allFalse,
174 [
175 'watchlisthideminor' => 0,
176 'watchlisthidebots' => 1,
177 'watchlisthideanons' => 1,
178 'watchlisthideliu' => 0,
179 ],
180 [
181 'hidebots' => 1,
182 'hideliu' => 1,
183 'action' => 'submit',
184 ],
185 ],
186 ];
187 }
188 }