MediaWiki  1.28.1
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 static $Success = [ 'options' => 'success' ];
21 
22  protected function setUp() {
23  parent::setUp();
24 
25  $this->mUserMock = $this->getMockBuilder( 'User' )
26  ->disableOriginalConstructor()
27  ->getMock();
28 
29  // Set up groups and rights
30  $this->mUserMock->expects( $this->any() )
31  ->method( 'getEffectiveGroups' )->will( $this->returnValue( [ '*', 'user' ] ) );
32  $this->mUserMock->expects( $this->any() )
33  ->method( 'isAllowed' )->will( $this->returnValue( true ) );
34 
35  // Set up callback for User::getOptionKinds
36  $this->mUserMock->expects( $this->any() )
37  ->method( 'getOptionKinds' )->will( $this->returnCallback( [ $this, 'getOptionKinds' ] ) );
38 
39  // No actual DB data
40  $this->mUserMock->expects( $this->any() )
41  ->method( 'getInstanceForUpdate' )->will( $this->returnValue( $this->mUserMock ) );
42 
43  // Create a new context
44  $this->mContext = new DerivativeContext( new RequestContext() );
45  $this->mContext->getContext()->setTitle( Title::newFromText( 'Test' ) );
46  $this->mContext->setUser( $this->mUserMock );
47 
48  $main = new ApiMain( $this->mContext );
49 
50  // Empty session
51  $this->mSession = [];
52 
53  $this->mTested = new ApiOptions( $main, 'options' );
54 
55  $this->mergeMwGlobalArrayValue( 'wgHooks', [
56  'GetPreferences' => [
57  [ $this, 'hookGetPreferences' ]
58  ]
59  ] );
60  }
61 
62  public function hookGetPreferences( $user, &$preferences ) {
63  $preferences = [];
64 
65  foreach ( [ 'name', 'willBeNull', 'willBeEmpty', 'willBeHappy' ] as $k ) {
66  $preferences[$k] = [
67  'type' => 'text',
68  'section' => 'test',
69  'label' => '&#160;',
70  ];
71  }
72 
73  $preferences['testmultiselect'] = [
74  'type' => 'multiselect',
75  'options' => [
76  'Test' => [
77  '<span dir="auto">Some HTML here for option 1</span>' => 'opt1',
78  '<span dir="auto">Some HTML here for option 2</span>' => 'opt2',
79  '<span dir="auto">Some HTML here for option 3</span>' => 'opt3',
80  '<span dir="auto">Some HTML here for option 4</span>' => 'opt4',
81  ],
82  ],
83  'section' => 'test',
84  'label' => '&#160;',
85  'prefix' => 'testmultiselect-',
86  'default' => [],
87  ];
88 
89  return true;
90  }
91 
98  public function getOptionKinds( IContextSource $context, $options = null ) {
99  // Match with above.
100  $kinds = [
101  'name' => 'registered',
102  'willBeNull' => 'registered',
103  'willBeEmpty' => 'registered',
104  'willBeHappy' => 'registered',
105  'testmultiselect-opt1' => 'registered-multiselect',
106  'testmultiselect-opt2' => 'registered-multiselect',
107  'testmultiselect-opt3' => 'registered-multiselect',
108  'testmultiselect-opt4' => 'registered-multiselect',
109  'special' => 'special',
110  ];
111 
112  if ( $options === null ) {
113  return $kinds;
114  }
115 
116  $mapping = [];
117  foreach ( $options as $key => $value ) {
118  if ( isset( $kinds[$key] ) ) {
119  $mapping[$key] = $kinds[$key];
120  } elseif ( substr( $key, 0, 7 ) === 'userjs-' ) {
121  $mapping[$key] = 'userjs';
122  } else {
123  $mapping[$key] = 'unused';
124  }
125  }
126 
127  return $mapping;
128  }
129 
130  private function getSampleRequest( $custom = [] ) {
131  $request = [
132  'token' => '123ABC',
133  'change' => null,
134  'optionname' => null,
135  'optionvalue' => null,
136  ];
137 
138  return array_merge( $request, $custom );
139  }
140 
141  private function executeQuery( $request ) {
142  $this->mContext->setRequest( new FauxRequest( $request, true, $this->mSession ) );
143  $this->mTested->execute();
144 
145  return $this->mTested->getResult()->getResultData( null, [ 'Strip' => 'all' ] );
146  }
147 
151  public function testNoToken() {
152  $request = $this->getSampleRequest( [ 'token' => null ] );
153 
154  $this->executeQuery( $request );
155  }
156 
157  public function testAnon() {
158  $this->mUserMock->expects( $this->once() )
159  ->method( 'isAnon' )
160  ->will( $this->returnValue( true ) );
161 
162  try {
163  $request = $this->getSampleRequest();
164 
165  $this->executeQuery( $request );
166  } catch ( UsageException $e ) {
167  $this->assertEquals( 'notloggedin', $e->getCodeString() );
168  $this->assertEquals( 'Anonymous users cannot change preferences', $e->getMessage() );
169 
170  return;
171  }
172  $this->fail( "UsageException was not thrown" );
173  }
174 
175  public function testNoOptionname() {
176  try {
177  $request = $this->getSampleRequest( [ 'optionvalue' => '1' ] );
178 
179  $this->executeQuery( $request );
180  } catch ( UsageException $e ) {
181  $this->assertEquals( 'nooptionname', $e->getCodeString() );
182  $this->assertEquals( 'The optionname parameter must be set', $e->getMessage() );
183 
184  return;
185  }
186  $this->fail( "UsageException was not thrown" );
187  }
188 
189  public function testNoChanges() {
190  $this->mUserMock->expects( $this->never() )
191  ->method( 'resetOptions' );
192 
193  $this->mUserMock->expects( $this->never() )
194  ->method( 'setOption' );
195 
196  $this->mUserMock->expects( $this->never() )
197  ->method( 'saveSettings' );
198 
199  try {
200  $request = $this->getSampleRequest();
201 
202  $this->executeQuery( $request );
203  } catch ( UsageException $e ) {
204  $this->assertEquals( 'nochanges', $e->getCodeString() );
205  $this->assertEquals( 'No changes were requested', $e->getMessage() );
206 
207  return;
208  }
209  $this->fail( "UsageException was not thrown" );
210  }
211 
212  public function testReset() {
213  $this->mUserMock->expects( $this->once() )
214  ->method( 'resetOptions' )
215  ->with( $this->equalTo( [ 'all' ] ) );
216 
217  $this->mUserMock->expects( $this->never() )
218  ->method( 'setOption' );
219 
220  $this->mUserMock->expects( $this->once() )
221  ->method( 'saveSettings' );
222 
223  $request = $this->getSampleRequest( [ 'reset' => '' ] );
224 
225  $response = $this->executeQuery( $request );
226 
227  $this->assertEquals( self::$Success, $response );
228  }
229 
230  public function testResetKinds() {
231  $this->mUserMock->expects( $this->once() )
232  ->method( 'resetOptions' )
233  ->with( $this->equalTo( [ 'registered' ] ) );
234 
235  $this->mUserMock->expects( $this->never() )
236  ->method( 'setOption' );
237 
238  $this->mUserMock->expects( $this->once() )
239  ->method( 'saveSettings' );
240 
241  $request = $this->getSampleRequest( [ 'reset' => '', 'resetkinds' => 'registered' ] );
242 
243  $response = $this->executeQuery( $request );
244 
245  $this->assertEquals( self::$Success, $response );
246  }
247 
248  public function testOptionWithValue() {
249  $this->mUserMock->expects( $this->never() )
250  ->method( 'resetOptions' );
251 
252  $this->mUserMock->expects( $this->once() )
253  ->method( 'setOption' )
254  ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
255 
256  $this->mUserMock->expects( $this->once() )
257  ->method( 'saveSettings' );
258 
259  $request = $this->getSampleRequest( [ 'optionname' => 'name', 'optionvalue' => 'value' ] );
260 
261  $response = $this->executeQuery( $request );
262 
263  $this->assertEquals( self::$Success, $response );
264  }
265 
266  public function testOptionResetValue() {
267  $this->mUserMock->expects( $this->never() )
268  ->method( 'resetOptions' );
269 
270  $this->mUserMock->expects( $this->once() )
271  ->method( 'setOption' )
272  ->with( $this->equalTo( 'name' ), $this->identicalTo( null ) );
273 
274  $this->mUserMock->expects( $this->once() )
275  ->method( 'saveSettings' );
276 
277  $request = $this->getSampleRequest( [ 'optionname' => 'name' ] );
278  $response = $this->executeQuery( $request );
279 
280  $this->assertEquals( self::$Success, $response );
281  }
282 
283  public function testChange() {
284  $this->mUserMock->expects( $this->never() )
285  ->method( 'resetOptions' );
286 
287  $this->mUserMock->expects( $this->at( 2 ) )
288  ->method( 'getOptions' );
289 
290  $this->mUserMock->expects( $this->at( 5 ) )
291  ->method( 'setOption' )
292  ->with( $this->equalTo( 'willBeNull' ), $this->identicalTo( null ) );
293 
294  $this->mUserMock->expects( $this->at( 6 ) )
295  ->method( 'getOptions' );
296 
297  $this->mUserMock->expects( $this->at( 7 ) )
298  ->method( 'setOption' )
299  ->with( $this->equalTo( 'willBeEmpty' ), $this->equalTo( '' ) );
300 
301  $this->mUserMock->expects( $this->at( 8 ) )
302  ->method( 'getOptions' );
303 
304  $this->mUserMock->expects( $this->at( 9 ) )
305  ->method( 'setOption' )
306  ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
307 
308  $this->mUserMock->expects( $this->once() )
309  ->method( 'saveSettings' );
310 
311  $request = $this->getSampleRequest( [
312  'change' => 'willBeNull|willBeEmpty=|willBeHappy=Happy'
313  ] );
314 
315  $response = $this->executeQuery( $request );
316 
317  $this->assertEquals( self::$Success, $response );
318  }
319 
320  public function testResetChangeOption() {
321  $this->mUserMock->expects( $this->once() )
322  ->method( 'resetOptions' );
323 
324  $this->mUserMock->expects( $this->at( 5 ) )
325  ->method( 'getOptions' );
326 
327  $this->mUserMock->expects( $this->at( 6 ) )
328  ->method( 'setOption' )
329  ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
330 
331  $this->mUserMock->expects( $this->at( 7 ) )
332  ->method( 'getOptions' );
333 
334  $this->mUserMock->expects( $this->at( 8 ) )
335  ->method( 'setOption' )
336  ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
337 
338  $this->mUserMock->expects( $this->once() )
339  ->method( 'saveSettings' );
340 
341  $args = [
342  'reset' => '',
343  'change' => 'willBeHappy=Happy',
344  'optionname' => 'name',
345  'optionvalue' => 'value'
346  ];
347 
348  $response = $this->executeQuery( $this->getSampleRequest( $args ) );
349 
350  $this->assertEquals( self::$Success, $response );
351  }
352 
353  public function testMultiSelect() {
354  $this->mUserMock->expects( $this->never() )
355  ->method( 'resetOptions' );
356 
357  $this->mUserMock->expects( $this->at( 4 ) )
358  ->method( 'setOption' )
359  ->with( $this->equalTo( 'testmultiselect-opt1' ), $this->identicalTo( true ) );
360 
361  $this->mUserMock->expects( $this->at( 5 ) )
362  ->method( 'setOption' )
363  ->with( $this->equalTo( 'testmultiselect-opt2' ), $this->identicalTo( null ) );
364 
365  $this->mUserMock->expects( $this->at( 6 ) )
366  ->method( 'setOption' )
367  ->with( $this->equalTo( 'testmultiselect-opt3' ), $this->identicalTo( false ) );
368 
369  $this->mUserMock->expects( $this->at( 7 ) )
370  ->method( 'setOption' )
371  ->with( $this->equalTo( 'testmultiselect-opt4' ), $this->identicalTo( false ) );
372 
373  $this->mUserMock->expects( $this->once() )
374  ->method( 'saveSettings' );
375 
376  $request = $this->getSampleRequest( [
377  'change' => 'testmultiselect-opt1=1|testmultiselect-opt2|'
378  . 'testmultiselect-opt3=|testmultiselect-opt4=0'
379  ] );
380 
381  $response = $this->executeQuery( $request );
382 
383  $this->assertEquals( self::$Success, $response );
384  }
385 
386  public function testSpecialOption() {
387  $this->mUserMock->expects( $this->never() )
388  ->method( 'resetOptions' );
389 
390  $this->mUserMock->expects( $this->never() )
391  ->method( 'saveSettings' );
392 
393  $request = $this->getSampleRequest( [
394  'change' => 'special=1'
395  ] );
396 
397  $response = $this->executeQuery( $request );
398 
399  $this->assertEquals( [
400  'options' => 'success',
401  'warnings' => [
402  'options' => [
403  'warnings' => "Validation error for 'special': cannot be set by this module"
404  ]
405  ]
406  ], $response );
407  }
408 
409  public function testUnknownOption() {
410  $this->mUserMock->expects( $this->never() )
411  ->method( 'resetOptions' );
412 
413  $this->mUserMock->expects( $this->never() )
414  ->method( 'saveSettings' );
415 
416  $request = $this->getSampleRequest( [
417  'change' => 'unknownOption=1'
418  ] );
419 
420  $response = $this->executeQuery( $request );
421 
422  $this->assertEquals( [
423  'options' => 'success',
424  'warnings' => [
425  'options' => [
426  'warnings' => "Validation error for 'unknownOption': not a valid preference"
427  ]
428  ]
429  ], $response );
430  }
431 
432  public function testUserjsOption() {
433  $this->mUserMock->expects( $this->never() )
434  ->method( 'resetOptions' );
435 
436  $this->mUserMock->expects( $this->once() )
437  ->method( 'setOption' )
438  ->with( $this->equalTo( 'userjs-option' ), $this->equalTo( '1' ) );
439 
440  $this->mUserMock->expects( $this->once() )
441  ->method( 'saveSettings' );
442 
443  $request = $this->getSampleRequest( [
444  'change' => 'userjs-option=1'
445  ] );
446 
447  $response = $this->executeQuery( $request );
448 
449  $this->assertEquals( self::$Success, $response );
450  }
451 }
mergeMwGlobalArrayValue($name, $values)
Merges the given values into a MW global array variable.
Interface for objects which can provide a MediaWiki context on request.
hookGetPreferences($user, &$preferences)
Group all the pieces relevant to the context of a request into one instance.
$context
Definition: load.php:50
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException'returning false will NOT prevent logging $e
Definition: hooks.txt:2102
An IContextSource implementation which will inherit context from another source but allow individual ...
$value
API Database medium.
static newFromText($text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:262
this hook is for auditing only $response
Definition: hooks.txt:802
getOptionKinds(IContextSource $context, $options=null)
if($line===false) $args
Definition: cdb.php:64
getSampleRequest($custom=[])
ApiOptions $mTested
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context $options
Definition: hooks.txt:1046
Base class that store and restore the Language objects.
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:43
DerivativeContext $mContext
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
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 local account $user
Definition: hooks.txt:242
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
API module that facilitates the changing of user's preferences.
Definition: ApiOptions.php:33
error also a ContextSource you ll probably need to make sure the header is varied on $request
Definition: hooks.txt:2573
executeQuery($request)
testNoToken()
UsageException.
PHPUnit_Framework_MockObject_MockObject $mUserMock
This exception will be thrown when dieUsage is called to stop module execution.
Definition: ApiMain.php:1860