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, $wikitext, $html ) {
257 $this->assertEquals( $wikitext, $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, $wikitext, $html) {
268 $this->assertEquals( $html, $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",
282 "<p>Internal error: Status::getWikiText called for a good result, this is incorrect\n</p>",
285 $status = new Status();
287 $testCases[ 'GoodButNoError' ] = array(
289 "Internal error: Status::getWikiText: Invalid result object: no error text but not OK\n",
290 "<p>Internal error: Status::getWikiText: Invalid result object: no error text but not OK\n</p>",
293 $status = new Status();
294 $status->warning( 'fooBar!' );
295 $testCases[ '1StringWarning' ] = array(
298 "<p><fooBar!>\n</p>",
301 $status = new Status();
302 $status->warning( 'fooBar!' );
303 $status->warning( 'fooBar2!' );
304 $testCases[ '2StringWarnings' ] = array(
306 "* <fooBar!>\n* <fooBar2!>\n",
307 "<ul>\n<li> <fooBar!>\n</li>\n<li> <fooBar2!>\n</li>\n</ul>\n",
310 $status = new Status();
311 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
312 $testCases[ '1MessageWarning' ] = array(
315 "<p><fooBar!>\n</p>",
318 $status = new Status();
319 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
320 $status->warning( new Message( 'fooBar2!' ) );
321 $testCases[ '2MessageWarnings' ] = array(
323 "* <fooBar!>\n* <fooBar2!>\n",
324 "<ul>\n<li> <fooBar!>\n</li>\n<li> <fooBar2!>\n</li>\n</ul>\n",
331 * @dataProvider provideGetMessage
332 * @covers Status::getMessage
333 * @todo test long and short context messages generated through this method
335 public function testGetMessage( Status
$status, $expectedParams = array(), $expectedKey ) {
336 $message = $status->getMessage();
337 $this->assertInstanceOf( 'Message', $message );
338 $this->assertEquals( $expectedParams, $message->getParams() );
339 $this->assertEquals( $expectedKey, $message->getKey() );
343 * @return array of arrays with values;
345 * 1 => expected Message Params (with no context)
347 public static function provideGetMessage() {
348 $testCases = array();
350 $testCases[ 'GoodStatus' ] = array(
352 array( "Status::getMessage called for a good result, this is incorrect\n" ),
356 $status = new Status();
358 $testCases[ 'GoodButNoError' ] = array(
360 array( "Status::getMessage: Invalid result object: no error text but not OK\n" ),
364 $status = new Status();
365 $status->warning( 'fooBar!' );
366 $testCases[ '1StringWarning' ] = array(
372 //NOTE: this seems to return a string instead of a Message object...
373 // $status = new Status();
374 // $status->warning( 'fooBar!' );
375 // $status->warning( 'fooBar2!' );
376 // $testCases[ '2StringWarnings' ] = array(
382 $status = new Status();
383 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
384 $testCases[ '1MessageWarning' ] = array(
386 array( 'foo', 'bar' ),
390 //NOTE: this seems to return a string instead of a Message object...
391 // $status = new Status();
392 // $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
393 // $status->warning( new Message( 'fooBar2!' ) );
394 // $testCases[ '2MessageWarnings' ] = array(
404 * @covers Status::replaceMessage
406 public function testReplaceMessage() {
407 $status = new Status();
408 $message = new Message( 'key1', array( 'foo1', 'bar1' ) );
409 $status->error( $message );
410 $newMessage = new Message( 'key2', array( 'foo2', 'bar2' ) );
412 $status->replaceMessage( $message, $newMessage );
414 $this->assertEquals( $newMessage, $status->errors
[0]['message'] );
418 * @covers Status::getErrorMessage
420 public function testGetErrorMessage() {
421 $method = new ReflectionMethod( 'Status', 'getErrorMessage' );
422 $method->setAccessible( true );
423 $status = new Status();
425 $params = array( 'bar' );
427 /** @var Message $message */
428 $message = $method->invoke( $status, array_merge( array( $key ), $params ) );
429 $this->assertInstanceOf( 'Message', $message );
430 $this->assertEquals( $key, $message->getKey() );
431 $this->assertEquals( $params, $message->getParams() );
435 * @covers Status::getErrorMessageArray
437 public function testGetErrorMessageArray() {
438 $method = new ReflectionMethod( 'Status', 'getErrorMessageArray' );
439 $method->setAccessible( true );
440 $status = new Status();
442 $params = array( 'bar' );
444 /** @var Message[] $messageArray */
445 $messageArray = $method->invoke(
448 array_merge( array( $key ), $params ),
449 array_merge( array( $key ), $params )
453 $this->assertInternalType( 'array', $messageArray );
454 $this->assertCount( 2, $messageArray );
455 foreach ( $messageArray as $message ) {
456 $this->assertInstanceOf( 'Message', $message );
457 $this->assertEquals( $key, $message->getKey() );
458 $this->assertEquals( $params, $message->getParams() );
463 * @covers Status::getErrorsByType
465 public function testGetErrorsByType() {
466 $status = new Status();
467 $warning = new Message( 'warning111' );
468 $error = new Message( 'error111' );
469 $status->warning( $warning );
470 $status->error( $error );
472 $warnings = $status->getErrorsByType( 'warning' );
473 $errors = $status->getErrorsByType( 'error' );
475 $this->assertCount( 1, $warnings );
476 $this->assertCount( 1, $errors );
477 $this->assertEquals( $warning, $warnings[0]['message'] );
478 $this->assertEquals( $error, $errors[0]['message'] );