Add class implementing MessagePack serialization
[lhc/web/wiklou.git] / tests / phpunit / includes / StatusTest.php
1 <?php
2
3 /**
4 * @author Adam Shorland
5 */
6 class StatusTest extends MediaWikiLangTestCase {
7
8 public function testCanConstruct() {
9 new Status();
10 $this->assertTrue( true );
11 }
12
13 /**
14 * @dataProvider provideValues
15 * @covers Status::newGood
16 */
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() );
22 }
23
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 ),
31 );
32 }
33
34 /**
35 * @covers Status::newFatal
36 */
37 public function testNewFatalWithMessage() {
38 $message = $this->getMockBuilder( 'Message' )
39 ->disableOriginalConstructor()
40 ->getMock();
41
42 $status = Status::newFatal( $message );
43 $this->assertFalse( $status->isGood() );
44 $this->assertFalse( $status->isOK() );
45 $this->assertEquals( $message, $status->getMessage() );
46 }
47
48 /**
49 * @covers Status::newFatal
50 */
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() );
57 }
58
59 /**
60 * @dataProvider provideSetResult
61 * @covers Status::setResult
62 */
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() );
68 }
69
70 public static function provideSetResult() {
71 return array(
72 array( true ),
73 array( false ),
74 array( true, 'value' ),
75 array( false, 'value' ),
76 );
77 }
78
79 /**
80 * @dataProvider provideIsOk
81 * @covers Status::isOk
82 */
83 public function testIsOk( $ok ) {
84 $status = new Status();
85 $status->ok = $ok;
86 $this->assertEquals( $ok, $status->isOK() );
87 }
88
89 public static function provideIsOk() {
90 return array(
91 array( true ),
92 array( false ),
93 );
94 }
95
96 /**
97 * @covers Status::getValue
98 */
99 public function testGetValue() {
100 $status = new Status();
101 $status->value = 'foobar';
102 $this->assertEquals( 'foobar', $status->getValue() );
103 }
104
105 /**
106 * @dataProvider provideIsGood
107 * @covers Status::isGood
108 */
109 public function testIsGood( $ok, $errors, $expected ) {
110 $status = new Status();
111 $status->ok = $ok;
112 $status->errors = $errors;
113 $this->assertEquals( $expected, $status->isGood() );
114 }
115
116 public static function provideIsGood() {
117 return array(
118 array( true, array(), true ),
119 array( true, array( 'foo' ), false ),
120 array( false, array(), false ),
121 array( false, array( 'foo' ), false ),
122 );
123 }
124
125 /**
126 * @dataProvider provideMockMessageDetails
127 * @covers Status::warning
128 * @covers Status::getWarningsArray
129 * @covers Status::getStatusArray
130 */
131 public function testWarningWithMessage( $mockDetails ) {
132 $status = new Status();
133 $messages = $this->getMockMessages( $mockDetails );
134
135 foreach ( $messages as $message ) {
136 $status->warning( $message );
137 }
138 $warnings = $status->getWarningsArray();
139
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 );
144 }
145 }
146
147 /**
148 * @dataProvider provideMockMessageDetails
149 * @covers Status::error
150 * @covers Status::getErrorsArray
151 * @covers Status::getStatusArray
152 */
153 public function testErrorWithMessage( $mockDetails ) {
154 $status = new Status();
155 $messages = $this->getMockMessages( $mockDetails );
156
157 foreach ( $messages as $message ) {
158 $status->error( $message );
159 }
160 $errors = $status->getErrorsArray();
161
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 );
166 }
167 }
168
169 protected function getMockMessage( $key = 'key', $params = array() ) {
170 $message = $this->getMockBuilder( 'Message' )
171 ->disableOriginalConstructor()
172 ->getMock();
173 $message->expects( $this->atLeastOnce() )
174 ->method( 'getKey' )
175 ->will( $this->returnValue( $key ) );
176 $message->expects( $this->atLeastOnce() )
177 ->method( 'getParams' )
178 ->will( $this->returnValue( $params ) );
179 return $message;
180 }
181
182 /**
183 * @param array $messageDetails eg. array( 'KEY' => array(/PARAMS/) )
184 * @return Message[]
185 */
186 protected function getMockMessages( $messageDetails ) {
187 $messages = array();
188 foreach ( $messageDetails as $key => $paramsArray ) {
189 $messages[] = $this->getMockMessage( $key, $paramsArray );
190 }
191 return $messages;
192 }
193
194 public static function provideMockMessageDetails() {
195 return array(
196 array( array( 'key1' => array( 'foo' => 'bar' ) ) ),
197 array( array( 'key1' => array( 'foo' => 'bar' ), 'key2' => array( 'foo2' => 'bar2' ) ) ),
198 );
199 }
200
201 /**
202 * @covers Status::merge
203 * @todo test merge with $overwriteValue true
204 */
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 );
212
213 $status1->merge( $status2 );
214 $this->assertEquals( 2, count( $status1->getWarningsArray() ) + count( $status1->getErrorsArray() ) );
215 }
216
217 /**
218 * @covers Status::hasMessage
219 */
220 public function testHasMessage() {
221 $status = new Status();
222 $status->fatal( 'bad' );
223 $this->assertTrue( $status->hasMessage( 'bad' ) );
224 $this->assertFalse( $status->hasMessage( 'good' ) );
225 }
226
227 /**
228 * @dataProvider provideCleanParams
229 * @covers Status::cleanParams
230 */
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;
236
237 $this->assertEquals( $expected, $method->invoke( $status, $params ) );
238 }
239
240 /**
241 * @todo test cleanParams with a callback
242 */
243 public static function provideCleanParams() {
244 return array(
245 array( false, array( 'foo' => 'bar' ), array( 'foo' => 'bar' ) ),
246 );
247 }
248
249 /**
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
255 */
256 public function testGetWikiText( Status $status, $wikitext, $html ) {
257 $this->assertEquals( $wikitext, $status->getWikiText() );
258 }
259
260 /**
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
266 */
267 public function testGetHtml( Status $status, $wikitext, $html ) {
268 $this->assertEquals( $html, $status->getHTML() );
269 }
270
271 /**
272 * @return array of arrays with values;
273 * 0 => status object
274 * 1 => expected string (with no context)
275 */
276 public static function provideGetWikiTextAndHtml() {
277 $testCases = array();
278
279 $testCases[ 'GoodStatus' ] = array(
280 new Status(),
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>",
283 );
284
285 $status = new Status();
286 $status->ok = false;
287 $testCases[ 'GoodButNoError' ] = array(
288 $status,
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>",
291 );
292
293 $status = new Status();
294 $status->warning( 'fooBar!' );
295 $testCases[ '1StringWarning' ] = array(
296 $status,
297 "<fooBar!>",
298 "<p>&lt;fooBar!&gt;\n</p>",
299 );
300
301 $status = new Status();
302 $status->warning( 'fooBar!' );
303 $status->warning( 'fooBar2!' );
304 $testCases[ '2StringWarnings' ] = array(
305 $status,
306 "* <fooBar!>\n* <fooBar2!>\n",
307 "<ul>\n<li> &lt;fooBar!&gt;\n</li>\n<li> &lt;fooBar2!&gt;\n</li>\n</ul>\n",
308 );
309
310 $status = new Status();
311 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
312 $testCases[ '1MessageWarning' ] = array(
313 $status,
314 "<fooBar!>",
315 "<p>&lt;fooBar!&gt;\n</p>",
316 );
317
318 $status = new Status();
319 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
320 $status->warning( new Message( 'fooBar2!' ) );
321 $testCases[ '2MessageWarnings' ] = array(
322 $status,
323 "* <fooBar!>\n* <fooBar2!>\n",
324 "<ul>\n<li> &lt;fooBar!&gt;\n</li>\n<li> &lt;fooBar2!&gt;\n</li>\n</ul>\n",
325 );
326
327 return $testCases;
328 }
329
330 /**
331 * @dataProvider provideGetMessage
332 * @covers Status::getMessage
333 * @todo test long and short context messages generated through this method
334 */
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() );
340 }
341
342 /**
343 * @return array of arrays with values;
344 * 0 => status object
345 * 1 => expected Message Params (with no context)
346 */
347 public static function provideGetMessage() {
348 $testCases = array();
349
350 $testCases[ 'GoodStatus' ] = array(
351 new Status(),
352 array( "Status::getMessage called for a good result, this is incorrect\n" ),
353 'internalerror_info'
354 );
355
356 $status = new Status();
357 $status->ok = false;
358 $testCases[ 'GoodButNoError' ] = array(
359 $status,
360 array( "Status::getMessage: Invalid result object: no error text but not OK\n" ),
361 'internalerror_info'
362 );
363
364 $status = new Status();
365 $status->warning( 'fooBar!' );
366 $testCases[ '1StringWarning' ] = array(
367 $status,
368 array(),
369 "fooBar!"
370 );
371
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(
377 // $status,
378 // array(),
379 // ''
380 // );
381
382 $status = new Status();
383 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
384 $testCases[ '1MessageWarning' ] = array(
385 $status,
386 array( 'foo', 'bar' ),
387 "fooBar!",
388 );
389
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(
395 // $status,
396 // array(),
397 // "",
398 // );
399
400 return $testCases;
401 }
402
403 /**
404 * @covers Status::replaceMessage
405 */
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' ) );
411
412 $status->replaceMessage( $message, $newMessage );
413
414 $this->assertEquals( $newMessage, $status->errors[0]['message'] );
415 }
416
417 /**
418 * @covers Status::getErrorMessage
419 */
420 public function testGetErrorMessage() {
421 $method = new ReflectionMethod( 'Status', 'getErrorMessage' );
422 $method->setAccessible( true );
423 $status = new Status();
424 $key = 'foo';
425 $params = array( 'bar' );
426
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() );
432 }
433
434 /**
435 * @covers Status::getErrorMessageArray
436 */
437 public function testGetErrorMessageArray() {
438 $method = new ReflectionMethod( 'Status', 'getErrorMessageArray' );
439 $method->setAccessible( true );
440 $status = new Status();
441 $key = 'foo';
442 $params = array( 'bar' );
443
444 /** @var Message[] $messageArray */
445 $messageArray = $method->invoke(
446 $status,
447 array(
448 array_merge( array( $key ), $params ),
449 array_merge( array( $key ), $params )
450 )
451 );
452
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() );
459 }
460 }
461
462 /**
463 * @covers Status::getErrorsByType
464 */
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 );
471
472 $warnings = $status->getErrorsByType( 'warning' );
473 $errors = $status->getErrorsByType( 'error' );
474
475 $this->assertCount( 1, $warnings );
476 $this->assertCount( 1, $errors );
477 $this->assertEquals( $warning, $warnings[0]['message'] );
478 $this->assertEquals( $error, $errors[0]['message'] );
479 }
480
481 }