| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Context\IContextSource; |
| 4 |
use MediaWiki\Context\RequestContext; |
| 5 |
use MediaWiki\Language\RawMessage; |
| 6 |
use MediaWiki\Message\Message; |
| 7 |
use MediaWiki\Status\Status; |
| 8 |
use Wikimedia\Message\MessageSpecifier; |
| 9 |
use Wikimedia\Message\MessageValue; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
class StatusTest extends MediaWikiLangTestCase { |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
public function testNewGood( $value = null ) { |
| 22 |
$status = Status::newGood( $value ); |
| 23 |
$this->assertTrue( $status->isGood() ); |
| 24 |
$this->assertTrue( $status->isOK() ); |
| 25 |
$this->assertEquals( $value, $status->getValue() ); |
| 26 |
} |
| 27 |
|
| 28 |
public static function provideValues() { |
| 29 |
return [ |
| 30 |
[], |
| 31 |
[ 'foo' ], |
| 32 |
[ [ 'foo' => 'bar' ] ], |
| 33 |
[ new Exception() ], |
| 34 |
[ 1234 ], |
| 35 |
]; |
| 36 |
} |
| 37 |
|
| 38 |
public function testNewFatalWithMessage() { |
| 39 |
$message = $this->getMockMessage(); |
| 40 |
$status = Status::newFatal( $message ); |
| 41 |
$this->assertFalse( $status->isGood() ); |
| 42 |
$this->assertFalse( $status->isOK() ); |
| 43 |
$this->assertEquals( $message, $status->getMessage() ); |
| 44 |
} |
| 45 |
|
| 46 |
public function testNewFatalWithString() { |
| 47 |
$message = 'foo'; |
| 48 |
$status = Status::newFatal( $message ); |
| 49 |
$this->assertFalse( $status->isGood() ); |
| 50 |
$this->assertFalse( $status->isOK() ); |
| 51 |
$this->assertEquals( $message, $status->getMessage()->getKey() ); |
| 52 |
} |
| 53 |
|
| 54 |
public function testOkAndErrorsGetters() { |
| 55 |
$status = Status::newGood( 'foo' ); |
| 56 |
$this->assertTrue( $status->ok ); |
| 57 |
$status = Status::newFatal( 'foo', 1, 2 ); |
| 58 |
$this->assertFalse( $status->ok ); |
| 59 |
$this->assertArrayEquals( |
| 60 |
[ |
| 61 |
[ |
| 62 |
'type' => 'error', |
| 63 |
'message' => 'foo', |
| 64 |
'params' => [ 1, 2 ] |
| 65 |
] |
| 66 |
], |
| 67 |
$status->errors |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
public function testOkSetter() { |
| 72 |
$status = new Status(); |
| 73 |
$status->ok = false; |
| 74 |
$this->assertFalse( $status->isOK() ); |
| 75 |
$status->ok = true; |
| 76 |
$this->assertTrue( $status->isOK() ); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public function testSetResult( $ok, $value = null ) { |
| 83 |
$status = new Status(); |
| 84 |
$status->setResult( $ok, $value ); |
| 85 |
$this->assertEquals( $ok, $status->isOK() ); |
| 86 |
$this->assertEquals( $value, $status->getValue() ); |
| 87 |
} |
| 88 |
|
| 89 |
public static function provideSetResult() { |
| 90 |
return [ |
| 91 |
[ true ], |
| 92 |
[ false ], |
| 93 |
[ true, 'value' ], |
| 94 |
[ false, 'value' ], |
| 95 |
]; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public function testIsOk( $ok ) { |
| 102 |
$status = new Status(); |
| 103 |
$status->setOK( $ok ); |
| 104 |
$this->assertEquals( $ok, $status->isOK() ); |
| 105 |
} |
| 106 |
|
| 107 |
public static function provideIsOk() { |
| 108 |
return [ |
| 109 |
[ true ], |
| 110 |
[ false ], |
| 111 |
]; |
| 112 |
} |
| 113 |
|
| 114 |
public function testGetValue() { |
| 115 |
$status = new Status(); |
| 116 |
$status->value = 'foobar'; |
| 117 |
$this->assertEquals( 'foobar', $status->getValue() ); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
public function testIsGood( $ok, $errors, $expected ) { |
| 124 |
$status = new Status(); |
| 125 |
$status->setOK( $ok ); |
| 126 |
foreach ( $errors as $error ) { |
| 127 |
$status->warning( $error ); |
| 128 |
} |
| 129 |
$this->assertEquals( $expected, $status->isGood() ); |
| 130 |
} |
| 131 |
|
| 132 |
public static function provideIsGood() { |
| 133 |
return [ |
| 134 |
[ true, [], true ], |
| 135 |
[ true, [ 'foo' ], false ], |
| 136 |
[ false, [], false ], |
| 137 |
[ false, [ 'foo' ], false ], |
| 138 |
]; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
public function testWarningWithMessage( $mockDetails ) { |
| 145 |
$status = new Status(); |
| 146 |
$messages = $this->getMockMessages( $mockDetails ); |
| 147 |
|
| 148 |
foreach ( $messages as $message ) { |
| 149 |
$status->warning( $message ); |
| 150 |
} |
| 151 |
$warnings = $status->getWarningsArray(); |
| 152 |
|
| 153 |
$this->assertSameSize( $messages, $warnings ); |
| 154 |
foreach ( $messages as $key => $message ) { |
| 155 |
$expectedArray = [ $message->getKey(), ...$message->getParams() ]; |
| 156 |
$this->assertEquals( $expectedArray, $warnings[$key] ); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public function testErrorWithMessage( $mockDetails ) { |
| 164 |
$status = new Status(); |
| 165 |
$messages = $this->getMockMessages( $mockDetails ); |
| 166 |
|
| 167 |
foreach ( $messages as $message ) { |
| 168 |
$status->error( $message ); |
| 169 |
} |
| 170 |
$errors = $status->getErrorsArray(); |
| 171 |
|
| 172 |
$this->assertSameSize( $messages, $errors ); |
| 173 |
foreach ( $messages as $key => $message ) { |
| 174 |
$expectedArray = [ $message->getKey(), ...$message->getParams() ]; |
| 175 |
$this->assertEquals( $expectedArray, $errors[$key] ); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public function testFatalWithMessage( $mockDetails ) { |
| 183 |
$status = new Status(); |
| 184 |
$messages = $this->getMockMessages( $mockDetails ); |
| 185 |
|
| 186 |
foreach ( $messages as $message ) { |
| 187 |
$status->fatal( $message ); |
| 188 |
} |
| 189 |
$errors = $status->getErrorsArray(); |
| 190 |
|
| 191 |
$this->assertSameSize( $messages, $errors ); |
| 192 |
foreach ( $messages as $key => $message ) { |
| 193 |
$expectedArray = [ $message->getKey(), ...$message->getParams() ]; |
| 194 |
$this->assertEquals( $expectedArray, $errors[$key] ); |
| 195 |
} |
| 196 |
$this->assertStatusNotOK( $status ); |
| 197 |
} |
| 198 |
|
| 199 |
public function testRawMessage() { |
| 200 |
$status = new Status(); |
| 201 |
$msg = new RawMessage( 'Foo Bar' ); |
| 202 |
$status->fatal( $msg ); |
| 203 |
|
| 204 |
$this->assertEquals( 'rawmessage', $status->getErrorsArray()[0][0] ); |
| 205 |
$this->assertEquals( $msg, $status->getMessages()[0] ); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
protected function getMockMessages( $messageDetails ) { |
| 213 |
$messages = []; |
| 214 |
foreach ( $messageDetails as $key => $paramsArray ) { |
| 215 |
$messages[] = $this->getMockMessage( $key, $paramsArray ); |
| 216 |
} |
| 217 |
return $messages; |
| 218 |
} |
| 219 |
|
| 220 |
public static function provideMockMessageDetails() { |
| 221 |
return [ |
| 222 |
[ [ 'key1' => [ 'bar' ] ] ], |
| 223 |
[ [ 'key1' => [ 'bar' ], 'key2' => [ 'bar2' ] ] ], |
| 224 |
]; |
| 225 |
} |
| 226 |
|
| 227 |
public function testMerge() { |
| 228 |
$status1 = new Status(); |
| 229 |
$status2 = new Status(); |
| 230 |
$message1 = $this->getMockMessage( 'warn1' ); |
| 231 |
$message2 = $this->getMockMessage( 'error2' ); |
| 232 |
$status1->warning( $message1 ); |
| 233 |
$status2->error( $message2 ); |
| 234 |
|
| 235 |
$status1->merge( $status2 ); |
| 236 |
$this->assertCount( 2, $status1->getMessages() ); |
| 237 |
} |
| 238 |
|
| 239 |
public function testMergeWithOverwriteValue() { |
| 240 |
$status1 = new Status(); |
| 241 |
$status2 = new Status(); |
| 242 |
$message1 = $this->getMockMessage( 'warn1' ); |
| 243 |
$message2 = $this->getMockMessage( 'error2' ); |
| 244 |
$status1->warning( $message1 ); |
| 245 |
$status2->error( $message2 ); |
| 246 |
$status2->value = 'FooValue'; |
| 247 |
|
| 248 |
$status1->merge( $status2, true ); |
| 249 |
$this->assertCount( 2, $status1->getMessages() ); |
| 250 |
$this->assertEquals( 'FooValue', $status1->getValue() ); |
| 251 |
} |
| 252 |
|
| 253 |
public function testHasMessage() { |
| 254 |
$status = new Status(); |
| 255 |
$status->fatal( 'bad' ); |
| 256 |
$status->fatal( wfMessage( 'bad-msg' ) ); |
| 257 |
$status->fatal( new MessageValue( 'bad-msg-value' ) ); |
| 258 |
$this->assertTrue( $status->hasMessage( 'bad' ) ); |
| 259 |
$this->assertTrue( $status->hasMessage( 'bad-msg' ) ); |
| 260 |
$this->expectDeprecationAndContinue( '/Passing MessageSpecifier/' ); |
| 261 |
$this->assertTrue( $status->hasMessage( wfMessage( 'bad-msg' ) ) ); |
| 262 |
$this->assertTrue( $status->hasMessage( wfMessage( 'bad-msg-value' ) ) ); |
| 263 |
$this->assertTrue( $status->hasMessage( new MessageValue( 'bad-msg' ) ) ); |
| 264 |
$this->assertTrue( $status->hasMessage( new MessageValue( 'bad-msg-value' ) ) ); |
| 265 |
$this->assertFalse( $status->hasMessage( 'good' ) ); |
| 266 |
} |
| 267 |
|
| 268 |
public function testHasMessagesExcept() { |
| 269 |
$status = new Status(); |
| 270 |
$status->fatal( 'bad' ); |
| 271 |
$status->fatal( wfMessage( 'bad-msg' ) ); |
| 272 |
$status->fatal( new MessageValue( 'bad-msg-value' ) ); |
| 273 |
$this->assertTrue( $status->hasMessagesExcept( 'good' ) ); |
| 274 |
$this->assertTrue( $status->hasMessagesExcept( 'bad' ) ); |
| 275 |
$this->assertFalse( $status->hasMessagesExcept( |
| 276 |
'bad', 'bad-msg', 'bad-msg-value' ) ); |
| 277 |
$this->assertFalse( $status->hasMessagesExcept( |
| 278 |
'good', 'bad', 'bad-msg', 'bad-msg-value' ) ); |
| 279 |
$this->expectDeprecationAndContinue( '/Passing MessageSpecifier/' ); |
| 280 |
$this->assertFalse( $status->hasMessagesExcept( |
| 281 |
wfMessage( 'bad' ), new MessageValue( 'bad-msg' ), 'bad-msg-value' ) ); |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
public function testCleanParams( $cleanCallback, $params, $expected, $unexpected ) { |
| 288 |
$this->setUserLang( 'qqx' ); |
| 289 |
|
| 290 |
$status = new Status(); |
| 291 |
$status->cleanCallback = $cleanCallback; |
| 292 |
$status->warning( 'ok', ...$params ); |
| 293 |
|
| 294 |
$wikitext = $status->getWikiText(); |
| 295 |
$this->assertStringContainsString( $expected, $wikitext ); |
| 296 |
$this->assertStringNotContainsString( $unexpected, $wikitext ); |
| 297 |
|
| 298 |
$html = $status->getHTML(); |
| 299 |
$this->assertStringContainsString( $expected, $html ); |
| 300 |
$this->assertStringNotContainsString( $unexpected, $html ); |
| 301 |
} |
| 302 |
|
| 303 |
public static function provideCleanParams() { |
| 304 |
$cleanCallback = static function ( $value ) { |
| 305 |
return 'xxx'; |
| 306 |
}; |
| 307 |
|
| 308 |
return [ |
| 309 |
[ false, [ 'secret' ], 'secret', 'xxx' ], |
| 310 |
[ $cleanCallback, [ 'secret' ], 'xxx', 'secret' ], |
| 311 |
]; |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
public function testGetWikiText( |
| 318 |
Status $status, $wikitext, $wrappedWikitext, $html, $wrappedHtml |
| 319 |
) { |
| 320 |
$this->assertEquals( $wikitext, $status->getWikiText() ); |
| 321 |
|
| 322 |
$this->assertEquals( $wrappedWikitext, $status->getWikiText( 'wrap-short', 'wrap-long', 'qqx' ) ); |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
public function testGetHtml( |
| 329 |
Status $status, $wikitext, $wrappedWikitext, $html, $wrappedHtml |
| 330 |
) { |
| 331 |
$this->assertEquals( $html, $status->getHTML() ); |
| 332 |
|
| 333 |
$this->assertEquals( $wrappedHtml, $status->getHTML( 'wrap-short', 'wrap-long', 'qqx' ) ); |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
public static function provideGetWikiTextAndHtml() { |
| 342 |
$testCases = []; |
| 343 |
|
| 344 |
$testCases['GoodStatus'] = [ |
| 345 |
new Status(), |
| 346 |
"Internal error: MediaWiki\Status\StatusFormatter::getWikiText called for a good result, this is incorrect ", |
| 347 |
"(wrap-short: (internalerror_info: MediaWiki\Status\StatusFormatter::getWikiText called for a good result, " . |
| 348 |
"this is incorrect ))", |
| 349 |
"<p>Internal error: MediaWiki\Status\StatusFormatter::getWikiText called for a good result, this is incorrect \n</p>", |
| 350 |
"<p>(wrap-short: (internalerror_info: MediaWiki\Status\StatusFormatter::getWikiText called for a good result, " . |
| 351 |
"this is incorrect ))\n</p>", |
| 352 |
]; |
| 353 |
|
| 354 |
$status = new Status(); |
| 355 |
$status->setOK( false ); |
| 356 |
$testCases['GoodButNoError'] = [ |
| 357 |
$status, |
| 358 |
"Internal error: MediaWiki\Status\StatusFormatter::getWikiText: Invalid result object: no error text but not OK ", |
| 359 |
"(wrap-short: (internalerror_info: MediaWiki\Status\StatusFormatter::getWikiText: Invalid result object: " . |
| 360 |
"no error text but not OK ))", |
| 361 |
"<p>Internal error: MediaWiki\Status\StatusFormatter::getWikiText: Invalid result object: no error text but not OK \n</p>", |
| 362 |
"<p>(wrap-short: (internalerror_info: MediaWiki\Status\StatusFormatter::getWikiText: Invalid result object: " . |
| 363 |
"no error text but not OK ))\n</p>", |
| 364 |
]; |
| 365 |
|
| 366 |
$status = new Status(); |
| 367 |
$status->warning( 'fooBar!' ); |
| 368 |
$testCases['1StringWarning'] = [ |
| 369 |
$status, |
| 370 |
"â§¼fooBar!â§½", |
| 371 |
"(wrap-short: (fooBar!))", |
| 372 |
"<p>â§¼fooBar!â§½\n</p>", |
| 373 |
"<p>(wrap-short: (fooBar!))\n</p>", |
| 374 |
]; |
| 375 |
|
| 376 |
$status = new Status(); |
| 377 |
$status->warning( 'fooBar!' ); |
| 378 |
$status->warning( 'fooBar2!' ); |
| 379 |
$testCases['2StringWarnings'] = [ |
| 380 |
$status, |
| 381 |
"<ul>\n<li>\nâ§¼fooBar!â§½\n</li>\n<li>\nâ§¼fooBar2!â§½\n</li>\n</ul>\n", |
| 382 |
"(wrap-long: <ul>\n<li>\n(fooBar!)\n</li>\n<li>\n(fooBar2!)\n</li>\n</ul>\n)", |
| 383 |
"<ul>\n<li>\nâ§¼fooBar!â§½\n</li>\n<li>\nâ§¼fooBar2!â§½\n</li>\n</ul>\n", |
| 384 |
"<p>(wrap-long: </p><ul>\n<li>\n(fooBar!)\n</li>\n<li>\n(fooBar2!)\n</li>\n</ul>\n<p>)\n</p>", |
| 385 |
]; |
| 386 |
|
| 387 |
$status = new Status(); |
| 388 |
$status->warning( new Message( 'fooBar!', [ 'foo', 'bar' ] ) ); |
| 389 |
$testCases['1MessageWarning'] = [ |
| 390 |
$status, |
| 391 |
"â§¼fooBar!â§½", |
| 392 |
"(wrap-short: (fooBar!: foo, bar))", |
| 393 |
"<p>â§¼fooBar!â§½\n</p>", |
| 394 |
"<p>(wrap-short: (fooBar!: foo, bar))\n</p>", |
| 395 |
]; |
| 396 |
|
| 397 |
$status = new Status(); |
| 398 |
$status->warning( new Message( 'fooBar!', [ 'foo', 'bar' ] ) ); |
| 399 |
$status->warning( new Message( 'fooBar2!' ) ); |
| 400 |
$testCases['2MessageWarnings'] = [ |
| 401 |
$status, |
| 402 |
"<ul>\n<li>\nâ§¼fooBar!â§½\n</li>\n<li>\nâ§¼fooBar2!â§½\n</li>\n</ul>\n", |
| 403 |
"(wrap-long: <ul>\n<li>\n(fooBar!: foo, bar)\n</li>\n<li>\n(fooBar2!)\n</li>\n</ul>\n)", |
| 404 |
"<ul>\n<li>\nâ§¼fooBar!â§½\n</li>\n<li>\nâ§¼fooBar2!â§½\n</li>\n</ul>\n", |
| 405 |
"<p>(wrap-long: </p><ul>\n<li>\n(fooBar!: foo, bar)\n</li>\n<li>\n(fooBar2!)\n</li>\n</ul>\n<p>)\n</p>", |
| 406 |
]; |
| 407 |
|
| 408 |
return $testCases; |
| 409 |
} |
| 410 |
|
| 411 |
private static function sanitizedMessageParams( Message $message ) { |
| 412 |
return array_map( static function ( $p ) { |
| 413 |
return $p instanceof Message |
| 414 |
? [ |
| 415 |
'key' => $p->getKey(), |
| 416 |
'params' => self::sanitizedMessageParams( $p ), |
| 417 |
'lang' => $p->getLanguage()->getCode(), |
| 418 |
] |
| 419 |
: $p; |
| 420 |
}, $message instanceof RawMessage ? $message->getParamsOfRawMessage() : $message->getParams() ); |
| 421 |
} |
| 422 |
|
| 423 |
private static function sanitizedMessageKey( Message $message ) { |
| 424 |
return $message instanceof RawMessage ? $message->getTextOfRawMessage() : $message->getKey(); |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
public function testGetMessage( |
| 431 |
Status $status, $expectedParams, $expectedKey, $expectedWrapper |
| 432 |
) { |
| 433 |
$message = $status->getMessage( null, null, 'qqx' ); |
| 434 |
$this->assertInstanceOf( Message::class, $message ); |
| 435 |
$this->assertEquals( $expectedParams, self::sanitizedMessageParams( $message ), |
| 436 |
'Message::getParams' ); |
| 437 |
$this->assertEquals( $expectedKey, self::sanitizedMessageKey( $message ), 'Message::getKey' ); |
| 438 |
|
| 439 |
$message = $status->getMessage( 'wrapper-short', 'wrapper-long' ); |
| 440 |
$this->assertInstanceOf( Message::class, $message ); |
| 441 |
$this->assertEquals( $expectedWrapper, $message->getKey(), 'Message::getKey with wrappers' ); |
| 442 |
$this->assertCount( 1, $message->getParams(), 'Message::getParams with wrappers' ); |
| 443 |
|
| 444 |
$message = $status->getMessage( 'wrapper' ); |
| 445 |
$this->assertInstanceOf( Message::class, $message ); |
| 446 |
$this->assertEquals( 'wrapper', $message->getKey(), 'Message::getKey with wrappers' ); |
| 447 |
$this->assertCount( 1, $message->getParams(), 'Message::getParams with wrappers' ); |
| 448 |
|
| 449 |
$message = $status->getMessage( false, 'wrapper' ); |
| 450 |
$this->assertInstanceOf( Message::class, $message ); |
| 451 |
$this->assertEquals( 'wrapper', $message->getKey(), 'Message::getKey with wrappers' ); |
| 452 |
$this->assertCount( 1, $message->getParams(), 'Message::getParams with wrappers' ); |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
public static function provideGetMessage() { |
| 462 |
$testCases = []; |
| 463 |
|
| 464 |
$testCases['GoodStatus'] = [ |
| 465 |
new Status(), |
| 466 |
[ "MediaWiki\Status\StatusFormatter::getMessage called for a good result, this is incorrect " ], |
| 467 |
'internalerror_info', |
| 468 |
'wrapper-short' |
| 469 |
]; |
| 470 |
|
| 471 |
$status = new Status(); |
| 472 |
$status->setOK( false ); |
| 473 |
$testCases['GoodButNoError'] = [ |
| 474 |
$status, |
| 475 |
[ "MediaWiki\Status\StatusFormatter::getMessage: Invalid result object: no error text but not OK " ], |
| 476 |
'internalerror_info', |
| 477 |
'wrapper-short' |
| 478 |
]; |
| 479 |
|
| 480 |
$status = new Status(); |
| 481 |
$status->warning( 'fooBar!' ); |
| 482 |
$testCases['1StringWarning'] = [ |
| 483 |
$status, |
| 484 |
[], |
| 485 |
'fooBar!', |
| 486 |
'wrapper-short' |
| 487 |
]; |
| 488 |
|
| 489 |
$status = new Status(); |
| 490 |
$status->warning( 'fooBar!' ); |
| 491 |
$status->warning( 'fooBar2!' ); |
| 492 |
$testCases[ '2StringWarnings' ] = [ |
| 493 |
$status, |
| 494 |
[ |
| 495 |
[ 'key' => 'fooBar!', 'params' => [], 'lang' => 'qqx' ], |
| 496 |
[ 'key' => 'fooBar2!', 'params' => [], 'lang' => 'qqx' ] |
| 497 |
], |
| 498 |
"* \$1\n* \$2", |
| 499 |
'wrapper-long' |
| 500 |
]; |
| 501 |
|
| 502 |
$status = new Status(); |
| 503 |
$status->warning( new Message( 'fooBar!', [ 'foo', 'bar' ] ) ); |
| 504 |
$testCases['1MessageWarning'] = [ |
| 505 |
$status, |
| 506 |
[ 'foo', 'bar' ], |
| 507 |
'fooBar!', |
| 508 |
'wrapper-short' |
| 509 |
]; |
| 510 |
|
| 511 |
$status = new Status(); |
| 512 |
$status->warning( new MessageValue( 'fooBar!', [ 'foo', 'bar' ] ) ); |
| 513 |
$status->warning( new MessageValue( 'fooBar2!' ) ); |
| 514 |
$testCases['2MessageWarnings'] = [ |
| 515 |
$status, |
| 516 |
[ |
| 517 |
[ 'key' => 'fooBar!', 'params' => [ 'foo', 'bar' ], 'lang' => 'qqx' ], |
| 518 |
[ 'key' => 'fooBar2!', 'params' => [], 'lang' => 'qqx' ] |
| 519 |
], |
| 520 |
"* \$1\n* \$2", |
| 521 |
'wrapper-long' |
| 522 |
]; |
| 523 |
|
| 524 |
return $testCases; |
| 525 |
} |
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
public function testGetPsr3MessageAndContext( |
| 531 |
array $errors, |
| 532 |
string $expectedMessage, |
| 533 |
array $expectedContext |
| 534 |
) { |
| 535 |
|
| 536 |
|
| 537 |
$this->setTemporaryHook( 'MessageCacheFetchOverrides', static function ( &$overrides ) { |
| 538 |
$overrides['rawmessage_2'] = 'rawmessage'; |
| 539 |
}, false ); |
| 540 |
|
| 541 |
$status = new Status(); |
| 542 |
foreach ( $errors as $error ) { |
| 543 |
$status->error( ...$error ); |
| 544 |
} |
| 545 |
[ $actualMessage, $actualContext ] = $status->getPsr3MessageAndContext(); |
| 546 |
$this->assertSame( $expectedMessage, $actualMessage ); |
| 547 |
$this->assertSame( $expectedContext, $actualContext ); |
| 548 |
} |
| 549 |
|
| 550 |
public static function provideGetPsr3MessageAndContext() { |
| 551 |
return [ |
| 552 |
|
| 553 |
'no errors' => [ |
| 554 |
[], |
| 555 |
"Internal error: MediaWiki\Status\StatusFormatter::getWikiText called for a good result, this is incorrect ", |
| 556 |
[], |
| 557 |
], |
| 558 |
|
| 559 |
'rawmessage_2' => [ |
| 560 |
[ [ 'rawmessage_2', 'foo' ] ], |
| 561 |
'{parameter1}', |
| 562 |
[ 'parameter1' => 'foo' ], |
| 563 |
], |
| 564 |
'two errors' => [ |
| 565 |
[ [ 'rawmessage_2', 'foo' ], [ 'rawmessage_2', 'bar' ] ], |
| 566 |
"<ul>\n<li>\nfoo\n</li>\n<li>\nbar\n</li>\n</ul>\n", |
| 567 |
[], |
| 568 |
], |
| 569 |
'unknown subclass' => [ |
| 570 |
|
| 571 |
[ [ new class( 'rawmessage_2', [ 'foo' ] ) extends Message {} ] ], |
| 572 |
'foo', |
| 573 |
[], |
| 574 |
], |
| 575 |
'non-scalar parameter' => [ |
| 576 |
[ [ new Message( 'rawmessage_2', [ new Message( 'rawmessage_2', [ 'foo' ] ) ] ) ] ], |
| 577 |
'foo', |
| 578 |
[], |
| 579 |
], |
| 580 |
'one parameter' => [ |
| 581 |
[ [ 'apiwarn-invalidtitle', 'foo' ] ], |
| 582 |
'"{parameter1}" is not a valid title.', |
| 583 |
[ 'parameter1' => 'foo' ], |
| 584 |
], |
| 585 |
'multiple parameters' => [ |
| 586 |
[ [ 'api-exception-trace', 'foo', 'bar', 'baz', 'boom' ] ], |
| 587 |
"{parameter1} at {parameter2}({parameter3})\n{parameter4}", |
| 588 |
[ 'parameter1' => 'foo', 'parameter2' => 'bar', 'parameter3' => 'baz', 'parameter4' => 'boom' ], |
| 589 |
], |
| 590 |
'formatted parameter' => [ |
| 591 |
[ [ 'apiwarn-invalidtitle', Message::numParam( 1000000 ) ] ], |
| 592 |
'"{parameter1}" is not a valid title.', |
| 593 |
[ 'parameter1' => 1000000 ], |
| 594 |
], |
| 595 |
'rawmessage' => [ |
| 596 |
[ [ 'rawmessage', 'foo' ] ], |
| 597 |
'foo', |
| 598 |
[], |
| 599 |
], |
| 600 |
'RawMessage' => [ |
| 601 |
[ [ new RawMessage( 'foo $1 baz', [ 'bar' ] ) ] ], |
| 602 |
'foo {parameter1} baz', |
| 603 |
[ 'parameter1' => 'bar' ], |
| 604 |
], |
| 605 |
]; |
| 606 |
} |
| 607 |
|
| 608 |
public function testReplaceMessageObj() { |
| 609 |
$this->expectDeprecationAndContinue( '/Passing MessageSpecifier/' ); |
| 610 |
|
| 611 |
$status = new Status(); |
| 612 |
$message = new Message( 'key1', [ 'foo1', 'bar1' ] ); |
| 613 |
$status->error( $message ); |
| 614 |
$newMessage = new Message( 'key2', [ 'foo2', 'bar2' ] ); |
| 615 |
|
| 616 |
|
| 617 |
$status->replaceMessage( $message, $newMessage ); |
| 618 |
$this->assertSame( $newMessage, $status->errors[0]['message'] ); |
| 619 |
|
| 620 |
$messageB = new Message( 'key-b', [ 'foo1', 'bar1' ] ); |
| 621 |
$status->error( $messageB ); |
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
$status->replaceMessage( new Message( 'key-b' ), 'huh' ); |
| 626 |
$status->replaceMessage( new Message( 'key-b', [ 'foo1', 'bar1' ] ), 'huh' ); |
| 627 |
$this->assertSame( $messageB, $status->errors[1]['message'] ); |
| 628 |
} |
| 629 |
|
| 630 |
public function testReplaceMessageValue() { |
| 631 |
$this->expectDeprecationAndContinue( '/Passing MessageSpecifier/' ); |
| 632 |
|
| 633 |
$status = new Status(); |
| 634 |
$messageVal = new MessageValue( 'key1', [ 'foo1', 'bar1' ] ); |
| 635 |
$status->error( $messageVal ); |
| 636 |
$newMessageVal = new MessageValue( 'key2', [ 'foo2', 'bar2' ] ); |
| 637 |
|
| 638 |
$status->replaceMessage( $messageVal, $newMessageVal ); |
| 639 |
|
| 640 |
$conv = new \MediaWiki\Message\Converter; |
| 641 |
$this->assertEquals( $newMessageVal, $conv->convertMessage( $status->errors[0]['message'] ) ); |
| 642 |
} |
| 643 |
|
| 644 |
public function testReplaceMessageByKey() { |
| 645 |
$status = new Status(); |
| 646 |
|
| 647 |
$status->error( new Message( 'key1', [ 'foo1', 'bar1' ] ) ); |
| 648 |
$newMessage = new Message( 'key2', [ 'foo2', 'bar2' ] ); |
| 649 |
|
| 650 |
$status->replaceMessage( 'key1', $newMessage ); |
| 651 |
|
| 652 |
$this->assertEquals( $newMessage, $status->errors[0]['message'] ); |
| 653 |
} |
| 654 |
|
| 655 |
public function testWakeUpSanitizesCallback() { |
| 656 |
$status = new Status(); |
| 657 |
$status->cleanCallback = static function ( $value ) { |
| 658 |
return '-' . $value . '-'; |
| 659 |
}; |
| 660 |
$status->__wakeup(); |
| 661 |
$this->assertFalse( $status->cleanCallback ); |
| 662 |
} |
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
public function testGetStatusArrayWithNonObjectMessages( $nonObjMsg ) { |
| 668 |
$status = new Status(); |
| 669 |
if ( !array_key_exists( 1, $nonObjMsg ) ) { |
| 670 |
$status->warning( $nonObjMsg[0] ); |
| 671 |
} else { |
| 672 |
$status->warning( $nonObjMsg[0], $nonObjMsg[1] ); |
| 673 |
} |
| 674 |
|
| 675 |
$array = $status->getWarningsArray(); |
| 676 |
|
| 677 |
$this->assertCount( 1, $array ); |
| 678 |
$this->assertEquals( $nonObjMsg, $array[0] ); |
| 679 |
} |
| 680 |
|
| 681 |
public static function provideNonObjectMessages() { |
| 682 |
return [ |
| 683 |
[ [ 'ImaString', [ 'param1' => 'value1' ] ] ], |
| 684 |
[ [ 'ImaString' ] ], |
| 685 |
]; |
| 686 |
} |
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
public function testGetErrorsWarningsOnlyStatus( $errorText, $warningText, $type, $errorResult, |
| 692 |
$warningResult |
| 693 |
) { |
| 694 |
$status = Status::newGood(); |
| 695 |
if ( $errorText ) { |
| 696 |
$status->fatal( $errorText ); |
| 697 |
} |
| 698 |
if ( $warningText ) { |
| 699 |
$status->warning( $warningText ); |
| 700 |
} |
| 701 |
$testStatus = $status->splitByErrorType()[$type]; |
| 702 |
$this->assertEquals( $errorResult, $testStatus->getErrorsByType( 'error' ) ); |
| 703 |
$this->assertEquals( $warningResult, $testStatus->getErrorsByType( 'warning' ) ); |
| 704 |
} |
| 705 |
|
| 706 |
public static function provideErrorsWarningsOnly() { |
| 707 |
return [ |
| 708 |
[ |
| 709 |
'Just an error', |
| 710 |
'Just a warning', |
| 711 |
0, |
| 712 |
[ |
| 713 |
0 => [ |
| 714 |
'type' => 'error', |
| 715 |
'message' => 'Just an error', |
| 716 |
'params' => [] |
| 717 |
], |
| 718 |
], |
| 719 |
[], |
| 720 |
], [ |
| 721 |
'Just an error', |
| 722 |
'Just a warning', |
| 723 |
1, |
| 724 |
[], |
| 725 |
[ |
| 726 |
0 => [ |
| 727 |
'type' => 'warning', |
| 728 |
'message' => 'Just a warning', |
| 729 |
'params' => [] |
| 730 |
], |
| 731 |
], |
| 732 |
], [ |
| 733 |
null, |
| 734 |
null, |
| 735 |
1, |
| 736 |
[], |
| 737 |
[], |
| 738 |
], [ |
| 739 |
null, |
| 740 |
null, |
| 741 |
0, |
| 742 |
[], |
| 743 |
[], |
| 744 |
] |
| 745 |
]; |
| 746 |
} |
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
public function testWrapAndSplitByErrorType() { |
| 753 |
$sv = StatusValue::newFatal( 'fatal' ); |
| 754 |
$sv->warning( 'warning' ); |
| 755 |
$s = Status::wrap( $sv ); |
| 756 |
[ $se, $sw ] = $s->splitByErrorType(); |
| 757 |
$this->assertTrue( $s->hasMessage( 'fatal' ) ); |
| 758 |
$this->assertTrue( $s->hasMessage( 'warning' ) ); |
| 759 |
$this->assertFalse( $s->isOK() ); |
| 760 |
$this->assertTrue( $se->hasMessage( 'fatal' ) ); |
| 761 |
$this->assertFalse( $se->hasMessage( 'warning' ) ); |
| 762 |
$this->assertFalse( $s->isOK() ); |
| 763 |
$this->assertFalse( $sw->hasMessage( 'fatal' ) ); |
| 764 |
$this->assertTrue( $sw->hasMessage( 'warning' ) ); |
| 765 |
$this->assertTrue( $sw->isOK() ); |
| 766 |
} |
| 767 |
|
| 768 |
public function testSetContext() { |
| 769 |
$status = Status::newFatal( 'foo' ); |
| 770 |
$status->fatal( 'bar' ); |
| 771 |
|
| 772 |
$messageLocalizer = $this->createNoOpMock( IContextSource::class, [ 'msg' ] ); |
| 773 |
$messageLocalizer->expects( $this->atLeastOnce() ) |
| 774 |
->method( 'msg' ) |
| 775 |
->willReturnCallback( static function ( $key ) { |
| 776 |
return new RawMessage( $key ); |
| 777 |
} ); |
| 778 |
|
| 779 |
$status->setMessageLocalizer( $messageLocalizer ); |
| 780 |
$status->getWikiText(); |
| 781 |
$status->getWikiText( false, false, 'en' ); |
| 782 |
$status->getWikiText( 'wrap-short', 'wrap-long' ); |
| 783 |
} |
| 784 |
|
| 785 |
public static function provideDuplicates() { |
| 786 |
yield [ [ 'foo', 1, 2 ], [ 'foo', 1, 2 ] ]; |
| 787 |
$message = new Message( 'foo', [ 1, 2 ] ); |
| 788 |
yield [ $message, $message ]; |
| 789 |
yield [ $message, [ 'foo', 1, 2 ] ]; |
| 790 |
yield [ [ 'foo', 1, 2 ], $message ]; |
| 791 |
$messageWithContext1 = ( new Message( 'foo' ) )->setContext( RequestContext::getMain() ); |
| 792 |
$messageWithContext2 = ( new Message( 'foo' ) )->setContext( RequestContext::getMain() ); |
| 793 |
yield [ $messageWithContext1, $messageWithContext2 ]; |
| 794 |
} |
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
public function testDuplicateError( $error1, $error2 ) { |
| 800 |
$status = Status::newGood(); |
| 801 |
if ( $error1 instanceof MessageSpecifier ) { |
| 802 |
$status->error( $error1 ); |
| 803 |
$expected = [ |
| 804 |
'type' => 'error', |
| 805 |
'message' => $error1, |
| 806 |
'params' => [] |
| 807 |
]; |
| 808 |
} else { |
| 809 |
$status->error( ...$error1 ); |
| 810 |
$message1 = $error1[0]; |
| 811 |
array_shift( $error1 ); |
| 812 |
$expected = [ |
| 813 |
'type' => 'error', |
| 814 |
'message' => $message1, |
| 815 |
'params' => $error1 |
| 816 |
]; |
| 817 |
} |
| 818 |
if ( $error2 instanceof MessageSpecifier ) { |
| 819 |
$status->error( $error2 ); |
| 820 |
} else { |
| 821 |
$message2 = $error2[0]; |
| 822 |
array_shift( $error2 ); |
| 823 |
$status->error( $message2, ...$error2 ); |
| 824 |
} |
| 825 |
$this->assertArrayEquals( [ $expected ], $status->errors ); |
| 826 |
} |
| 827 |
|
| 828 |
public function testDuplicateWarning() { |
| 829 |
$status = Status::newGood(); |
| 830 |
$status->warning( 'foo', 1, 2 ); |
| 831 |
$status->warning( 'foo', 1, 2 ); |
| 832 |
$this->assertArrayEquals( |
| 833 |
[ |
| 834 |
[ |
| 835 |
'type' => 'warning', |
| 836 |
'message' => 'foo', |
| 837 |
'params' => [ 1, 2 ] |
| 838 |
] |
| 839 |
], |
| 840 |
$status->errors |
| 841 |
); |
| 842 |
} |
| 843 |
|
| 844 |
public function testErrorNotDowngradedToWarning() { |
| 845 |
$status = Status::newGood(); |
| 846 |
$status->error( 'foo', 1, 2 ); |
| 847 |
$status->warning( 'foo', 1, 2 ); |
| 848 |
$this->assertArrayEquals( |
| 849 |
[ |
| 850 |
[ |
| 851 |
'type' => 'error', |
| 852 |
'message' => 'foo', |
| 853 |
'params' => [ 1, 2 ] |
| 854 |
] |
| 855 |
], |
| 856 |
$status->errors |
| 857 |
); |
| 858 |
} |
| 859 |
|
| 860 |
public function testErrorNotDowngradedToWarningMessage() { |
| 861 |
$status = Status::newGood(); |
| 862 |
$message = new Message( 'foo', [ 1, 2 ] ); |
| 863 |
$status->error( $message ); |
| 864 |
$status->warning( $message ); |
| 865 |
$this->assertArrayEquals( |
| 866 |
[ |
| 867 |
[ |
| 868 |
'type' => 'error', |
| 869 |
'message' => $message, |
| 870 |
'params' => [] |
| 871 |
] |
| 872 |
], |
| 873 |
$status->errors |
| 874 |
); |
| 875 |
} |
| 876 |
|
| 877 |
public function testWarningUpgradedToError() { |
| 878 |
$status = Status::newGood(); |
| 879 |
$status->warning( 'foo', 1, 2 ); |
| 880 |
$status->error( 'foo', 1, 2 ); |
| 881 |
$this->assertArrayEquals( |
| 882 |
[ |
| 883 |
[ |
| 884 |
'type' => 'error', |
| 885 |
'message' => 'foo', |
| 886 |
'params' => [ 1, 2 ] |
| 887 |
] |
| 888 |
], |
| 889 |
$status->errors |
| 890 |
); |
| 891 |
} |
| 892 |
|
| 893 |
public function testWarningUpgradedToErrorMessage() { |
| 894 |
$status = Status::newGood(); |
| 895 |
$message = new Message( 'foo', [ 1, 2 ] ); |
| 896 |
$status->warning( $message ); |
| 897 |
$status->error( $message ); |
| 898 |
$this->assertArrayEquals( |
| 899 |
[ |
| 900 |
[ |
| 901 |
'type' => 'error', |
| 902 |
'message' => $message, |
| 903 |
'params' => [] |
| 904 |
] |
| 905 |
], |
| 906 |
$status->errors |
| 907 |
); |
| 908 |
} |
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
public function testCompareMessages() { |
| 915 |
$status = Status::newGood(); |
| 916 |
$message1 = new Message( 'foo', [ 1, 2 ] ); |
| 917 |
$status->error( $message1 ); |
| 918 |
$message2 = new Message( 'foo', [ 1, 2 ] ); |
| 919 |
$status->error( $message2 ); |
| 920 |
$this->assertCount( 1, $status->errors ); |
| 921 |
} |
| 922 |
|
| 923 |
public function testDuplicateMerge() { |
| 924 |
$status1 = Status::newGood(); |
| 925 |
$status1->error( 'cat', 1, 2 ); |
| 926 |
$status1->warning( 'dog', 3, 4 ); |
| 927 |
$status2 = Status::newGood(); |
| 928 |
$status2->warning( 'cat', 1, 2 ); |
| 929 |
$status1->error( 'dog', 3, 4 ); |
| 930 |
$status2->warning( 'rabbit', 5, 6 ); |
| 931 |
$status1->merge( $status2 ); |
| 932 |
$this->assertArrayEquals( |
| 933 |
[ |
| 934 |
[ |
| 935 |
'type' => 'error', |
| 936 |
'message' => 'cat', |
| 937 |
'params' => [ 1, 2 ] |
| 938 |
], |
| 939 |
[ |
| 940 |
'type' => 'error', |
| 941 |
'message' => 'dog', |
| 942 |
'params' => [ 3, 4 ] |
| 943 |
], |
| 944 |
[ |
| 945 |
'type' => 'warning', |
| 946 |
'message' => 'rabbit', |
| 947 |
'params' => [ 5, 6 ] |
| 948 |
] |
| 949 |
], |
| 950 |
$status1->errors |
| 951 |
); |
| 952 |
} |
| 953 |
|
| 954 |
public function testNotDuplicateIfKeyDiffers() { |
| 955 |
$status = Status::newGood(); |
| 956 |
$status->error( 'foo', 1, 2 ); |
| 957 |
$status->error( 'bar', 1, 2 ); |
| 958 |
$this->assertArrayEquals( |
| 959 |
[ |
| 960 |
[ |
| 961 |
'type' => 'error', |
| 962 |
'message' => 'foo', |
| 963 |
'params' => [ 1, 2 ] |
| 964 |
], |
| 965 |
[ |
| 966 |
'type' => 'error', |
| 967 |
'message' => 'bar', |
| 968 |
'params' => [ 1, 2 ] |
| 969 |
] |
| 970 |
], |
| 971 |
$status->errors |
| 972 |
); |
| 973 |
} |
| 974 |
|
| 975 |
public function testNotDuplicateIfParamsDiffer() { |
| 976 |
$status = Status::newGood(); |
| 977 |
$status->error( 'foo', 1, 2 ); |
| 978 |
$status->error( 'foo', 3, 4 ); |
| 979 |
$this->assertArrayEquals( [ |
| 980 |
[ |
| 981 |
'type' => 'error', |
| 982 |
'message' => 'foo', |
| 983 |
'params' => [ 1, 2 ] |
| 984 |
], |
| 985 |
[ |
| 986 |
'type' => 'error', |
| 987 |
'message' => 'foo', |
| 988 |
'params' => [ 3, 4 ] |
| 989 |
] |
| 990 |
], $status->errors ); |
| 991 |
} |
| 992 |
|
| 993 |
public function testToString() { |
| 994 |
$loremIpsum = 'Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor' . |
| 995 |
' incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud ' . |
| 996 |
'exercitation ullamco laboris nisi ut aliquid ex ea commodi consequat. Quis aute iure ' . |
| 997 |
'reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.'; |
| 998 |
$abc = [ |
| 999 |
'x' => [ 'a', 'b', 'c' ], |
| 1000 |
'z' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFGHIJKLMNOPQRSTUVWXYZ ' . |
| 1001 |
'ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFGHIJKLMNOPQRSTUVWXYZ ' |
| 1002 |
]; |
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
$this->expectNotToPerformAssertions(); |
| 1007 |
(string)Status::newGood(); |
| 1008 |
(string)Status::newGood( new MessageValue( 'foo' ) ); |
| 1009 |
(string)Status::newFatal( 'foo' ); |
| 1010 |
(string)Status::newFatal( 'foo', $loremIpsum, $abc ); |
| 1011 |
(string)Status::newFatal( wfMessage( 'foo' ) ); |
| 1012 |
(string)( Status::newFatal( 'foo' )->fatal( 'bar' ) ); |
| 1013 |
|
| 1014 |
$status = Status::newGood(); |
| 1015 |
$status->warning( 'foo', $loremIpsum ); |
| 1016 |
$status->error( 'bar', $abc ); |
| 1017 |
(string)$status; |
| 1018 |
} |
| 1019 |
} |