Merge "(bug 42915) make MovePage aware of whether redirects are supported."
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiOptionsTest.php
1 <?php
2
3 /**
4 * @group API
5 * @group Database
6 * @group medium
7 */
8 class ApiOptionsTest extends MediaWikiLangTestCase {
9
10 private $mTested, $mUserMock, $mContext, $mSession;
11
12 private $mOldGetPreferencesHooks = false;
13
14 private static $Success = array( 'options' => 'success' );
15
16 protected function setUp() {
17 parent::setUp();
18
19 $this->mUserMock = $this->getMockBuilder( 'User' )
20 ->disableOriginalConstructor()
21 ->getMock();
22
23 // Set up groups
24 $this->mUserMock->expects( $this->any() )
25 ->method( 'getEffectiveGroups' )->will( $this->returnValue( array( '*', 'user')) );
26
27 // Create a new context
28 $this->mContext = new DerivativeContext( new RequestContext() );
29 $this->mContext->getContext()->setTitle( Title::newFromText( 'Test' ) );
30 $this->mContext->setUser( $this->mUserMock );
31
32 $main = new ApiMain( $this->mContext );
33
34 // Empty session
35 $this->mSession = array();
36
37 $this->mTested = new ApiOptions( $main, 'options' );
38
39 global $wgHooks;
40 if ( !isset( $wgHooks['GetPreferences'] ) ) {
41 $wgHooks['GetPreferences'] = array();
42 }
43 $this->mOldGetPreferencesHooks = $wgHooks['GetPreferences'];
44 $wgHooks['GetPreferences'][] = array( $this, 'hookGetPreferences' );
45 }
46
47 protected function tearDown() {
48 global $wgHooks;
49
50 if ( $this->mOldGetPreferencesHooks !== false ) {
51 $wgHooks['GetPreferences'] = $this->mOldGetPreferencesHooks;
52 $this->mOldGetPreferencesHooks = false;
53 }
54
55 parent::tearDown();
56 }
57
58 public function hookGetPreferences( $user, &$preferences ) {
59 foreach ( array( 'name', 'willBeNull', 'willBeEmpty', 'willBeHappy' ) as $k ) {
60 $preferences[$k] = array(
61 'type' => 'text',
62 'section' => 'test',
63 'label' => '&#160;',
64 );
65 }
66
67 $preferences['testmultiselect'] = array(
68 'type' => 'multiselect',
69 'options' => array(
70 'Test' => array(
71 '<span dir="auto">Some HTML here for option 1</span>' => 'opt1',
72 '<span dir="auto">Some HTML here for option 2</span>' => 'opt2',
73 '<span dir="auto">Some HTML here for option 3</span>' => 'opt3',
74 '<span dir="auto">Some HTML here for option 4</span>' => 'opt4',
75 ),
76 ),
77 'section' => 'test',
78 'label' => '&#160;',
79 'prefix' => 'testmultiselect-',
80 'default' => array(),
81 );
82
83 return true;
84 }
85
86 private function getSampleRequest( $custom = array() ) {
87 $request = array(
88 'token' => '123ABC',
89 'change' => null,
90 'optionname' => null,
91 'optionvalue' => null,
92 );
93 return array_merge( $request, $custom );
94 }
95
96 private function executeQuery( $request ) {
97 $this->mContext->setRequest( new FauxRequest( $request, true, $this->mSession ) );
98 $this->mTested->execute();
99 return $this->mTested->getResult()->getData();
100 }
101
102 /**
103 * @expectedException UsageException
104 */
105 public function testNoToken() {
106 $request = $this->getSampleRequest( array( 'token' => null ) );
107
108 $this->executeQuery( $request );
109 }
110
111 public function testAnon() {
112 $this->mUserMock->expects( $this->once() )
113 ->method( 'isAnon' )
114 ->will( $this->returnValue( true ) );
115
116 try {
117 $request = $this->getSampleRequest();
118
119 $this->executeQuery( $request );
120 } catch ( UsageException $e ) {
121 $this->assertEquals( 'notloggedin', $e->getCodeString() );
122 $this->assertEquals( 'Anonymous users cannot change preferences', $e->getMessage() );
123 return;
124 }
125 $this->fail( "UsageException was not thrown" );
126 }
127
128 public function testNoOptionname() {
129 try {
130 $request = $this->getSampleRequest( array( 'optionvalue' => '1' ) );
131
132 $this->executeQuery( $request );
133 } catch ( UsageException $e ) {
134 $this->assertEquals( 'nooptionname', $e->getCodeString() );
135 $this->assertEquals( 'The optionname parameter must be set', $e->getMessage() );
136 return;
137 }
138 $this->fail( "UsageException was not thrown" );
139 }
140
141 public function testNoChanges() {
142 $this->mUserMock->expects( $this->never() )
143 ->method( 'resetOptions' );
144
145 $this->mUserMock->expects( $this->never() )
146 ->method( 'setOption' );
147
148 $this->mUserMock->expects( $this->never() )
149 ->method( 'saveSettings' );
150
151 try {
152 $request = $this->getSampleRequest();
153
154 $this->executeQuery( $request );
155 } catch ( UsageException $e ) {
156 $this->assertEquals( 'nochanges', $e->getCodeString() );
157 $this->assertEquals( 'No changes were requested', $e->getMessage() );
158 return;
159 }
160 $this->fail( "UsageException was not thrown" );
161 }
162
163 public function testReset() {
164 $this->mUserMock->expects( $this->once() )
165 ->method( 'resetOptions' );
166
167 $this->mUserMock->expects( $this->never() )
168 ->method( 'setOption' );
169
170 $this->mUserMock->expects( $this->once() )
171 ->method( 'saveSettings' );
172
173 $request = $this->getSampleRequest( array( 'reset' => '' ) );
174
175 $response = $this->executeQuery( $request );
176
177 $this->assertEquals( self::$Success, $response );
178 }
179
180 public function testOptionWithValue() {
181 $this->mUserMock->expects( $this->never() )
182 ->method( 'resetOptions' );
183
184 $this->mUserMock->expects( $this->once() )
185 ->method( 'setOption' )
186 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
187
188 $this->mUserMock->expects( $this->once() )
189 ->method( 'saveSettings' );
190
191 $request = $this->getSampleRequest( array( 'optionname' => 'name', 'optionvalue' => 'value' ) );
192
193 $response = $this->executeQuery( $request );
194
195 $this->assertEquals( self::$Success, $response );
196 }
197
198 public function testOptionResetValue() {
199 $this->mUserMock->expects( $this->never() )
200 ->method( 'resetOptions' );
201
202 $this->mUserMock->expects( $this->once() )
203 ->method( 'setOption' )
204 ->with( $this->equalTo( 'name' ), $this->equalTo( null ) );
205
206 $this->mUserMock->expects( $this->once() )
207 ->method( 'saveSettings' );
208
209 $request = $this->getSampleRequest( array( 'optionname' => 'name' ) );
210 $response = $this->executeQuery( $request );
211
212 $this->assertEquals( self::$Success, $response );
213 }
214
215 public function testChange() {
216 $this->mUserMock->expects( $this->never() )
217 ->method( 'resetOptions' );
218
219 $this->mUserMock->expects( $this->at( 1 ) )
220 ->method( 'getOptions' );
221
222 $this->mUserMock->expects( $this->at( 2 ) )
223 ->method( 'setOption' )
224 ->with( $this->equalTo( 'willBeNull' ), $this->equalTo( null ) );
225
226 $this->mUserMock->expects( $this->at( 3 ) )
227 ->method( 'getOptions' );
228
229 $this->mUserMock->expects( $this->at( 4 ) )
230 ->method( 'setOption' )
231 ->with( $this->equalTo( 'willBeEmpty' ), $this->equalTo( '' ) );
232
233 $this->mUserMock->expects( $this->at( 5 ) )
234 ->method( 'getOptions' );
235
236 $this->mUserMock->expects( $this->at( 6 ) )
237 ->method( 'setOption' )
238 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
239
240 $this->mUserMock->expects( $this->once() )
241 ->method( 'saveSettings' );
242
243 $request = $this->getSampleRequest( array( 'change' => 'willBeNull|willBeEmpty=|willBeHappy=Happy' ) );
244
245 $response = $this->executeQuery( $request );
246
247 $this->assertEquals( self::$Success, $response );
248 }
249
250 public function testResetChangeOption() {
251 $this->mUserMock->expects( $this->once() )
252 ->method( 'resetOptions' );
253
254 $this->mUserMock->expects( $this->at( 2 ) )
255 ->method( 'getOptions' );
256
257 $this->mUserMock->expects( $this->at( 3 ) )
258 ->method( 'setOption' )
259 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
260
261 $this->mUserMock->expects( $this->at( 4 ) )
262 ->method( 'getOptions' );
263
264 $this->mUserMock->expects( $this->at( 5 ) )
265 ->method( 'setOption' )
266 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
267
268 $this->mUserMock->expects( $this->once() )
269 ->method( 'saveSettings' );
270
271 $args = array(
272 'reset' => '',
273 'change' => 'willBeHappy=Happy',
274 'optionname' => 'name',
275 'optionvalue' => 'value'
276 );
277
278 $response = $this->executeQuery( $this->getSampleRequest( $args ) );
279
280 $this->assertEquals( self::$Success, $response );
281 }
282
283 public function testMultiSelect() {
284 $this->mUserMock->expects( $this->never() )
285 ->method( 'resetOptions' );
286
287 $this->mUserMock->expects( $this->at( 1 ) )
288 ->method( 'setOption' )
289 ->with( $this->equalTo( 'testmultiselect-opt1' ), $this->equalTo( true ) );
290
291 $this->mUserMock->expects( $this->at( 2 ) )
292 ->method( 'setOption' )
293 ->with( $this->equalTo( 'testmultiselect-opt2' ), $this->equalTo( false ) );
294
295 $this->mUserMock->expects( $this->at( 3 ) )
296 ->method( 'setOption' )
297 ->with( $this->equalTo( 'testmultiselect-opt3' ), $this->equalTo( false ) );
298
299 $this->mUserMock->expects( $this->at( 4 ) )
300 ->method( 'setOption' )
301 ->with( $this->equalTo( 'testmultiselect-opt4' ), $this->equalTo( false ) );
302
303 $this->mUserMock->expects( $this->once() )
304 ->method( 'saveSettings' );
305
306 $request = $this->getSampleRequest( array(
307 'change' => 'testmultiselect-opt1=1|testmultiselect-opt2|testmultiselect-opt3=|testmultiselect-opt4=0'
308 ) );
309
310 $response = $this->executeQuery( $request );
311
312 $this->assertEquals( self::$Success, $response );
313 }
314 }