MediaWiki
REL1_23
|
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( 2, count( $status1->getWarningsArray() ) + count( $status1->getErrorsArray() ) ); 00237 } 00238 00242 public function testMergeWithOverwriteValue() { 00243 $status1 = new Status(); 00244 $status2 = new Status(); 00245 $message1 = $this->getMockMessage( 'warn1' ); 00246 $message2 = $this->getMockMessage( 'error2' ); 00247 $status1->warning( $message1 ); 00248 $status2->error( $message2 ); 00249 $status2->value = 'FooValue'; 00250 00251 $status1->merge( $status2, true ); 00252 $this->assertEquals( 2, count( $status1->getWarningsArray() ) + count( $status1->getErrorsArray() ) ); 00253 $this->assertEquals( 'FooValue', $status1->getValue() ); 00254 } 00255 00259 public function testHasMessage() { 00260 $status = new Status(); 00261 $status->fatal( 'bad' ); 00262 $this->assertTrue( $status->hasMessage( 'bad' ) ); 00263 $this->assertFalse( $status->hasMessage( 'good' ) ); 00264 } 00265 00270 public function testCleanParams( $cleanCallback, $params, $expected ) { 00271 $method = new ReflectionMethod( 'Status', 'cleanParams' ); 00272 $method->setAccessible( true ); 00273 $status = new Status(); 00274 $status->cleanCallback = $cleanCallback; 00275 00276 $this->assertEquals( $expected, $method->invoke( $status, $params ) ); 00277 } 00278 00279 public static function provideCleanParams() { 00280 $cleanCallback = function( $value ) { 00281 return '-' . $value . '-'; 00282 }; 00283 00284 return array( 00285 array( false, array( 'foo' => 'bar' ), array( 'foo' => 'bar' ) ), 00286 array( $cleanCallback, array( 'foo' => 'bar' ), array( 'foo' => '-bar-' ) ), 00287 ); 00288 } 00289 00297 public function testGetWikiText( Status $status, $wikitext, $html ) { 00298 $this->assertEquals( $wikitext, $status->getWikiText() ); 00299 } 00300 00308 public function testGetHtml( Status $status, $wikitext, $html ) { 00309 $this->assertEquals( $html, $status->getHTML() ); 00310 } 00311 00317 public static function provideGetWikiTextAndHtml() { 00318 $testCases = array(); 00319 00320 $testCases[ 'GoodStatus' ] = array( 00321 new Status(), 00322 "Internal error: Status::getWikiText called for a good result, this is incorrect\n", 00323 "<p>Internal error: Status::getWikiText called for a good result, this is incorrect\n</p>", 00324 ); 00325 00326 $status = new Status(); 00327 $status->ok = false; 00328 $testCases[ 'GoodButNoError' ] = array( 00329 $status, 00330 "Internal error: Status::getWikiText: Invalid result object: no error text but not OK\n", 00331 "<p>Internal error: Status::getWikiText: Invalid result object: no error text but not OK\n</p>", 00332 ); 00333 00334 $status = new Status(); 00335 $status->warning( 'fooBar!' ); 00336 $testCases[ '1StringWarning' ] = array( 00337 $status, 00338 "<fooBar!>", 00339 "<p><fooBar!>\n</p>", 00340 ); 00341 00342 $status = new Status(); 00343 $status->warning( 'fooBar!' ); 00344 $status->warning( 'fooBar2!' ); 00345 $testCases[ '2StringWarnings' ] = array( 00346 $status, 00347 "* <fooBar!>\n* <fooBar2!>\n", 00348 "<ul>\n<li> <fooBar!>\n</li>\n<li> <fooBar2!>\n</li>\n</ul>\n", 00349 ); 00350 00351 $status = new Status(); 00352 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) ); 00353 $testCases[ '1MessageWarning' ] = array( 00354 $status, 00355 "<fooBar!>", 00356 "<p><fooBar!>\n</p>", 00357 ); 00358 00359 $status = new Status(); 00360 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) ); 00361 $status->warning( new Message( 'fooBar2!' ) ); 00362 $testCases[ '2MessageWarnings' ] = array( 00363 $status, 00364 "* <fooBar!>\n* <fooBar2!>\n", 00365 "<ul>\n<li> <fooBar!>\n</li>\n<li> <fooBar2!>\n</li>\n</ul>\n", 00366 ); 00367 00368 return $testCases; 00369 } 00370 00376 public function testGetMessage( Status $status, $expectedParams = array(), $expectedKey ) { 00377 $message = $status->getMessage(); 00378 $this->assertInstanceOf( 'Message', $message ); 00379 $this->assertEquals( $expectedParams, $message->getParams(), 'Message::getParams' ); 00380 $this->assertEquals( $expectedKey, $message->getKey(), 'Message::getKey' ); 00381 } 00382 00389 public static function provideGetMessage() { 00390 $testCases = array(); 00391 00392 $testCases[ 'GoodStatus' ] = array( 00393 new Status(), 00394 array( "Status::getMessage called for a good result, this is incorrect\n" ), 00395 'internalerror_info' 00396 ); 00397 00398 $status = new Status(); 00399 $status->ok = false; 00400 $testCases[ 'GoodButNoError' ] = array( 00401 $status, 00402 array( "Status::getMessage: Invalid result object: no error text but not OK\n" ), 00403 'internalerror_info' 00404 ); 00405 00406 $status = new Status(); 00407 $status->warning( 'fooBar!' ); 00408 $testCases[ '1StringWarning' ] = array( 00409 $status, 00410 array(), 00411 'fooBar!' 00412 ); 00413 00414 // FIXME: Assertion tries to compare a StubUserLang with a Language object, because 00415 // "data providers are executed before both the call to the setUpBeforeClass static method 00416 // and the first call to the setUp method. Because of that you can't access any variables 00417 // you create there from within a data provider." 00418 // http://phpunit.de/manual/3.7/en/writing-tests-for-phpunit.html 00419 // $status = new Status(); 00420 // $status->warning( 'fooBar!' ); 00421 // $status->warning( 'fooBar2!' ); 00422 // $testCases[ '2StringWarnings' ] = array( 00423 // $status, 00424 // array( new Message( 'fooBar!' ), new Message( 'fooBar2!' ) ), 00425 // "* \$1\n* \$2" 00426 // ); 00427 00428 $status = new Status(); 00429 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) ); 00430 $testCases[ '1MessageWarning' ] = array( 00431 $status, 00432 array( 'foo', 'bar' ), 00433 'fooBar!' 00434 ); 00435 00436 $status = new Status(); 00437 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) ); 00438 $status->warning( new Message( 'fooBar2!' ) ); 00439 $testCases[ '2MessageWarnings' ] = array( 00440 $status, 00441 array( new Message( 'fooBar!', array( 'foo', 'bar' ) ), new Message( 'fooBar2!' ) ), 00442 "* \$1\n* \$2" 00443 ); 00444 00445 return $testCases; 00446 } 00447 00451 public function testReplaceMessage() { 00452 $status = new Status(); 00453 $message = new Message( 'key1', array( 'foo1', 'bar1' ) ); 00454 $status->error( $message ); 00455 $newMessage = new Message( 'key2', array( 'foo2', 'bar2' ) ); 00456 00457 $status->replaceMessage( $message, $newMessage ); 00458 00459 $this->assertEquals( $newMessage, $status->errors[0]['message'] ); 00460 } 00461 00465 public function testGetErrorMessage() { 00466 $method = new ReflectionMethod( 'Status', 'getErrorMessage' ); 00467 $method->setAccessible( true ); 00468 $status = new Status(); 00469 $key = 'foo'; 00470 $params = array( 'bar' ); 00471 00473 $message = $method->invoke( $status, array_merge( array( $key ), $params ) ); 00474 $this->assertInstanceOf( 'Message', $message ); 00475 $this->assertEquals( $key, $message->getKey() ); 00476 $this->assertEquals( $params, $message->getParams() ); 00477 } 00478 00482 public function testGetErrorMessageArray() { 00483 $method = new ReflectionMethod( 'Status', 'getErrorMessageArray' ); 00484 $method->setAccessible( true ); 00485 $status = new Status(); 00486 $key = 'foo'; 00487 $params = array( 'bar' ); 00488 00490 $messageArray = $method->invoke( 00491 $status, 00492 array( 00493 array_merge( array( $key ), $params ), 00494 array_merge( array( $key ), $params ) 00495 ) 00496 ); 00497 00498 $this->assertInternalType( 'array', $messageArray ); 00499 $this->assertCount( 2, $messageArray ); 00500 foreach ( $messageArray as $message ) { 00501 $this->assertInstanceOf( 'Message', $message ); 00502 $this->assertEquals( $key, $message->getKey() ); 00503 $this->assertEquals( $params, $message->getParams() ); 00504 } 00505 } 00506 00510 public function testGetErrorsByType() { 00511 $status = new Status(); 00512 $warning = new Message( 'warning111' ); 00513 $error = new Message( 'error111' ); 00514 $status->warning( $warning ); 00515 $status->error( $error ); 00516 00517 $warnings = $status->getErrorsByType( 'warning' ); 00518 $errors = $status->getErrorsByType( 'error' ); 00519 00520 $this->assertCount( 1, $warnings ); 00521 $this->assertCount( 1, $errors ); 00522 $this->assertEquals( $warning, $warnings[0]['message'] ); 00523 $this->assertEquals( $error, $errors[0]['message'] ); 00524 } 00525 00529 public function testWakeUpSanitizesCallback() { 00530 $status = new Status(); 00531 $status->cleanCallback = function( $value ) { 00532 return '-' . $value . '-'; 00533 }; 00534 $status->__wakeup(); 00535 $this->assertEquals( false, $status->cleanCallback ); 00536 } 00537 00542 public function testGetStatusArrayWithNonObjectMessages( $nonObjMsg ) { 00543 $status = new Status(); 00544 if ( !array_key_exists( 1, $nonObjMsg ) ) { 00545 $status->warning( $nonObjMsg[0] ); 00546 } else { 00547 $status->warning( $nonObjMsg[0], $nonObjMsg[1] ); 00548 } 00549 00550 $array = $status->getWarningsArray(); // We use getWarningsArray to access getStatusArray 00551 00552 $this->assertEquals( 1, count( $array ) ); 00553 $this->assertEquals( $nonObjMsg, $array[0] ); 00554 } 00555 00556 public static function provideNonObjectMessages() { 00557 return array( 00558 array( array( 'ImaString', array( 'param1' => 'value1' ) ) ), 00559 array( array( 'ImaString' ) ), 00560 ); 00561 } 00562 00563 }