| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Api\ApiMessage; |
| 4 |
use MediaWiki\Language\RawMessage; |
| 5 |
use MediaWiki\MainConfigNames; |
| 6 |
use MediaWiki\MediaWikiServices; |
| 7 |
use MediaWiki\Message\Message; |
| 8 |
use MediaWiki\Message\UserGroupMembershipParam; |
| 9 |
use MediaWiki\Page\PageReferenceValue; |
| 10 |
use MediaWiki\Title\Title; |
| 11 |
use MediaWiki\User\UserIdentityValue; |
| 12 |
use Wikimedia\Assert\ParameterTypeException; |
| 13 |
use Wikimedia\Bcp47Code\Bcp47CodeValue; |
| 14 |
use Wikimedia\Message\MessageSpecifier; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
class MessageTest extends MediaWikiLangTestCase { |
| 23 |
|
| 24 |
protected function setUp(): void { |
| 25 |
parent::setUp(); |
| 26 |
|
| 27 |
$this->overrideConfigValue( MainConfigNames::ForceUIMsgAsContentMsg, [] ); |
| 28 |
$this->setUserLang( 'en' ); |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public function testConstructor( $expectedLang, $key, $params, $language ) { |
| 35 |
$message = new Message( $key, $params, $language ); |
| 36 |
|
| 37 |
$this->assertSame( $key, $message->getKey() ); |
| 38 |
$this->assertSame( $params, $message->getParams() ); |
| 39 |
$this->assertSame( $expectedLang->getCode(), $message->getLanguage()->getCode() ); |
| 40 |
|
| 41 |
$messageSpecifier = $this->getMockForAbstractClass( MessageSpecifier::class ); |
| 42 |
$messageSpecifier->method( 'getKey' )->willReturn( $key ); |
| 43 |
$messageSpecifier->method( 'getParams' )->willReturn( $params ); |
| 44 |
$message = new Message( $messageSpecifier, [], $language ); |
| 45 |
|
| 46 |
$this->assertSame( $key, $message->getKey() ); |
| 47 |
$this->assertSame( $params, $message->getParams() ); |
| 48 |
$this->assertSame( $expectedLang->getCode(), $message->getLanguage()->getCode() ); |
| 49 |
} |
| 50 |
|
| 51 |
public static function provideConstructor() { |
| 52 |
$langDe = MediaWikiServices::getInstance()->getLanguageFactory()->getLanguage( 'de' ); |
| 53 |
$langEn = MediaWikiServices::getInstance()->getLanguageFactory()->getLanguage( 'en' ); |
| 54 |
|
| 55 |
return [ |
| 56 |
[ $langDe, 'foo', [], $langDe ], |
| 57 |
[ $langDe, 'foo', [ 'bar' ], $langDe ], |
| 58 |
[ $langEn, 'foo', [ 'bar' ], null ] |
| 59 |
]; |
| 60 |
} |
| 61 |
|
| 62 |
public static function provideConstructorParams() { |
| 63 |
return [ |
| 64 |
[ |
| 65 |
[], |
| 66 |
[], |
| 67 |
], |
| 68 |
[ |
| 69 |
[], |
| 70 |
[ [] ], |
| 71 |
], |
| 72 |
[ |
| 73 |
[ 'foo' ], |
| 74 |
[ 'foo' ], |
| 75 |
], |
| 76 |
[ |
| 77 |
[ 'foo', 'bar' ], |
| 78 |
[ 'foo', 'bar' ], |
| 79 |
], |
| 80 |
[ |
| 81 |
[ 'baz' ], |
| 82 |
[ [ 'baz' ] ], |
| 83 |
], |
| 84 |
[ |
| 85 |
[ 'baz', 'foo' ], |
| 86 |
[ [ 'baz', 'foo' ] ], |
| 87 |
], |
| 88 |
[ |
| 89 |
[ Message::rawParam( 'baz' ) ], |
| 90 |
[ Message::rawParam( 'baz' ) ], |
| 91 |
], |
| 92 |
[ |
| 93 |
[ Message::rawParam( 'baz' ), 'foo' ], |
| 94 |
[ Message::rawParam( 'baz' ), 'foo' ], |
| 95 |
], |
| 96 |
[ |
| 97 |
[ Message::rawParam( 'baz' ) ], |
| 98 |
[ [ Message::rawParam( 'baz' ) ] ], |
| 99 |
], |
| 100 |
[ |
| 101 |
[ Message::rawParam( 'baz' ), 'foo' ], |
| 102 |
[ [ Message::rawParam( 'baz' ), 'foo' ] ], |
| 103 |
], |
| 104 |
|
| 105 |
|
| 106 |
[ |
| 107 |
[ [ 'baz', 'foo' ], 'hhh' ], |
| 108 |
[ [ 'baz', 'foo' ], 'hhh' ], |
| 109 |
], |
| 110 |
[ |
| 111 |
[ [ 'baz', 'foo' ], 'hhh', [ 'ahahahahha' ] ], |
| 112 |
[ [ 'baz', 'foo' ], 'hhh', [ 'ahahahahha' ] ], |
| 113 |
], |
| 114 |
[ |
| 115 |
[ [ 'baz', 'foo' ], [ 'ahahahahha' ] ], |
| 116 |
[ [ 'baz', 'foo' ], [ 'ahahahahha' ] ], |
| 117 |
], |
| 118 |
[ |
| 119 |
[ [ 'baz' ], [ 'ahahahahha' ] ], |
| 120 |
[ [ 'baz' ], [ 'ahahahahha' ] ], |
| 121 |
], |
| 122 |
]; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public function testConstructorParams( $expected, $args ) { |
| 129 |
$msg = new Message( 'imasomething' ); |
| 130 |
|
| 131 |
$returned = $msg->params( ...$args ); |
| 132 |
|
| 133 |
$this->assertSame( $msg, $returned ); |
| 134 |
$this->assertEquals( $expected, $msg->getParams() ); |
| 135 |
} |
| 136 |
|
| 137 |
public static function provideConstructorLanguage() { |
| 138 |
return [ |
| 139 |
[ 'foo', [ 'bar' ], 'en' ], |
| 140 |
[ 'foo', [ 'bar' ], 'de' ] |
| 141 |
]; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public function testConstructorLanguage( $key, $params, $languageCode ) { |
| 148 |
$language = $this->getServiceContainer()->getLanguageFactory() |
| 149 |
->getLanguage( $languageCode ); |
| 150 |
$message = new Message( $key, $params, $language ); |
| 151 |
|
| 152 |
$this->assertEquals( $language, $message->getLanguage() ); |
| 153 |
} |
| 154 |
|
| 155 |
public static function provideKeys() { |
| 156 |
return [ |
| 157 |
'string' => [ |
| 158 |
'key' => 'mainpage', |
| 159 |
'expected' => [ 'mainpage' ], |
| 160 |
], |
| 161 |
'single' => [ |
| 162 |
'key' => [ 'mainpage' ], |
| 163 |
'expected' => [ 'mainpage' ], |
| 164 |
], |
| 165 |
'multi' => [ |
| 166 |
'key' => [ 'mainpage-foo', 'mainpage-bar', 'mainpage' ], |
| 167 |
'expected' => [ 'mainpage-foo', 'mainpage-bar', 'mainpage' ], |
| 168 |
], |
| 169 |
'empty' => [ |
| 170 |
'key' => [], |
| 171 |
'expected' => null, |
| 172 |
'exception' => InvalidArgumentException::class, |
| 173 |
], |
| 174 |
'null' => [ |
| 175 |
'key' => null, |
| 176 |
'expected' => null, |
| 177 |
'exception' => InvalidArgumentException::class, |
| 178 |
], |
| 179 |
'bad type' => [ |
| 180 |
'key' => 123, |
| 181 |
'expected' => null, |
| 182 |
'exception' => InvalidArgumentException::class, |
| 183 |
], |
| 184 |
]; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public function testKeys( $key, $expected, $exception = null ) { |
| 191 |
if ( $exception ) { |
| 192 |
$this->expectException( $exception ); |
| 193 |
} |
| 194 |
|
| 195 |
$msg = new Message( $key ); |
| 196 |
$this->assertContains( $msg->getKey(), $expected ); |
| 197 |
$this->assertSame( $expected, $msg->getKeysToTry() ); |
| 198 |
$this->assertSame( count( $expected ) > 1, $msg->isMultiKey() ); |
| 199 |
} |
| 200 |
|
| 201 |
public function testWfMessage() { |
| 202 |
$this->assertInstanceOf( Message::class, wfMessage( 'mainpage' ) ); |
| 203 |
$this->assertInstanceOf( Message::class, wfMessage( 'i-dont-exist-evar' ) ); |
| 204 |
} |
| 205 |
|
| 206 |
public function testNewFromKey() { |
| 207 |
$this->assertInstanceOf( Message::class, Message::newFromKey( 'mainpage' ) ); |
| 208 |
$this->assertInstanceOf( Message::class, Message::newFromKey( 'i-dont-exist-evar' ) ); |
| 209 |
} |
| 210 |
|
| 211 |
public function testWfMessageParams() { |
| 212 |
$this->assertSame( 'Return to $1.', wfMessage( 'returnto' )->text() ); |
| 213 |
$this->assertSame( 'Return to $1.', wfMessage( 'returnto', [] )->text() ); |
| 214 |
$this->assertSame( |
| 215 |
'Return to 1,024.', |
| 216 |
wfMessage( 'returnto', Message::numParam( 1024 ) )->text() |
| 217 |
); |
| 218 |
$this->assertSame( |
| 219 |
'Return to 1,024.', |
| 220 |
wfMessage( 'returnto', [ Message::numParam( 1024 ) ] )->text() |
| 221 |
); |
| 222 |
$this->assertSame( |
| 223 |
'You have foo (bar).', |
| 224 |
wfMessage( 'new-messages', 'foo', 'bar' )->text() |
| 225 |
); |
| 226 |
$this->assertSame( |
| 227 |
'You have foo (bar).', |
| 228 |
wfMessage( 'new-messages', [ 'foo', 'bar' ] )->text() |
| 229 |
); |
| 230 |
$this->assertSame( |
| 231 |
'You have 1,024 (bar).', |
| 232 |
wfMessage( |
| 233 |
'new-messages', |
| 234 |
Message::numParam( 1024 ), 'bar' |
| 235 |
)->text() |
| 236 |
); |
| 237 |
$this->assertSame( |
| 238 |
'You have foo (2,048).', |
| 239 |
wfMessage( |
| 240 |
'new-messages', |
| 241 |
'foo', Message::numParam( 2048 ) |
| 242 |
)->text() |
| 243 |
); |
| 244 |
$this->assertSame( |
| 245 |
'You have 1,024 (2,048).', |
| 246 |
wfMessage( |
| 247 |
'new-messages', |
| 248 |
[ Message::numParam( 1024 ), Message::numParam( 2048 ) ] |
| 249 |
)->text() |
| 250 |
); |
| 251 |
} |
| 252 |
|
| 253 |
public function testExists() { |
| 254 |
$this->assertTrue( wfMessage( 'mainpage' )->exists() ); |
| 255 |
$this->assertTrue( wfMessage( 'mainpage' )->params( [] )->exists() ); |
| 256 |
$this->assertTrue( wfMessage( 'mainpage' )->rawParams( 'foo', 123 )->exists() ); |
| 257 |
$this->assertFalse( wfMessage( 'i-dont-exist-evar' )->exists() ); |
| 258 |
$this->assertFalse( wfMessage( 'i-dont-exist-evar' )->params( [] )->exists() ); |
| 259 |
$this->assertFalse( wfMessage( 'i-dont-exist-evar' )->rawParams( 'foo', 123 )->exists() ); |
| 260 |
} |
| 261 |
|
| 262 |
public function testToStringKey() { |
| 263 |
$this->assertSame( 'Main Page', wfMessage( 'mainpage' )->text() ); |
| 264 |
$this->assertSame( '⧼i-dont-exist-evar⧽', wfMessage( 'i-dont-exist-evar' )->text() ); |
| 265 |
$this->assertSame( '⧼i<dont>exist-evar⧽', wfMessage( 'i<dont>exist-evar' )->text() ); |
| 266 |
$this->assertSame( '⧼i-dont-exist-evar⧽', wfMessage( 'i-dont-exist-evar' )->plain() ); |
| 267 |
$this->assertSame( '⧼i<dont>exist-evar⧽', wfMessage( 'i<dont>exist-evar' )->plain() ); |
| 268 |
$this->assertSame( '⧼i-dont-exist-evar⧽', wfMessage( 'i-dont-exist-evar' )->escaped() ); |
| 269 |
$this->assertSame( |
| 270 |
'⧼i<dont>exist-evar⧽', |
| 271 |
wfMessage( 'i<dont>exist-evar' )->escaped() |
| 272 |
); |
| 273 |
} |
| 274 |
|
| 275 |
public static function provideToString() { |
| 276 |
return [ |
| 277 |
|
| 278 |
[ 'mainpage', 'plain', 'Main Page', 'Main Page' ], |
| 279 |
[ 'i-dont-exist-evar', 'plain', '⧼i-dont-exist-evar⧽', '⧼i-dont-exist-evar⧽' ], |
| 280 |
[ 'i-dont-exist-evar', 'escaped', '⧼i-dont-exist-evar⧽', '⧼i-dont-exist-evar⧽' ], |
| 281 |
[ 'script>alert(1)</script', 'escaped', '⧼script>alert(1)</script⧽', |
| 282 |
'⧼script>alert(1)</script⧽' ], |
| 283 |
[ 'script>alert(1)</script', 'plain', '⧼script>alert(1)</script⧽', |
| 284 |
'⧼script>alert(1)</script⧽' ], |
| 285 |
[ "\u{0338}isolated combining char", 'escaped', '⧼̸isolated combining char⧽', '⧼̸isolated combining char⧽' ], |
| 286 |
]; |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
public function testToString( $key, $format, $expect, $expectImplicit ) { |
| 293 |
$msg = new Message( $key ); |
| 294 |
$this->assertSame( $expect, $msg->$format() ); |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
$this->assertSame( $expectImplicit, $msg->__toString(), '__toString is not affected by format call' ); |
| 299 |
} |
| 300 |
|
| 301 |
public static function provideToString_raw() { |
| 302 |
return [ |
| 303 |
[ '<span>foo</span>', 'parse', '<span>foo</span>', '<span>foo</span>' ], |
| 304 |
[ '<span>foo</span>', 'escaped', '<span>foo</span>', |
| 305 |
'<span>foo</span>' ], |
| 306 |
[ '<span>foo</span>', 'plain', '<span>foo</span>', '<span>foo</span>' ], |
| 307 |
[ '<script>alert(1)</script>', 'parse', '<script>alert(1)</script>', |
| 308 |
'<script>alert(1)</script>' ], |
| 309 |
[ '<script>alert(1)</script>', 'escaped', '<script>alert(1)</script>', |
| 310 |
'<script>alert(1)</script>' ], |
| 311 |
[ '<script>alert(1)</script>', 'plain', '<script>alert(1)</script>', |
| 312 |
'<script>alert(1)</script>' ], |
| 313 |
[ "\u{0338}isolated combining char", 'escaped', '̸isolated combining char', '̸isolated combining char' ], |
| 314 |
]; |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
public function testToString_raw( $message, $format, $expect, $expectImplicit ) { |
| 321 |
|
| 322 |
$msg = $this->getMockBuilder( Message::class )->onlyMethods( [ 'fetchMessage' ] ) |
| 323 |
->disableOriginalConstructor() |
| 324 |
->getMock(); |
| 325 |
$msg->method( 'fetchMessage' )->willReturn( $message ); |
| 326 |
|
| 327 |
|
| 328 |
$this->assertSame( $expect, $msg->$format() ); |
| 329 |
|
| 330 |
$this->assertSame( $expectImplicit, $msg->__toString() ); |
| 331 |
} |
| 332 |
|
| 333 |
public function testInLanguage() { |
| 334 |
$this->assertSame( 'Main Page', wfMessage( 'mainpage' )->inLanguage( 'en' )->text() ); |
| 335 |
$this->assertSame( 'Главна страна', |
| 336 |
wfMessage( 'mainpage' )->inLanguage( 'sr-ec' )->text() ); |
| 337 |
|
| 338 |
|
| 339 |
$msg = wfMessage( 'mainpage' ); |
| 340 |
$this->assertSame( 'Main Page', $msg->inLanguage( 'en' )->text() ); |
| 341 |
$this->assertSame( |
| 342 |
'Главна страна', |
| 343 |
$msg->inLanguage( 'sr-ec' )->text() |
| 344 |
); |
| 345 |
} |
| 346 |
|
| 347 |
public function testInLanguageBcp47() { |
| 348 |
$en = new Bcp47CodeValue( 'en' ); |
| 349 |
$sr = new Bcp47CodeValue( 'sr-Cyrl' ); |
| 350 |
$this->assertSame( 'Main Page', wfMessage( 'mainpage' )->inLanguage( $en )->text() ); |
| 351 |
$this->assertSame( 'Главна страна', |
| 352 |
wfMessage( 'mainpage' )->inLanguage( $sr )->text() ); |
| 353 |
|
| 354 |
|
| 355 |
$msg = wfMessage( 'mainpage' ); |
| 356 |
$this->assertSame( 'Main Page', $msg->inLanguage( $en )->text() ); |
| 357 |
$this->assertSame( |
| 358 |
'Главна страна', |
| 359 |
$msg->inLanguage( $sr )->text() |
| 360 |
); |
| 361 |
} |
| 362 |
|
| 363 |
public function testRawParams() { |
| 364 |
$this->assertSame( |
| 365 |
'(Заглавная страница)', |
| 366 |
wfMessage( 'parentheses', 'Заглавная страница' )->plain() |
| 367 |
); |
| 368 |
$this->assertSame( |
| 369 |
'(Заглавная страница $1)', |
| 370 |
wfMessage( 'parentheses', 'Заглавная страница $1' )->plain() |
| 371 |
); |
| 372 |
$this->assertSame( |
| 373 |
'(Заглавная страница)', |
| 374 |
wfMessage( 'parentheses' )->rawParams( 'Заглавная страница' )->plain() |
| 375 |
); |
| 376 |
$this->assertSame( |
| 377 |
'(Заглавная страница $1)', |
| 378 |
wfMessage( 'parentheses' )->rawParams( 'Заглавная страница $1' )->plain() |
| 379 |
); |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
public function testRawMessage() { |
| 386 |
$msg = new RawMessage( 'example &' ); |
| 387 |
$this->assertSame( 'example &', $msg->plain() ); |
| 388 |
$this->assertSame( 'example &', $msg->escaped() ); |
| 389 |
} |
| 390 |
|
| 391 |
public static function provideRawMessage() { |
| 392 |
yield 'No params' => [ |
| 393 |
new RawMessage( 'Foo Bar' ), |
| 394 |
'Foo Bar', |
| 395 |
]; |
| 396 |
yield 'Single param' => [ |
| 397 |
new RawMessage( '$1', [ 'Foo Bar' ] ), |
| 398 |
'Foo Bar', |
| 399 |
]; |
| 400 |
yield 'Multiple params' => [ |
| 401 |
new RawMessage( '$2 and $1', [ 'One', 'Two' ] ), |
| 402 |
'Two and One', |
| 403 |
]; |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
public function testRawMessageParams( RawMessage $m, string $param ) { |
| 411 |
$this->assertEquals( [ $param ], $m->getParams() ); |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
public function testRawMessageDisassembleSpecifier( RawMessage $m, string $text ) { |
| 419 |
|
| 420 |
$this->assertEquals( $text, $m->text(), 'output from RawMessage itself' ); |
| 421 |
|
| 422 |
$msg = wfMessage( $m ); |
| 423 |
$this->assertEquals( $text, $msg->text(), 'output from RawMessage used as MessageSpecifier' ); |
| 424 |
|
| 425 |
|
| 426 |
$msg2 = wfMessage( $m->getKey(), ...$m->getParams() ); |
| 427 |
$this->assertEquals( $text, $msg2->text(), 'output from RawMessage disassembled' ); |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
public function testRawHtmlInMsg() { |
| 435 |
$this->overrideConfigValue( MainConfigNames::RawHtml, true ); |
| 436 |
|
| 437 |
$msg = new RawMessage( '<html><script>alert("xss")</script></html>' ); |
| 438 |
$txt = '<span class="error"><html> tags cannot be' . |
| 439 |
' used outside of normal pages.</span>'; |
| 440 |
$this->assertSame( $txt, $msg->parse() ); |
| 441 |
} |
| 442 |
|
| 443 |
public function testReplaceManyParams() { |
| 444 |
$msg = new RawMessage( '$1$2$3$4$5$6$7$8$9$10$11$12' ); |
| 445 |
|
| 446 |
$params = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k' ]; |
| 447 |
$this->assertSame( |
| 448 |
'abcdefghijka2', |
| 449 |
$msg->params( $params )->plain(), |
| 450 |
'Params > 9 are replaced correctly' |
| 451 |
); |
| 452 |
} |
| 453 |
|
| 454 |
public function testNumParams() { |
| 455 |
$lang = $this->getServiceContainer()->getLanguageFactory()->getLanguage( 'en' ); |
| 456 |
$msg = new RawMessage( '$1' ); |
| 457 |
|
| 458 |
$this->assertSame( |
| 459 |
$lang->formatNum( 123456.789 ), |
| 460 |
$msg->inLanguage( $lang )->numParams( 123456.789 )->plain(), |
| 461 |
'numParams is handled correctly' |
| 462 |
); |
| 463 |
} |
| 464 |
|
| 465 |
public function testDurationParams() { |
| 466 |
$lang = $this->getServiceContainer()->getLanguageFactory()->getLanguage( 'en' ); |
| 467 |
$msg = new RawMessage( '$1' ); |
| 468 |
|
| 469 |
$this->assertSame( |
| 470 |
$lang->formatDuration( 1234 ), |
| 471 |
$msg->inLanguage( $lang )->durationParams( 1234 )->plain(), |
| 472 |
'durationParams is handled correctly' |
| 473 |
); |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
public function testExpiryParams() { |
| 480 |
$lang = $this->getServiceContainer()->getLanguageFactory()->getLanguage( 'en' ); |
| 481 |
$msg = new RawMessage( '$1' ); |
| 482 |
|
| 483 |
$ts = wfTimestampNow(); |
| 484 |
$this->assertSame( |
| 485 |
$lang->formatExpiry( $ts ), |
| 486 |
$msg->inLanguage( $lang )->expiryParams( $ts )->plain(), |
| 487 |
'expiryParams is handled correctly' |
| 488 |
); |
| 489 |
} |
| 490 |
|
| 491 |
public function testDateTimeParams() { |
| 492 |
$lang = $this->getServiceContainer()->getLanguageFactory()->getLanguage( 'en' ); |
| 493 |
$msg = new RawMessage( '$1' ); |
| 494 |
|
| 495 |
$ts = wfTimestampNow(); |
| 496 |
$this->assertSame( |
| 497 |
$lang->timeanddate( $ts ), |
| 498 |
$msg->inLanguage( $lang )->dateTimeParams( $ts )->plain(), |
| 499 |
'dateTime is handled correctly' |
| 500 |
); |
| 501 |
} |
| 502 |
|
| 503 |
public function testDateParams() { |
| 504 |
$lang = $this->getServiceContainer()->getLanguageFactory()->getLanguage( 'en' ); |
| 505 |
$msg = new RawMessage( '$1' ); |
| 506 |
|
| 507 |
$ts = wfTimestampNow(); |
| 508 |
$this->assertSame( |
| 509 |
$lang->date( $ts ), |
| 510 |
$msg->inLanguage( $lang )->dateParams( $ts )->plain(), |
| 511 |
'date is handled correctly' |
| 512 |
); |
| 513 |
} |
| 514 |
|
| 515 |
public function testTimeParams() { |
| 516 |
$lang = $this->getServiceContainer()->getLanguageFactory()->getLanguage( 'en' ); |
| 517 |
$msg = new RawMessage( '$1' ); |
| 518 |
|
| 519 |
$ts = wfTimestampNow(); |
| 520 |
$this->assertSame( |
| 521 |
$lang->time( $ts ), |
| 522 |
$msg->inLanguage( $lang )->timeParams( $ts )->plain(), |
| 523 |
'time is handled correctly' |
| 524 |
); |
| 525 |
} |
| 526 |
|
| 527 |
public function testUserGroupParams() { |
| 528 |
$lang = $this->getServiceContainer()->getLanguageFactory()->getLanguage( 'qqx' ); |
| 529 |
$msg = new RawMessage( '$1' ); |
| 530 |
$this->setUserLang( $lang ); |
| 531 |
$this->assertSame( |
| 532 |
'(group-bot)', |
| 533 |
$msg->userGroupParams( 'bot' )->plain(), |
| 534 |
'user group is handled correctly' |
| 535 |
); |
| 536 |
} |
| 537 |
|
| 538 |
public function testUserGroupMemberParams() { |
| 539 |
$this->expectDeprecationAndContinue( '/UserGroupMembershipParam/' ); |
| 540 |
$this->expectDeprecationAndContinue( '/objectParams/' ); |
| 541 |
$lang = $this->getServiceContainer()->getLanguageFactory()->getLanguage( 'qqx' ); |
| 542 |
$msg = new RawMessage( '$1' ); |
| 543 |
$this->setUserLang( $lang ); |
| 544 |
$this->assertSame( |
| 545 |
'(group-bot-member: user)', |
| 546 |
$msg->objectParams( |
| 547 |
new UserGroupMembershipParam( 'bot', new UserIdentityValue( 1, 'user' ) ) |
| 548 |
)->plain(), |
| 549 |
'user group member is handled correctly' |
| 550 |
); |
| 551 |
} |
| 552 |
|
| 553 |
public function testTimeperiodParams() { |
| 554 |
$lang = $this->getServiceContainer()->getLanguageFactory()->getLanguage( 'en' ); |
| 555 |
$msg = new RawMessage( '$1' ); |
| 556 |
|
| 557 |
$this->assertSame( |
| 558 |
$lang->formatTimePeriod( 1234 ), |
| 559 |
$msg->inLanguage( $lang )->timeperiodParams( 1234 )->plain(), |
| 560 |
'timeperiodParams is handled correctly' |
| 561 |
); |
| 562 |
} |
| 563 |
|
| 564 |
public function testSizeParams() { |
| 565 |
$lang = $this->getServiceContainer()->getLanguageFactory()->getLanguage( 'en' ); |
| 566 |
$msg = new RawMessage( '$1' ); |
| 567 |
|
| 568 |
$this->assertSame( |
| 569 |
$lang->formatSize( 123456 ), |
| 570 |
$msg->inLanguage( $lang )->sizeParams( 123456 )->plain(), |
| 571 |
'sizeParams is handled correctly' |
| 572 |
); |
| 573 |
} |
| 574 |
|
| 575 |
public function testBitrateParams() { |
| 576 |
$lang = $this->getServiceContainer()->getLanguageFactory()->getLanguage( 'en' ); |
| 577 |
$msg = new RawMessage( '$1' ); |
| 578 |
|
| 579 |
$this->assertSame( |
| 580 |
$lang->formatBitrate( 123456 ), |
| 581 |
$msg->inLanguage( $lang )->bitrateParams( 123456 )->plain(), |
| 582 |
'bitrateParams is handled correctly' |
| 583 |
); |
| 584 |
} |
| 585 |
|
| 586 |
public static function providePlaintextParams() { |
| 587 |
return [ |
| 588 |
[ |
| 589 |
"one $2 <div>\u{0338}foo</div> [[Bar]] {{Baz}} <", |
| 590 |
'plain', |
| 591 |
], |
| 592 |
|
| 593 |
[ |
| 594 |
|
| 595 |
"one $2 <div>\u{0338}foo</div> [[Bar]] {{Baz}} <", |
| 596 |
|
| 597 |
'text', |
| 598 |
], |
| 599 |
[ |
| 600 |
'one $2 <div≯foo</div> [[Bar]] {{Baz}} &lt;', |
| 601 |
'escaped', |
| 602 |
], |
| 603 |
|
| 604 |
[ |
| 605 |
'one $2 <div≯foo</div> [[Bar]] {{Baz}} &lt;', |
| 606 |
'parse', |
| 607 |
], |
| 608 |
|
| 609 |
[ |
| 610 |
"<p>one $2 <div≯foo</div> [[Bar]] {{Baz}} &lt;\n</p>", |
| 611 |
'parseAsBlock', |
| 612 |
], |
| 613 |
]; |
| 614 |
} |
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
public function testPlaintextParams( $expect, $format ) { |
| 620 |
$msg = new RawMessage( '$1 $2' ); |
| 621 |
$params = [ |
| 622 |
'one $2', |
| 623 |
"<div>\u{0338}foo</div> [[Bar]] {{Baz}} <", |
| 624 |
]; |
| 625 |
$this->assertSame( |
| 626 |
$expect, |
| 627 |
$msg->inLanguage( 'en' )->plaintextParams( $params )->$format(), |
| 628 |
"Fail formatting for $format" |
| 629 |
); |
| 630 |
} |
| 631 |
|
| 632 |
public static function provideListParam() { |
| 633 |
$lang = MediaWikiServices::getInstance()->getLanguageFactory()->getLanguage( 'de' ); |
| 634 |
$msg1 = new Message( 'mainpage', [], $lang ); |
| 635 |
$msg2 = new RawMessage( "''link''", [], $lang ); |
| 636 |
|
| 637 |
return [ |
| 638 |
'Simple comma list' => [ |
| 639 |
[ 'a', 'b', 'c' ], |
| 640 |
'comma', |
| 641 |
'text', |
| 642 |
'a, b, c' |
| 643 |
], |
| 644 |
|
| 645 |
'Simple semicolon list' => [ |
| 646 |
[ 'a', 'b', 'c' ], |
| 647 |
'semicolon', |
| 648 |
'text', |
| 649 |
'a; b; c' |
| 650 |
], |
| 651 |
|
| 652 |
'Simple pipe list' => [ |
| 653 |
[ 'a', 'b', 'c' ], |
| 654 |
'pipe', |
| 655 |
'text', |
| 656 |
'a | b | c' |
| 657 |
], |
| 658 |
|
| 659 |
'Simple text list' => [ |
| 660 |
[ 'a', 'b', 'c' ], |
| 661 |
'text', |
| 662 |
'text', |
| 663 |
'a, b and c' |
| 664 |
], |
| 665 |
|
| 666 |
'Empty list' => [ |
| 667 |
[], |
| 668 |
'comma', |
| 669 |
'text', |
| 670 |
'' |
| 671 |
], |
| 672 |
|
| 673 |
'List with all "before" params, ->text()' => [ |
| 674 |
[ "''link''", Message::numParam( 12345678 ) ], |
| 675 |
'semicolon', |
| 676 |
'text', |
| 677 |
'\'\'link\'\'; 12,345,678' |
| 678 |
], |
| 679 |
|
| 680 |
'List with all "before" params, ->parse()' => [ |
| 681 |
[ "''link''", Message::numParam( 12345678 ) ], |
| 682 |
'semicolon', |
| 683 |
'parse', |
| 684 |
'<i>link</i>; 12,345,678' |
| 685 |
], |
| 686 |
|
| 687 |
'List with all "after" params, ->text()' => [ |
| 688 |
[ $msg1, $msg2, Message::rawParam( '[[foo]]' ) ], |
| 689 |
'semicolon', |
| 690 |
'text', |
| 691 |
'Main Page; \'\'link\'\'; [[foo]]' |
| 692 |
], |
| 693 |
|
| 694 |
'List with all "after" params, ->parse()' => [ |
| 695 |
[ $msg1, $msg2, Message::rawParam( '[[foo]]' ) ], |
| 696 |
'semicolon', |
| 697 |
'parse', |
| 698 |
'Main Page; <i>link</i>; [[foo]]' |
| 699 |
], |
| 700 |
|
| 701 |
'List with both "before" and "after" params, ->text()' => [ |
| 702 |
[ $msg1, $msg2, Message::rawParam( '[[foo]]' ), "''link''", Message::numParam( 12345678 ) ], |
| 703 |
'semicolon', |
| 704 |
'text', |
| 705 |
'Main Page; \'\'link\'\'; [[foo]]; \'\'link\'\'; 12,345,678' |
| 706 |
], |
| 707 |
|
| 708 |
'List with both "before" and "after" params, ->parse()' => [ |
| 709 |
[ $msg1, $msg2, Message::rawParam( '[[foo]]' ), "''link''", Message::numParam( 12345678 ) ], |
| 710 |
'semicolon', |
| 711 |
'parse', |
| 712 |
'Main Page; <i>link</i>; [[foo]]; <i>link</i>; 12,345,678' |
| 713 |
], |
| 714 |
]; |
| 715 |
} |
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
public function testListParam( $list, $type, $format, $expect ) { |
| 721 |
$msg = new RawMessage( '$1' ); |
| 722 |
$msg->params( [ Message::listParam( $list, $type ) ] ); |
| 723 |
$this->assertEquals( |
| 724 |
$expect, |
| 725 |
$msg->inLanguage( 'en' )->$format() |
| 726 |
); |
| 727 |
} |
| 728 |
|
| 729 |
public function testMessageAsParam() { |
| 730 |
$msg = new Message( 'returnto', [ |
| 731 |
new Message( 'apihelp-link', [ |
| 732 |
'foo', new Message( 'mainpage', [], |
| 733 |
$this->getServiceContainer()->getLanguageFactory()->getLanguage( 'en' ) ) |
| 734 |
], $this->getServiceContainer()->getLanguageFactory()->getLanguage( 'de' ) ) |
| 735 |
], $this->getServiceContainer()->getLanguageFactory()->getLanguage( 'es' ) ); |
| 736 |
|
| 737 |
$this->assertEquals( |
| 738 |
'Volver a [[Special:ApiHelp/foo|Página principal]].', |
| 739 |
$msg->text(), |
| 740 |
'Process with ->text()' |
| 741 |
); |
| 742 |
$this->assertEquals( |
| 743 |
'<p>Volver a <a href="/wiki/Special:ApiHelp/foo" title="Special:ApiHelp/foo">Página ' |
| 744 |
. "principal</a>.\n</p>", |
| 745 |
$msg->parseAsBlock(), |
| 746 |
'Process with ->parseAsBlock()' |
| 747 |
); |
| 748 |
} |
| 749 |
|
| 750 |
public static function provideParser() { |
| 751 |
return [ |
| 752 |
[ |
| 753 |
"''&'' <x><!-- x -->", |
| 754 |
'plain', |
| 755 |
], |
| 756 |
|
| 757 |
[ |
| 758 |
"''&'' <x><!-- x -->", |
| 759 |
'text', |
| 760 |
], |
| 761 |
[ |
| 762 |
'<i>&</i> <x>', |
| 763 |
'parse', |
| 764 |
], |
| 765 |
|
| 766 |
[ |
| 767 |
"<p><i>&</i> <x>\n</p>", |
| 768 |
'parseAsBlock', |
| 769 |
], |
| 770 |
]; |
| 771 |
} |
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
public function testParser( $expect, $format ) { |
| 777 |
$msg = new RawMessage( "''&'' <x><!-- x -->" ); |
| 778 |
$this->assertSame( |
| 779 |
$expect, |
| 780 |
$msg->inLanguage( 'en' )->$format() |
| 781 |
); |
| 782 |
} |
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
public function testQqxPlaceholders() { |
| 788 |
$this->assertSame( |
| 789 |
'(test)', |
| 790 |
wfMessage( 'test' )->inLanguage( 'qqx' )->text() |
| 791 |
); |
| 792 |
$this->assertSame( |
| 793 |
'(test: a, b)', |
| 794 |
wfMessage( 'test' )->params( 'a', 'b' )->inLanguage( 'qqx' )->text() |
| 795 |
); |
| 796 |
$this->assertSame( |
| 797 |
'(test / other-test)', |
| 798 |
wfMessageFallback( 'test', 'other-test' )->inLanguage( 'qqx' )->text() |
| 799 |
); |
| 800 |
$this->assertSame( |
| 801 |
'(test / other-test: a, b)', |
| 802 |
wfMessageFallback( 'test', 'other-test' )->params( 'a', 'b' )->inLanguage( 'qqx' )->text() |
| 803 |
); |
| 804 |
} |
| 805 |
|
| 806 |
public function testInContentLanguage() { |
| 807 |
$this->setUserLang( 'fr' ); |
| 808 |
|
| 809 |
|
| 810 |
$msg = wfMessage( 'mainpage' ); |
| 811 |
$this->assertSame( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" ); |
| 812 |
$this->assertSame( 'Main Page', $msg->inContentLanguage()->plain(), "inContentLanguage()" ); |
| 813 |
$this->assertSame( 'Accueil', $msg->inLanguage( 'fr' )->plain(), "inLanguage( 'fr' )" ); |
| 814 |
} |
| 815 |
|
| 816 |
public function testInContentLanguageOverride() { |
| 817 |
$this->overrideConfigValue( MainConfigNames::ForceUIMsgAsContentMsg, [ 'mainpage' ] ); |
| 818 |
$this->setUserLang( 'fr' ); |
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
$msg = wfMessage( 'mainpage' ); |
| 823 |
$this->assertSame( |
| 824 |
'Accueil', |
| 825 |
$msg->inContentLanguage()->plain(), |
| 826 |
'inContentLanguage() with ForceUIMsg override enabled' |
| 827 |
); |
| 828 |
$this->assertSame( 'Main Page', $msg->inLanguage( 'en' )->plain(), "inLanguage( 'en' )" ); |
| 829 |
$this->assertSame( |
| 830 |
'Main Page', |
| 831 |
$msg->inContentLanguage()->plain(), |
| 832 |
'inContentLanguage() with ForceUIMsg override enabled' |
| 833 |
); |
| 834 |
$this->assertSame( 'Hauptseite', $msg->inLanguage( 'de' )->plain(), "inLanguage( 'de' )" ); |
| 835 |
} |
| 836 |
|
| 837 |
public function testInLanguageThrows() { |
| 838 |
$this->expectException( ParameterTypeException::class ); |
| 839 |
wfMessage( 'foo' )->inLanguage( 123 ); |
| 840 |
} |
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
public function testSerialization( $msgCallback, $serialized, $parsed ) { |
| 846 |
$msg = $msgCallback(); |
| 847 |
$this->assertSame( $serialized, serialize( $msg ) ); |
| 848 |
$this->assertSame( $parsed, $msg->parse() ); |
| 849 |
} |
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
public function testUnserialization( $msgCallback, $serialized, $parsed ) { |
| 856 |
|
| 857 |
|
| 858 |
$msg = $msgCallback(); |
| 859 |
$this->assertEquals( $msg, unserialize( $serialized ) ); |
| 860 |
$this->assertSame( $parsed, unserialize( $serialized )->parse() ); |
| 861 |
} |
| 862 |
|
| 863 |
public function provideSerializationRoundtrip() { |
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
yield "Serializing raw parameters" => [ |
| 869 |
fn () => ( new Message( 'parentheses' ) )->rawParams( '<a>foo</a>' ), |
| 870 |
'O:25:"MediaWiki\Message\Message":7:{s:9:"interface";b:1;s:8:"language";N;s:3:"key";s:11:"parentheses";s:9:"keysToTry";a:1:{i:0;s:11:"parentheses";}s:10:"parameters";a:1:{i:0;O:29:"Wikimedia\Message\ScalarParam":2:{s:7:"' . chr( 0 ) . '*' . chr( 0 ) . 'type";s:3:"raw";s:8:"' . chr( 0 ) . '*' . chr( 0 ) . 'value";s:10:"<a>foo</a>";}}s:11:"useDatabase";b:1;s:10:"titlevalue";N;}', |
| 871 |
'(<a>foo</a>)', |
| 872 |
]; |
| 873 |
|
| 874 |
yield "Serializing message with a context page" => [ |
| 875 |
fn () => ( new Message( 'rawmessage', [ '{{PAGENAME}}' ] ) )->page( PageReferenceValue::localReference( NS_MAIN, 'Testing' ) ), |
| 876 |
'O:25:"MediaWiki\Message\Message":7:{s:9:"interface";b:1;s:8:"language";N;s:3:"key";s:10:"rawmessage";s:9:"keysToTry";a:1:{i:0;s:10:"rawmessage";}s:10:"parameters";a:1:{i:0;s:12:"{{PAGENAME}}";}s:11:"useDatabase";b:1;s:10:"titlevalue";a:2:{i:0;i:0;i:1;s:7:"Testing";}}', |
| 877 |
'Testing', |
| 878 |
]; |
| 879 |
|
| 880 |
yield "Serializing language" => [ |
| 881 |
fn () => ( new Message( 'mainpage' ) )->inLanguage( 'de' ), |
| 882 |
'O:25:"MediaWiki\Message\Message":7:{s:9:"interface";b:0;s:8:"language";s:2:"de";s:3:"key";s:8:"mainpage";s:9:"keysToTry";a:1:{i:0;s:8:"mainpage";}s:10:"parameters";a:0:{}s:11:"useDatabase";b:1;s:10:"titlevalue";N;}', |
| 883 |
'Hauptseite', |
| 884 |
]; |
| 885 |
} |
| 886 |
|
| 887 |
public function provideSerializationLegacy() { |
| 888 |
|
| 889 |
|
| 890 |
yield "MW 1.42: Magic arrays instead of MessageParam objects" => [ |
| 891 |
fn () => ( new Message( 'parentheses' ) )->rawParams( '<a>foo</a>' ), |
| 892 |
'O:25:"MediaWiki\Message\Message":7:{s:9:"interface";b:1;s:8:"language";N;s:3:"key";s:11:"parentheses";s:9:"keysToTry";a:1:{i:0;s:11:"parentheses";}s:10:"parameters";a:1:{i:0;a:1:{s:3:"raw";s:10:"<a>foo</a>";}}s:11:"useDatabase";b:1;s:10:"titlevalue";N;}', |
| 893 |
'(<a>foo</a>)', |
| 894 |
]; |
| 895 |
|
| 896 |
yield "MW 1.41: Un-namespaced class" => [ |
| 897 |
fn () => new Message( 'mainpage' ), |
| 898 |
'O:7:"Message":7:{s:9:"interface";b:1;s:8:"language";N;s:3:"key";s:8:"mainpage";s:9:"keysToTry";a:1:{i:0;s:8:"mainpage";}s:10:"parameters";a:0:{}s:11:"useDatabase";b:1;s:10:"titlevalue";N;}', |
| 899 |
'Main Page', |
| 900 |
]; |
| 901 |
|
| 902 |
yield "MW 1.34: 'titlestr' instead of 'titlevalue'" => [ |
| 903 |
fn () => ( new Message( 'rawmessage', [ '{{PAGENAME}}' ] ) )->title( Title::newFromText( 'Testing' ) ), |
| 904 |
'C:7:"Message":242:{a:8:{s:9:"interface";b:1;s:8:"language";b:0;s:3:"key";s:10:"rawmessage";s:9:"keysToTry";a:1:{i:0;s:10:"rawmessage";}s:10:"parameters";a:1:{i:0;s:12:"{{PAGENAME}}";}s:6:"format";s:5:"parse";s:11:"useDatabase";b:1;s:8:"titlestr";s:7:"Testing";}}', |
| 905 |
'Testing', |
| 906 |
]; |
| 907 |
} |
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
public function testNewFromSpecifier( $value, $expectedText ) { |
| 913 |
$message = Message::newFromSpecifier( $value ); |
| 914 |
$this->assertInstanceOf( Message::class, $message ); |
| 915 |
if ( $value instanceof Message ) { |
| 916 |
$this->assertInstanceOf( get_class( $value ), $message ); |
| 917 |
$this->assertEquals( $value, $message ); |
| 918 |
} |
| 919 |
$this->assertSame( $expectedText, $message->text() ); |
| 920 |
} |
| 921 |
|
| 922 |
public function provideNewFromSpecifier() { |
| 923 |
$messageSpecifier = $this->getMockForAbstractClass( MessageSpecifier::class ); |
| 924 |
$messageSpecifier->method( 'getKey' )->willReturn( 'mainpage' ); |
| 925 |
$messageSpecifier->method( 'getParams' )->willReturn( [] ); |
| 926 |
|
| 927 |
return [ |
| 928 |
'string' => [ 'mainpage', 'Main Page' ], |
| 929 |
'array' => [ [ 'new-messages', 'foo', 'bar' ], 'You have foo (bar).' ], |
| 930 |
'Message' => [ new Message( 'new-messages', [ 'foo', 'bar' ] ), 'You have foo (bar).' ], |
| 931 |
'RawMessage' => [ new RawMessage( 'foo ($1)', [ 'bar' ] ), 'foo (bar)' ], |
| 932 |
'ApiMessage' => [ new ApiMessage( [ 'mainpage' ], 'code', [ 'data' ] ), 'Main Page' ], |
| 933 |
'MessageSpecifier' => [ $messageSpecifier, 'Main Page' ], |
| 934 |
'nested RawMessage' => [ [ new RawMessage( 'foo ($1)', [ 'bar' ] ) ], 'foo (bar)' ], |
| 935 |
]; |
| 936 |
} |
| 937 |
} |