Merge "Remove useless "src" param when wrapping doGetFileStatMulti()"
[lhc/web/wiklou.git] / tests / phpunit / includes / StatusTest.php
1 <?php
2
3 /**
4 * @author Adam Shorland
5 */
6 class StatusTest extends MediaWikiLangTestCase {
7
8 public function testCanConstruct() {
9 new Status();
10 $this->assertTrue( true );
11 }
12
13 /**
14 * @dataProvider provideValues
15 * @covers Status::newGood
16 */
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() );
22 }
23
24 public static function provideValues() {
25 return array(
26 array(),
27 array( 'foo' ),
28 array( array( 'foo' => 'bar' ) ),
29 array( new Exception() ),
30 array( 1234 ),
31 );
32 }
33
34 /**
35 * @covers Status::newFatal
36 */
37 public function testNewFatalWithMessage() {
38 $message = $this->getMockBuilder( 'Message' )
39 ->disableOriginalConstructor()
40 ->getMock();
41
42 $status = Status::newFatal( $message );
43 $this->assertFalse( $status->isGood() );
44 $this->assertFalse( $status->isOK() );
45 $this->assertEquals( $message, $status->getMessage() );
46 }
47
48 /**
49 * @covers Status::newFatal
50 */
51 public function testNewFatalWithString() {
52 $message = 'foo';
53 $status = Status::newFatal( $message );
54 $this->assertFalse( $status->isGood() );
55 $this->assertFalse( $status->isOK() );
56 $this->assertEquals( $message, $status->getMessage()->getKey() );
57 }
58
59 /**
60 * @dataProvider provideSetResult
61 * @covers Status::setResult
62 */
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() );
68 }
69
70 public static function provideSetResult() {
71 return array(
72 array( true ),
73 array( false ),
74 array( true, 'value' ),
75 array( false, 'value' ),
76 );
77 }
78
79 /**
80 * @dataProvider provideIsOk
81 * @covers Status::isOk
82 */
83 public function testIsOk( $ok ) {
84 $status = new Status();
85 $status->ok = $ok;
86 $this->assertEquals( $ok, $status->isOK() );
87 }
88
89 public static function provideIsOk() {
90 return array(
91 array( true ),
92 array( false ),
93 );
94 }
95
96 /**
97 * @covers Status::getValue
98 */
99 public function testGetValue() {
100 $status = new Status();
101 $status->value = 'foobar';
102 $this->assertEquals( 'foobar', $status->getValue() );
103 }
104
105 /**
106 * @dataProvider provideIsGood
107 * @covers Status::isGood
108 */
109 public function testIsGood( $ok, $errors, $expected ) {
110 $status = new Status();
111 $status->ok = $ok;
112 $status->errors = $errors;
113 $this->assertEquals( $expected, $status->isGood() );
114 }
115
116 public static function provideIsGood() {
117 return array(
118 array( true, array(), true ),
119 array( true, array( 'foo' ), false ),
120 array( false, array(), false ),
121 array( false, array( 'foo' ), false ),
122 );
123 }
124
125 /**
126 * @dataProvider provideMockMessageDetails
127 * @covers Status::warning
128 * @covers Status::getWarningsArray
129 * @covers Status::getStatusArray
130 */
131 public function testWarningWithMessage( $mockDetails ) {
132 $status = new Status();
133 $messages = $this->getMockMessages( $mockDetails );
134
135 foreach ( $messages as $message ) {
136 $status->warning( $message );
137 }
138 $warnings = $status->getWarningsArray();
139
140 $this->assertEquals( count( $messages ), count( $warnings ) );
141 foreach ( $messages as $key => $message ) {
142 $expectedArray = array_merge( array( $message->getKey() ), $message->getParams() );
143 $this->assertEquals( $warnings[$key], $expectedArray );
144 }
145 }
146
147 /**
148 * @dataProvider provideMockMessageDetails
149 * @covers Status::error
150 * @covers Status::getErrorsArray
151 * @covers Status::getStatusArray
152 */
153 public function testErrorWithMessage( $mockDetails ) {
154 $status = new Status();
155 $messages = $this->getMockMessages( $mockDetails );
156
157 foreach ( $messages as $message ) {
158 $status->error( $message );
159 }
160 $errors = $status->getErrorsArray();
161
162 $this->assertEquals( count( $messages ), count( $errors ) );
163 foreach ( $messages as $key => $message ) {
164 $expectedArray = array_merge( array( $message->getKey() ), $message->getParams() );
165 $this->assertEquals( $errors[$key], $expectedArray );
166 }
167 }
168
169 /**
170 * @dataProvider provideMockMessageDetails
171 * @covers Status::fatal
172 * @covers Status::getErrorsArray
173 * @covers Status::getStatusArray
174 */
175 public function testFatalWithMessage( $mockDetails ) {
176 $status = new Status();
177 $messages = $this->getMockMessages( $mockDetails );
178
179 foreach ( $messages as $message ) {
180 $status->fatal( $message );
181 }
182 $errors = $status->getErrorsArray();
183
184 $this->assertEquals( count( $messages ), count( $errors ) );
185 foreach ( $messages as $key => $message ) {
186 $expectedArray = array_merge( array( $message->getKey() ), $message->getParams() );
187 $this->assertEquals( $errors[$key], $expectedArray );
188 }
189 $this->assertFalse( $status->isOK() );
190 }
191
192 protected function getMockMessage( $key = 'key', $params = array() ) {
193 $message = $this->getMockBuilder( 'Message' )
194 ->disableOriginalConstructor()
195 ->getMock();
196 $message->expects( $this->atLeastOnce() )
197 ->method( 'getKey' )
198 ->will( $this->returnValue( $key ) );
199 $message->expects( $this->atLeastOnce() )
200 ->method( 'getParams' )
201 ->will( $this->returnValue( $params ) );
202 return $message;
203 }
204
205 /**
206 * @param array $messageDetails eg. array( 'KEY' => array(/PARAMS/) )
207 * @return Message[]
208 */
209 protected function getMockMessages( $messageDetails ) {
210 $messages = array();
211 foreach ( $messageDetails as $key => $paramsArray ) {
212 $messages[] = $this->getMockMessage( $key, $paramsArray );
213 }
214 return $messages;
215 }
216
217 public static function provideMockMessageDetails() {
218 return array(
219 array( array( 'key1' => array( 'foo' => 'bar' ) ) ),
220 array( array( 'key1' => array( 'foo' => 'bar' ), 'key2' => array( 'foo2' => 'bar2' ) ) ),
221 );
222 }
223
224 /**
225 * @covers Status::merge
226 */
227 public function testMerge() {
228 $status1 = new Status();
229 $status2 = new Status();
230 $message1 = $this->getMockMessage( 'warn1' );
231 $message2 = $this->getMockMessage( 'error2' );
232 $status1->warning( $message1 );
233 $status2->error( $message2 );
234
235 $status1->merge( $status2 );
236 $this->assertEquals( 2, count( $status1->getWarningsArray() ) + count( $status1->getErrorsArray() ) );
237 }
238
239 /**
240 * @covers Status::merge
241 */
242 public function testMergeWithOverwriteValue() {
243 $status1 = new Status();
244 $status2 = new Status();
245 $message1 = $this->getMockMessage( 'warn1' );
246 $message2 = $this->getMockMessage( 'error2' );
247 $status1->warning( $message1 );
248 $status2->error( $message2 );
249 $status2->value = 'FooValue';
250
251 $status1->merge( $status2, true );
252 $this->assertEquals( 2, count( $status1->getWarningsArray() ) + count( $status1->getErrorsArray() ) );
253 $this->assertEquals( 'FooValue', $status1->getValue() );
254 }
255
256 /**
257 * @covers Status::hasMessage
258 */
259 public function testHasMessage() {
260 $status = new Status();
261 $status->fatal( 'bad' );
262 $this->assertTrue( $status->hasMessage( 'bad' ) );
263 $this->assertFalse( $status->hasMessage( 'good' ) );
264 }
265
266 /**
267 * @dataProvider provideCleanParams
268 * @covers Status::cleanParams
269 */
270 public function testCleanParams( $cleanCallback, $params, $expected ) {
271 $method = new ReflectionMethod( 'Status', 'cleanParams' );
272 $method->setAccessible( true );
273 $status = new Status();
274 $status->cleanCallback = $cleanCallback;
275
276 $this->assertEquals( $expected, $method->invoke( $status, $params ) );
277 }
278
279 public static function provideCleanParams() {
280 $cleanCallback = function( $value ) {
281 return '-' . $value . '-';
282 };
283
284 return array(
285 array( false, array( 'foo' => 'bar' ), array( 'foo' => 'bar' ) ),
286 array( $cleanCallback, array( 'foo' => 'bar' ), array( 'foo' => '-bar-' ) ),
287 );
288 }
289
290 /**
291 * @dataProvider provideGetWikiTextAndHtml
292 * @covers Status::getWikiText
293 * @todo test long and short context messages generated through this method
294 * this can not really be done now due to use of wfMessage()->plain()
295 * It is possible to mock such methods but only if namespaces are used
296 */
297 public function testGetWikiText( Status $status, $wikitext, $html ) {
298 $this->assertEquals( $wikitext, $status->getWikiText() );
299 }
300
301 /**
302 * @dataProvider provideGetWikiTextAndHtml
303 * @covers Status::getHtml
304 * @todo test long and short context messages generated through this method
305 * this can not really be done now due to use of $this->getWikiText using wfMessage()->plain()
306 * It is possible to mock such methods but only if namespaces are used
307 */
308 public function testGetHtml( Status $status, $wikitext, $html ) {
309 $this->assertEquals( $html, $status->getHTML() );
310 }
311
312 /**
313 * @return array of arrays with values;
314 * 0 => status object
315 * 1 => expected string (with no context)
316 */
317 public static function provideGetWikiTextAndHtml() {
318 $testCases = array();
319
320 $testCases[ 'GoodStatus' ] = array(
321 new Status(),
322 "Internal error: Status::getWikiText called for a good result, this is incorrect\n",
323 "<p>Internal error: Status::getWikiText called for a good result, this is incorrect\n</p>",
324 );
325
326 $status = new Status();
327 $status->ok = false;
328 $testCases[ 'GoodButNoError' ] = array(
329 $status,
330 "Internal error: Status::getWikiText: Invalid result object: no error text but not OK\n",
331 "<p>Internal error: Status::getWikiText: Invalid result object: no error text but not OK\n</p>",
332 );
333
334 $status = new Status();
335 $status->warning( 'fooBar!' );
336 $testCases[ '1StringWarning' ] = array(
337 $status,
338 "<fooBar!>",
339 "<p>&lt;fooBar!&gt;\n</p>",
340 );
341
342 $status = new Status();
343 $status->warning( 'fooBar!' );
344 $status->warning( 'fooBar2!' );
345 $testCases[ '2StringWarnings' ] = array(
346 $status,
347 "* <fooBar!>\n* <fooBar2!>\n",
348 "<ul>\n<li> &lt;fooBar!&gt;\n</li>\n<li> &lt;fooBar2!&gt;\n</li>\n</ul>\n",
349 );
350
351 $status = new Status();
352 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
353 $testCases[ '1MessageWarning' ] = array(
354 $status,
355 "<fooBar!>",
356 "<p>&lt;fooBar!&gt;\n</p>",
357 );
358
359 $status = new Status();
360 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
361 $status->warning( new Message( 'fooBar2!' ) );
362 $testCases[ '2MessageWarnings' ] = array(
363 $status,
364 "* <fooBar!>\n* <fooBar2!>\n",
365 "<ul>\n<li> &lt;fooBar!&gt;\n</li>\n<li> &lt;fooBar2!&gt;\n</li>\n</ul>\n",
366 );
367
368 return $testCases;
369 }
370
371 /**
372 * @dataProvider provideGetMessage
373 * @covers Status::getMessage
374 * @todo test with multiple messages at once
375 */
376 public function testGetMessage( Status $status, $expectedParams = array(), $expectedKey, $shortContext = false, $longContext = false ) {
377 $message = $status->getMessage( $shortContext, $longContext );
378 $this->assertInstanceOf( 'Message', $message );
379
380 // Loop through until we get to the appropriate depth for the message
381 $loops = $shortContext ? 1 : ( $longContext ? 2 : 0 );
382 for( $i = 1; $i <= $loops; $i++ ) {
383 $params = $message->getParams();
384 $this->assertInstanceOf( 'Message', $params[0] );
385 $message = $params[0];
386 }
387
388 $this->assertEquals( $expectedParams, $message->getParams() );
389 $this->assertEquals( $expectedKey, $message->getKey() );
390 }
391
392 /**
393 * @return array of arrays with values;
394 * 0 => status object
395 * 1 => expected Message Params
396 */
397 public static function provideGetMessage() {
398 $testCases = array();
399
400 $testCases[ 'GoodStatus' ] = array(
401 new Status(),
402 array( "Status::getMessage called for a good result, this is incorrect\n" ),
403 'internalerror_info'
404 );
405
406 $status = new Status();
407 $status->ok = false;
408 $testCases[ 'GoodButNoError' ] = array(
409 $status,
410 array( "Status::getMessage: Invalid result object: no error text but not OK\n" ),
411 'internalerror_info'
412 );
413
414 $testCases[ 'GoodButNoErrorShortContext' ] = array(
415 $status,
416 array( "Status::getMessage: Invalid result object: no error text but not OK\n" ),
417 'internalerror_info',
418 true
419 );
420
421 $testCases[ 'GoodButNoErrorLongContext' ] = array(
422 $status,
423 array( "Status::getMessage: Invalid result object: no error text but not OK\n" ),
424 'internalerror_info',
425 false,
426 true
427 );
428
429 $status = new Status();
430 $status->warning( 'fooBar!' );
431 $testCases[ '1StringWarning' ] = array(
432 $status,
433 array(),
434 "fooBar!"
435 );
436
437 //NOTE: this seems to return a string instead of a Message object...
438 // $status = new Status();
439 // $status->warning( 'fooBar!' );
440 // $status->warning( 'fooBar2!' );
441 // $testCases[ '2StringWarnings' ] = array(
442 // $status,
443 // array(),
444 // ''
445 // );
446
447 $status = new Status();
448 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
449 $testCases[ '1MessageWarning' ] = array(
450 $status,
451 array( 'foo', 'bar' ),
452 "fooBar!",
453 );
454
455 //NOTE: this seems to return a string instead of a Message object...
456 // $status = new Status();
457 // $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
458 // $status->warning( new Message( 'fooBar2!' ) );
459 // $testCases[ '2MessageWarnings' ] = array(
460 // $status,
461 // array(),
462 // "",
463 // );
464
465 $status = new Status();
466 $status->error( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
467 $testCases[ '1MessageError' ] = array(
468 $status,
469 array( 'foo', 'bar' ),
470 "fooBar!",
471 );
472
473 $status = new Status();
474 $status->error( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
475 $testCases[ '1MessageErrorShortContext' ] = array(
476 $status,
477 array( 'foo', 'bar' ),
478 "fooBar!",
479 true,
480 );
481
482 $status = new Status();
483 $status->error( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
484 $testCases[ '1MessageErrorLongContext' ] = array(
485 $status,
486 array( 'foo', 'bar' ),
487 "fooBar!",
488 false,
489 true,
490 );
491
492 return $testCases;
493 }
494
495 /**
496 * @covers Status::replaceMessage
497 */
498 public function testReplaceMessage() {
499 $status = new Status();
500 $message = new Message( 'key1', array( 'foo1', 'bar1' ) );
501 $status->error( $message );
502 $newMessage = new Message( 'key2', array( 'foo2', 'bar2' ) );
503
504 $status->replaceMessage( $message, $newMessage );
505
506 $this->assertEquals( $newMessage, $status->errors[0]['message'] );
507 }
508
509 /**
510 * @covers Status::getErrorMessage
511 */
512 public function testGetErrorMessage() {
513 $method = new ReflectionMethod( 'Status', 'getErrorMessage' );
514 $method->setAccessible( true );
515 $status = new Status();
516 $key = 'foo';
517 $params = array( 'bar' );
518
519 /** @var Message $message */
520 $message = $method->invoke( $status, array_merge( array( $key ), $params ) );
521 $this->assertInstanceOf( 'Message', $message );
522 $this->assertEquals( $key, $message->getKey() );
523 $this->assertEquals( $params, $message->getParams() );
524 }
525
526 /**
527 * @covers Status::getErrorMessageArray
528 */
529 public function testGetErrorMessageArray() {
530 $method = new ReflectionMethod( 'Status', 'getErrorMessageArray' );
531 $method->setAccessible( true );
532 $status = new Status();
533 $key = 'foo';
534 $params = array( 'bar' );
535
536 /** @var Message[] $messageArray */
537 $messageArray = $method->invoke(
538 $status,
539 array(
540 array_merge( array( $key ), $params ),
541 array_merge( array( $key ), $params )
542 )
543 );
544
545 $this->assertInternalType( 'array', $messageArray );
546 $this->assertCount( 2, $messageArray );
547 foreach ( $messageArray as $message ) {
548 $this->assertInstanceOf( 'Message', $message );
549 $this->assertEquals( $key, $message->getKey() );
550 $this->assertEquals( $params, $message->getParams() );
551 }
552 }
553
554 /**
555 * @covers Status::getErrorsByType
556 */
557 public function testGetErrorsByType() {
558 $status = new Status();
559 $warning = new Message( 'warning111' );
560 $error = new Message( 'error111' );
561 $status->warning( $warning );
562 $status->error( $error );
563
564 $warnings = $status->getErrorsByType( 'warning' );
565 $errors = $status->getErrorsByType( 'error' );
566
567 $this->assertCount( 1, $warnings );
568 $this->assertCount( 1, $errors );
569 $this->assertEquals( $warning, $warnings[0]['message'] );
570 $this->assertEquals( $error, $errors[0]['message'] );
571 }
572
573 /**
574 * @covers Status::__wakeup
575 */
576 public function testWakeUpSanitizesCallback() {
577 $status = new Status();
578 $status->cleanCallback = function( $value ) {
579 return '-' . $value . '-';
580 };
581 $status->__wakeup();
582 $this->assertEquals( false, $status->cleanCallback );
583 }
584
585 /**
586 * @dataProvider provideNonObjectMessages
587 * @covers Status::getStatusArray
588 */
589 public function testGetStatusArrayWithNonObjectMessages( $nonObjMsg ) {
590 $status = new Status();
591 if( !array_key_exists( 1, $nonObjMsg ) ) {
592 $status->warning( $nonObjMsg[0] );
593 } else {
594 $status->warning( $nonObjMsg[0], $nonObjMsg[1] );
595 }
596
597 $array = $status->getWarningsArray(); // We use getWarningsArray to access getStatusArray
598
599 $this->assertEquals( 1, count( $array ) );
600 $this->assertEquals( $nonObjMsg, $array[0] );
601 }
602
603 public static function provideNonObjectMessages() {
604 return array(
605 array( array( 'ImaString', array( 'param1' => 'value1' ) ) ),
606 array( array( 'ImaString' ) ),
607 );
608 }
609
610 }