MediaWiki  1.23.1
StatusTest.php
Go to the documentation of this file.
1 <?php
2 
7 
8  public function testCanConstruct() {
9  new Status();
10  $this->assertTrue( true );
11  }
12 
17  public function testNewGood( $value = null ) {
18  $status = Status::newGood( $value );
19  $this->assertTrue( $status->isGood() );
20  $this->assertTrue( $status->isOK() );
21  $this->assertEquals( $value, $status->getValue() );
22  }
23 
24  public static function provideValues() {
25  return array(
26  array(),
27  array( 'foo' ),
28  array( array( 'foo' => 'bar' ) ),
29  array( new Exception() ),
30  array( 1234 ),
31  );
32  }
33 
37  public function testNewFatalWithMessage() {
38  $message = $this->getMockBuilder( 'Message' )
39  ->disableOriginalConstructor()
40  ->getMock();
41 
42  $status = Status::newFatal( $message );
43  $this->assertFalse( $status->isGood() );
44  $this->assertFalse( $status->isOK() );
45  $this->assertEquals( $message, $status->getMessage() );
46  }
47 
51  public function testNewFatalWithString() {
52  $message = 'foo';
53  $status = Status::newFatal( $message );
54  $this->assertFalse( $status->isGood() );
55  $this->assertFalse( $status->isOK() );
56  $this->assertEquals( $message, $status->getMessage()->getKey() );
57  }
58 
63  public function testSetResult( $ok, $value = null ) {
64  $status = new Status();
65  $status->setResult( $ok, $value );
66  $this->assertEquals( $ok, $status->isOK() );
67  $this->assertEquals( $value, $status->getValue() );
68  }
69 
70  public static function provideSetResult() {
71  return array(
72  array( true ),
73  array( false ),
74  array( true, 'value' ),
75  array( false, 'value' ),
76  );
77  }
78 
83  public function testIsOk( $ok ) {
84  $status = new Status();
85  $status->ok = $ok;
86  $this->assertEquals( $ok, $status->isOK() );
87  }
88 
89  public static function provideIsOk() {
90  return array(
91  array( true ),
92  array( false ),
93  );
94  }
95 
99  public function testGetValue() {
100  $status = new Status();
101  $status->value = 'foobar';
102  $this->assertEquals( 'foobar', $status->getValue() );
103  }
104 
109  public function testIsGood( $ok, $errors, $expected ) {
110  $status = new Status();
111  $status->ok = $ok;
112  $status->errors = $errors;
113  $this->assertEquals( $expected, $status->isGood() );
114  }
115 
116  public static function provideIsGood() {
117  return array(
118  array( true, array(), true ),
119  array( true, array( 'foo' ), false ),
120  array( false, array(), false ),
121  array( false, array( 'foo' ), false ),
122  );
123  }
124 
131  public function testWarningWithMessage( $mockDetails ) {
132  $status = new Status();
133  $messages = $this->getMockMessages( $mockDetails );
134 
135  foreach ( $messages as $message ) {
136  $status->warning( $message );
137  }
138  $warnings = $status->getWarningsArray();
139 
140  $this->assertEquals( count( $messages ), count( $warnings ) );
141  foreach ( $messages as $key => $message ) {
142  $expectedArray = array_merge( array( $message->getKey() ), $message->getParams() );
143  $this->assertEquals( $warnings[$key], $expectedArray );
144  }
145  }
146 
153  public function testErrorWithMessage( $mockDetails ) {
154  $status = new Status();
155  $messages = $this->getMockMessages( $mockDetails );
156 
157  foreach ( $messages as $message ) {
158  $status->error( $message );
159  }
160  $errors = $status->getErrorsArray();
161 
162  $this->assertEquals( count( $messages ), count( $errors ) );
163  foreach ( $messages as $key => $message ) {
164  $expectedArray = array_merge( array( $message->getKey() ), $message->getParams() );
165  $this->assertEquals( $errors[$key], $expectedArray );
166  }
167  }
168 
175  public function testFatalWithMessage( $mockDetails ) {
176  $status = new Status();
177  $messages = $this->getMockMessages( $mockDetails );
178 
179  foreach ( $messages as $message ) {
180  $status->fatal( $message );
181  }
182  $errors = $status->getErrorsArray();
183 
184  $this->assertEquals( count( $messages ), count( $errors ) );
185  foreach ( $messages as $key => $message ) {
186  $expectedArray = array_merge( array( $message->getKey() ), $message->getParams() );
187  $this->assertEquals( $errors[$key], $expectedArray );
188  }
189  $this->assertFalse( $status->isOK() );
190  }
191 
192  protected function getMockMessage( $key = 'key', $params = array() ) {
193  $message = $this->getMockBuilder( 'Message' )
194  ->disableOriginalConstructor()
195  ->getMock();
196  $message->expects( $this->atLeastOnce() )
197  ->method( 'getKey' )
198  ->will( $this->returnValue( $key ) );
199  $message->expects( $this->atLeastOnce() )
200  ->method( 'getParams' )
201  ->will( $this->returnValue( $params ) );
202  return $message;
203  }
204 
209  protected function getMockMessages( $messageDetails ) {
210  $messages = array();
211  foreach ( $messageDetails as $key => $paramsArray ) {
212  $messages[] = $this->getMockMessage( $key, $paramsArray );
213  }
214  return $messages;
215  }
216 
217  public static function provideMockMessageDetails() {
218  return array(
219  array( array( 'key1' => array( 'foo' => 'bar' ) ) ),
220  array( array( 'key1' => array( 'foo' => 'bar' ), 'key2' => array( 'foo2' => 'bar2' ) ) ),
221  );
222  }
223 
227  public function testMerge() {
228  $status1 = new Status();
229  $status2 = new Status();
230  $message1 = $this->getMockMessage( 'warn1' );
231  $message2 = $this->getMockMessage( 'error2' );
232  $status1->warning( $message1 );
233  $status2->error( $message2 );
234 
235  $status1->merge( $status2 );
236  $this->assertEquals( 2, count( $status1->getWarningsArray() ) + count( $status1->getErrorsArray() ) );
237  }
238 
242  public function testMergeWithOverwriteValue() {
243  $status1 = new Status();
244  $status2 = new Status();
245  $message1 = $this->getMockMessage( 'warn1' );
246  $message2 = $this->getMockMessage( 'error2' );
247  $status1->warning( $message1 );
248  $status2->error( $message2 );
249  $status2->value = 'FooValue';
250 
251  $status1->merge( $status2, true );
252  $this->assertEquals( 2, count( $status1->getWarningsArray() ) + count( $status1->getErrorsArray() ) );
253  $this->assertEquals( 'FooValue', $status1->getValue() );
254  }
255 
259  public function testHasMessage() {
260  $status = new Status();
261  $status->fatal( 'bad' );
262  $this->assertTrue( $status->hasMessage( 'bad' ) );
263  $this->assertFalse( $status->hasMessage( 'good' ) );
264  }
265 
270  public function testCleanParams( $cleanCallback, $params, $expected ) {
271  $method = new ReflectionMethod( 'Status', 'cleanParams' );
272  $method->setAccessible( true );
273  $status = new Status();
274  $status->cleanCallback = $cleanCallback;
275 
276  $this->assertEquals( $expected, $method->invoke( $status, $params ) );
277  }
278 
279  public static function provideCleanParams() {
280  $cleanCallback = function( $value ) {
281  return '-' . $value . '-';
282  };
283 
284  return array(
285  array( false, array( 'foo' => 'bar' ), array( 'foo' => 'bar' ) ),
286  array( $cleanCallback, array( 'foo' => 'bar' ), array( 'foo' => '-bar-' ) ),
287  );
288  }
289 
297  public function testGetWikiText( Status $status, $wikitext, $html ) {
298  $this->assertEquals( $wikitext, $status->getWikiText() );
299  }
300 
308  public function testGetHtml( Status $status, $wikitext, $html ) {
309  $this->assertEquals( $html, $status->getHTML() );
310  }
311 
317  public static function provideGetWikiTextAndHtml() {
318  $testCases = array();
319 
320  $testCases[ 'GoodStatus' ] = array(
321  new Status(),
322  "Internal error: Status::getWikiText called for a good result, this is incorrect\n",
323  "<p>Internal error: Status::getWikiText called for a good result, this is incorrect\n</p>",
324  );
325 
326  $status = new Status();
327  $status->ok = false;
328  $testCases[ 'GoodButNoError' ] = array(
329  $status,
330  "Internal error: Status::getWikiText: Invalid result object: no error text but not OK\n",
331  "<p>Internal error: Status::getWikiText: Invalid result object: no error text but not OK\n</p>",
332  );
333 
334  $status = new Status();
335  $status->warning( 'fooBar!' );
336  $testCases[ '1StringWarning' ] = array(
337  $status,
338  "<fooBar!>",
339  "<p>&lt;fooBar!&gt;\n</p>",
340  );
341 
342  $status = new Status();
343  $status->warning( 'fooBar!' );
344  $status->warning( 'fooBar2!' );
345  $testCases[ '2StringWarnings' ] = array(
346  $status,
347  "* <fooBar!>\n* <fooBar2!>\n",
348  "<ul>\n<li> &lt;fooBar!&gt;\n</li>\n<li> &lt;fooBar2!&gt;\n</li>\n</ul>\n",
349  );
350 
351  $status = new Status();
352  $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
353  $testCases[ '1MessageWarning' ] = array(
354  $status,
355  "<fooBar!>",
356  "<p>&lt;fooBar!&gt;\n</p>",
357  );
358 
359  $status = new Status();
360  $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
361  $status->warning( new Message( 'fooBar2!' ) );
362  $testCases[ '2MessageWarnings' ] = array(
363  $status,
364  "* <fooBar!>\n* <fooBar2!>\n",
365  "<ul>\n<li> &lt;fooBar!&gt;\n</li>\n<li> &lt;fooBar2!&gt;\n</li>\n</ul>\n",
366  );
367 
368  return $testCases;
369  }
370 
376  public function testGetMessage( Status $status, $expectedParams = array(), $expectedKey ) {
377  $message = $status->getMessage();
378  $this->assertInstanceOf( 'Message', $message );
379  $this->assertEquals( $expectedParams, $message->getParams(), 'Message::getParams' );
380  $this->assertEquals( $expectedKey, $message->getKey(), 'Message::getKey' );
381  }
382 
389  public static function provideGetMessage() {
390  $testCases = array();
391 
392  $testCases[ 'GoodStatus' ] = array(
393  new Status(),
394  array( "Status::getMessage called for a good result, this is incorrect\n" ),
395  'internalerror_info'
396  );
397 
398  $status = new Status();
399  $status->ok = false;
400  $testCases[ 'GoodButNoError' ] = array(
401  $status,
402  array( "Status::getMessage: Invalid result object: no error text but not OK\n" ),
403  'internalerror_info'
404  );
405 
406  $status = new Status();
407  $status->warning( 'fooBar!' );
408  $testCases[ '1StringWarning' ] = array(
409  $status,
410  array(),
411  'fooBar!'
412  );
413 
414  // FIXME: Assertion tries to compare a StubUserLang with a Language object, because
415  // "data providers are executed before both the call to the setUpBeforeClass static method
416  // and the first call to the setUp method. Because of that you can't access any variables
417  // you create there from within a data provider."
418  // http://phpunit.de/manual/3.7/en/writing-tests-for-phpunit.html
419 // $status = new Status();
420 // $status->warning( 'fooBar!' );
421 // $status->warning( 'fooBar2!' );
422 // $testCases[ '2StringWarnings' ] = array(
423 // $status,
424 // array( new Message( 'fooBar!' ), new Message( 'fooBar2!' ) ),
425 // "* \$1\n* \$2"
426 // );
427 
428  $status = new Status();
429  $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
430  $testCases[ '1MessageWarning' ] = array(
431  $status,
432  array( 'foo', 'bar' ),
433  'fooBar!'
434  );
435 
436  $status = new Status();
437  $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
438  $status->warning( new Message( 'fooBar2!' ) );
439  $testCases[ '2MessageWarnings' ] = array(
440  $status,
441  array( new Message( 'fooBar!', array( 'foo', 'bar' ) ), new Message( 'fooBar2!' ) ),
442  "* \$1\n* \$2"
443  );
444 
445  return $testCases;
446  }
447 
451  public function testReplaceMessage() {
452  $status = new Status();
453  $message = new Message( 'key1', array( 'foo1', 'bar1' ) );
454  $status->error( $message );
455  $newMessage = new Message( 'key2', array( 'foo2', 'bar2' ) );
456 
457  $status->replaceMessage( $message, $newMessage );
458 
459  $this->assertEquals( $newMessage, $status->errors[0]['message'] );
460  }
461 
465  public function testGetErrorMessage() {
466  $method = new ReflectionMethod( 'Status', 'getErrorMessage' );
467  $method->setAccessible( true );
468  $status = new Status();
469  $key = 'foo';
470  $params = array( 'bar' );
471 
473  $message = $method->invoke( $status, array_merge( array( $key ), $params ) );
474  $this->assertInstanceOf( 'Message', $message );
475  $this->assertEquals( $key, $message->getKey() );
476  $this->assertEquals( $params, $message->getParams() );
477  }
478 
482  public function testGetErrorMessageArray() {
483  $method = new ReflectionMethod( 'Status', 'getErrorMessageArray' );
484  $method->setAccessible( true );
485  $status = new Status();
486  $key = 'foo';
487  $params = array( 'bar' );
488 
490  $messageArray = $method->invoke(
491  $status,
492  array(
493  array_merge( array( $key ), $params ),
494  array_merge( array( $key ), $params )
495  )
496  );
497 
498  $this->assertInternalType( 'array', $messageArray );
499  $this->assertCount( 2, $messageArray );
500  foreach ( $messageArray as $message ) {
501  $this->assertInstanceOf( 'Message', $message );
502  $this->assertEquals( $key, $message->getKey() );
503  $this->assertEquals( $params, $message->getParams() );
504  }
505  }
506 
510  public function testGetErrorsByType() {
511  $status = new Status();
512  $warning = new Message( 'warning111' );
513  $error = new Message( 'error111' );
514  $status->warning( $warning );
515  $status->error( $error );
516 
517  $warnings = $status->getErrorsByType( 'warning' );
518  $errors = $status->getErrorsByType( 'error' );
519 
520  $this->assertCount( 1, $warnings );
521  $this->assertCount( 1, $errors );
522  $this->assertEquals( $warning, $warnings[0]['message'] );
523  $this->assertEquals( $error, $errors[0]['message'] );
524  }
525 
529  public function testWakeUpSanitizesCallback() {
530  $status = new Status();
531  $status->cleanCallback = function( $value ) {
532  return '-' . $value . '-';
533  };
534  $status->__wakeup();
535  $this->assertEquals( false, $status->cleanCallback );
536  }
537 
542  public function testGetStatusArrayWithNonObjectMessages( $nonObjMsg ) {
543  $status = new Status();
544  if ( !array_key_exists( 1, $nonObjMsg ) ) {
545  $status->warning( $nonObjMsg[0] );
546  } else {
547  $status->warning( $nonObjMsg[0], $nonObjMsg[1] );
548  }
549 
550  $array = $status->getWarningsArray(); // We use getWarningsArray to access getStatusArray
551 
552  $this->assertEquals( 1, count( $array ) );
553  $this->assertEquals( $nonObjMsg, $array[0] );
554  }
555 
556  public static function provideNonObjectMessages() {
557  return array(
558  array( array( 'ImaString', array( 'param1' => 'value1' ) ) ),
559  array( array( 'ImaString' ) ),
560  );
561  }
562 
563 }
StatusTest\provideSetResult
static provideSetResult()
Definition: StatusTest.php:70
StatusTest\provideGetWikiTextAndHtml
static provideGetWikiTextAndHtml()
Definition: StatusTest.php:317
Status\getHTML
getHTML( $shortContext=false, $longContext=false)
Get the error message as HTML.
Definition: Status.php:304
StatusTest\provideCleanParams
static provideCleanParams()
Definition: StatusTest.php:279
StatusTest\provideGetMessage
static provideGetMessage()
Definition: StatusTest.php:389
StatusTest\testGetErrorsByType
testGetErrorsByType()
@covers Status::getErrorsByType
Definition: StatusTest.php:510
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
StatusTest\testGetStatusArrayWithNonObjectMessages
testGetStatusArrayWithNonObjectMessages( $nonObjMsg)
@dataProvider provideNonObjectMessages @covers Status::getStatusArray
Definition: StatusTest.php:542
$html
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 noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses & $html
Definition: hooks.txt:1530
StatusTest\testGetWikiText
testGetWikiText(Status $status, $wikitext, $html)
@dataProvider provideGetWikiTextAndHtml @covers Status::getWikiText
Definition: StatusTest.php:297
StatusTest\provideMockMessageDetails
static provideMockMessageDetails()
Definition: StatusTest.php:217
StatusTest\testNewGood
testNewGood( $value=null)
@dataProvider provideValues @covers Status::newGood
Definition: StatusTest.php:17
StatusTest\provideValues
static provideValues()
Definition: StatusTest.php:24
StatusTest\testSetResult
testSetResult( $ok, $value=null)
@dataProvider provideSetResult @covers Status::setResult
Definition: StatusTest.php:63
Status\newGood
static newGood( $value=null)
Factory function for good results.
Definition: Status.php:77
Status\getErrorsByType
getErrorsByType( $type)
Returns a list of status messages of the given type, with message and params left untouched,...
Definition: Status.php:388
$params
$params
Definition: styleTest.css.php:40
StatusTest\testCleanParams
testCleanParams( $cleanCallback, $params, $expected)
@dataProvider provideCleanParams @covers Status::cleanParams
Definition: StatusTest.php:270
$messages
$messages
Definition: LogTests.i18n.php:8
StatusTest\testErrorWithMessage
testErrorWithMessage( $mockDetails)
@dataProvider provideMockMessageDetails @covers Status::error @covers Status::getErrorsArray @covers ...
Definition: StatusTest.php:153
StatusTest\getMockMessage
getMockMessage( $key='key', $params=array())
Definition: StatusTest.php:192
Status
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: Status.php:40
StatusTest
Definition: StatusTest.php:6
StatusTest\testGetErrorMessage
testGetErrorMessage()
@covers Status::getErrorMessage
Definition: StatusTest.php:465
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
StatusTest\testCanConstruct
testCanConstruct()
Definition: StatusTest.php:8
StatusTest\testReplaceMessage
testReplaceMessage()
@covers Status::replaceMessage
Definition: StatusTest.php:451
StatusTest\testMergeWithOverwriteValue
testMergeWithOverwriteValue()
@covers Status::merge
Definition: StatusTest.php:242
$ok
$ok
Definition: UtfNormalTest.php:71
$value
$value
Definition: styleTest.css.php:45
StatusTest\testNewFatalWithMessage
testNewFatalWithMessage()
@covers Status::newFatal
Definition: StatusTest.php:37
StatusTest\testMerge
testMerge()
@covers Status::merge
Definition: StatusTest.php:227
Status\__wakeup
__wakeup()
Sanitize the callback parameter on wakeup, to avoid arbitrary execution.
Definition: Status.php:158
StatusTest\testGetErrorMessageArray
testGetErrorMessageArray()
@covers Status::getErrorMessageArray
Definition: StatusTest.php:482
MediaWikiLangTestCase
Base class that store and restore the Language objects.
Definition: MediaWikiLangTestCase.php:6
StatusTest\testWarningWithMessage
testWarningWithMessage( $mockDetails)
@dataProvider provideMockMessageDetails @covers Status::warning @covers Status::getWarningsArray @cov...
Definition: StatusTest.php:131
Status\getMessage
getMessage( $shortContext=false, $longContext=false)
Get the error list as a Message object.
Definition: Status.php:227
StatusTest\testIsOk
testIsOk( $ok)
@dataProvider provideIsOk @covers Status::isOk
Definition: StatusTest.php:83
Status\warning
warning( $message)
Add a new warning.
Definition: Status.php:118
StatusTest\provideIsOk
static provideIsOk()
Definition: StatusTest.php:89
StatusTest\testGetValue
testGetValue()
@covers Status::getValue
Definition: StatusTest.php:99
StatusTest\testGetMessage
testGetMessage(Status $status, $expectedParams=array(), $expectedKey)
@dataProvider provideGetMessage @covers Status::getMessage
Definition: StatusTest.php:376
StatusTest\provideNonObjectMessages
static provideNonObjectMessages()
Definition: StatusTest.php:556
StatusTest\testNewFatalWithString
testNewFatalWithString()
@covers Status::newFatal
Definition: StatusTest.php:51
StatusTest\provideIsGood
static provideIsGood()
Definition: StatusTest.php:116
StatusTest\getMockMessages
getMockMessages( $messageDetails)
Definition: StatusTest.php:209
Status\getWarningsArray
getWarningsArray()
Get the list of warnings (but not errors)
Definition: Status.php:351
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
StatusTest\testFatalWithMessage
testFatalWithMessage( $mockDetails)
@dataProvider provideMockMessageDetails @covers Status::fatal @covers Status::getErrorsArray @covers ...
Definition: StatusTest.php:175
StatusTest\testHasMessage
testHasMessage()
@covers Status::hasMessage
Definition: StatusTest.php:259
$error
usually copyright or history_copyright This message must be in HTML not wikitext $subpages will be ignored and the rest of subPageSubtitle() will run. 'SkinTemplateBuildNavUrlsNav_urlsAfterPermalink' whether MediaWiki currently thinks this is a CSS JS page Hooks may change this value to override the return value of Title::isCssOrJsPage(). 'TitleIsAlwaysKnown' whether MediaWiki currently thinks this page is known isMovable() always returns false. $title whether MediaWiki currently thinks this page is movable Hooks may change this value to override the return value of Title::isMovable(). 'TitleIsWikitextPage' whether MediaWiki currently thinks this is a wikitext page Hooks may change this value to override the return value of Title::isWikitextPage() 'TitleMove' use UploadVerification and UploadVerifyFile instead where the first element is the message key and the remaining elements are used as parameters to the message based on mime etc Preferred in most cases over UploadVerification object with all info about the upload string as detected by MediaWiki Handlers will typically only apply for specific mime types object & $error
Definition: hooks.txt:2573
Status\replaceMessage
replaceMessage( $source, $dest)
If the specified source message exists, replace it with the specified destination message,...
Definition: Status.php:427
StatusTest\testWakeUpSanitizesCallback
testWakeUpSanitizesCallback()
@covers Status::__wakeup
Definition: StatusTest.php:529
StatusTest\testGetHtml
testGetHtml(Status $status, $wikitext, $html)
@dataProvider provideGetWikiTextAndHtml @covers Status::getHtml
Definition: StatusTest.php:308
Status\error
error( $message)
Add an error, do not set fatal flag This can be used for non-fatal errors.
Definition: Status.php:132
StatusTest\testIsGood
testIsGood( $ok, $errors, $expected)
@dataProvider provideIsGood @covers Status::isGood
Definition: StatusTest.php:109
Status\getWikiText
getWikiText( $shortContext=false, $longContext=false)
Get the error list as a wikitext formatted list.
Definition: Status.php:185
Status\newFatal
static newFatal( $message)
Factory function for fatal errors.
Definition: Status.php:63