30a554e2a8410f57a4c6f9bb1422a4f5bd9d9f19
4 * @author Adam Shorland
6 class StatusTest
extends MediaWikiLangTestCase
{
8 public function testCanConstruct() {
10 $this->assertTrue( true );
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() {
28 array( array( 'foo' => 'bar' ) ),
29 array( new Exception() ),
35 * @covers Status::newFatal
37 public function testNewFatalWithMessage() {
38 $message = $this->getMockBuilder( 'Message' )
39 ->disableOriginalConstructor()
42 $status = Status
::newFatal( $message );
43 $this->assertFalse( $status->isGood() );
44 $this->assertFalse( $status->isOK() );
45 $this->assertEquals( $message, $status->getMessage() );
49 * @covers Status::newFatal
51 public function testNewFatalWithString() {
53 $status = Status
::newFatal( $message );
54 $this->assertFalse( $status->isGood() );
55 $this->assertFalse( $status->isOK() );
56 $this->assertEquals( $message, $status->getMessage()->getKey() );
60 * @dataProvider provideSetResult
61 * @covers Status::setResult
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() );
70 public static function provideSetResult() {
74 array( true, 'value' ),
75 array( false, 'value' ),
80 * @dataProvider provideIsOk
81 * @covers Status::isOk
83 public function testIsOk( $ok ) {
84 $status = new Status();
86 $this->assertEquals( $ok, $status->isOK() );
89 public static function provideIsOk() {
97 * @covers Status::getValue
99 public function testGetValue() {
100 $status = new Status();
101 $status->value
= 'foobar';
102 $this->assertEquals( 'foobar', $status->getValue() );
106 * @dataProvider provideIsGood
107 * @covers Status::isGood
109 public function testIsGood( $ok, $errors, $expected ) {
110 $status = new Status();
112 $status->errors
= $errors;
113 $this->assertEquals( $expected, $status->isGood() );
116 public static function provideIsGood() {
118 array( true, array(), true ),
119 array( true, array( 'foo' ), false ),
120 array( false, array(), false ),
121 array( false, array( 'foo' ), false ),
126 * @dataProvider provideMockMessageDetails
127 * @covers Status::warning
128 * @covers Status::getWarningsArray
129 * @covers Status::getStatusArray
131 public function testWarningWithMessage( $mockDetails ) {
132 $status = new Status();
133 $messages = $this->getMockMessages( $mockDetails );
135 foreach ( $messages as $message ) {
136 $status->warning( $message );
138 $warnings = $status->getWarningsArray();
140 $this->assertEquals( count( $messages ), count( $warnings ) );
141 foreach ( $messages as $key => $message ) {
142 $expectedArray = array_merge( array( $message->getKey() ), $message->getParams() );
143 $this->assertEquals( $warnings[$key], $expectedArray );
148 * @dataProvider provideMockMessageDetails
149 * @covers Status::error
150 * @covers Status::getErrorsArray
151 * @covers Status::getStatusArray
153 public function testErrorWithMessage( $mockDetails ) {
154 $status = new Status();
155 $messages = $this->getMockMessages( $mockDetails );
157 foreach ( $messages as $message ) {
158 $status->error( $message );
160 $errors = $status->getErrorsArray();
162 $this->assertEquals( count( $messages ), count( $errors ) );
163 foreach ( $messages as $key => $message ) {
164 $expectedArray = array_merge( array( $message->getKey() ), $message->getParams() );
165 $this->assertEquals( $errors[$key], $expectedArray );
169 protected function getMockMessage( $key = 'key', $params = array() ) {
170 $message = $this->getMockBuilder( 'Message' )
171 ->disableOriginalConstructor()
173 $message->expects( $this->atLeastOnce() )
175 ->will( $this->returnValue( $key ) );
176 $message->expects( $this->atLeastOnce() )
177 ->method( 'getParams' )
178 ->will( $this->returnValue( $params ) );
183 * @param array $messageDetails eg. array( 'KEY' => array(/PARAMS/) )
186 protected function getMockMessages( $messageDetails ) {
188 foreach ( $messageDetails as $key => $paramsArray ) {
189 $messages[] = $this->getMockMessage( $key, $paramsArray );
194 public static function provideMockMessageDetails() {
196 array( array( 'key1' => array( 'foo' => 'bar' ) ) ),
197 array( array( 'key1' => array( 'foo' => 'bar' ), 'key2' => array( 'foo2' => 'bar2' ) ) ),
202 * @covers Status::merge
203 * @todo test merge with $overwriteValue true
205 public function testMerge() {
206 $status1 = new Status();
207 $status2 = new Status();
208 $message1 = $this->getMockMessage( 'warn1' );
209 $message2 = $this->getMockMessage( 'error2' );
210 $status1->warning( $message1 );
211 $status2->error( $message2 );
213 $status1->merge( $status2 );
214 $this->assertEquals( 2, count( $status1->getWarningsArray() ) +
count( $status1->getErrorsArray() ) );
218 * @covers Status::hasMessage
220 public function testHasMessage() {
221 $status = new Status();
222 $status->fatal( 'bad' );
223 $this->assertTrue( $status->hasMessage( 'bad' ) );
224 $this->assertFalse( $status->hasMessage( 'good' ) );
228 * @dataProvider provideCleanParams
229 * @covers Status::cleanParams
231 public function testCleanParams( $cleanCallback, $params, $expected ) {
232 $method = new ReflectionMethod( 'Status', 'cleanParams' );
233 $method->setAccessible( true );
234 $status = new Status();
235 $status->cleanCallback
= $cleanCallback;
237 $this->assertEquals( $expected, $method->invoke( $status, $params ) );
241 * @todo test cleanParams with a callback
243 public static function provideCleanParams() {
245 array( false, array( 'foo' => 'bar' ), array( 'foo' => 'bar' ) ),
250 * @dataProvider provideGetWikiTextAndHtml
251 * @covers Status::getWikiText
252 * @todo test long and short context messages generated through this method
253 * this can not really be done now due to use of wfMessage()->plain()
254 * It is possible to mock such methods but only if namespaces are used
256 public function testGetWikiText( Status
$status, $expected ) {
257 $this->assertEquals( $expected, $status->getWikiText() );
261 * @dataProvider provideGetWikiTextAndHtml
262 * @covers Status::getHtml
263 * @todo test long and short context messages generated through this method
264 * this can not really be done now due to use of $this->getWikiText using wfMessage()->plain()
265 * It is possible to mock such methods but only if namespaces are used
267 public function testGetHtml( Status
$status, $expected ) {
268 $this->assertEquals( $expected, $status->getHTML() );
272 * @return array of arrays with values;
274 * 1 => expected string (with no context)
276 public static function provideGetWikiTextAndHtml() {
277 $testCases = array();
279 $testCases[ 'GoodStatus' ] = array(
281 "Internal error: Status::getWikiText called for a good result, this is incorrect\n",
284 $status = new Status();
286 $testCases[ 'GoodButNoError' ] = array(
288 "Internal error: Status::getWikiText: Invalid result object: no error text but not OK\n",
291 $status = new Status();
292 $status->warning( 'fooBar!' );
293 $testCases[ '1StringWarning' ] = array(
298 $status = new Status();
299 $status->warning( 'fooBar!' );
300 $status->warning( 'fooBar2!' );
301 $testCases[ '2StringWarnings' ] = array(
303 "* <fooBar!>\n* <fooBar2!>\n",
306 $status = new Status();
307 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
308 $testCases[ '1MessageWarning' ] = array(
313 $status = new Status();
314 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
315 $status->warning( new Message( 'fooBar2!' ) );
316 $testCases[ '2MessageWarnings' ] = array(
318 "* <fooBar!>\n* <fooBar2!>\n",
325 * @dataProvider provideGetMessage
326 * @covers Status::getMessage
327 * @todo test long and short context messages generated through this method
329 public function testGetMessage( Status
$status, $expectedParams = array(), $expectedKey ) {
330 $message = $status->getMessage();
331 $this->assertInstanceOf( 'Message', $message );
332 $this->assertEquals( $expectedParams, $message->getParams() );
333 $this->assertEquals( $expectedKey, $message->getKey() );
337 * @return array of arrays with values;
339 * 1 => expected Message Params (with no context)
341 public static function provideGetMessage() {
342 $testCases = array();
344 $testCases[ 'GoodStatus' ] = array(
346 array( "Status::getMessage called for a good result, this is incorrect\n" ),
350 $status = new Status();
352 $testCases[ 'GoodButNoError' ] = array(
354 array( "Status::getMessage: Invalid result object: no error text but not OK\n" ),
358 $status = new Status();
359 $status->warning( 'fooBar!' );
360 $testCases[ '1StringWarning' ] = array(
366 //NOTE: this seems to return a string instead of a Message object...
367 // $status = new Status();
368 // $status->warning( 'fooBar!' );
369 // $status->warning( 'fooBar2!' );
370 // $testCases[ '2StringWarnings' ] = array(
376 $status = new Status();
377 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
378 $testCases[ '1MessageWarning' ] = array(
380 array( 'foo', 'bar' ),
384 //NOTE: this seems to return a string instead of a Message object...
385 // $status = new Status();
386 // $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
387 // $status->warning( new Message( 'fooBar2!' ) );
388 // $testCases[ '2MessageWarnings' ] = array(
398 * @covers Status::replaceMessage
400 public function testReplaceMessage() {
401 $status = new Status();
402 $message = new Message( 'key1', array( 'foo1', 'bar1' ) );
403 $status->error( $message );
404 $newMessage = new Message( 'key2', array( 'foo2', 'bar2' ) );
406 $status->replaceMessage( $message, $newMessage );
408 $this->assertEquals( $newMessage, $status->errors
[0]['message'] );
412 * @covers Status::getErrorMessage
414 public function testGetErrorMessage() {
415 $method = new ReflectionMethod( 'Status', 'getErrorMessage' );
416 $method->setAccessible( true );
417 $status = new Status();
419 $params = array( 'bar' );
421 /** @var Message $message */
422 $message = $method->invoke( $status, array_merge( array( $key ), $params ) );
423 $this->assertInstanceOf( 'Message', $message );
424 $this->assertEquals( $key, $message->getKey() );
425 $this->assertEquals( $params, $message->getParams() );
429 * @covers Status::getErrorMessageArray
431 public function testGetErrorMessageArray() {
432 $method = new ReflectionMethod( 'Status', 'getErrorMessageArray' );
433 $method->setAccessible( true );
434 $status = new Status();
436 $params = array( 'bar' );
438 /** @var Message[] $messageArray */
439 $messageArray = $method->invoke(
442 array_merge( array( $key ), $params ),
443 array_merge( array( $key ), $params )
447 $this->assertInternalType( 'array', $messageArray );
448 $this->assertCount( 2, $messageArray );
449 foreach ( $messageArray as $message ) {
450 $this->assertInstanceOf( 'Message', $message );
451 $this->assertEquals( $key, $message->getKey() );
452 $this->assertEquals( $params, $message->getParams() );
457 * @covers Status::getErrorsByType
459 public function testGetErrorsByType() {
460 $status = new Status();
461 $warning = new Message( 'warning111' );
462 $error = new Message( 'error111' );
463 $status->warning( $warning );
464 $status->error( $error );
466 $warnings = $status->getErrorsByType( 'warning' );
467 $errors = $status->getErrorsByType( 'error' );
469 $this->assertCount( 1, $warnings );
470 $this->assertCount( 1, $errors );
471 $this->assertEquals( $warning, $warnings[0]['message'] );
472 $this->assertEquals( $error, $errors[0]['message'] );