Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / tests / phpunit / includes / StatusTest.php
blobc95e69b60c713a9ffefb05a52344b1a956596b18
1 <?php
3 /**
4 * @author Adam Shorland
5 */
6 class StatusTest extends MediaWikiLangTestCase {
8 public function testCanConstruct() {
9 new Status();
10 $this->assertTrue( true );
13 /**
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() {
25 return array(
26 array(),
27 array( 'foo' ),
28 array( array( 'foo' => 'bar' ) ),
29 array( new Exception() ),
30 array( 1234 ),
34 /**
35 * @covers Status::newFatal
37 public function testNewFatalWithMessage() {
38 $message = $this->getMockBuilder( 'Message' )
39 ->disableOriginalConstructor()
40 ->getMock();
42 $status = Status::newFatal( $message );
43 $this->assertFalse( $status->isGood() );
44 $this->assertFalse( $status->isOK() );
45 $this->assertEquals( $message, $status->getMessage() );
48 /**
49 * @covers Status::newFatal
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() );
59 /**
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(
68 array(
69 array(
70 'type' => 'error',
71 'message' => 'foo',
72 'params' => array( 1, 2 )
75 $status->errors
79 /**
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() {
91 return array(
92 array( true ),
93 array( false ),
94 array( true, 'value' ),
95 array( false, 'value' ),
99 /**
100 * @dataProvider provideIsOk
101 * @covers Status::isOk
103 public function testIsOk( $ok ) {
104 $status = new Status();
105 $status->ok = $ok;
106 $this->assertEquals( $ok, $status->isOK() );
109 public static function provideIsOk() {
110 return array(
111 array( true ),
112 array( false ),
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();
131 $status->ok = $ok;
132 foreach ( $errors as $error ) {
133 $status->warning( $error );
135 $this->assertEquals( $expected, $status->isGood() );
138 public static function provideIsGood() {
139 return array(
140 array( true, array(), true ),
141 array( true, array( 'foo' ), false ),
142 array( false, array(), false ),
143 array( false, array( '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( array( $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( array( $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( array( $message->getKey() ), $message->getParams() );
209 $this->assertEquals( $errors[$key], $expectedArray );
211 $this->assertFalse( $status->isOK() );
214 protected function getMockMessage( $key = 'key', $params = array() ) {
215 $message = $this->getMockBuilder( 'Message' )
216 ->disableOriginalConstructor()
217 ->getMock();
218 $message->expects( $this->atLeastOnce() )
219 ->method( 'getKey' )
220 ->will( $this->returnValue( $key ) );
221 $message->expects( $this->atLeastOnce() )
222 ->method( 'getParams' )
223 ->will( $this->returnValue( $params ) );
224 return $message;
228 * @param array $messageDetails E.g. array( 'KEY' => array(/PARAMS/) )
229 * @return Message[]
231 protected function getMockMessages( $messageDetails ) {
232 $messages = array();
233 foreach ( $messageDetails as $key => $paramsArray ) {
234 $messages[] = $this->getMockMessage( $key, $paramsArray );
236 return $messages;
239 public static function provideMockMessageDetails() {
240 return array(
241 array( array( 'key1' => array( 'foo' => 'bar' ) ) ),
242 array( array( 'key1' => array( 'foo' => 'bar' ), 'key2' => array( '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 );
258 $this->assertEquals(
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 );
277 $this->assertEquals(
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 . '-';
315 return array(
316 array( false, array( 'foo' => 'bar' ), array( 'foo' => 'bar' ) ),
317 array( $cleanCallback, array( 'foo' => 'bar' ), array( 'foo' => '-bar-' ) ),
322 * @dataProvider provideGetWikiTextAndHtml
323 * @covers Status::getWikiText
324 * @todo test long and short context messages generated through this method
325 * this can not really be done now due to use of wfMessage()->plain()
326 * It is possible to mock such methods but only if namespaces are used
328 public function testGetWikiText( Status $status, $wikitext, $html ) {
329 $this->assertEquals( $wikitext, $status->getWikiText() );
333 * @dataProvider provideGetWikiTextAndHtml
334 * @covers Status::getHtml
335 * @todo test long and short context messages generated through this method
336 * this can not really be done now due to use of $this->getWikiText using
337 * wfMessage()->plain(). It is possible to mock such methods but only if
338 * namespaces are used.
340 public function testGetHtml( Status $status, $wikitext, $html ) {
341 $this->assertEquals( $html, $status->getHTML() );
345 * @return array Array of arrays with values;
346 * 0 => status object
347 * 1 => expected string (with no context)
349 public static function provideGetWikiTextAndHtml() {
350 $testCases = array();
352 $testCases['GoodStatus'] = array(
353 new Status(),
354 "Internal error: Status::getWikiText called for a good result, this is incorrect\n",
355 "<p>Internal error: Status::getWikiText called for a good result, this is incorrect\n</p>",
358 $status = new Status();
359 $status->ok = false;
360 $testCases['GoodButNoError'] = array(
361 $status,
362 "Internal error: Status::getWikiText: Invalid result object: no error text but not OK\n",
363 "<p>Internal error: Status::getWikiText: Invalid result object: no error text but not OK\n</p>",
366 $status = new Status();
367 $status->warning( 'fooBar!' );
368 $testCases['1StringWarning'] = array(
369 $status,
370 "<fooBar!>",
371 "<p>&lt;fooBar!&gt;\n</p>",
374 $status = new Status();
375 $status->warning( 'fooBar!' );
376 $status->warning( 'fooBar2!' );
377 $testCases['2StringWarnings'] = array(
378 $status,
379 "* <fooBar!>\n* <fooBar2!>\n",
380 "<ul><li> &lt;fooBar!&gt;</li>\n<li> &lt;fooBar2!&gt;</li></ul>\n",
383 $status = new Status();
384 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
385 $testCases['1MessageWarning'] = array(
386 $status,
387 "<fooBar!>",
388 "<p>&lt;fooBar!&gt;\n</p>",
391 $status = new Status();
392 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
393 $status->warning( new Message( 'fooBar2!' ) );
394 $testCases['2MessageWarnings'] = array(
395 $status,
396 "* <fooBar!>\n* <fooBar2!>\n",
397 "<ul><li> &lt;fooBar!&gt;</li>\n<li> &lt;fooBar2!&gt;</li></ul>\n",
400 return $testCases;
404 * @dataProvider provideGetMessage
405 * @covers Status::getMessage
406 * @todo test long and short context messages generated through this method
408 public function testGetMessage( Status $status, $expectedParams = array(), $expectedKey ) {
409 $message = $status->getMessage();
410 $this->assertInstanceOf( 'Message', $message );
411 $this->assertEquals( $expectedParams, $message->getParams(), 'Message::getParams' );
412 $this->assertEquals( $expectedKey, $message->getKey(), 'Message::getKey' );
416 * @return array Array of arrays with values;
417 * 0 => status object
418 * 1 => expected Message parameters (with no context)
419 * 2 => expected Message key
421 public static function provideGetMessage() {
422 $testCases = array();
424 $testCases['GoodStatus'] = array(
425 new Status(),
426 array( "Status::getMessage called for a good result, this is incorrect\n" ),
427 'internalerror_info'
430 $status = new Status();
431 $status->ok = false;
432 $testCases['GoodButNoError'] = array(
433 $status,
434 array( "Status::getMessage: Invalid result object: no error text but not OK\n" ),
435 'internalerror_info'
438 $status = new Status();
439 $status->warning( 'fooBar!' );
440 $testCases['1StringWarning'] = array(
441 $status,
442 array(),
443 'fooBar!'
446 // FIXME: Assertion tries to compare a StubUserLang with a Language object, because
447 // "data providers are executed before both the call to the setUpBeforeClass static method
448 // and the first call to the setUp method. Because of that you can't access any variables
449 // you create there from within a data provider."
450 // http://phpunit.de/manual/3.7/en/writing-tests-for-phpunit.html
451 // $status = new Status();
452 // $status->warning( 'fooBar!' );
453 // $status->warning( 'fooBar2!' );
454 // $testCases[ '2StringWarnings' ] = array(
455 // $status,
456 // array( new Message( 'fooBar!' ), new Message( 'fooBar2!' ) ),
457 // "* \$1\n* \$2"
458 // );
460 $status = new Status();
461 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
462 $testCases['1MessageWarning'] = array(
463 $status,
464 array( 'foo', 'bar' ),
465 'fooBar!'
468 $status = new Status();
469 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
470 $status->warning( new Message( 'fooBar2!' ) );
471 $testCases['2MessageWarnings'] = array(
472 $status,
473 array( new Message( 'fooBar!', array( 'foo', 'bar' ) ), new Message( 'fooBar2!' ) ),
474 "* \$1\n* \$2"
477 return $testCases;
481 * @covers Status::replaceMessage
483 public function testReplaceMessage() {
484 $status = new Status();
485 $message = new Message( 'key1', array( 'foo1', 'bar1' ) );
486 $status->error( $message );
487 $newMessage = new Message( 'key2', array( 'foo2', 'bar2' ) );
489 $status->replaceMessage( $message, $newMessage );
491 $this->assertEquals( $newMessage, $status->errors[0]['message'] );
495 * @covers Status::getErrorMessage
497 public function testGetErrorMessage() {
498 $method = new ReflectionMethod( 'Status', 'getErrorMessage' );
499 $method->setAccessible( true );
500 $status = new Status();
501 $key = 'foo';
502 $params = array( 'bar' );
504 /** @var Message $message */
505 $message = $method->invoke( $status, array_merge( array( $key ), $params ) );
506 $this->assertInstanceOf( 'Message', $message );
507 $this->assertEquals( $key, $message->getKey() );
508 $this->assertEquals( $params, $message->getParams() );
512 * @covers Status::getErrorMessageArray
514 public function testGetErrorMessageArray() {
515 $method = new ReflectionMethod( 'Status', 'getErrorMessageArray' );
516 $method->setAccessible( true );
517 $status = new Status();
518 $key = 'foo';
519 $params = array( 'bar' );
521 /** @var Message[] $messageArray */
522 $messageArray = $method->invoke(
523 $status,
524 array(
525 array_merge( array( $key ), $params ),
526 array_merge( array( $key ), $params )
530 $this->assertInternalType( 'array', $messageArray );
531 $this->assertCount( 2, $messageArray );
532 foreach ( $messageArray as $message ) {
533 $this->assertInstanceOf( 'Message', $message );
534 $this->assertEquals( $key, $message->getKey() );
535 $this->assertEquals( $params, $message->getParams() );
540 * @covers Status::getErrorsByType
542 public function testGetErrorsByType() {
543 $status = new Status();
544 $warning = new Message( 'warning111' );
545 $error = new Message( 'error111' );
546 $status->warning( $warning );
547 $status->error( $error );
549 $warnings = $status->getErrorsByType( 'warning' );
550 $errors = $status->getErrorsByType( 'error' );
552 $this->assertCount( 1, $warnings );
553 $this->assertCount( 1, $errors );
554 $this->assertEquals( $warning, $warnings[0]['message'] );
555 $this->assertEquals( $error, $errors[0]['message'] );
559 * @covers Status::__wakeup
561 public function testWakeUpSanitizesCallback() {
562 $status = new Status();
563 $status->cleanCallback = function ( $value ) {
564 return '-' . $value . '-';
566 $status->__wakeup();
567 $this->assertEquals( false, $status->cleanCallback );
571 * @dataProvider provideNonObjectMessages
572 * @covers Status::getStatusArray
574 public function testGetStatusArrayWithNonObjectMessages( $nonObjMsg ) {
575 $status = new Status();
576 if ( !array_key_exists( 1, $nonObjMsg ) ) {
577 $status->warning( $nonObjMsg[0] );
578 } else {
579 $status->warning( $nonObjMsg[0], $nonObjMsg[1] );
582 $array = $status->getWarningsArray(); // We use getWarningsArray to access getStatusArray
584 $this->assertEquals( 1, count( $array ) );
585 $this->assertEquals( $nonObjMsg, $array[0] );
588 public static function provideNonObjectMessages() {
589 return array(
590 array( array( 'ImaString', array( 'param1' => 'value1' ) ) ),
591 array( array( 'ImaString' ) ),