209b54c3e62e84d2ccbf02b4358fd8f7610ba495
4 * @author Adam Shorland
6 class StatusTest
extends MediaWikiLangTestCase
{
8 public function testCanConstruct() {
10 $this->assertTrue( true );
14 * @dataProvider provideValues
15 * @covers Status::newGood
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() );
24 public static function provideValues() {
28 array( array( 'foo' => 'bar' ) ),
29 array( new Exception() ),
35 * @covers Status::newFatal
37 public function testNewFatalWithMessage() {
38 $message = $this->getMockBuilder( 'Message' )
39 ->disableOriginalConstructor()
42 $status = Status
::newFatal( $message );
43 $this->assertFalse( $status->isGood() );
44 $this->assertFalse( $status->isOK() );
45 $this->assertEquals( $message, $status->getMessage() );
49 * @covers Status::newFatal
51 public function testNewFatalWithString() {
53 $status = Status
::newFatal( $message );
54 $this->assertFalse( $status->isGood() );
55 $this->assertFalse( $status->isOK() );
56 $this->assertEquals( $message, $status->getMessage()->getKey() );
60 * @dataProvider provideSetResult
61 * @covers Status::setResult
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() );
70 public static function provideSetResult() {
74 array( true, 'value' ),
75 array( false, 'value' ),
80 * @dataProvider provideIsOk
81 * @covers Status::isOk
83 public function testIsOk( $ok ) {
84 $status = new Status();
86 $this->assertEquals( $ok, $status->isOK() );
89 public static function provideIsOk() {
97 * @covers Status::getValue
99 public function testGetValue() {
100 $status = new Status();
101 $status->value
= 'foobar';
102 $this->assertEquals( 'foobar', $status->getValue() );
106 * @dataProvider provideIsGood
107 * @covers Status::isGood
109 public function testIsGood( $ok, $errors, $expected ) {
110 $status = new Status();
112 $status->errors
= $errors;
113 $this->assertEquals( $expected, $status->isGood() );
116 public static function provideIsGood() {
118 array( true, array(), true ),
119 array( true, array( 'foo' ), false ),
120 array( false, array(), false ),
121 array( false, array( 'foo' ), false ),
126 * @dataProvider provideMockMessageDetails
127 * @covers Status::warning
128 * @covers Status::getWarningsArray
129 * @covers Status::getStatusArray
131 public function testWarningWithMessage( $mockDetails ) {
132 $status = new Status();
133 $messages = $this->getMockMessages( $mockDetails );
135 foreach ( $messages as $message ) {
136 $status->warning( $message );
138 $warnings = $status->getWarningsArray();
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 );
148 * @dataProvider provideMockMessageDetails
149 * @covers Status::error
150 * @covers Status::getErrorsArray
151 * @covers Status::getStatusArray
153 public function testErrorWithMessage( $mockDetails ) {
154 $status = new Status();
155 $messages = $this->getMockMessages( $mockDetails );
157 foreach ( $messages as $message ) {
158 $status->error( $message );
160 $errors = $status->getErrorsArray();
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 );
170 * @dataProvider provideMockMessageDetails
171 * @covers Status::fatal
172 * @covers Status::getErrorsArray
173 * @covers Status::getStatusArray
175 public function testFatalWithMessage( $mockDetails ) {
176 $status = new Status();
177 $messages = $this->getMockMessages( $mockDetails );
179 foreach ( $messages as $message ) {
180 $status->fatal( $message );
182 $errors = $status->getErrorsArray();
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 );
189 $this->assertFalse( $status->isOK() );
192 protected function getMockMessage( $key = 'key', $params = array() ) {
193 $message = $this->getMockBuilder( 'Message' )
194 ->disableOriginalConstructor()
196 $message->expects( $this->atLeastOnce() )
198 ->will( $this->returnValue( $key ) );
199 $message->expects( $this->atLeastOnce() )
200 ->method( 'getParams' )
201 ->will( $this->returnValue( $params ) );
206 * @param array $messageDetails eg. array( 'KEY' => array(/PARAMS/) )
209 protected function getMockMessages( $messageDetails ) {
211 foreach ( $messageDetails as $key => $paramsArray ) {
212 $messages[] = $this->getMockMessage( $key, $paramsArray );
217 public static function provideMockMessageDetails() {
219 array( array( 'key1' => array( 'foo' => 'bar' ) ) ),
220 array( array( 'key1' => array( 'foo' => 'bar' ), 'key2' => array( 'foo2' => 'bar2' ) ) ),
225 * @covers Status::merge
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 );
235 $status1->merge( $status2 );
236 $this->assertEquals( 2, count( $status1->getWarningsArray() ) +
count( $status1->getErrorsArray() ) );
240 * @covers Status::merge
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';
251 $status1->merge( $status2, true );
252 $this->assertEquals( 2, count( $status1->getWarningsArray() ) +
count( $status1->getErrorsArray() ) );
253 $this->assertEquals( 'FooValue', $status1->getValue() );
257 * @covers Status::hasMessage
259 public function testHasMessage() {
260 $status = new Status();
261 $status->fatal( 'bad' );
262 $this->assertTrue( $status->hasMessage( 'bad' ) );
263 $this->assertFalse( $status->hasMessage( 'good' ) );
267 * @dataProvider provideCleanParams
268 * @covers Status::cleanParams
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;
276 $this->assertEquals( $expected, $method->invoke( $status, $params ) );
279 public static function provideCleanParams() {
280 $cleanCallback = function( $value ) {
281 return '-' . $value . '-';
285 array( false, array( 'foo' => 'bar' ), array( 'foo' => 'bar' ) ),
286 array( $cleanCallback, array( 'foo' => 'bar' ), array( 'foo' => '-bar-' ) ),
291 * @dataProvider provideGetWikiTextAndHtml
292 * @covers Status::getWikiText
293 * @todo test long and short context messages generated through this method
294 * this can not really be done now due to use of wfMessage()->plain()
295 * It is possible to mock such methods but only if namespaces are used
297 public function testGetWikiText( Status
$status, $wikitext, $html ) {
298 $this->assertEquals( $wikitext, $status->getWikiText() );
302 * @dataProvider provideGetWikiTextAndHtml
303 * @covers Status::getHtml
304 * @todo test long and short context messages generated through this method
305 * this can not really be done now due to use of $this->getWikiText using wfMessage()->plain()
306 * It is possible to mock such methods but only if namespaces are used
308 public function testGetHtml( Status
$status, $wikitext, $html ) {
309 $this->assertEquals( $html, $status->getHTML() );
313 * @return array of arrays with values;
315 * 1 => expected string (with no context)
317 public static function provideGetWikiTextAndHtml() {
318 $testCases = array();
320 $testCases[ 'GoodStatus' ] = array(
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>",
326 $status = new Status();
328 $testCases[ 'GoodButNoError' ] = array(
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>",
334 $status = new Status();
335 $status->warning( 'fooBar!' );
336 $testCases[ '1StringWarning' ] = array(
339 "<p><fooBar!>\n</p>",
342 $status = new Status();
343 $status->warning( 'fooBar!' );
344 $status->warning( 'fooBar2!' );
345 $testCases[ '2StringWarnings' ] = array(
347 "* <fooBar!>\n* <fooBar2!>\n",
348 "<ul>\n<li> <fooBar!>\n</li>\n<li> <fooBar2!>\n</li>\n</ul>\n",
351 $status = new Status();
352 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
353 $testCases[ '1MessageWarning' ] = array(
356 "<p><fooBar!>\n</p>",
359 $status = new Status();
360 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
361 $status->warning( new Message( 'fooBar2!' ) );
362 $testCases[ '2MessageWarnings' ] = array(
364 "* <fooBar!>\n* <fooBar2!>\n",
365 "<ul>\n<li> <fooBar!>\n</li>\n<li> <fooBar2!>\n</li>\n</ul>\n",
372 * @dataProvider provideGetMessage
373 * @covers Status::getMessage
374 * @todo test long and short context messages generated through this method
376 public function testGetMessage( Status
$status, $expectedParams = array(), $expectedKey ) {
377 $message = $status->getMessage();
378 $this->assertInstanceOf( 'Message', $message );
379 $this->assertEquals( $expectedParams, $message->getParams() );
380 $this->assertEquals( $expectedKey, $message->getKey() );
384 * @return array of arrays with values;
386 * 1 => expected Message Params (with no context)
388 public static function provideGetMessage() {
389 $testCases = array();
391 $testCases[ 'GoodStatus' ] = array(
393 array( "Status::getMessage called for a good result, this is incorrect\n" ),
397 $status = new Status();
399 $testCases[ 'GoodButNoError' ] = array(
401 array( "Status::getMessage: Invalid result object: no error text but not OK\n" ),
405 $status = new Status();
406 $status->warning( 'fooBar!' );
407 $testCases[ '1StringWarning' ] = array(
413 //NOTE: this seems to return a string instead of a Message object...
414 // $status = new Status();
415 // $status->warning( 'fooBar!' );
416 // $status->warning( 'fooBar2!' );
417 // $testCases[ '2StringWarnings' ] = array(
423 $status = new Status();
424 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
425 $testCases[ '1MessageWarning' ] = array(
427 array( 'foo', 'bar' ),
431 //NOTE: this seems to return a string instead of a Message object...
432 // $status = new Status();
433 // $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
434 // $status->warning( new Message( 'fooBar2!' ) );
435 // $testCases[ '2MessageWarnings' ] = array(
445 * @covers Status::replaceMessage
447 public function testReplaceMessage() {
448 $status = new Status();
449 $message = new Message( 'key1', array( 'foo1', 'bar1' ) );
450 $status->error( $message );
451 $newMessage = new Message( 'key2', array( 'foo2', 'bar2' ) );
453 $status->replaceMessage( $message, $newMessage );
455 $this->assertEquals( $newMessage, $status->errors
[0]['message'] );
459 * @covers Status::getErrorMessage
461 public function testGetErrorMessage() {
462 $method = new ReflectionMethod( 'Status', 'getErrorMessage' );
463 $method->setAccessible( true );
464 $status = new Status();
466 $params = array( 'bar' );
468 /** @var Message $message */
469 $message = $method->invoke( $status, array_merge( array( $key ), $params ) );
470 $this->assertInstanceOf( 'Message', $message );
471 $this->assertEquals( $key, $message->getKey() );
472 $this->assertEquals( $params, $message->getParams() );
476 * @covers Status::getErrorMessageArray
478 public function testGetErrorMessageArray() {
479 $method = new ReflectionMethod( 'Status', 'getErrorMessageArray' );
480 $method->setAccessible( true );
481 $status = new Status();
483 $params = array( 'bar' );
485 /** @var Message[] $messageArray */
486 $messageArray = $method->invoke(
489 array_merge( array( $key ), $params ),
490 array_merge( array( $key ), $params )
494 $this->assertInternalType( 'array', $messageArray );
495 $this->assertCount( 2, $messageArray );
496 foreach ( $messageArray as $message ) {
497 $this->assertInstanceOf( 'Message', $message );
498 $this->assertEquals( $key, $message->getKey() );
499 $this->assertEquals( $params, $message->getParams() );
504 * @covers Status::getErrorsByType
506 public function testGetErrorsByType() {
507 $status = new Status();
508 $warning = new Message( 'warning111' );
509 $error = new Message( 'error111' );
510 $status->warning( $warning );
511 $status->error( $error );
513 $warnings = $status->getErrorsByType( 'warning' );
514 $errors = $status->getErrorsByType( 'error' );
516 $this->assertCount( 1, $warnings );
517 $this->assertCount( 1, $errors );
518 $this->assertEquals( $warning, $warnings[0]['message'] );
519 $this->assertEquals( $error, $errors[0]['message'] );
523 * @covers Status::__wakeup
525 public function testWakeUpSanitizesCallback() {
526 $status = new Status();
527 $status->cleanCallback
= function( $value ) {
528 return '-' . $value . '-';
531 $this->assertEquals( false, $status->cleanCallback
);
535 * @dataProvider provideNonObjectMessages
536 * @covers Status::getStatusArray
538 public function testGetStatusArrayWithNonObjectMessages( $nonObjMsg ) {
539 $status = new Status();
540 if( !array_key_exists( 1, $nonObjMsg ) ) {
541 $status->warning( $nonObjMsg[0] );
543 $status->warning( $nonObjMsg[0], $nonObjMsg[1] );
546 $array = $status->getWarningsArray(); // We use getWarningsArray to access getStatusArray
548 $this->assertEquals( 1, count( $array ) );
549 $this->assertEquals( $nonObjMsg, $array[0] );
552 public static function provideNonObjectMessages() {
554 array( array( 'ImaString', array( 'param1' => 'value1' ) ) ),
555 array( array( 'ImaString' ) ),