Merge "Revert "Add AvailableRightsTest for User::getAllRights completeness""
[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 foreach ( $errors as $error ) {
113 $status->warning( $error );
114 }
115 $this->assertEquals( $expected, $status->isGood() );
116 }
117
118 public static function provideIsGood() {
119 return array(
120 array( true, array(), true ),
121 array( true, array( 'foo' ), false ),
122 array( false, array(), false ),
123 array( false, array( 'foo' ), false ),
124 );
125 }
126
127 /**
128 * @dataProvider provideMockMessageDetails
129 * @covers Status::warning
130 * @covers Status::getWarningsArray
131 * @covers Status::getStatusArray
132 */
133 public function testWarningWithMessage( $mockDetails ) {
134 $status = new Status();
135 $messages = $this->getMockMessages( $mockDetails );
136
137 foreach ( $messages as $message ) {
138 $status->warning( $message );
139 }
140 $warnings = $status->getWarningsArray();
141
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 );
146 }
147 }
148
149 /**
150 * @dataProvider provideMockMessageDetails
151 * @covers Status::error
152 * @covers Status::getErrorsArray
153 * @covers Status::getStatusArray
154 */
155 public function testErrorWithMessage( $mockDetails ) {
156 $status = new Status();
157 $messages = $this->getMockMessages( $mockDetails );
158
159 foreach ( $messages as $message ) {
160 $status->error( $message );
161 }
162 $errors = $status->getErrorsArray();
163
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 );
168 }
169 }
170
171 /**
172 * @dataProvider provideMockMessageDetails
173 * @covers Status::fatal
174 * @covers Status::getErrorsArray
175 * @covers Status::getStatusArray
176 */
177 public function testFatalWithMessage( $mockDetails ) {
178 $status = new Status();
179 $messages = $this->getMockMessages( $mockDetails );
180
181 foreach ( $messages as $message ) {
182 $status->fatal( $message );
183 }
184 $errors = $status->getErrorsArray();
185
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 );
190 }
191 $this->assertFalse( $status->isOK() );
192 }
193
194 protected function getMockMessage( $key = 'key', $params = array() ) {
195 $message = $this->getMockBuilder( 'Message' )
196 ->disableOriginalConstructor()
197 ->getMock();
198 $message->expects( $this->atLeastOnce() )
199 ->method( 'getKey' )
200 ->will( $this->returnValue( $key ) );
201 $message->expects( $this->atLeastOnce() )
202 ->method( 'getParams' )
203 ->will( $this->returnValue( $params ) );
204 return $message;
205 }
206
207 /**
208 * @param array $messageDetails E.g. array( 'KEY' => array(/PARAMS/) )
209 * @return Message[]
210 */
211 protected function getMockMessages( $messageDetails ) {
212 $messages = array();
213 foreach ( $messageDetails as $key => $paramsArray ) {
214 $messages[] = $this->getMockMessage( $key, $paramsArray );
215 }
216 return $messages;
217 }
218
219 public static function provideMockMessageDetails() {
220 return array(
221 array( array( 'key1' => array( 'foo' => 'bar' ) ) ),
222 array( array( 'key1' => array( 'foo' => 'bar' ), 'key2' => array( 'foo2' => 'bar2' ) ) ),
223 );
224 }
225
226 /**
227 * @covers Status::merge
228 */
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 );
236
237 $status1->merge( $status2 );
238 $this->assertEquals(
239 2,
240 count( $status1->getWarningsArray() ) + count( $status1->getErrorsArray() )
241 );
242 }
243
244 /**
245 * @covers Status::merge
246 */
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';
255
256 $status1->merge( $status2, true );
257 $this->assertEquals(
258 2,
259 count( $status1->getWarningsArray() ) + count( $status1->getErrorsArray() )
260 );
261 $this->assertEquals( 'FooValue', $status1->getValue() );
262 }
263
264 /**
265 * @covers Status::hasMessage
266 */
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' ) );
275 }
276
277 /**
278 * @dataProvider provideCleanParams
279 * @covers Status::cleanParams
280 */
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;
286
287 $this->assertEquals( $expected, $method->invoke( $status, $params ) );
288 }
289
290 public static function provideCleanParams() {
291 $cleanCallback = function ( $value ) {
292 return '-' . $value . '-';
293 };
294
295 return array(
296 array( false, array( 'foo' => 'bar' ), array( 'foo' => 'bar' ) ),
297 array( $cleanCallback, array( 'foo' => 'bar' ), array( 'foo' => '-bar-' ) ),
298 );
299 }
300
301 /**
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
307 */
308 public function testGetWikiText( Status $status, $wikitext, $html ) {
309 $this->assertEquals( $wikitext, $status->getWikiText() );
310 }
311
312 /**
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.
319 */
320 public function testGetHtml( Status $status, $wikitext, $html ) {
321 $this->assertEquals( $html, $status->getHTML() );
322 }
323
324 /**
325 * @return array Array of arrays with values;
326 * 0 => status object
327 * 1 => expected string (with no context)
328 */
329 public static function provideGetWikiTextAndHtml() {
330 $testCases = array();
331
332 $testCases['GoodStatus'] = array(
333 new Status(),
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>",
336 );
337
338 $status = new Status();
339 $status->ok = false;
340 $testCases['GoodButNoError'] = array(
341 $status,
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>",
344 );
345
346 $status = new Status();
347 $status->warning( 'fooBar!' );
348 $testCases['1StringWarning'] = array(
349 $status,
350 "<fooBar!>",
351 "<p>&lt;fooBar!&gt;\n</p>",
352 );
353
354 $status = new Status();
355 $status->warning( 'fooBar!' );
356 $status->warning( 'fooBar2!' );
357 $testCases['2StringWarnings'] = array(
358 $status,
359 "* <fooBar!>\n* <fooBar2!>\n",
360 "<ul><li> &lt;fooBar!&gt;</li>\n<li> &lt;fooBar2!&gt;</li></ul>\n",
361 );
362
363 $status = new Status();
364 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
365 $testCases['1MessageWarning'] = array(
366 $status,
367 "<fooBar!>",
368 "<p>&lt;fooBar!&gt;\n</p>",
369 );
370
371 $status = new Status();
372 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
373 $status->warning( new Message( 'fooBar2!' ) );
374 $testCases['2MessageWarnings'] = array(
375 $status,
376 "* <fooBar!>\n* <fooBar2!>\n",
377 "<ul><li> &lt;fooBar!&gt;</li>\n<li> &lt;fooBar2!&gt;</li></ul>\n",
378 );
379
380 return $testCases;
381 }
382
383 /**
384 * @dataProvider provideGetMessage
385 * @covers Status::getMessage
386 * @todo test long and short context messages generated through this method
387 */
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' );
393 }
394
395 /**
396 * @return array Array of arrays with values;
397 * 0 => status object
398 * 1 => expected Message parameters (with no context)
399 * 2 => expected Message key
400 */
401 public static function provideGetMessage() {
402 $testCases = array();
403
404 $testCases['GoodStatus'] = array(
405 new Status(),
406 array( "Status::getMessage called for a good result, this is incorrect\n" ),
407 'internalerror_info'
408 );
409
410 $status = new Status();
411 $status->ok = false;
412 $testCases['GoodButNoError'] = array(
413 $status,
414 array( "Status::getMessage: Invalid result object: no error text but not OK\n" ),
415 'internalerror_info'
416 );
417
418 $status = new Status();
419 $status->warning( 'fooBar!' );
420 $testCases['1StringWarning'] = array(
421 $status,
422 array(),
423 'fooBar!'
424 );
425
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(
435 // $status,
436 // array( new Message( 'fooBar!' ), new Message( 'fooBar2!' ) ),
437 // "* \$1\n* \$2"
438 // );
439
440 $status = new Status();
441 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
442 $testCases['1MessageWarning'] = array(
443 $status,
444 array( 'foo', 'bar' ),
445 'fooBar!'
446 );
447
448 $status = new Status();
449 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
450 $status->warning( new Message( 'fooBar2!' ) );
451 $testCases['2MessageWarnings'] = array(
452 $status,
453 array( new Message( 'fooBar!', array( 'foo', 'bar' ) ), new Message( 'fooBar2!' ) ),
454 "* \$1\n* \$2"
455 );
456
457 return $testCases;
458 }
459
460 /**
461 * @covers Status::replaceMessage
462 */
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' ) );
468
469 $status->replaceMessage( $message, $newMessage );
470
471 $this->assertEquals( $newMessage, $status->errors[0]['message'] );
472 }
473
474 /**
475 * @covers Status::getErrorMessage
476 */
477 public function testGetErrorMessage() {
478 $method = new ReflectionMethod( 'Status', 'getErrorMessage' );
479 $method->setAccessible( true );
480 $status = new Status();
481 $key = 'foo';
482 $params = array( 'bar' );
483
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() );
489 }
490
491 /**
492 * @covers Status::getErrorMessageArray
493 */
494 public function testGetErrorMessageArray() {
495 $method = new ReflectionMethod( 'Status', 'getErrorMessageArray' );
496 $method->setAccessible( true );
497 $status = new Status();
498 $key = 'foo';
499 $params = array( 'bar' );
500
501 /** @var Message[] $messageArray */
502 $messageArray = $method->invoke(
503 $status,
504 array(
505 array_merge( array( $key ), $params ),
506 array_merge( array( $key ), $params )
507 )
508 );
509
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() );
516 }
517 }
518
519 /**
520 * @covers Status::getErrorsByType
521 */
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 );
528
529 $warnings = $status->getErrorsByType( 'warning' );
530 $errors = $status->getErrorsByType( 'error' );
531
532 $this->assertCount( 1, $warnings );
533 $this->assertCount( 1, $errors );
534 $this->assertEquals( $warning, $warnings[0]['message'] );
535 $this->assertEquals( $error, $errors[0]['message'] );
536 }
537
538 /**
539 * @covers Status::__wakeup
540 */
541 public function testWakeUpSanitizesCallback() {
542 $status = new Status();
543 $status->cleanCallback = function ( $value ) {
544 return '-' . $value . '-';
545 };
546 $status->__wakeup();
547 $this->assertEquals( false, $status->cleanCallback );
548 }
549
550 /**
551 * @dataProvider provideNonObjectMessages
552 * @covers Status::getStatusArray
553 */
554 public function testGetStatusArrayWithNonObjectMessages( $nonObjMsg ) {
555 $status = new Status();
556 if ( !array_key_exists( 1, $nonObjMsg ) ) {
557 $status->warning( $nonObjMsg[0] );
558 } else {
559 $status->warning( $nonObjMsg[0], $nonObjMsg[1] );
560 }
561
562 $array = $status->getWarningsArray(); // We use getWarningsArray to access getStatusArray
563
564 $this->assertEquals( 1, count( $array ) );
565 $this->assertEquals( $nonObjMsg, $array[0] );
566 }
567
568 public static function provideNonObjectMessages() {
569 return array(
570 array( array( 'ImaString', array( 'param1' => 'value1' ) ) ),
571 array( array( 'ImaString' ) ),
572 );
573 }
574
575 }