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