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
325 public function testGetWikiText(
326 Status
$status, $wikitext, $wrappedWikitext, $html, $wrappedHtml
328 $this->assertEquals( $wikitext, $status->getWikiText() );
330 $this->assertEquals( $wrappedWikitext, $status->getWikiText( 'wrap-short', 'wrap-long', 'qqx' ) );
334 * @dataProvider provideGetWikiTextAndHtml
335 * @covers Status::getHtml
337 public function testGetHtml(
338 Status
$status, $wikitext, $wrappedWikitext, $html, $wrappedHtml
340 $this->assertEquals( $html, $status->getHTML() );
342 $this->assertEquals( $wrappedHtml, $status->getHTML( 'wrap-short', 'wrap-long', 'qqx' ) );
346 * @return array Array of arrays with values;
348 * 1 => expected string (with no context)
350 public static function provideGetWikiTextAndHtml() {
353 $testCases['GoodStatus'] = [
355 "Internal error: Status::getWikiText called for a good result, this is incorrect\n",
356 "(wrap-short: (internalerror_info: Status::getWikiText called for a good result, " .
357 "this is incorrect\n))",
358 "<p>Internal error: Status::getWikiText called for a good result, this is incorrect\n</p>",
359 "<p>(wrap-short: (internalerror_info: Status::getWikiText called for a good result, " .
360 "this is incorrect\n))\n</p>",
363 $status = new Status();
365 $testCases['GoodButNoError'] = [
367 "Internal error: Status::getWikiText: Invalid result object: no error text but not OK\n",
368 "(wrap-short: (internalerror_info: Status::getWikiText: Invalid result object: " .
369 "no error text but not OK\n))",
370 "<p>Internal error: Status::getWikiText: Invalid result object: no error text but not OK\n</p>",
371 "<p>(wrap-short: (internalerror_info: Status::getWikiText: Invalid result object: " .
372 "no error text but not OK\n))\n</p>",
375 $status = new Status();
376 $status->warning( 'fooBar!' );
377 $testCases['1StringWarning'] = [
380 "(wrap-short: (fooBar!))",
381 "<p><fooBar!>\n</p>",
382 "<p>(wrap-short: (fooBar!))\n</p>",
385 $status = new Status();
386 $status->warning( 'fooBar!' );
387 $status->warning( 'fooBar2!' );
388 $testCases['2StringWarnings'] = [
390 "* <fooBar!>\n* <fooBar2!>\n",
391 "(wrap-long: * (fooBar!)\n* (fooBar2!)\n)",
392 "<ul><li> <fooBar!></li>\n<li> <fooBar2!></li></ul>\n",
393 "<p>(wrap-long: * (fooBar!)\n</p>\n<ul><li> (fooBar2!)</li></ul>\n<p>)\n</p>",
396 $status = new Status();
397 $status->warning( new Message( 'fooBar!', [ 'foo', 'bar' ] ) );
398 $testCases['1MessageWarning'] = [
401 "(wrap-short: (fooBar!: foo, bar))",
402 "<p><fooBar!>\n</p>",
403 "<p>(wrap-short: (fooBar!: foo, bar))\n</p>",
406 $status = new Status();
407 $status->warning( new Message( 'fooBar!', [ 'foo', 'bar' ] ) );
408 $status->warning( new Message( 'fooBar2!' ) );
409 $testCases['2MessageWarnings'] = [
411 "* <fooBar!>\n* <fooBar2!>\n",
412 "(wrap-long: * (fooBar!: foo, bar)\n* (fooBar2!)\n)",
413 "<ul><li> <fooBar!></li>\n<li> <fooBar2!></li></ul>\n",
414 "<p>(wrap-long: * (fooBar!: foo, bar)\n</p>\n<ul><li> (fooBar2!)</li></ul>\n<p>)\n</p>",
420 private static function sanitizedMessageParams( Message
$message ) {
421 return array_map( function ( $p ) {
422 return $p instanceof Message
424 'key' => $p->getKey(),
425 'params' => self
::sanitizedMessageParams( $p ),
426 'lang' => $p->getLanguage()->getCode(),
429 }, $message->getParams() );
433 * @dataProvider provideGetMessage
434 * @covers Status::getMessage
436 public function testGetMessage(
437 Status
$status, $expectedParams = [], $expectedKey, $expectedWrapper
439 $message = $status->getMessage( null, null, 'qqx' );
440 $this->assertInstanceOf( 'Message', $message );
441 $this->assertEquals( $expectedParams, self
::sanitizedMessageParams( $message ),
442 'Message::getParams' );
443 $this->assertEquals( $expectedKey, $message->getKey(), 'Message::getKey' );
445 $message = $status->getMessage( 'wrapper-short', 'wrapper-long' );
446 $this->assertInstanceOf( 'Message', $message );
447 $this->assertEquals( $expectedWrapper, $message->getKey(), 'Message::getKey with wrappers' );
448 $this->assertCount( 1, $message->getParams(), 'Message::getParams with wrappers' );
450 $message = $status->getMessage( 'wrapper' );
451 $this->assertInstanceOf( 'Message', $message );
452 $this->assertEquals( 'wrapper', $message->getKey(), 'Message::getKey with wrappers' );
453 $this->assertCount( 1, $message->getParams(), 'Message::getParams with wrappers' );
455 $message = $status->getMessage( false, 'wrapper' );
456 $this->assertInstanceOf( 'Message', $message );
457 $this->assertEquals( 'wrapper', $message->getKey(), 'Message::getKey with wrappers' );
458 $this->assertCount( 1, $message->getParams(), 'Message::getParams with wrappers' );
462 * @return array Array of arrays with values;
464 * 1 => expected Message parameters (with no context)
465 * 2 => expected Message key
467 public static function provideGetMessage() {
470 $testCases['GoodStatus'] = [
472 [ "Status::getMessage called for a good result, this is incorrect\n" ],
473 'internalerror_info',
477 $status = new Status();
479 $testCases['GoodButNoError'] = [
481 [ "Status::getMessage: Invalid result object: no error text but not OK\n" ],
482 'internalerror_info',
486 $status = new Status();
487 $status->warning( 'fooBar!' );
488 $testCases['1StringWarning'] = [
495 $status = new Status();
496 $status->warning( 'fooBar!' );
497 $status->warning( 'fooBar2!' );
498 $testCases[ '2StringWarnings' ] = [
501 [ 'key' => 'fooBar!', 'params' => [], 'lang' => 'qqx' ],
502 [ 'key' => 'fooBar2!', 'params' => [], 'lang' => 'qqx' ]
508 $status = new Status();
509 $status->warning( new Message( 'fooBar!', [ 'foo', 'bar' ] ) );
510 $testCases['1MessageWarning'] = [
517 $status = new Status();
518 $status->warning( new Message( 'fooBar!', [ 'foo', 'bar' ] ) );
519 $status->warning( new Message( 'fooBar2!' ) );
520 $testCases['2MessageWarnings'] = [
523 [ 'key' => 'fooBar!', 'params' => [ 'foo', 'bar' ], 'lang' => 'qqx' ],
524 [ 'key' => 'fooBar2!', 'params' => [], 'lang' => 'qqx' ]
534 * @covers Status::replaceMessage
536 public function testReplaceMessage() {
537 $status = new Status();
538 $message = new Message( 'key1', [ 'foo1', 'bar1' ] );
539 $status->error( $message );
540 $newMessage = new Message( 'key2', [ 'foo2', 'bar2' ] );
542 $status->replaceMessage( $message, $newMessage );
544 $this->assertEquals( $newMessage, $status->errors
[0]['message'] );
548 * @covers Status::getErrorMessage
550 public function testGetErrorMessage() {
551 $method = new ReflectionMethod( 'Status', 'getErrorMessage' );
552 $method->setAccessible( true );
553 $status = new Status();
557 /** @var Message $message */
558 $message = $method->invoke( $status, array_merge( [ $key ], $params ) );
559 $this->assertInstanceOf( 'Message', $message );
560 $this->assertEquals( $key, $message->getKey() );
561 $this->assertEquals( $params, $message->getParams() );
565 * @covers Status::getErrorMessageArray
567 public function testGetErrorMessageArray() {
568 $method = new ReflectionMethod( 'Status', 'getErrorMessageArray' );
569 $method->setAccessible( true );
570 $status = new Status();
574 /** @var Message[] $messageArray */
575 $messageArray = $method->invoke(
578 array_merge( [ $key ], $params ),
579 array_merge( [ $key ], $params )
583 $this->assertInternalType( 'array', $messageArray );
584 $this->assertCount( 2, $messageArray );
585 foreach ( $messageArray as $message ) {
586 $this->assertInstanceOf( 'Message', $message );
587 $this->assertEquals( $key, $message->getKey() );
588 $this->assertEquals( $params, $message->getParams() );
593 * @covers Status::getErrorsByType
595 public function testGetErrorsByType() {
596 $status = new Status();
597 $warning = new Message( 'warning111' );
598 $error = new Message( 'error111' );
599 $status->warning( $warning );
600 $status->error( $error );
602 $warnings = $status->getErrorsByType( 'warning' );
603 $errors = $status->getErrorsByType( 'error' );
605 $this->assertCount( 1, $warnings );
606 $this->assertCount( 1, $errors );
607 $this->assertEquals( $warning, $warnings[0]['message'] );
608 $this->assertEquals( $error, $errors[0]['message'] );
612 * @covers Status::__wakeup
614 public function testWakeUpSanitizesCallback() {
615 $status = new Status();
616 $status->cleanCallback
= function ( $value ) {
617 return '-' . $value . '-';
620 $this->assertEquals( false, $status->cleanCallback
);
624 * @dataProvider provideNonObjectMessages
625 * @covers Status::getStatusArray
627 public function testGetStatusArrayWithNonObjectMessages( $nonObjMsg ) {
628 $status = new Status();
629 if ( !array_key_exists( 1, $nonObjMsg ) ) {
630 $status->warning( $nonObjMsg[0] );
632 $status->warning( $nonObjMsg[0], $nonObjMsg[1] );
635 $array = $status->getWarningsArray(); // We use getWarningsArray to access getStatusArray
637 $this->assertEquals( 1, count( $array ) );
638 $this->assertEquals( $nonObjMsg, $array[0] );
641 public static function provideNonObjectMessages() {
643 [ [ 'ImaString', [ 'param1' => 'value1' ] ] ],