08a5495ab3929dbd74169f7e38b1168ed3980480
[lhc/web/wiklou.git] / tests / phpunit / includes / StatusTest.php
1 <?php
2
3 /**
4 * @author Adam Shorland
5 */
6 class StatusTest extends MediaWikiTestCase {
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 */
130 public function testWarningWithMessage( $mockDetails ) {
131 $status = new Status();
132 $messages = $this->getMockMessages( $mockDetails );
133
134 foreach ( $messages as $message ) {
135 $status->warning( $message );
136 }
137 $warnings = $status->getWarningsArray();
138
139 $this->assertEquals( count( $messages ), count( $warnings ) );
140 foreach ( $messages as $key => $message ) {
141 $expectedArray = array_merge( array( $message->getKey() ), $message->getParams() );
142 $this->assertEquals( $warnings[$key], $expectedArray );
143 }
144 }
145
146 /**
147 * @dataProvider provideMockMessageDetails
148 * @covers Status::error
149 * @covers Status::getErrorsArray
150 */
151 public function testErrorWithMessage( $mockDetails ) {
152 $status = new Status();
153 $messages = $this->getMockMessages( $mockDetails );
154
155 foreach ( $messages as $message ) {
156 $status->error( $message );
157 }
158 $errors = $status->getErrorsArray();
159
160 $this->assertEquals( count( $messages ), count( $errors ) );
161 foreach ( $messages as $key => $message ) {
162 $expectedArray = array_merge( array( $message->getKey() ), $message->getParams() );
163 $this->assertEquals( $errors[$key], $expectedArray );
164 }
165 }
166
167 protected function getMockMessage( $key = 'key', $params = array() ) {
168 $message = $this->getMockBuilder( 'Message' )
169 ->disableOriginalConstructor()
170 ->getMock();
171 $message->expects( $this->atLeastOnce() )
172 ->method( 'getKey' )
173 ->will( $this->returnValue( $key ) );
174 $message->expects( $this->atLeastOnce() )
175 ->method( 'getParams' )
176 ->will( $this->returnValue( $params ) );
177 return $message;
178 }
179
180 /**
181 * @param array $messageDetails eg. array( 'KEY' => array(/PARAMS/) )
182 * @return Message[]
183 */
184 protected function getMockMessages( $messageDetails ) {
185 $messages = array();
186 foreach ( $messageDetails as $key => $paramsArray ) {
187 $messages[] = $this->getMockMessage( $key, $paramsArray );
188 }
189 return $messages;
190 }
191
192 public static function provideMockMessageDetails() {
193 return array(
194 array( array( 'key1' => array( 'foo' => 'bar' ) ) ),
195 array( array( 'key1' => array( 'foo' => 'bar' ), 'key2' => array( 'foo2' => 'bar2' ) ) ),
196 );
197 }
198
199 /**
200 * @covers Status::merge
201 * @todo test merge with $overwriteValue true
202 */
203 public function testMerge() {
204 $status1 = new Status();
205 $status2 = new Status();
206 $message1 = $this->getMockMessage( 'warn1' );
207 $message2 = $this->getMockMessage( 'error2' );
208 $status1->warning( $message1 );
209 $status2->error( $message2 );
210
211 $status1->merge( $status2 );
212 $this->assertEquals( 2, count( $status1->getWarningsArray() ) + count( $status1->getErrorsArray() ) );
213 }
214
215 /**
216 * @covers Status::hasMessage
217 */
218 public function testHasMessage() {
219 $status = new Status();
220 $status->fatal( 'bad' );
221 $this->assertTrue( $status->hasMessage( 'bad' ) );
222 $this->assertFalse( $status->hasMessage( 'good' ) );
223 }
224
225 /**
226 * @dataProvider provideCleanParams
227 * @covers Status::cleanParams
228 */
229 public function testCleanParams( $cleanCallback, $params, $expected ) {
230 $method = new ReflectionMethod( 'Status', 'cleanParams' );
231 $method->setAccessible(TRUE);
232 $status = new Status();
233 $status->cleanCallback = $cleanCallback;
234
235 $this->assertEquals( $expected, $method->invoke( $status, $params ) );
236 }
237
238 /**
239 * @todo test cleanParams with a callback
240 */
241 public static function provideCleanParams() {
242 return array(
243 array( false, array( 'foo' => 'bar' ), array( 'foo' => 'bar' ) ),
244 );
245 }
246
247 /**
248 * @dataProvider provideGetWikiText
249 * @covers Status::getWikiText
250 * @todo test long and short context messages generated through this method
251 * this can not really be done now due to use of wfMessage()->plain()
252 * It is possible to mock such methods but only if namespaces are used
253 */
254 public function testGetWikiText( Status $status, $expected ) {
255 $this->assertEquals( $expected, $status->getWikiText() );
256 }
257
258 /**
259 * @return array of arrays with values;
260 * 0 => status object
261 * 1 => expected string (with no context)
262 */
263 public static function provideGetWikiText() {
264 $testCases = array();
265
266 $testCases[ 'GoodStatus' ] = array(
267 new Status(),
268 "Internal error: Status::getWikiText called for a good result, this is incorrect\n",
269 );
270
271 $status = new Status();
272 $status->ok = false;
273 $testCases[ 'GoodButNoError' ] = array(
274 $status,
275 "Internal error: Status::getWikiText: Invalid result object: no error text but not OK\n",
276 );
277
278 $status = new Status();
279 $status->warning( 'fooBar!' );
280 $testCases[ '1StringWarning' ] = array(
281 $status,
282 "<fooBar!>",
283 );
284
285 $status = new Status();
286 $status->warning( 'fooBar!' );
287 $status->warning( 'fooBar2!' );
288 $testCases[ '2StringWarnings' ] = array(
289 $status,
290 "* <fooBar!>\n* <fooBar2!>\n",
291 );
292
293 $status = new Status();
294 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
295 $testCases[ '1MessageWarning' ] = array(
296 $status,
297 "<fooBar!>",
298 );
299
300 $status = new Status();
301 $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
302 $status->warning( new Message( 'fooBar2!' ) );
303 $testCases[ '2MessageWarnings' ] = array(
304 $status,
305 "* <fooBar!>\n* <fooBar2!>\n",
306 );
307
308 return $testCases;
309 }
310
311 //todo test getMessage
312 //todo test getErrorMessage
313 //todo test getHTML
314 //todo test getErrorMessageArray
315 //todo test getStatusArray
316 //todo test getErrorsByType
317 //todo test replaceMessage
318
319 }