6 class StatusTest
extends MediaWikiLangTestCase
{
9 * @dataProvider provideValues
10 * @covers Status::newGood
12 public function testNewGood( $value = null ) {
13 $status = Status
::newGood( $value );
14 $this->assertTrue( $status->isGood() );
15 $this->assertTrue( $status->isOK() );
16 $this->assertEquals( $value, $status->getValue() );
19 public static function provideValues() {
23 [ [ 'foo' => 'bar' ] ],
30 * @covers Status::newFatal
32 public function testNewFatalWithMessage() {
33 $message = $this->getMockBuilder( Message
::class )
34 ->disableOriginalConstructor()
37 $status = Status
::newFatal( $message );
38 $this->assertFalse( $status->isGood() );
39 $this->assertFalse( $status->isOK() );
40 $this->assertEquals( $message, $status->getMessage() );
44 * @covers Status::newFatal
46 public function testNewFatalWithString() {
48 $status = Status
::newFatal( $message );
49 $this->assertFalse( $status->isGood() );
50 $this->assertFalse( $status->isOK() );
51 $this->assertEquals( $message, $status->getMessage()->getKey() );
55 * Test 'ok' and 'errors' getters.
57 * @covers Status::__get
59 public function testOkAndErrorsGetters() {
60 $status = Status
::newGood( 'foo' );
61 $this->assertTrue( $status->ok
);
62 $status = Status
::newFatal( 'foo', 1, 2 );
63 $this->assertFalse( $status->ok
);
64 $this->assertArrayEquals(
79 * @covers Status::__set
81 public function testOkSetter() {
82 $status = new Status();
84 $this->assertFalse( $status->isOK() );
86 $this->assertTrue( $status->isOK() );
90 * @dataProvider provideSetResult
91 * @covers Status::setResult
93 public function testSetResult( $ok, $value = null ) {
94 $status = new Status();
95 $status->setResult( $ok, $value );
96 $this->assertEquals( $ok, $status->isOK() );
97 $this->assertEquals( $value, $status->getValue() );
100 public static function provideSetResult() {
110 * @dataProvider provideIsOk
111 * @covers Status::setOK
112 * @covers Status::isOK
114 public function testIsOk( $ok ) {
115 $status = new Status();
116 $status->setOK( $ok );
117 $this->assertEquals( $ok, $status->isOK() );
120 public static function provideIsOk() {
128 * @covers Status::getValue
130 public function testGetValue() {
131 $status = new Status();
132 $status->value
= 'foobar';
133 $this->assertEquals( 'foobar', $status->getValue() );
137 * @dataProvider provideIsGood
138 * @covers Status::isGood
140 public function testIsGood( $ok, $errors, $expected ) {
141 $status = new Status();
142 $status->setOK( $ok );
143 foreach ( $errors as $error ) {
144 $status->warning( $error );
146 $this->assertEquals( $expected, $status->isGood() );
149 public static function provideIsGood() {
152 [ true, [ 'foo' ], false ],
153 [ false, [], false ],
154 [ false, [ 'foo' ], false ],
159 * @dataProvider provideMockMessageDetails
160 * @covers Status::warning
161 * @covers Status::getWarningsArray
162 * @covers Status::getStatusArray
164 public function testWarningWithMessage( $mockDetails ) {
165 $status = new Status();
166 $messages = $this->getMockMessages( $mockDetails );
168 foreach ( $messages as $message ) {
169 $status->warning( $message );
171 $warnings = $status->getWarningsArray();
173 $this->assertEquals( count( $messages ), count( $warnings ) );
174 foreach ( $messages as $key => $message ) {
175 $expectedArray = array_merge( [ $message->getKey() ], $message->getParams() );
176 $this->assertEquals( $warnings[$key], $expectedArray );
181 * @dataProvider provideMockMessageDetails
182 * @covers Status::error
183 * @covers Status::getErrorsArray
184 * @covers Status::getStatusArray
185 * @covers Status::getErrors
187 public function testErrorWithMessage( $mockDetails ) {
188 $status = new Status();
189 $messages = $this->getMockMessages( $mockDetails );
191 foreach ( $messages as $message ) {
192 $status->error( $message );
194 $errors = $status->getErrorsArray();
196 $this->assertEquals( count( $messages ), count( $errors ) );
197 foreach ( $messages as $key => $message ) {
198 $expectedArray = array_merge( [ $message->getKey() ], $message->getParams() );
199 $this->assertEquals( $errors[$key], $expectedArray );
204 * @dataProvider provideMockMessageDetails
205 * @covers Status::fatal
206 * @covers Status::getErrorsArray
207 * @covers Status::getStatusArray
209 public function testFatalWithMessage( $mockDetails ) {
210 $status = new Status();
211 $messages = $this->getMockMessages( $mockDetails );
213 foreach ( $messages as $message ) {
214 $status->fatal( $message );
216 $errors = $status->getErrorsArray();
218 $this->assertEquals( count( $messages ), count( $errors ) );
219 foreach ( $messages as $key => $message ) {
220 $expectedArray = array_merge( [ $message->getKey() ], $message->getParams() );
221 $this->assertEquals( $errors[$key], $expectedArray );
223 $this->assertFalse( $status->isOK() );
226 protected function getMockMessage( $key = 'key', $params = [] ) {
227 $message = $this->getMockBuilder( Message
::class )
228 ->disableOriginalConstructor()
230 $message->expects( $this->atLeastOnce() )
232 ->will( $this->returnValue( $key ) );
233 $message->expects( $this->atLeastOnce() )
234 ->method( 'getParams' )
235 ->will( $this->returnValue( $params ) );
240 * @param array $messageDetails E.g. array( 'KEY' => array(/PARAMS/) )
243 protected function getMockMessages( $messageDetails ) {
245 foreach ( $messageDetails as $key => $paramsArray ) {
246 $messages[] = $this->getMockMessage( $key, $paramsArray );
251 public static function provideMockMessageDetails() {
253 [ [ 'key1' => [ 'foo' => 'bar' ] ] ],
254 [ [ 'key1' => [ 'foo' => 'bar' ], 'key2' => [ 'foo2' => 'bar2' ] ] ],
259 * @covers Status::merge
261 public function testMerge() {
262 $status1 = new Status();
263 $status2 = new Status();
264 $message1 = $this->getMockMessage( 'warn1' );
265 $message2 = $this->getMockMessage( 'error2' );
266 $status1->warning( $message1 );
267 $status2->error( $message2 );
269 $status1->merge( $status2 );
272 count( $status1->getWarningsArray() ) +
count( $status1->getErrorsArray() )
277 * @covers Status::merge
279 public function testMergeWithOverwriteValue() {
280 $status1 = new Status();
281 $status2 = new Status();
282 $message1 = $this->getMockMessage( 'warn1' );
283 $message2 = $this->getMockMessage( 'error2' );
284 $status1->warning( $message1 );
285 $status2->error( $message2 );
286 $status2->value
= 'FooValue';
288 $status1->merge( $status2, true );
291 count( $status1->getWarningsArray() ) +
count( $status1->getErrorsArray() )
293 $this->assertEquals( 'FooValue', $status1->getValue() );
297 * @covers Status::hasMessage
299 public function testHasMessage() {
300 $status = new Status();
301 $status->fatal( 'bad' );
302 $status->fatal( wfMessage( 'bad-msg' ) );
303 $this->assertTrue( $status->hasMessage( 'bad' ) );
304 $this->assertTrue( $status->hasMessage( 'bad-msg' ) );
305 $this->assertTrue( $status->hasMessage( wfMessage( 'bad-msg' ) ) );
306 $this->assertFalse( $status->hasMessage( 'good' ) );
310 * @dataProvider provideCleanParams
311 * @covers Status::cleanParams
313 public function testCleanParams( $cleanCallback, $params, $expected ) {
314 $method = new ReflectionMethod( Status
::class, 'cleanParams' );
315 $method->setAccessible( true );
316 $status = new Status();
317 $status->cleanCallback
= $cleanCallback;
319 $this->assertEquals( $expected, $method->invoke( $status, $params ) );
322 public static function provideCleanParams() {
323 $cleanCallback = function ( $value ) {
324 return '-' . $value . '-';
328 [ false, [ 'foo' => 'bar' ], [ 'foo' => 'bar' ] ],
329 [ $cleanCallback, [ 'foo' => 'bar' ], [ 'foo' => '-bar-' ] ],
334 * @dataProvider provideGetWikiTextAndHtml
335 * @covers Status::getWikiText
337 public function testGetWikiText(
338 Status
$status, $wikitext, $wrappedWikitext, $html, $wrappedHtml
340 $this->assertEquals( $wikitext, $status->getWikiText() );
342 $this->assertEquals( $wrappedWikitext, $status->getWikiText( 'wrap-short', 'wrap-long', 'qqx' ) );
346 * @dataProvider provideGetWikiTextAndHtml
347 * @covers Status::getHtml
349 public function testGetHtml(
350 Status
$status, $wikitext, $wrappedWikitext, $html, $wrappedHtml
352 $this->assertEquals( $html, $status->getHTML() );
354 $this->assertEquals( $wrappedHtml, $status->getHTML( 'wrap-short', 'wrap-long', 'qqx' ) );
358 * @return array Array of arrays with values;
360 * 1 => expected string (with no context)
362 public static function provideGetWikiTextAndHtml() {
365 $testCases['GoodStatus'] = [
367 "Internal error: Status::getWikiText called for a good result, this is incorrect\n",
368 "(wrap-short: (internalerror_info: Status::getWikiText called for a good result, " .
369 "this is incorrect\n))",
370 "<p>Internal error: Status::getWikiText called for a good result, this is incorrect\n</p>",
371 "<p>(wrap-short: (internalerror_info: Status::getWikiText called for a good result, " .
372 "this is incorrect\n))\n</p>",
375 $status = new Status();
376 $status->setOK( false );
377 $testCases['GoodButNoError'] = [
379 "Internal error: Status::getWikiText: Invalid result object: no error text but not OK\n",
380 "(wrap-short: (internalerror_info: Status::getWikiText: Invalid result object: " .
381 "no error text but not OK\n))",
382 "<p>Internal error: Status::getWikiText: Invalid result object: no error text but not OK\n</p>",
383 "<p>(wrap-short: (internalerror_info: Status::getWikiText: Invalid result object: " .
384 "no error text but not OK\n))\n</p>",
387 $status = new Status();
388 $status->warning( 'fooBar!' );
389 $testCases['1StringWarning'] = [
392 "(wrap-short: (fooBar!))",
393 "<p>⧼fooBar!⧽\n</p>",
394 "<p>(wrap-short: (fooBar!))\n</p>",
397 $status = new Status();
398 $status->warning( 'fooBar!' );
399 $status->warning( 'fooBar2!' );
400 $testCases['2StringWarnings'] = [
402 "* ⧼fooBar!⧽\n* ⧼fooBar2!⧽\n",
403 "(wrap-long: * (fooBar!)\n* (fooBar2!)\n)",
404 "<ul><li>⧼fooBar!⧽</li>\n<li>⧼fooBar2!⧽</li></ul>\n",
405 "<p>(wrap-long: * (fooBar!)\n</p>\n<ul><li>(fooBar2!)</li></ul>\n<p>)\n</p>",
408 $status = new Status();
409 $status->warning( new Message( 'fooBar!', [ 'foo', 'bar' ] ) );
410 $testCases['1MessageWarning'] = [
413 "(wrap-short: (fooBar!: foo, bar))",
414 "<p>⧼fooBar!⧽\n</p>",
415 "<p>(wrap-short: (fooBar!: foo, bar))\n</p>",
418 $status = new Status();
419 $status->warning( new Message( 'fooBar!', [ 'foo', 'bar' ] ) );
420 $status->warning( new Message( 'fooBar2!' ) );
421 $testCases['2MessageWarnings'] = [
423 "* ⧼fooBar!⧽\n* ⧼fooBar2!⧽\n",
424 "(wrap-long: * (fooBar!: foo, bar)\n* (fooBar2!)\n)",
425 "<ul><li>⧼fooBar!⧽</li>\n<li>⧼fooBar2!⧽</li></ul>\n",
426 "<p>(wrap-long: * (fooBar!: foo, bar)\n</p>\n<ul><li>(fooBar2!)</li></ul>\n<p>)\n</p>",
432 private static function sanitizedMessageParams( Message
$message ) {
433 return array_map( function ( $p ) {
434 return $p instanceof Message
436 'key' => $p->getKey(),
437 'params' => self
::sanitizedMessageParams( $p ),
438 'lang' => $p->getLanguage()->getCode(),
441 }, $message->getParams() );
445 * @dataProvider provideGetMessage
446 * @covers Status::getMessage
448 public function testGetMessage(
449 Status
$status, $expectedParams = [], $expectedKey, $expectedWrapper
451 $message = $status->getMessage( null, null, 'qqx' );
452 $this->assertInstanceOf( Message
::class, $message );
453 $this->assertEquals( $expectedParams, self
::sanitizedMessageParams( $message ),
454 'Message::getParams' );
455 $this->assertEquals( $expectedKey, $message->getKey(), 'Message::getKey' );
457 $message = $status->getMessage( 'wrapper-short', 'wrapper-long' );
458 $this->assertInstanceOf( Message
::class, $message );
459 $this->assertEquals( $expectedWrapper, $message->getKey(), 'Message::getKey with wrappers' );
460 $this->assertCount( 1, $message->getParams(), 'Message::getParams with wrappers' );
462 $message = $status->getMessage( 'wrapper' );
463 $this->assertInstanceOf( Message
::class, $message );
464 $this->assertEquals( 'wrapper', $message->getKey(), 'Message::getKey with wrappers' );
465 $this->assertCount( 1, $message->getParams(), 'Message::getParams with wrappers' );
467 $message = $status->getMessage( false, 'wrapper' );
468 $this->assertInstanceOf( Message
::class, $message );
469 $this->assertEquals( 'wrapper', $message->getKey(), 'Message::getKey with wrappers' );
470 $this->assertCount( 1, $message->getParams(), 'Message::getParams with wrappers' );
474 * @return array Array of arrays with values;
476 * 1 => expected Message parameters (with no context)
477 * 2 => expected Message key
479 public static function provideGetMessage() {
482 $testCases['GoodStatus'] = [
484 [ "Status::getMessage called for a good result, this is incorrect\n" ],
485 'internalerror_info',
489 $status = new Status();
490 $status->setOK( false );
491 $testCases['GoodButNoError'] = [
493 [ "Status::getMessage: Invalid result object: no error text but not OK\n" ],
494 'internalerror_info',
498 $status = new Status();
499 $status->warning( 'fooBar!' );
500 $testCases['1StringWarning'] = [
507 $status = new Status();
508 $status->warning( 'fooBar!' );
509 $status->warning( 'fooBar2!' );
510 $testCases[ '2StringWarnings' ] = [
513 [ 'key' => 'fooBar!', 'params' => [], 'lang' => 'qqx' ],
514 [ 'key' => 'fooBar2!', 'params' => [], 'lang' => 'qqx' ]
520 $status = new Status();
521 $status->warning( new Message( 'fooBar!', [ 'foo', 'bar' ] ) );
522 $testCases['1MessageWarning'] = [
529 $status = new Status();
530 $status->warning( new Message( 'fooBar!', [ 'foo', 'bar' ] ) );
531 $status->warning( new Message( 'fooBar2!' ) );
532 $testCases['2MessageWarnings'] = [
535 [ 'key' => 'fooBar!', 'params' => [ 'foo', 'bar' ], 'lang' => 'qqx' ],
536 [ 'key' => 'fooBar2!', 'params' => [], 'lang' => 'qqx' ]
546 * @covers Status::replaceMessage
548 public function testReplaceMessage() {
549 $status = new Status();
550 $message = new Message( 'key1', [ 'foo1', 'bar1' ] );
551 $status->error( $message );
552 $newMessage = new Message( 'key2', [ 'foo2', 'bar2' ] );
554 $status->replaceMessage( $message, $newMessage );
556 $this->assertEquals( $newMessage, $status->errors
[0]['message'] );
560 * @covers Status::getErrorMessage
562 public function testGetErrorMessage() {
563 $method = new ReflectionMethod( Status
::class, 'getErrorMessage' );
564 $method->setAccessible( true );
565 $status = new Status();
569 /** @var Message $message */
570 $message = $method->invoke( $status, array_merge( [ $key ], $params ) );
571 $this->assertInstanceOf( Message
::class, $message );
572 $this->assertEquals( $key, $message->getKey() );
573 $this->assertEquals( $params, $message->getParams() );
577 * @covers Status::getErrorMessageArray
579 public function testGetErrorMessageArray() {
580 $method = new ReflectionMethod( Status
::class, 'getErrorMessageArray' );
581 $method->setAccessible( true );
582 $status = new Status();
586 /** @var Message[] $messageArray */
587 $messageArray = $method->invoke(
590 array_merge( [ $key ], $params ),
591 array_merge( [ $key ], $params )
595 $this->assertInternalType( 'array', $messageArray );
596 $this->assertCount( 2, $messageArray );
597 foreach ( $messageArray as $message ) {
598 $this->assertInstanceOf( Message
::class, $message );
599 $this->assertEquals( $key, $message->getKey() );
600 $this->assertEquals( $params, $message->getParams() );
605 * @covers Status::getErrorsByType
607 public function testGetErrorsByType() {
608 $status = new Status();
609 $warning = new Message( 'warning111' );
610 $error = new Message( 'error111' );
611 $status->warning( $warning );
612 $status->error( $error );
614 $warnings = $status->getErrorsByType( 'warning' );
615 $errors = $status->getErrorsByType( 'error' );
617 $this->assertCount( 1, $warnings );
618 $this->assertCount( 1, $errors );
619 $this->assertEquals( $warning, $warnings[0]['message'] );
620 $this->assertEquals( $error, $errors[0]['message'] );
624 * @covers Status::__wakeup
626 public function testWakeUpSanitizesCallback() {
627 $status = new Status();
628 $status->cleanCallback
= function ( $value ) {
629 return '-' . $value . '-';
632 $this->assertEquals( false, $status->cleanCallback
);
636 * @dataProvider provideNonObjectMessages
637 * @covers Status::getStatusArray
639 public function testGetStatusArrayWithNonObjectMessages( $nonObjMsg ) {
640 $status = new Status();
641 if ( !array_key_exists( 1, $nonObjMsg ) ) {
642 $status->warning( $nonObjMsg[0] );
644 $status->warning( $nonObjMsg[0], $nonObjMsg[1] );
647 $array = $status->getWarningsArray(); // We use getWarningsArray to access getStatusArray
649 $this->assertEquals( 1, count( $array ) );
650 $this->assertEquals( $nonObjMsg, $array[0] );
653 public static function provideNonObjectMessages() {
655 [ [ 'ImaString', [ 'param1' => 'value1' ] ] ],
661 * @dataProvider provideErrorsWarningsOnly
662 * @covers Status::splitByErrorType
663 * @covers StatusValue::splitByErrorType
665 public function testGetErrorsWarningsOnlyStatus( $errorText, $warningText, $type, $errorResult,
668 $status = Status
::newGood();
670 $status->fatal( $errorText );
672 if ( $warningText ) {
673 $status->warning( $warningText );
675 $testStatus = $status->splitByErrorType()[$type];
676 $this->assertEquals( $errorResult, $testStatus->getErrorsByType( 'error' ) );
677 $this->assertEquals( $warningResult, $testStatus->getErrorsByType( 'warning' ) );
680 public static function provideErrorsWarningsOnly() {
689 'message' => 'Just an error',
702 'message' => 'Just a warning',