MediaWiki
REL1_24
|
00001 <?php 00002 00006 class StatusTest extends MediaWikiLangTestCase { 00007 00008 public function testCanConstruct() { 00009 new Status(); 00010 $this->assertTrue( true ); 00011 } 00012 00017 public function testNewGood( $value = null ) { 00018 $status = Status::newGood( $value ); 00019 $this->assertTrue( $status->isGood() ); 00020 $this->assertTrue( $status->isOK() ); 00021 $this->assertEquals( $value, $status->getValue() ); 00022 } 00023 00024 public static function provideValues() { 00025 return array( 00026 array(), 00027 array( 'foo' ), 00028 array( array( 'foo' => 'bar' ) ), 00029 array( new Exception() ), 00030 array( 1234 ), 00031 ); 00032 } 00033 00037 public function testNewFatalWithMessage() { 00038 $message = $this->getMockBuilder( 'Message' ) 00039 ->disableOriginalConstructor() 00040 ->getMock(); 00041 00042 $status = Status::newFatal( $message ); 00043 $this->assertFalse( $status->isGood() ); 00044 $this->assertFalse( $status->isOK() ); 00045 $this->assertEquals( $message, $status->getMessage() ); 00046 } 00047 00051 public function testNewFatalWithString() { 00052 $message = 'foo'; 00053 $status = Status::newFatal( $message ); 00054 $this->assertFalse( $status->isGood() ); 00055 $this->assertFalse( $status->isOK() ); 00056 $this->assertEquals( $message, $status->getMessage()->getKey() ); 00057 } 00058 00063 public function testSetResult( $ok, $value = null ) { 00064 $status = new Status(); 00065 $status->setResult( $ok, $value ); 00066 $this->assertEquals( $ok, $status->isOK() ); 00067 $this->assertEquals( $value, $status->getValue() ); 00068 } 00069 00070 public static function provideSetResult() { 00071 return array( 00072 array( true ), 00073 array( false ), 00074 array( true, 'value' ), 00075 array( false, 'value' ), 00076 ); 00077 } 00078 00083 public function testIsOk( $ok ) { 00084 $status = new Status(); 00085 $status->ok = $ok; 00086 $this->assertEquals( $ok, $status->isOK() ); 00087 } 00088 00089 public static function provideIsOk() { 00090 return array( 00091 array( true ), 00092 array( false ), 00093 ); 00094 } 00095 00099 public function testGetValue() { 00100 $status = new Status(); 00101 $status->value = 'foobar'; 00102 $this->assertEquals( 'foobar', $status->getValue() ); 00103 } 00104 00109 public function testIsGood( $ok, $errors, $expected ) { 00110 $status = new Status(); 00111 $status->ok = $ok; 00112 $status->errors = $errors; 00113 $this->assertEquals( $expected, $status->isGood() ); 00114 } 00115 00116 public static function provideIsGood() { 00117 return array( 00118 array( true, array(), true ), 00119 array( true, array( 'foo' ), false ), 00120 array( false, array(), false ), 00121 array( false, array( 'foo' ), false ), 00122 ); 00123 } 00124 00131 public function testWarningWithMessage( $mockDetails ) { 00132 $status = new Status(); 00133 $messages = $this->getMockMessages( $mockDetails ); 00134 00135 foreach ( $messages as $message ) { 00136 $status->warning( $message ); 00137 } 00138 $warnings = $status->getWarningsArray(); 00139 00140 $this->assertEquals( count( $messages ), count( $warnings ) ); 00141 foreach ( $messages as $key => $message ) { 00142 $expectedArray = array_merge( array( $message->getKey() ), $message->getParams() ); 00143 $this->assertEquals( $warnings[$key], $expectedArray ); 00144 } 00145 } 00146 00153 public function testErrorWithMessage( $mockDetails ) { 00154 $status = new Status(); 00155 $messages = $this->getMockMessages( $mockDetails ); 00156 00157 foreach ( $messages as $message ) { 00158 $status->error( $message ); 00159 } 00160 $errors = $status->getErrorsArray(); 00161 00162 $this->assertEquals( count( $messages ), count( $errors ) ); 00163 foreach ( $messages as $key => $message ) { 00164 $expectedArray = array_merge( array( $message->getKey() ), $message->getParams() ); 00165 $this->assertEquals( $errors[$key], $expectedArray ); 00166 } 00167 } 00168 00175 public function testFatalWithMessage( $mockDetails ) { 00176 $status = new Status(); 00177 $messages = $this->getMockMessages( $mockDetails ); 00178 00179 foreach ( $messages as $message ) { 00180 $status->fatal( $message ); 00181 } 00182 $errors = $status->getErrorsArray(); 00183 00184 $this->assertEquals( count( $messages ), count( $errors ) ); 00185 foreach ( $messages as $key => $message ) { 00186 $expectedArray = array_merge( array( $message->getKey() ), $message->getParams() ); 00187 $this->assertEquals( $errors[$key], $expectedArray ); 00188 } 00189 $this->assertFalse( $status->isOK() ); 00190 } 00191 00192 protected function getMockMessage( $key = 'key', $params = array() ) { 00193 $message = $this->getMockBuilder( 'Message' ) 00194 ->disableOriginalConstructor() 00195 ->getMock(); 00196 $message->expects( $this->atLeastOnce() ) 00197 ->method( 'getKey' ) 00198 ->will( $this->returnValue( $key ) ); 00199 $message->expects( $this->atLeastOnce() ) 00200 ->method( 'getParams' ) 00201 ->will( $this->returnValue( $params ) ); 00202 return $message; 00203 } 00204 00209 protected function getMockMessages( $messageDetails ) { 00210 $messages = array(); 00211 foreach ( $messageDetails as $key => $paramsArray ) { 00212 $messages[] = $this->getMockMessage( $key, $paramsArray ); 00213 } 00214 return $messages; 00215 } 00216 00217 public static function provideMockMessageDetails() { 00218 return array( 00219 array( array( 'key1' => array( 'foo' => 'bar' ) ) ), 00220 array( array( 'key1' => array( 'foo' => 'bar' ), 'key2' => array( 'foo2' => 'bar2' ) ) ), 00221 ); 00222 } 00223 00227 public function testMerge() { 00228 $status1 = new Status(); 00229 $status2 = new Status(); 00230 $message1 = $this->getMockMessage( 'warn1' ); 00231 $message2 = $this->getMockMessage( 'error2' ); 00232 $status1->warning( $message1 ); 00233 $status2->error( $message2 ); 00234 00235 $status1->merge( $status2 ); 00236 $this->assertEquals( 00237 2, 00238 count( $status1->getWarningsArray() ) + count( $status1->getErrorsArray() ) 00239 ); 00240 } 00241 00245 public function testMergeWithOverwriteValue() { 00246 $status1 = new Status(); 00247 $status2 = new Status(); 00248 $message1 = $this->getMockMessage( 'warn1' ); 00249 $message2 = $this->getMockMessage( 'error2' ); 00250 $status1->warning( $message1 ); 00251 $status2->error( $message2 ); 00252 $status2->value = 'FooValue'; 00253 00254 $status1->merge( $status2, true ); 00255 $this->assertEquals( 00256 2, 00257 count( $status1->getWarningsArray() ) + count( $status1->getErrorsArray() ) 00258 ); 00259 $this->assertEquals( 'FooValue', $status1->getValue() ); 00260 } 00261 00265 public function testHasMessage() { 00266 $status = new Status(); 00267 $status->fatal( 'bad' ); 00268 $status->fatal( wfMessage( 'bad-msg' ) ); 00269 $this->assertTrue( $status->hasMessage( 'bad' ) ); 00270 $this->assertTrue( $status->hasMessage( 'bad-msg' ) ); 00271 $this->assertTrue( $status->hasMessage( wfMessage( 'bad-msg' ) ) ); 00272 $this->assertFalse( $status->hasMessage( 'good' ) ); 00273 } 00274 00279 public function testCleanParams( $cleanCallback, $params, $expected ) { 00280 $method = new ReflectionMethod( 'Status', 'cleanParams' ); 00281 $method->setAccessible( true ); 00282 $status = new Status(); 00283 $status->cleanCallback = $cleanCallback; 00284 00285 $this->assertEquals( $expected, $method->invoke( $status, $params ) ); 00286 } 00287 00288 public static function provideCleanParams() { 00289 $cleanCallback = function ( $value ) { 00290 return '-' . $value . '-'; 00291 }; 00292 00293 return array( 00294 array( false, array( 'foo' => 'bar' ), array( 'foo' => 'bar' ) ), 00295 array( $cleanCallback, array( 'foo' => 'bar' ), array( 'foo' => '-bar-' ) ), 00296 ); 00297 } 00298 00306 public function testGetWikiText( Status $status, $wikitext, $html ) { 00307 $this->assertEquals( $wikitext, $status->getWikiText() ); 00308 } 00309 00318 public function testGetHtml( Status $status, $wikitext, $html ) { 00319 $this->assertEquals( $html, $status->getHTML() ); 00320 } 00321 00327 public static function provideGetWikiTextAndHtml() { 00328 $testCases = array(); 00329 00330 $testCases['GoodStatus'] = array( 00331 new Status(), 00332 "Internal error: Status::getWikiText called for a good result, this is incorrect\n", 00333 "<p>Internal error: Status::getWikiText called for a good result, this is incorrect\n</p>", 00334 ); 00335 00336 $status = new Status(); 00337 $status->ok = false; 00338 $testCases['GoodButNoError'] = array( 00339 $status, 00340 "Internal error: Status::getWikiText: Invalid result object: no error text but not OK\n", 00341 "<p>Internal error: Status::getWikiText: Invalid result object: no error text but not OK\n</p>", 00342 ); 00343 00344 $status = new Status(); 00345 $status->warning( 'fooBar!' ); 00346 $testCases['1StringWarning'] = array( 00347 $status, 00348 "<fooBar!>", 00349 "<p><fooBar!>\n</p>", 00350 ); 00351 00352 $status = new Status(); 00353 $status->warning( 'fooBar!' ); 00354 $status->warning( 'fooBar2!' ); 00355 $testCases['2StringWarnings'] = array( 00356 $status, 00357 "* <fooBar!>\n* <fooBar2!>\n", 00358 "<ul><li> <fooBar!></li>\n<li> <fooBar2!></li></ul>\n", 00359 ); 00360 00361 $status = new Status(); 00362 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) ); 00363 $testCases['1MessageWarning'] = array( 00364 $status, 00365 "<fooBar!>", 00366 "<p><fooBar!>\n</p>", 00367 ); 00368 00369 $status = new Status(); 00370 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) ); 00371 $status->warning( new Message( 'fooBar2!' ) ); 00372 $testCases['2MessageWarnings'] = array( 00373 $status, 00374 "* <fooBar!>\n* <fooBar2!>\n", 00375 "<ul><li> <fooBar!></li>\n<li> <fooBar2!></li></ul>\n", 00376 ); 00377 00378 return $testCases; 00379 } 00380 00386 public function testGetMessage( Status $status, $expectedParams = array(), $expectedKey ) { 00387 $message = $status->getMessage(); 00388 $this->assertInstanceOf( 'Message', $message ); 00389 $this->assertEquals( $expectedParams, $message->getParams(), 'Message::getParams' ); 00390 $this->assertEquals( $expectedKey, $message->getKey(), 'Message::getKey' ); 00391 } 00392 00399 public static function provideGetMessage() { 00400 $testCases = array(); 00401 00402 $testCases['GoodStatus'] = array( 00403 new Status(), 00404 array( "Status::getMessage called for a good result, this is incorrect\n" ), 00405 'internalerror_info' 00406 ); 00407 00408 $status = new Status(); 00409 $status->ok = false; 00410 $testCases['GoodButNoError'] = array( 00411 $status, 00412 array( "Status::getMessage: Invalid result object: no error text but not OK\n" ), 00413 'internalerror_info' 00414 ); 00415 00416 $status = new Status(); 00417 $status->warning( 'fooBar!' ); 00418 $testCases['1StringWarning'] = array( 00419 $status, 00420 array(), 00421 'fooBar!' 00422 ); 00423 00424 // FIXME: Assertion tries to compare a StubUserLang with a Language object, because 00425 // "data providers are executed before both the call to the setUpBeforeClass static method 00426 // and the first call to the setUp method. Because of that you can't access any variables 00427 // you create there from within a data provider." 00428 // http://phpunit.de/manual/3.7/en/writing-tests-for-phpunit.html 00429 // $status = new Status(); 00430 // $status->warning( 'fooBar!' ); 00431 // $status->warning( 'fooBar2!' ); 00432 // $testCases[ '2StringWarnings' ] = array( 00433 // $status, 00434 // array( new Message( 'fooBar!' ), new Message( 'fooBar2!' ) ), 00435 // "* \$1\n* \$2" 00436 // ); 00437 00438 $status = new Status(); 00439 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) ); 00440 $testCases['1MessageWarning'] = array( 00441 $status, 00442 array( 'foo', 'bar' ), 00443 'fooBar!' 00444 ); 00445 00446 $status = new Status(); 00447 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) ); 00448 $status->warning( new Message( 'fooBar2!' ) ); 00449 $testCases['2MessageWarnings'] = array( 00450 $status, 00451 array( new Message( 'fooBar!', array( 'foo', 'bar' ) ), new Message( 'fooBar2!' ) ), 00452 "* \$1\n* \$2" 00453 ); 00454 00455 return $testCases; 00456 } 00457 00461 public function testReplaceMessage() { 00462 $status = new Status(); 00463 $message = new Message( 'key1', array( 'foo1', 'bar1' ) ); 00464 $status->error( $message ); 00465 $newMessage = new Message( 'key2', array( 'foo2', 'bar2' ) ); 00466 00467 $status->replaceMessage( $message, $newMessage ); 00468 00469 $this->assertEquals( $newMessage, $status->errors[0]['message'] ); 00470 } 00471 00475 public function testGetErrorMessage() { 00476 $method = new ReflectionMethod( 'Status', 'getErrorMessage' ); 00477 $method->setAccessible( true ); 00478 $status = new Status(); 00479 $key = 'foo'; 00480 $params = array( 'bar' ); 00481 00483 $message = $method->invoke( $status, array_merge( array( $key ), $params ) ); 00484 $this->assertInstanceOf( 'Message', $message ); 00485 $this->assertEquals( $key, $message->getKey() ); 00486 $this->assertEquals( $params, $message->getParams() ); 00487 } 00488 00492 public function testGetErrorMessageArray() { 00493 $method = new ReflectionMethod( 'Status', 'getErrorMessageArray' ); 00494 $method->setAccessible( true ); 00495 $status = new Status(); 00496 $key = 'foo'; 00497 $params = array( 'bar' ); 00498 00500 $messageArray = $method->invoke( 00501 $status, 00502 array( 00503 array_merge( array( $key ), $params ), 00504 array_merge( array( $key ), $params ) 00505 ) 00506 ); 00507 00508 $this->assertInternalType( 'array', $messageArray ); 00509 $this->assertCount( 2, $messageArray ); 00510 foreach ( $messageArray as $message ) { 00511 $this->assertInstanceOf( 'Message', $message ); 00512 $this->assertEquals( $key, $message->getKey() ); 00513 $this->assertEquals( $params, $message->getParams() ); 00514 } 00515 } 00516 00520 public function testGetErrorsByType() { 00521 $status = new Status(); 00522 $warning = new Message( 'warning111' ); 00523 $error = new Message( 'error111' ); 00524 $status->warning( $warning ); 00525 $status->error( $error ); 00526 00527 $warnings = $status->getErrorsByType( 'warning' ); 00528 $errors = $status->getErrorsByType( 'error' ); 00529 00530 $this->assertCount( 1, $warnings ); 00531 $this->assertCount( 1, $errors ); 00532 $this->assertEquals( $warning, $warnings[0]['message'] ); 00533 $this->assertEquals( $error, $errors[0]['message'] ); 00534 } 00535 00539 public function testWakeUpSanitizesCallback() { 00540 $status = new Status(); 00541 $status->cleanCallback = function ( $value ) { 00542 return '-' . $value . '-'; 00543 }; 00544 $status->__wakeup(); 00545 $this->assertEquals( false, $status->cleanCallback ); 00546 } 00547 00552 public function testGetStatusArrayWithNonObjectMessages( $nonObjMsg ) { 00553 $status = new Status(); 00554 if ( !array_key_exists( 1, $nonObjMsg ) ) { 00555 $status->warning( $nonObjMsg[0] ); 00556 } else { 00557 $status->warning( $nonObjMsg[0], $nonObjMsg[1] ); 00558 } 00559 00560 $array = $status->getWarningsArray(); // We use getWarningsArray to access getStatusArray 00561 00562 $this->assertEquals( 1, count( $array ) ); 00563 $this->assertEquals( $nonObjMsg, $array[0] ); 00564 } 00565 00566 public static function provideNonObjectMessages() { 00567 return array( 00568 array( array( 'ImaString', array( 'param1' => 'value1' ) ) ), 00569 array( array( 'ImaString' ) ), 00570 ); 00571 } 00572 00573 }