MediaWiki  1.23.0
ApiOptionsTest.php
Go to the documentation of this file.
1 <?php
2 
11 
13  private $mUserMock;
15  private $mTested;
16  private $mSession;
18  private $mContext;
19 
20  private $mOldGetPreferencesHooks = false;
21 
22  private static $Success = array( 'options' => 'success' );
23 
24  protected function setUp() {
25  parent::setUp();
26 
27  $this->mUserMock = $this->getMockBuilder( 'User' )
28  ->disableOriginalConstructor()
29  ->getMock();
30 
31  // Set up groups and rights
32  $this->mUserMock->expects( $this->any() )
33  ->method( 'getEffectiveGroups' )->will( $this->returnValue( array( '*', 'user' ) ) );
34  $this->mUserMock->expects( $this->any() )
35  ->method( 'isAllowed' )->will( $this->returnValue( true ) );
36 
37  // Set up callback for User::getOptionKinds
38  $this->mUserMock->expects( $this->any() )
39  ->method( 'getOptionKinds' )->will( $this->returnCallback( array( $this, 'getOptionKinds' ) ) );
40 
41  // Create a new context
42  $this->mContext = new DerivativeContext( new RequestContext() );
43  $this->mContext->getContext()->setTitle( Title::newFromText( 'Test' ) );
44  $this->mContext->setUser( $this->mUserMock );
45 
46  $main = new ApiMain( $this->mContext );
47 
48  // Empty session
49  $this->mSession = array();
50 
51  $this->mTested = new ApiOptions( $main, 'options' );
52 
54  if ( !isset( $wgHooks['GetPreferences'] ) ) {
55  $wgHooks['GetPreferences'] = array();
56  }
57  $this->mOldGetPreferencesHooks = $wgHooks['GetPreferences'];
58  $wgHooks['GetPreferences'][] = array( $this, 'hookGetPreferences' );
59  }
60 
61  protected function tearDown() {
63 
64  if ( $this->mOldGetPreferencesHooks !== false ) {
65  $wgHooks['GetPreferences'] = $this->mOldGetPreferencesHooks;
66  $this->mOldGetPreferencesHooks = false;
67  }
68 
69  parent::tearDown();
70  }
71 
72  public function hookGetPreferences( $user, &$preferences ) {
73  $preferences = array();
74 
75  foreach ( array( 'name', 'willBeNull', 'willBeEmpty', 'willBeHappy' ) as $k ) {
76  $preferences[$k] = array(
77  'type' => 'text',
78  'section' => 'test',
79  'label' => '&#160;',
80  );
81  }
82 
83  $preferences['testmultiselect'] = array(
84  'type' => 'multiselect',
85  'options' => array(
86  'Test' => array(
87  '<span dir="auto">Some HTML here for option 1</span>' => 'opt1',
88  '<span dir="auto">Some HTML here for option 2</span>' => 'opt2',
89  '<span dir="auto">Some HTML here for option 3</span>' => 'opt3',
90  '<span dir="auto">Some HTML here for option 4</span>' => 'opt4',
91  ),
92  ),
93  'section' => 'test',
94  'label' => '&#160;',
95  'prefix' => 'testmultiselect-',
96  'default' => array(),
97  );
98 
99  return true;
100  }
101 
108  public function getOptionKinds( IContextSource $context, $options = null ) {
109  // Match with above.
110  $kinds = array(
111  'name' => 'registered',
112  'willBeNull' => 'registered',
113  'willBeEmpty' => 'registered',
114  'willBeHappy' => 'registered',
115  'testmultiselect-opt1' => 'registered-multiselect',
116  'testmultiselect-opt2' => 'registered-multiselect',
117  'testmultiselect-opt3' => 'registered-multiselect',
118  'testmultiselect-opt4' => 'registered-multiselect',
119  'special' => 'special',
120  );
121 
122  if ( $options === null ) {
123  return $kinds;
124  }
125 
126  $mapping = array();
127  foreach ( $options as $key => $value ) {
128  if ( isset( $kinds[$key] ) ) {
129  $mapping[$key] = $kinds[$key];
130  } elseif ( substr( $key, 0, 7 ) === 'userjs-' ) {
131  $mapping[$key] = 'userjs';
132  } else {
133  $mapping[$key] = 'unused';
134  }
135  }
136 
137  return $mapping;
138  }
139 
140  private function getSampleRequest( $custom = array() ) {
141  $request = array(
142  'token' => '123ABC',
143  'change' => null,
144  'optionname' => null,
145  'optionvalue' => null,
146  );
147 
148  return array_merge( $request, $custom );
149  }
150 
151  private function executeQuery( $request ) {
152  $this->mContext->setRequest( new FauxRequest( $request, true, $this->mSession ) );
153  $this->mTested->execute();
154 
155  return $this->mTested->getResult()->getData();
156  }
157 
161  public function testNoToken() {
162  $request = $this->getSampleRequest( array( 'token' => null ) );
163 
164  $this->executeQuery( $request );
165  }
166 
167  public function testAnon() {
168  $this->mUserMock->expects( $this->once() )
169  ->method( 'isAnon' )
170  ->will( $this->returnValue( true ) );
171 
172  try {
173  $request = $this->getSampleRequest();
174 
175  $this->executeQuery( $request );
176  } catch ( UsageException $e ) {
177  $this->assertEquals( 'notloggedin', $e->getCodeString() );
178  $this->assertEquals( 'Anonymous users cannot change preferences', $e->getMessage() );
179 
180  return;
181  }
182  $this->fail( "UsageException was not thrown" );
183  }
184 
185  public function testNoOptionname() {
186  try {
187  $request = $this->getSampleRequest( array( 'optionvalue' => '1' ) );
188 
189  $this->executeQuery( $request );
190  } catch ( UsageException $e ) {
191  $this->assertEquals( 'nooptionname', $e->getCodeString() );
192  $this->assertEquals( 'The optionname parameter must be set', $e->getMessage() );
193 
194  return;
195  }
196  $this->fail( "UsageException was not thrown" );
197  }
198 
199  public function testNoChanges() {
200  $this->mUserMock->expects( $this->never() )
201  ->method( 'resetOptions' );
202 
203  $this->mUserMock->expects( $this->never() )
204  ->method( 'setOption' );
205 
206  $this->mUserMock->expects( $this->never() )
207  ->method( 'saveSettings' );
208 
209  try {
210  $request = $this->getSampleRequest();
211 
212  $this->executeQuery( $request );
213  } catch ( UsageException $e ) {
214  $this->assertEquals( 'nochanges', $e->getCodeString() );
215  $this->assertEquals( 'No changes were requested', $e->getMessage() );
216 
217  return;
218  }
219  $this->fail( "UsageException was not thrown" );
220  }
221 
222  public function testReset() {
223  $this->mUserMock->expects( $this->once() )
224  ->method( 'resetOptions' )
225  ->with( $this->equalTo( array( 'all' ) ) );
226 
227  $this->mUserMock->expects( $this->never() )
228  ->method( 'setOption' );
229 
230  $this->mUserMock->expects( $this->once() )
231  ->method( 'saveSettings' );
232 
233  $request = $this->getSampleRequest( array( 'reset' => '' ) );
234 
235  $response = $this->executeQuery( $request );
236 
237  $this->assertEquals( self::$Success, $response );
238  }
239 
240  public function testResetKinds() {
241  $this->mUserMock->expects( $this->once() )
242  ->method( 'resetOptions' )
243  ->with( $this->equalTo( array( 'registered' ) ) );
244 
245  $this->mUserMock->expects( $this->never() )
246  ->method( 'setOption' );
247 
248  $this->mUserMock->expects( $this->once() )
249  ->method( 'saveSettings' );
250 
251  $request = $this->getSampleRequest( array( 'reset' => '', 'resetkinds' => 'registered' ) );
252 
253  $response = $this->executeQuery( $request );
254 
255  $this->assertEquals( self::$Success, $response );
256  }
257 
258  public function testOptionWithValue() {
259  $this->mUserMock->expects( $this->never() )
260  ->method( 'resetOptions' );
261 
262  $this->mUserMock->expects( $this->once() )
263  ->method( 'setOption' )
264  ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
265 
266  $this->mUserMock->expects( $this->once() )
267  ->method( 'saveSettings' );
268 
269  $request = $this->getSampleRequest( array( 'optionname' => 'name', 'optionvalue' => 'value' ) );
270 
271  $response = $this->executeQuery( $request );
272 
273  $this->assertEquals( self::$Success, $response );
274  }
275 
276  public function testOptionResetValue() {
277  $this->mUserMock->expects( $this->never() )
278  ->method( 'resetOptions' );
279 
280  $this->mUserMock->expects( $this->once() )
281  ->method( 'setOption' )
282  ->with( $this->equalTo( 'name' ), $this->identicalTo( null ) );
283 
284  $this->mUserMock->expects( $this->once() )
285  ->method( 'saveSettings' );
286 
287  $request = $this->getSampleRequest( array( 'optionname' => 'name' ) );
288  $response = $this->executeQuery( $request );
289 
290  $this->assertEquals( self::$Success, $response );
291  }
292 
293  public function testChange() {
294  $this->mUserMock->expects( $this->never() )
295  ->method( 'resetOptions' );
296 
297  $this->mUserMock->expects( $this->at( 2 ) )
298  ->method( 'getOptions' );
299 
300  $this->mUserMock->expects( $this->at( 4 ) )
301  ->method( 'setOption' )
302  ->with( $this->equalTo( 'willBeNull' ), $this->identicalTo( null ) );
303 
304  $this->mUserMock->expects( $this->at( 5 ) )
305  ->method( 'getOptions' );
306 
307  $this->mUserMock->expects( $this->at( 6 ) )
308  ->method( 'setOption' )
309  ->with( $this->equalTo( 'willBeEmpty' ), $this->equalTo( '' ) );
310 
311  $this->mUserMock->expects( $this->at( 7 ) )
312  ->method( 'getOptions' );
313 
314  $this->mUserMock->expects( $this->at( 8 ) )
315  ->method( 'setOption' )
316  ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
317 
318  $this->mUserMock->expects( $this->once() )
319  ->method( 'saveSettings' );
320 
321  $request = $this->getSampleRequest( array( 'change' => 'willBeNull|willBeEmpty=|willBeHappy=Happy' ) );
322 
323  $response = $this->executeQuery( $request );
324 
325  $this->assertEquals( self::$Success, $response );
326  }
327 
328  public function testResetChangeOption() {
329  $this->mUserMock->expects( $this->once() )
330  ->method( 'resetOptions' );
331 
332  $this->mUserMock->expects( $this->at( 4 ) )
333  ->method( 'getOptions' );
334 
335  $this->mUserMock->expects( $this->at( 5 ) )
336  ->method( 'setOption' )
337  ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
338 
339  $this->mUserMock->expects( $this->at( 6 ) )
340  ->method( 'getOptions' );
341 
342  $this->mUserMock->expects( $this->at( 7 ) )
343  ->method( 'setOption' )
344  ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
345 
346  $this->mUserMock->expects( $this->once() )
347  ->method( 'saveSettings' );
348 
349  $args = array(
350  'reset' => '',
351  'change' => 'willBeHappy=Happy',
352  'optionname' => 'name',
353  'optionvalue' => 'value'
354  );
355 
356  $response = $this->executeQuery( $this->getSampleRequest( $args ) );
357 
358  $this->assertEquals( self::$Success, $response );
359  }
360 
361  public function testMultiSelect() {
362  $this->mUserMock->expects( $this->never() )
363  ->method( 'resetOptions' );
364 
365  $this->mUserMock->expects( $this->at( 3 ) )
366  ->method( 'setOption' )
367  ->with( $this->equalTo( 'testmultiselect-opt1' ), $this->identicalTo( true ) );
368 
369  $this->mUserMock->expects( $this->at( 4 ) )
370  ->method( 'setOption' )
371  ->with( $this->equalTo( 'testmultiselect-opt2' ), $this->identicalTo( null ) );
372 
373  $this->mUserMock->expects( $this->at( 5 ) )
374  ->method( 'setOption' )
375  ->with( $this->equalTo( 'testmultiselect-opt3' ), $this->identicalTo( false ) );
376 
377  $this->mUserMock->expects( $this->at( 6 ) )
378  ->method( 'setOption' )
379  ->with( $this->equalTo( 'testmultiselect-opt4' ), $this->identicalTo( false ) );
380 
381  $this->mUserMock->expects( $this->once() )
382  ->method( 'saveSettings' );
383 
384  $request = $this->getSampleRequest( array(
385  'change' => 'testmultiselect-opt1=1|testmultiselect-opt2|testmultiselect-opt3=|testmultiselect-opt4=0'
386  ) );
387 
388  $response = $this->executeQuery( $request );
389 
390  $this->assertEquals( self::$Success, $response );
391  }
392 
393  public function testSpecialOption() {
394  $this->mUserMock->expects( $this->never() )
395  ->method( 'resetOptions' );
396 
397  $this->mUserMock->expects( $this->never() )
398  ->method( 'saveSettings' );
399 
400  $request = $this->getSampleRequest( array(
401  'change' => 'special=1'
402  ) );
403 
404  $response = $this->executeQuery( $request );
405 
406  $this->assertEquals( array(
407  'options' => 'success',
408  'warnings' => array(
409  'options' => array(
410  '*' => "Validation error for 'special': cannot be set by this module"
411  )
412  )
413  ), $response );
414  }
415 
416  public function testUnknownOption() {
417  $this->mUserMock->expects( $this->never() )
418  ->method( 'resetOptions' );
419 
420  $this->mUserMock->expects( $this->never() )
421  ->method( 'saveSettings' );
422 
423  $request = $this->getSampleRequest( array(
424  'change' => 'unknownOption=1'
425  ) );
426 
427  $response = $this->executeQuery( $request );
428 
429  $this->assertEquals( array(
430  'options' => 'success',
431  'warnings' => array(
432  'options' => array(
433  '*' => "Validation error for 'unknownOption': not a valid preference"
434  )
435  )
436  ), $response );
437  }
438 
439  public function testUserjsOption() {
440  $this->mUserMock->expects( $this->never() )
441  ->method( 'resetOptions' );
442 
443  $this->mUserMock->expects( $this->at( 3 ) )
444  ->method( 'setOption' )
445  ->with( $this->equalTo( 'userjs-option' ), $this->equalTo( '1' ) );
446 
447  $this->mUserMock->expects( $this->once() )
448  ->method( 'saveSettings' );
449 
450  $request = $this->getSampleRequest( array(
451  'change' => 'userjs-option=1'
452  ) );
453 
454  $response = $this->executeQuery( $request );
455 
456  $this->assertEquals( self::$Success, $response );
457  }
458 }
ApiMain
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:41
FauxRequest
WebRequest clone which takes values from a provided array.
Definition: WebRequest.php:1275
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:189
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
ApiOptionsTest\testReset
testReset()
Definition: ApiOptionsTest.php:219
$response
$response
Definition: opensearch_desc.php:32
ApiOptionsTest\testSpecialOption
testSpecialOption()
Definition: ApiOptionsTest.php:390
ApiOptionsTest\hookGetPreferences
hookGetPreferences( $user, &$preferences)
Definition: ApiOptionsTest.php:69
ApiOptionsTest\testResetKinds
testResetKinds()
Definition: ApiOptionsTest.php:237
fail
as a message key or array as accepted by ApiBase::dieUsageMsg after processing request parameters Return false to let the request fail
Definition: hooks.txt:375
$wgHooks
$wgHooks['ArticleShow'][]
Definition: hooks.txt:110
ApiOptionsTest\setUp
setUp()
Definition: ApiOptionsTest.php:21
ApiOptionsTest\executeQuery
executeQuery( $request)
Definition: ApiOptionsTest.php:148
ApiOptionsTest\testUnknownOption
testUnknownOption()
Definition: ApiOptionsTest.php:413
ApiOptionsTest\testNoChanges
testNoChanges()
Definition: ApiOptionsTest.php:196
DerivativeContext
An IContextSource implementation which will inherit context from another source but allow individual ...
Definition: DerivativeContext.php:32
ApiOptionsTest\$mTested
ApiOptions $mTested
Definition: ApiOptionsTest.php:13
UsageException
This exception will be thrown when dieUsage is called to stop module execution.
Definition: ApiMain.php:1406
ApiOptionsTest\$mContext
DerivativeContext $mContext
Definition: ApiOptionsTest.php:15
ApiOptionsTest\testUserjsOption
testUserjsOption()
Definition: ApiOptionsTest.php:436
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
ApiOptionsTest\$mSession
$mSession
Definition: ApiOptionsTest.php:14
ApiOptionsTest\testMultiSelect
testMultiSelect()
Definition: ApiOptionsTest.php:358
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
RequestContext
Group all the pieces relevant to the context of a request into one instance.
Definition: RequestContext.php:30
ApiOptionsTest\getOptionKinds
getOptionKinds(IContextSource $context, $options=null)
Definition: ApiOptionsTest.php:105
ApiOptionsTest\getSampleRequest
getSampleRequest( $custom=array())
Definition: ApiOptionsTest.php:137
$options
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition: hooks.txt:1530
ApiOptions
API module that facilitates the changing of user's preferences.
Definition: ApiOptions.php:33
ApiOptionsTest\tearDown
tearDown()
Definition: ApiOptionsTest.php:58
$value
$value
Definition: styleTest.css.php:45
ApiOptionsTest\testChange
testChange()
Definition: ApiOptionsTest.php:290
ApiOptionsTest\testNoToken
testNoToken()
@expectedException UsageException
Definition: ApiOptionsTest.php:158
ApiOptionsTest\$mUserMock
PHPUnit_Framework_MockObject_MockObject $mUserMock
Definition: ApiOptionsTest.php:12
ApiOptionsTest\testOptionWithValue
testOptionWithValue()
Definition: ApiOptionsTest.php:255
MediaWikiLangTestCase
Base class that store and restore the Language objects.
Definition: MediaWikiLangTestCase.php:6
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:237
ApiOptionsTest\$mOldGetPreferencesHooks
$mOldGetPreferencesHooks
Definition: ApiOptionsTest.php:17
IContextSource
Interface for objects which can provide a context on request.
Definition: IContextSource.php:29
$args
if( $line===false) $args
Definition: cdb.php:62
ApiOptionsTest\testNoOptionname
testNoOptionname()
Definition: ApiOptionsTest.php:182
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
ApiOptionsTest\testResetChangeOption
testResetChangeOption()
Definition: ApiOptionsTest.php:325
ApiOptionsTest
@group API @group Database @group medium
Definition: ApiOptionsTest.php:10
$e
if( $useReadline) $e
Definition: eval.php:66
ApiOptionsTest\$Success
static $Success
Definition: ApiOptionsTest.php:19
ApiOptionsTest\testOptionResetValue
testOptionResetValue()
Definition: ApiOptionsTest.php:273
ApiOptionsTest\testAnon
testAnon()
Definition: ApiOptionsTest.php:164