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() );
62 public function testOkAndErrors() {
63 $status = Status
::newGood( 'foo' );
64 $this->assertTrue( $status->ok
);
65 $status = Status
::newFatal( 'foo', 1, 2 );
66 $this->assertFalse( $status->ok
);
67 $this->assertArrayEquals( array( array( 'type' => 'error', 'message' => 'foo', 'params' => array( 1, 2 ) ) ), $status->errors
);
71 * @dataProvider provideSetResult
72 * @covers Status::setResult
74 public function testSetResult( $ok, $value = null ) {
75 $status = new Status();
76 $status->setResult( $ok, $value );
77 $this->assertEquals( $ok, $status->isOK() );
78 $this->assertEquals( $value, $status->getValue() );
81 public static function provideSetResult() {
85 array( true, 'value' ),
86 array( false, 'value' ),
91 * @dataProvider provideIsOk
92 * @covers Status::isOk
94 public function testIsOk( $ok ) {
95 $status = new Status();
97 $this->assertEquals( $ok, $status->isOK() );
100 public static function provideIsOk() {
108 * @covers Status::getValue
110 public function testGetValue() {
111 $status = new Status();
112 $status->value
= 'foobar';
113 $this->assertEquals( 'foobar', $status->getValue() );
117 * @dataProvider provideIsGood
118 * @covers Status::isGood
120 public function testIsGood( $ok, $errors, $expected ) {
121 $status = new Status();
123 foreach ( $errors as $error ) {
124 $status->warning( $error );
126 $this->assertEquals( $expected, $status->isGood() );
129 public static function provideIsGood() {
131 array( true, array(), true ),
132 array( true, array( 'foo' ), false ),
133 array( false, array(), false ),
134 array( false, array( 'foo' ), false ),
139 * @dataProvider provideMockMessageDetails
140 * @covers Status::warning
141 * @covers Status::getWarningsArray
142 * @covers Status::getStatusArray
144 public function testWarningWithMessage( $mockDetails ) {
145 $status = new Status();
146 $messages = $this->getMockMessages( $mockDetails );
148 foreach ( $messages as $message ) {
149 $status->warning( $message );
151 $warnings = $status->getWarningsArray();
153 $this->assertEquals( count( $messages ), count( $warnings ) );
154 foreach ( $messages as $key => $message ) {
155 $expectedArray = array_merge( array( $message->getKey() ), $message->getParams() );
156 $this->assertEquals( $warnings[$key], $expectedArray );
161 * @dataProvider provideMockMessageDetails
162 * @covers Status::error
163 * @covers Status::getErrorsArray
164 * @covers Status::getStatusArray
166 public function testErrorWithMessage( $mockDetails ) {
167 $status = new Status();
168 $messages = $this->getMockMessages( $mockDetails );
170 foreach ( $messages as $message ) {
171 $status->error( $message );
173 $errors = $status->getErrorsArray();
175 $this->assertEquals( count( $messages ), count( $errors ) );
176 foreach ( $messages as $key => $message ) {
177 $expectedArray = array_merge( array( $message->getKey() ), $message->getParams() );
178 $this->assertEquals( $errors[$key], $expectedArray );
183 * @dataProvider provideMockMessageDetails
184 * @covers Status::fatal
185 * @covers Status::getErrorsArray
186 * @covers Status::getStatusArray
188 public function testFatalWithMessage( $mockDetails ) {
189 $status = new Status();
190 $messages = $this->getMockMessages( $mockDetails );
192 foreach ( $messages as $message ) {
193 $status->fatal( $message );
195 $errors = $status->getErrorsArray();
197 $this->assertEquals( count( $messages ), count( $errors ) );
198 foreach ( $messages as $key => $message ) {
199 $expectedArray = array_merge( array( $message->getKey() ), $message->getParams() );
200 $this->assertEquals( $errors[$key], $expectedArray );
202 $this->assertFalse( $status->isOK() );
205 protected function getMockMessage( $key = 'key', $params = array() ) {
206 $message = $this->getMockBuilder( 'Message' )
207 ->disableOriginalConstructor()
209 $message->expects( $this->atLeastOnce() )
211 ->will( $this->returnValue( $key ) );
212 $message->expects( $this->atLeastOnce() )
213 ->method( 'getParams' )
214 ->will( $this->returnValue( $params ) );
219 * @param array $messageDetails E.g. array( 'KEY' => array(/PARAMS/) )
222 protected function getMockMessages( $messageDetails ) {
224 foreach ( $messageDetails as $key => $paramsArray ) {
225 $messages[] = $this->getMockMessage( $key, $paramsArray );
230 public static function provideMockMessageDetails() {
232 array( array( 'key1' => array( 'foo' => 'bar' ) ) ),
233 array( array( 'key1' => array( 'foo' => 'bar' ), 'key2' => array( 'foo2' => 'bar2' ) ) ),
238 * @covers Status::merge
240 public function testMerge() {
241 $status1 = new Status();
242 $status2 = new Status();
243 $message1 = $this->getMockMessage( 'warn1' );
244 $message2 = $this->getMockMessage( 'error2' );
245 $status1->warning( $message1 );
246 $status2->error( $message2 );
248 $status1->merge( $status2 );
251 count( $status1->getWarningsArray() ) +
count( $status1->getErrorsArray() )
256 * @covers Status::merge
258 public function testMergeWithOverwriteValue() {
259 $status1 = new Status();
260 $status2 = new Status();
261 $message1 = $this->getMockMessage( 'warn1' );
262 $message2 = $this->getMockMessage( 'error2' );
263 $status1->warning( $message1 );
264 $status2->error( $message2 );
265 $status2->value
= 'FooValue';
267 $status1->merge( $status2, true );
270 count( $status1->getWarningsArray() ) +
count( $status1->getErrorsArray() )
272 $this->assertEquals( 'FooValue', $status1->getValue() );
276 * @covers Status::hasMessage
278 public function testHasMessage() {
279 $status = new Status();
280 $status->fatal( 'bad' );
281 $status->fatal( wfMessage( 'bad-msg' ) );
282 $this->assertTrue( $status->hasMessage( 'bad' ) );
283 $this->assertTrue( $status->hasMessage( 'bad-msg' ) );
284 $this->assertTrue( $status->hasMessage( wfMessage( 'bad-msg' ) ) );
285 $this->assertFalse( $status->hasMessage( 'good' ) );
289 * @dataProvider provideCleanParams
290 * @covers Status::cleanParams
292 public function testCleanParams( $cleanCallback, $params, $expected ) {
293 $method = new ReflectionMethod( 'Status', 'cleanParams' );
294 $method->setAccessible( true );
295 $status = new Status();
296 $status->cleanCallback
= $cleanCallback;
298 $this->assertEquals( $expected, $method->invoke( $status, $params ) );
301 public static function provideCleanParams() {
302 $cleanCallback = function ( $value ) {
303 return '-' . $value . '-';
307 array( false, array( 'foo' => 'bar' ), array( 'foo' => 'bar' ) ),
308 array( $cleanCallback, array( 'foo' => 'bar' ), array( 'foo' => '-bar-' ) ),
313 * @dataProvider provideGetWikiTextAndHtml
314 * @covers Status::getWikiText
315 * @todo test long and short context messages generated through this method
316 * this can not really be done now due to use of wfMessage()->plain()
317 * It is possible to mock such methods but only if namespaces are used
319 public function testGetWikiText( Status
$status, $wikitext, $html ) {
320 $this->assertEquals( $wikitext, $status->getWikiText() );
324 * @dataProvider provideGetWikiTextAndHtml
325 * @covers Status::getHtml
326 * @todo test long and short context messages generated through this method
327 * this can not really be done now due to use of $this->getWikiText using
328 * wfMessage()->plain(). It is possible to mock such methods but only if
329 * namespaces are used.
331 public function testGetHtml( Status
$status, $wikitext, $html ) {
332 $this->assertEquals( $html, $status->getHTML() );
336 * @return array Array of arrays with values;
338 * 1 => expected string (with no context)
340 public static function provideGetWikiTextAndHtml() {
341 $testCases = array();
343 $testCases['GoodStatus'] = array(
345 "Internal error: Status::getWikiText called for a good result, this is incorrect\n",
346 "<p>Internal error: Status::getWikiText called for a good result, this is incorrect\n</p>",
349 $status = new Status();
351 $testCases['GoodButNoError'] = array(
353 "Internal error: Status::getWikiText: Invalid result object: no error text but not OK\n",
354 "<p>Internal error: Status::getWikiText: Invalid result object: no error text but not OK\n</p>",
357 $status = new Status();
358 $status->warning( 'fooBar!' );
359 $testCases['1StringWarning'] = array(
362 "<p><fooBar!>\n</p>",
365 $status = new Status();
366 $status->warning( 'fooBar!' );
367 $status->warning( 'fooBar2!' );
368 $testCases['2StringWarnings'] = array(
370 "* <fooBar!>\n* <fooBar2!>\n",
371 "<ul><li> <fooBar!></li>\n<li> <fooBar2!></li></ul>\n",
374 $status = new Status();
375 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
376 $testCases['1MessageWarning'] = array(
379 "<p><fooBar!>\n</p>",
382 $status = new Status();
383 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
384 $status->warning( new Message( 'fooBar2!' ) );
385 $testCases['2MessageWarnings'] = array(
387 "* <fooBar!>\n* <fooBar2!>\n",
388 "<ul><li> <fooBar!></li>\n<li> <fooBar2!></li></ul>\n",
395 * @dataProvider provideGetMessage
396 * @covers Status::getMessage
397 * @todo test long and short context messages generated through this method
399 public function testGetMessage( Status
$status, $expectedParams = array(), $expectedKey ) {
400 $message = $status->getMessage();
401 $this->assertInstanceOf( 'Message', $message );
402 $this->assertEquals( $expectedParams, $message->getParams(), 'Message::getParams' );
403 $this->assertEquals( $expectedKey, $message->getKey(), 'Message::getKey' );
407 * @return array Array of arrays with values;
409 * 1 => expected Message parameters (with no context)
410 * 2 => expected Message key
412 public static function provideGetMessage() {
413 $testCases = array();
415 $testCases['GoodStatus'] = array(
417 array( "Status::getMessage called for a good result, this is incorrect\n" ),
421 $status = new Status();
423 $testCases['GoodButNoError'] = array(
425 array( "Status::getMessage: Invalid result object: no error text but not OK\n" ),
429 $status = new Status();
430 $status->warning( 'fooBar!' );
431 $testCases['1StringWarning'] = array(
437 // FIXME: Assertion tries to compare a StubUserLang with a Language object, because
438 // "data providers are executed before both the call to the setUpBeforeClass static method
439 // and the first call to the setUp method. Because of that you can't access any variables
440 // you create there from within a data provider."
441 // http://phpunit.de/manual/3.7/en/writing-tests-for-phpunit.html
442 // $status = new Status();
443 // $status->warning( 'fooBar!' );
444 // $status->warning( 'fooBar2!' );
445 // $testCases[ '2StringWarnings' ] = array(
447 // array( new Message( 'fooBar!' ), new Message( 'fooBar2!' ) ),
451 $status = new Status();
452 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
453 $testCases['1MessageWarning'] = array(
455 array( 'foo', 'bar' ),
459 $status = new Status();
460 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
461 $status->warning( new Message( 'fooBar2!' ) );
462 $testCases['2MessageWarnings'] = array(
464 array( new Message( 'fooBar!', array( 'foo', 'bar' ) ), new Message( 'fooBar2!' ) ),
472 * @covers Status::replaceMessage
474 public function testReplaceMessage() {
475 $status = new Status();
476 $message = new Message( 'key1', array( 'foo1', 'bar1' ) );
477 $status->error( $message );
478 $newMessage = new Message( 'key2', array( 'foo2', 'bar2' ) );
480 $status->replaceMessage( $message, $newMessage );
482 $this->assertEquals( $newMessage, $status->errors
[0]['message'] );
486 * @covers Status::getErrorMessage
488 public function testGetErrorMessage() {
489 $method = new ReflectionMethod( 'Status', 'getErrorMessage' );
490 $method->setAccessible( true );
491 $status = new Status();
493 $params = array( 'bar' );
495 /** @var Message $message */
496 $message = $method->invoke( $status, array_merge( array( $key ), $params ) );
497 $this->assertInstanceOf( 'Message', $message );
498 $this->assertEquals( $key, $message->getKey() );
499 $this->assertEquals( $params, $message->getParams() );
503 * @covers Status::getErrorMessageArray
505 public function testGetErrorMessageArray() {
506 $method = new ReflectionMethod( 'Status', 'getErrorMessageArray' );
507 $method->setAccessible( true );
508 $status = new Status();
510 $params = array( 'bar' );
512 /** @var Message[] $messageArray */
513 $messageArray = $method->invoke(
516 array_merge( array( $key ), $params ),
517 array_merge( array( $key ), $params )
521 $this->assertInternalType( 'array', $messageArray );
522 $this->assertCount( 2, $messageArray );
523 foreach ( $messageArray as $message ) {
524 $this->assertInstanceOf( 'Message', $message );
525 $this->assertEquals( $key, $message->getKey() );
526 $this->assertEquals( $params, $message->getParams() );
531 * @covers Status::getErrorsByType
533 public function testGetErrorsByType() {
534 $status = new Status();
535 $warning = new Message( 'warning111' );
536 $error = new Message( 'error111' );
537 $status->warning( $warning );
538 $status->error( $error );
540 $warnings = $status->getErrorsByType( 'warning' );
541 $errors = $status->getErrorsByType( 'error' );
543 $this->assertCount( 1, $warnings );
544 $this->assertCount( 1, $errors );
545 $this->assertEquals( $warning, $warnings[0]['message'] );
546 $this->assertEquals( $error, $errors[0]['message'] );
550 * @covers Status::__wakeup
552 public function testWakeUpSanitizesCallback() {
553 $status = new Status();
554 $status->cleanCallback
= function ( $value ) {
555 return '-' . $value . '-';
558 $this->assertEquals( false, $status->cleanCallback
);
562 * @dataProvider provideNonObjectMessages
563 * @covers Status::getStatusArray
565 public function testGetStatusArrayWithNonObjectMessages( $nonObjMsg ) {
566 $status = new Status();
567 if ( !array_key_exists( 1, $nonObjMsg ) ) {
568 $status->warning( $nonObjMsg[0] );
570 $status->warning( $nonObjMsg[0], $nonObjMsg[1] );
573 $array = $status->getWarningsArray(); // We use getWarningsArray to access getStatusArray
575 $this->assertEquals( 1, count( $array ) );
576 $this->assertEquals( $nonObjMsg, $array[0] );
579 public static function provideNonObjectMessages() {
581 array( array( 'ImaString', array( 'param1' => 'value1' ) ) ),
582 array( array( 'ImaString' ) ),