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 [ [ 'foo' => 'bar' ] ],
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(
80 * @dataProvider provideSetResult
81 * @covers Status::setResult
83 public function testSetResult( $ok, $value = null ) {
84 $status = new Status();
85 $status->setResult( $ok, $value );
86 $this->assertEquals( $ok, $status->isOK() );
87 $this->assertEquals( $value, $status->getValue() );
90 public static function provideSetResult() {
100 * @dataProvider provideIsOk
101 * @covers Status::isOk
103 public function testIsOk( $ok ) {
104 $status = new Status();
106 $this->assertEquals( $ok, $status->isOK() );
109 public static function provideIsOk() {
117 * @covers Status::getValue
119 public function testGetValue() {
120 $status = new Status();
121 $status->value
= 'foobar';
122 $this->assertEquals( 'foobar', $status->getValue() );
126 * @dataProvider provideIsGood
127 * @covers Status::isGood
129 public function testIsGood( $ok, $errors, $expected ) {
130 $status = new Status();
132 foreach ( $errors as $error ) {
133 $status->warning( $error );
135 $this->assertEquals( $expected, $status->isGood() );
138 public static function provideIsGood() {
141 [ true, [ 'foo' ], false ],
142 [ false, [], false ],
143 [ false, [ 'foo' ], false ],
148 * @dataProvider provideMockMessageDetails
149 * @covers Status::warning
150 * @covers Status::getWarningsArray
151 * @covers Status::getStatusArray
153 public function testWarningWithMessage( $mockDetails ) {
154 $status = new Status();
155 $messages = $this->getMockMessages( $mockDetails );
157 foreach ( $messages as $message ) {
158 $status->warning( $message );
160 $warnings = $status->getWarningsArray();
162 $this->assertEquals( count( $messages ), count( $warnings ) );
163 foreach ( $messages as $key => $message ) {
164 $expectedArray = array_merge( [ $message->getKey() ], $message->getParams() );
165 $this->assertEquals( $warnings[$key], $expectedArray );
170 * @dataProvider provideMockMessageDetails
171 * @covers Status::error
172 * @covers Status::getErrorsArray
173 * @covers Status::getStatusArray
175 public function testErrorWithMessage( $mockDetails ) {
176 $status = new Status();
177 $messages = $this->getMockMessages( $mockDetails );
179 foreach ( $messages as $message ) {
180 $status->error( $message );
182 $errors = $status->getErrorsArray();
184 $this->assertEquals( count( $messages ), count( $errors ) );
185 foreach ( $messages as $key => $message ) {
186 $expectedArray = array_merge( [ $message->getKey() ], $message->getParams() );
187 $this->assertEquals( $errors[$key], $expectedArray );
192 * @dataProvider provideMockMessageDetails
193 * @covers Status::fatal
194 * @covers Status::getErrorsArray
195 * @covers Status::getStatusArray
197 public function testFatalWithMessage( $mockDetails ) {
198 $status = new Status();
199 $messages = $this->getMockMessages( $mockDetails );
201 foreach ( $messages as $message ) {
202 $status->fatal( $message );
204 $errors = $status->getErrorsArray();
206 $this->assertEquals( count( $messages ), count( $errors ) );
207 foreach ( $messages as $key => $message ) {
208 $expectedArray = array_merge( [ $message->getKey() ], $message->getParams() );
209 $this->assertEquals( $errors[$key], $expectedArray );
211 $this->assertFalse( $status->isOK() );
214 protected function getMockMessage( $key = 'key', $params = [] ) {
215 $message = $this->getMockBuilder( 'Message' )
216 ->disableOriginalConstructor()
218 $message->expects( $this->atLeastOnce() )
220 ->will( $this->returnValue( $key ) );
221 $message->expects( $this->atLeastOnce() )
222 ->method( 'getParams' )
223 ->will( $this->returnValue( $params ) );
228 * @param array $messageDetails E.g. array( 'KEY' => array(/PARAMS/) )
231 protected function getMockMessages( $messageDetails ) {
233 foreach ( $messageDetails as $key => $paramsArray ) {
234 $messages[] = $this->getMockMessage( $key, $paramsArray );
239 public static function provideMockMessageDetails() {
241 [ [ 'key1' => [ 'foo' => 'bar' ] ] ],
242 [ [ 'key1' => [ 'foo' => 'bar' ], 'key2' => [ 'foo2' => 'bar2' ] ] ],
247 * @covers Status::merge
249 public function testMerge() {
250 $status1 = new Status();
251 $status2 = new Status();
252 $message1 = $this->getMockMessage( 'warn1' );
253 $message2 = $this->getMockMessage( 'error2' );
254 $status1->warning( $message1 );
255 $status2->error( $message2 );
257 $status1->merge( $status2 );
260 count( $status1->getWarningsArray() ) +
count( $status1->getErrorsArray() )
265 * @covers Status::merge
267 public function testMergeWithOverwriteValue() {
268 $status1 = new Status();
269 $status2 = new Status();
270 $message1 = $this->getMockMessage( 'warn1' );
271 $message2 = $this->getMockMessage( 'error2' );
272 $status1->warning( $message1 );
273 $status2->error( $message2 );
274 $status2->value
= 'FooValue';
276 $status1->merge( $status2, true );
279 count( $status1->getWarningsArray() ) +
count( $status1->getErrorsArray() )
281 $this->assertEquals( 'FooValue', $status1->getValue() );
285 * @covers Status::hasMessage
287 public function testHasMessage() {
288 $status = new Status();
289 $status->fatal( 'bad' );
290 $status->fatal( wfMessage( 'bad-msg' ) );
291 $this->assertTrue( $status->hasMessage( 'bad' ) );
292 $this->assertTrue( $status->hasMessage( 'bad-msg' ) );
293 $this->assertTrue( $status->hasMessage( wfMessage( 'bad-msg' ) ) );
294 $this->assertFalse( $status->hasMessage( 'good' ) );
298 * @dataProvider provideCleanParams
299 * @covers Status::cleanParams
301 public function testCleanParams( $cleanCallback, $params, $expected ) {
302 $method = new ReflectionMethod( 'Status', 'cleanParams' );
303 $method->setAccessible( true );
304 $status = new Status();
305 $status->cleanCallback
= $cleanCallback;
307 $this->assertEquals( $expected, $method->invoke( $status, $params ) );
310 public static function provideCleanParams() {
311 $cleanCallback = function ( $value ) {
312 return '-' . $value . '-';
316 [ false, [ 'foo' => 'bar' ], [ 'foo' => 'bar' ] ],
317 [ $cleanCallback, [ 'foo' => 'bar' ], [ 'foo' => '-bar-' ] ],
322 * @dataProvider provideGetWikiTextAndHtml
323 * @covers Status::getWikiText
324 * @todo test long and short context messages generated through this method
325 * this can not really be done now due to use of wfMessage()->plain()
326 * It is possible to mock such methods but only if namespaces are used
328 public function testGetWikiText( Status
$status, $wikitext, $html ) {
329 $this->assertEquals( $wikitext, $status->getWikiText() );
333 * @dataProvider provideGetWikiTextAndHtml
334 * @covers Status::getHtml
335 * @todo test long and short context messages generated through this method
336 * this can not really be done now due to use of $this->getWikiText using
337 * wfMessage()->plain(). It is possible to mock such methods but only if
338 * namespaces are used.
340 public function testGetHtml( Status
$status, $wikitext, $html ) {
341 $this->assertEquals( $html, $status->getHTML() );
345 * @return array Array of arrays with values;
347 * 1 => expected string (with no context)
349 public static function provideGetWikiTextAndHtml() {
352 $testCases['GoodStatus'] = [
354 "Internal error: Status::getWikiText called for a good result, this is incorrect\n",
355 "<p>Internal error: Status::getWikiText called for a good result, this is incorrect\n</p>",
358 $status = new Status();
360 $testCases['GoodButNoError'] = [
362 "Internal error: Status::getWikiText: Invalid result object: no error text but not OK\n",
363 "<p>Internal error: Status::getWikiText: Invalid result object: no error text but not OK\n</p>",
366 $status = new Status();
367 $status->warning( 'fooBar!' );
368 $testCases['1StringWarning'] = [
371 "<p><fooBar!>\n</p>",
374 $status = new Status();
375 $status->warning( 'fooBar!' );
376 $status->warning( 'fooBar2!' );
377 $testCases['2StringWarnings'] = [
379 "* <fooBar!>\n* <fooBar2!>\n",
380 "<ul><li> <fooBar!></li>\n<li> <fooBar2!></li></ul>\n",
383 $status = new Status();
384 $status->warning( new Message( 'fooBar!', [ 'foo', 'bar' ] ) );
385 $testCases['1MessageWarning'] = [
388 "<p><fooBar!>\n</p>",
391 $status = new Status();
392 $status->warning( new Message( 'fooBar!', [ 'foo', 'bar' ] ) );
393 $status->warning( new Message( 'fooBar2!' ) );
394 $testCases['2MessageWarnings'] = [
396 "* <fooBar!>\n* <fooBar2!>\n",
397 "<ul><li> <fooBar!></li>\n<li> <fooBar2!></li></ul>\n",
404 * @dataProvider provideGetMessage
405 * @covers Status::getMessage
407 public function testGetMessage(
408 Status
$status, $expectedParams = [], $expectedKey, $expectedWrapper
410 $message = $status->getMessage();
411 $this->assertInstanceOf( 'Message', $message );
412 $this->assertEquals( $expectedParams, $message->getParams(), 'Message::getParams' );
413 $this->assertEquals( $expectedKey, $message->getKey(), 'Message::getKey' );
415 $message = $status->getMessage( 'wrapper-short', 'wrapper-long' );
416 $this->assertInstanceOf( 'Message', $message );
417 $this->assertEquals( $expectedWrapper, $message->getKey(), 'Message::getKey with wrappers' );
418 $this->assertCount( 1, $message->getParams(), 'Message::getParams with wrappers' );
420 $message = $status->getMessage( 'wrapper' );
421 $this->assertInstanceOf( 'Message', $message );
422 $this->assertEquals( 'wrapper', $message->getKey(), 'Message::getKey with wrappers' );
423 $this->assertCount( 1, $message->getParams(), 'Message::getParams with wrappers' );
425 $message = $status->getMessage( false, 'wrapper' );
426 $this->assertInstanceOf( 'Message', $message );
427 $this->assertEquals( 'wrapper', $message->getKey(), 'Message::getKey with wrappers' );
428 $this->assertCount( 1, $message->getParams(), 'Message::getParams with wrappers' );
432 * @return array Array of arrays with values;
434 * 1 => expected Message parameters (with no context)
435 * 2 => expected Message key
437 public static function provideGetMessage() {
440 $testCases['GoodStatus'] = [
442 [ "Status::getMessage called for a good result, this is incorrect\n" ],
443 'internalerror_info',
447 $status = new Status();
449 $testCases['GoodButNoError'] = [
451 [ "Status::getMessage: Invalid result object: no error text but not OK\n" ],
452 'internalerror_info',
456 $status = new Status();
457 $status->warning( 'fooBar!' );
458 $testCases['1StringWarning'] = [
465 // FIXME: Assertion tries to compare a StubUserLang with a Language object, because
466 // "data providers are executed before both the call to the setUpBeforeClass static method
467 // and the first call to the setUp method. Because of that you can't access any variables
468 // you create there from within a data provider."
469 // http://phpunit.de/manual/3.7/en/writing-tests-for-phpunit.html
470 // $status = new Status();
471 // $status->warning( 'fooBar!' );
472 // $status->warning( 'fooBar2!' );
473 // $testCases[ '2StringWarnings' ] = array(
475 // array( new Message( 'fooBar!' ), new Message( 'fooBar2!' ) ),
480 $status = new Status();
481 $status->warning( new Message( 'fooBar!', [ 'foo', 'bar' ] ) );
482 $testCases['1MessageWarning'] = [
489 $status = new Status();
490 $status->warning( new Message( 'fooBar!', [ 'foo', 'bar' ] ) );
491 $status->warning( new Message( 'fooBar2!' ) );
492 $testCases['2MessageWarnings'] = [
494 [ new Message( 'fooBar!', [ 'foo', 'bar' ] ), new Message( 'fooBar2!' ) ],
503 * @covers Status::replaceMessage
505 public function testReplaceMessage() {
506 $status = new Status();
507 $message = new Message( 'key1', [ 'foo1', 'bar1' ] );
508 $status->error( $message );
509 $newMessage = new Message( 'key2', [ 'foo2', 'bar2' ] );
511 $status->replaceMessage( $message, $newMessage );
513 $this->assertEquals( $newMessage, $status->errors
[0]['message'] );
517 * @covers Status::getErrorMessage
519 public function testGetErrorMessage() {
520 $method = new ReflectionMethod( 'Status', 'getErrorMessage' );
521 $method->setAccessible( true );
522 $status = new Status();
526 /** @var Message $message */
527 $message = $method->invoke( $status, array_merge( [ $key ], $params ) );
528 $this->assertInstanceOf( 'Message', $message );
529 $this->assertEquals( $key, $message->getKey() );
530 $this->assertEquals( $params, $message->getParams() );
534 * @covers Status::getErrorMessageArray
536 public function testGetErrorMessageArray() {
537 $method = new ReflectionMethod( 'Status', 'getErrorMessageArray' );
538 $method->setAccessible( true );
539 $status = new Status();
543 /** @var Message[] $messageArray */
544 $messageArray = $method->invoke(
547 array_merge( [ $key ], $params ),
548 array_merge( [ $key ], $params )
552 $this->assertInternalType( 'array', $messageArray );
553 $this->assertCount( 2, $messageArray );
554 foreach ( $messageArray as $message ) {
555 $this->assertInstanceOf( 'Message', $message );
556 $this->assertEquals( $key, $message->getKey() );
557 $this->assertEquals( $params, $message->getParams() );
562 * @covers Status::getErrorsByType
564 public function testGetErrorsByType() {
565 $status = new Status();
566 $warning = new Message( 'warning111' );
567 $error = new Message( 'error111' );
568 $status->warning( $warning );
569 $status->error( $error );
571 $warnings = $status->getErrorsByType( 'warning' );
572 $errors = $status->getErrorsByType( 'error' );
574 $this->assertCount( 1, $warnings );
575 $this->assertCount( 1, $errors );
576 $this->assertEquals( $warning, $warnings[0]['message'] );
577 $this->assertEquals( $error, $errors[0]['message'] );
581 * @covers Status::__wakeup
583 public function testWakeUpSanitizesCallback() {
584 $status = new Status();
585 $status->cleanCallback
= function ( $value ) {
586 return '-' . $value . '-';
589 $this->assertEquals( false, $status->cleanCallback
);
593 * @dataProvider provideNonObjectMessages
594 * @covers Status::getStatusArray
596 public function testGetStatusArrayWithNonObjectMessages( $nonObjMsg ) {
597 $status = new Status();
598 if ( !array_key_exists( 1, $nonObjMsg ) ) {
599 $status->warning( $nonObjMsg[0] );
601 $status->warning( $nonObjMsg[0], $nonObjMsg[1] );
604 $array = $status->getWarningsArray(); // We use getWarningsArray to access getStatusArray
606 $this->assertEquals( 1, count( $array ) );
607 $this->assertEquals( $nonObjMsg, $array[0] );
610 public static function provideNonObjectMessages() {
612 [ [ 'ImaString', [ 'param1' => 'value1' ] ] ],