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