Merge "Begin exposing SiteConfiguration via site contexts"
[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, $expected ) {
257 $this->assertEquals( $expected, $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, $expected ) {
268 $this->assertEquals( $expected, $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 );
283
284 $status = new Status();
285 $status->ok = false;
286 $testCases[ 'GoodButNoError' ] = array(
287 $status,
288 "Internal error: Status::getWikiText: Invalid result object: no error text but not OK\n",
289 );
290
291 $status = new Status();
292 $status->warning( 'fooBar!' );
293 $testCases[ '1StringWarning' ] = array(
294 $status,
295 "<fooBar!>",
296 );
297
298 $status = new Status();
299 $status->warning( 'fooBar!' );
300 $status->warning( 'fooBar2!' );
301 $testCases[ '2StringWarnings' ] = array(
302 $status,
303 "* <fooBar!>\n* <fooBar2!>\n",
304 );
305
306 $status = new Status();
307 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
308 $testCases[ '1MessageWarning' ] = array(
309 $status,
310 "<fooBar!>",
311 );
312
313 $status = new Status();
314 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
315 $status->warning( new Message( 'fooBar2!' ) );
316 $testCases[ '2MessageWarnings' ] = array(
317 $status,
318 "* <fooBar!>\n* <fooBar2!>\n",
319 );
320
321 return $testCases;
322 }
323
324 /**
325 * @dataProvider provideGetMessage
326 * @covers Status::getMessage
327 * @todo test long and short context messages generated through this method
328 */
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() );
334 }
335
336 /**
337 * @return array of arrays with values;
338 * 0 => status object
339 * 1 => expected Message Params (with no context)
340 */
341 public static function provideGetMessage() {
342 $testCases = array();
343
344 $testCases[ 'GoodStatus' ] = array(
345 new Status(),
346 array( "Status::getMessage called for a good result, this is incorrect\n" ),
347 'internalerror_info'
348 );
349
350 $status = new Status();
351 $status->ok = false;
352 $testCases[ 'GoodButNoError' ] = array(
353 $status,
354 array( "Status::getMessage: Invalid result object: no error text but not OK\n" ),
355 'internalerror_info'
356 );
357
358 $status = new Status();
359 $status->warning( 'fooBar!' );
360 $testCases[ '1StringWarning' ] = array(
361 $status,
362 array(),
363 "fooBar!"
364 );
365
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(
371 // $status,
372 // array(),
373 // ''
374 // );
375
376 $status = new Status();
377 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
378 $testCases[ '1MessageWarning' ] = array(
379 $status,
380 array( 'foo', 'bar' ),
381 "fooBar!",
382 );
383
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(
389 // $status,
390 // array(),
391 // "",
392 // );
393
394 return $testCases;
395 }
396
397 /**
398 * @covers Status::replaceMessage
399 */
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' ) );
405
406 $status->replaceMessage( $message, $newMessage );
407
408 $this->assertEquals( $newMessage, $status->errors[0]['message'] );
409 }
410
411 /**
412 * @covers Status::getErrorMessage
413 */
414 public function testGetErrorMessage() {
415 $method = new ReflectionMethod( 'Status', 'getErrorMessage' );
416 $method->setAccessible( true );
417 $status = new Status();
418 $key = 'foo';
419 $params = array( 'bar' );
420
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() );
426 }
427
428 /**
429 * @covers Status::getErrorMessageArray
430 */
431 public function testGetErrorMessageArray() {
432 $method = new ReflectionMethod( 'Status', 'getErrorMessageArray' );
433 $method->setAccessible( true );
434 $status = new Status();
435 $key = 'foo';
436 $params = array( 'bar' );
437
438 /** @var Message[] $messageArray */
439 $messageArray = $method->invoke(
440 $status,
441 array(
442 array_merge( array( $key ), $params ),
443 array_merge( array( $key ), $params )
444 )
445 );
446
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() );
453 }
454 }
455
456 /**
457 * @covers Status::getErrorsByType
458 */
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 );
465
466 $warnings = $status->getErrorsByType( 'warning' );
467 $errors = $status->getErrorsByType( 'error' );
468
469 $this->assertCount( 1, $warnings );
470 $this->assertCount( 1, $errors );
471 $this->assertEquals( $warning, $warnings[0]['message'] );
472 $this->assertEquals( $error, $errors[0]['message'] );
473 }
474
475 }