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 foreach ( $errors as $error ) {
113 $status->warning( $error );
115 $this->assertEquals( $expected, $status->isGood() );
118 public static function provideIsGood() {
120 array( true, array(), true ),
121 array( true, array( 'foo' ), false ),
122 array( false, array(), false ),
123 array( false, array( 'foo' ), false ),
128 * @dataProvider provideMockMessageDetails
129 * @covers Status::warning
130 * @covers Status::getWarningsArray
131 * @covers Status::getStatusArray
133 public function testWarningWithMessage( $mockDetails ) {
134 $status = new Status();
135 $messages = $this->getMockMessages( $mockDetails );
137 foreach ( $messages as $message ) {
138 $status->warning( $message );
140 $warnings = $status->getWarningsArray();
142 $this->assertEquals( count( $messages ), count( $warnings ) );
143 foreach ( $messages as $key => $message ) {
144 $expectedArray = array_merge( array( $message->getKey() ), $message->getParams() );
145 $this->assertEquals( $warnings[$key], $expectedArray );
150 * @dataProvider provideMockMessageDetails
151 * @covers Status::error
152 * @covers Status::getErrorsArray
153 * @covers Status::getStatusArray
155 public function testErrorWithMessage( $mockDetails ) {
156 $status = new Status();
157 $messages = $this->getMockMessages( $mockDetails );
159 foreach ( $messages as $message ) {
160 $status->error( $message );
162 $errors = $status->getErrorsArray();
164 $this->assertEquals( count( $messages ), count( $errors ) );
165 foreach ( $messages as $key => $message ) {
166 $expectedArray = array_merge( array( $message->getKey() ), $message->getParams() );
167 $this->assertEquals( $errors[$key], $expectedArray );
172 * @dataProvider provideMockMessageDetails
173 * @covers Status::fatal
174 * @covers Status::getErrorsArray
175 * @covers Status::getStatusArray
177 public function testFatalWithMessage( $mockDetails ) {
178 $status = new Status();
179 $messages = $this->getMockMessages( $mockDetails );
181 foreach ( $messages as $message ) {
182 $status->fatal( $message );
184 $errors = $status->getErrorsArray();
186 $this->assertEquals( count( $messages ), count( $errors ) );
187 foreach ( $messages as $key => $message ) {
188 $expectedArray = array_merge( array( $message->getKey() ), $message->getParams() );
189 $this->assertEquals( $errors[$key], $expectedArray );
191 $this->assertFalse( $status->isOK() );
194 protected function getMockMessage( $key = 'key', $params = array() ) {
195 $message = $this->getMockBuilder( 'Message' )
196 ->disableOriginalConstructor()
198 $message->expects( $this->atLeastOnce() )
200 ->will( $this->returnValue( $key ) );
201 $message->expects( $this->atLeastOnce() )
202 ->method( 'getParams' )
203 ->will( $this->returnValue( $params ) );
208 * @param array $messageDetails E.g. array( 'KEY' => array(/PARAMS/) )
211 protected function getMockMessages( $messageDetails ) {
213 foreach ( $messageDetails as $key => $paramsArray ) {
214 $messages[] = $this->getMockMessage( $key, $paramsArray );
219 public static function provideMockMessageDetails() {
221 array( array( 'key1' => array( 'foo' => 'bar' ) ) ),
222 array( array( 'key1' => array( 'foo' => 'bar' ), 'key2' => array( 'foo2' => 'bar2' ) ) ),
227 * @covers Status::merge
229 public function testMerge() {
230 $status1 = new Status();
231 $status2 = new Status();
232 $message1 = $this->getMockMessage( 'warn1' );
233 $message2 = $this->getMockMessage( 'error2' );
234 $status1->warning( $message1 );
235 $status2->error( $message2 );
237 $status1->merge( $status2 );
240 count( $status1->getWarningsArray() ) +
count( $status1->getErrorsArray() )
245 * @covers Status::merge
247 public function testMergeWithOverwriteValue() {
248 $status1 = new Status();
249 $status2 = new Status();
250 $message1 = $this->getMockMessage( 'warn1' );
251 $message2 = $this->getMockMessage( 'error2' );
252 $status1->warning( $message1 );
253 $status2->error( $message2 );
254 $status2->value
= 'FooValue';
256 $status1->merge( $status2, true );
259 count( $status1->getWarningsArray() ) +
count( $status1->getErrorsArray() )
261 $this->assertEquals( 'FooValue', $status1->getValue() );
265 * @covers Status::hasMessage
267 public function testHasMessage() {
268 $status = new Status();
269 $status->fatal( 'bad' );
270 $status->fatal( wfMessage( 'bad-msg' ) );
271 $this->assertTrue( $status->hasMessage( 'bad' ) );
272 $this->assertTrue( $status->hasMessage( 'bad-msg' ) );
273 $this->assertTrue( $status->hasMessage( wfMessage( 'bad-msg' ) ) );
274 $this->assertFalse( $status->hasMessage( 'good' ) );
278 * @dataProvider provideCleanParams
279 * @covers Status::cleanParams
281 public function testCleanParams( $cleanCallback, $params, $expected ) {
282 $method = new ReflectionMethod( 'Status', 'cleanParams' );
283 $method->setAccessible( true );
284 $status = new Status();
285 $status->cleanCallback
= $cleanCallback;
287 $this->assertEquals( $expected, $method->invoke( $status, $params ) );
290 public static function provideCleanParams() {
291 $cleanCallback = function ( $value ) {
292 return '-' . $value . '-';
296 array( false, array( 'foo' => 'bar' ), array( 'foo' => 'bar' ) ),
297 array( $cleanCallback, array( 'foo' => 'bar' ), array( 'foo' => '-bar-' ) ),
302 * @dataProvider provideGetWikiTextAndHtml
303 * @covers Status::getWikiText
304 * @todo test long and short context messages generated through this method
305 * this can not really be done now due to use of wfMessage()->plain()
306 * It is possible to mock such methods but only if namespaces are used
308 public function testGetWikiText( Status
$status, $wikitext, $html ) {
309 $this->assertEquals( $wikitext, $status->getWikiText() );
313 * @dataProvider provideGetWikiTextAndHtml
314 * @covers Status::getHtml
315 * @todo test long and short context messages generated through this method
316 * this can not really be done now due to use of $this->getWikiText using
317 * wfMessage()->plain(). It is possible to mock such methods but only if
318 * namespaces are used.
320 public function testGetHtml( Status
$status, $wikitext, $html ) {
321 $this->assertEquals( $html, $status->getHTML() );
325 * @return array Array of arrays with values;
327 * 1 => expected string (with no context)
329 public static function provideGetWikiTextAndHtml() {
330 $testCases = array();
332 $testCases['GoodStatus'] = array(
334 "Internal error: Status::getWikiText called for a good result, this is incorrect\n",
335 "<p>Internal error: Status::getWikiText called for a good result, this is incorrect\n</p>",
338 $status = new Status();
340 $testCases['GoodButNoError'] = array(
342 "Internal error: Status::getWikiText: Invalid result object: no error text but not OK\n",
343 "<p>Internal error: Status::getWikiText: Invalid result object: no error text but not OK\n</p>",
346 $status = new Status();
347 $status->warning( 'fooBar!' );
348 $testCases['1StringWarning'] = array(
351 "<p><fooBar!>\n</p>",
354 $status = new Status();
355 $status->warning( 'fooBar!' );
356 $status->warning( 'fooBar2!' );
357 $testCases['2StringWarnings'] = array(
359 "* <fooBar!>\n* <fooBar2!>\n",
360 "<ul><li> <fooBar!></li>\n<li> <fooBar2!></li></ul>\n",
363 $status = new Status();
364 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
365 $testCases['1MessageWarning'] = array(
368 "<p><fooBar!>\n</p>",
371 $status = new Status();
372 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
373 $status->warning( new Message( 'fooBar2!' ) );
374 $testCases['2MessageWarnings'] = array(
376 "* <fooBar!>\n* <fooBar2!>\n",
377 "<ul><li> <fooBar!></li>\n<li> <fooBar2!></li></ul>\n",
384 * @dataProvider provideGetMessage
385 * @covers Status::getMessage
386 * @todo test long and short context messages generated through this method
388 public function testGetMessage( Status
$status, $expectedParams = array(), $expectedKey ) {
389 $message = $status->getMessage();
390 $this->assertInstanceOf( 'Message', $message );
391 $this->assertEquals( $expectedParams, $message->getParams(), 'Message::getParams' );
392 $this->assertEquals( $expectedKey, $message->getKey(), 'Message::getKey' );
396 * @return array Array of arrays with values;
398 * 1 => expected Message parameters (with no context)
399 * 2 => expected Message key
401 public static function provideGetMessage() {
402 $testCases = array();
404 $testCases['GoodStatus'] = array(
406 array( "Status::getMessage called for a good result, this is incorrect\n" ),
410 $status = new Status();
412 $testCases['GoodButNoError'] = array(
414 array( "Status::getMessage: Invalid result object: no error text but not OK\n" ),
418 $status = new Status();
419 $status->warning( 'fooBar!' );
420 $testCases['1StringWarning'] = array(
426 // FIXME: Assertion tries to compare a StubUserLang with a Language object, because
427 // "data providers are executed before both the call to the setUpBeforeClass static method
428 // and the first call to the setUp method. Because of that you can't access any variables
429 // you create there from within a data provider."
430 // http://phpunit.de/manual/3.7/en/writing-tests-for-phpunit.html
431 // $status = new Status();
432 // $status->warning( 'fooBar!' );
433 // $status->warning( 'fooBar2!' );
434 // $testCases[ '2StringWarnings' ] = array(
436 // array( new Message( 'fooBar!' ), new Message( 'fooBar2!' ) ),
440 $status = new Status();
441 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
442 $testCases['1MessageWarning'] = array(
444 array( 'foo', 'bar' ),
448 $status = new Status();
449 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
450 $status->warning( new Message( 'fooBar2!' ) );
451 $testCases['2MessageWarnings'] = array(
453 array( new Message( 'fooBar!', array( 'foo', 'bar' ) ), new Message( 'fooBar2!' ) ),
461 * @covers Status::replaceMessage
463 public function testReplaceMessage() {
464 $status = new Status();
465 $message = new Message( 'key1', array( 'foo1', 'bar1' ) );
466 $status->error( $message );
467 $newMessage = new Message( 'key2', array( 'foo2', 'bar2' ) );
469 $status->replaceMessage( $message, $newMessage );
471 $this->assertEquals( $newMessage, $status->errors
[0]['message'] );
475 * @covers Status::getErrorMessage
477 public function testGetErrorMessage() {
478 $method = new ReflectionMethod( 'Status', 'getErrorMessage' );
479 $method->setAccessible( true );
480 $status = new Status();
482 $params = array( 'bar' );
484 /** @var Message $message */
485 $message = $method->invoke( $status, array_merge( array( $key ), $params ) );
486 $this->assertInstanceOf( 'Message', $message );
487 $this->assertEquals( $key, $message->getKey() );
488 $this->assertEquals( $params, $message->getParams() );
492 * @covers Status::getErrorMessageArray
494 public function testGetErrorMessageArray() {
495 $method = new ReflectionMethod( 'Status', 'getErrorMessageArray' );
496 $method->setAccessible( true );
497 $status = new Status();
499 $params = array( 'bar' );
501 /** @var Message[] $messageArray */
502 $messageArray = $method->invoke(
505 array_merge( array( $key ), $params ),
506 array_merge( array( $key ), $params )
510 $this->assertInternalType( 'array', $messageArray );
511 $this->assertCount( 2, $messageArray );
512 foreach ( $messageArray as $message ) {
513 $this->assertInstanceOf( 'Message', $message );
514 $this->assertEquals( $key, $message->getKey() );
515 $this->assertEquals( $params, $message->getParams() );
520 * @covers Status::getErrorsByType
522 public function testGetErrorsByType() {
523 $status = new Status();
524 $warning = new Message( 'warning111' );
525 $error = new Message( 'error111' );
526 $status->warning( $warning );
527 $status->error( $error );
529 $warnings = $status->getErrorsByType( 'warning' );
530 $errors = $status->getErrorsByType( 'error' );
532 $this->assertCount( 1, $warnings );
533 $this->assertCount( 1, $errors );
534 $this->assertEquals( $warning, $warnings[0]['message'] );
535 $this->assertEquals( $error, $errors[0]['message'] );
539 * @covers Status::__wakeup
541 public function testWakeUpSanitizesCallback() {
542 $status = new Status();
543 $status->cleanCallback
= function ( $value ) {
544 return '-' . $value . '-';
547 $this->assertEquals( false, $status->cleanCallback
);
551 * @dataProvider provideNonObjectMessages
552 * @covers Status::getStatusArray
554 public function testGetStatusArrayWithNonObjectMessages( $nonObjMsg ) {
555 $status = new Status();
556 if ( !array_key_exists( 1, $nonObjMsg ) ) {
557 $status->warning( $nonObjMsg[0] );
559 $status->warning( $nonObjMsg[0], $nonObjMsg[1] );
562 $array = $status->getWarningsArray(); // We use getWarningsArray to access getStatusArray
564 $this->assertEquals( 1, count( $array ) );
565 $this->assertEquals( $nonObjMsg, $array[0] );
568 public static function provideNonObjectMessages() {
570 array( array( 'ImaString', array( 'param1' => 'value1' ) ) ),
571 array( array( 'ImaString' ) ),