3 use Wikimedia\TestingAccessWrapper
;
5 class LanguageTest
extends LanguageClassesTestCase
{
6 use LanguageNameUtilsTestTrait
;
8 /** @var array Copy of $wgHooks from before we unset LanguageGetTranslatedLanguageNames */
11 public function setUp() {
16 // Don't allow installed hooks to run, except if a test restores them via origHooks (needed
17 // for testIsKnownLanguageTag_cldr)
18 $this->origHooks
= $wgHooks;
20 unset( $newHooks['LanguageGetTranslatedLanguageNames'] );
21 $this->setMwGlobals( 'wgHooks', $newHooks );
25 * @covers Language::convertDoubleWidth
26 * @covers Language::normalizeForSearch
28 public function testLanguageConvertDoubleWidthToSingleWidth() {
30 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
31 $this->getLang()->normalizeForSearch(
32 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
34 'convertDoubleWidth() with the full alphabet and digits'
39 * @dataProvider provideFormattableTimes
40 * @covers Language::formatTimePeriod
42 public function testFormatTimePeriod( $seconds, $format, $expected, $desc ) {
43 $this->assertEquals( $expected, $this->getLang()->formatTimePeriod( $seconds, $format ), $desc );
46 public static function provideFormattableTimes() {
52 'formatTimePeriod() rounding (<10s)'
56 [ 'noabbrevs' => true ],
58 'formatTimePeriod() rounding (<10s)'
64 'formatTimePeriod() rounding (<10s)'
68 [ 'noabbrevs' => true ],
70 'formatTimePeriod() rounding (<10s)'
76 'formatTimePeriod() rounding (<60s)'
80 [ 'noabbrevs' => true ],
82 'formatTimePeriod() rounding (<60s)'
88 'formatTimePeriod() rounding (<1h)'
92 [ 'noabbrevs' => true ],
93 '2 minutes 0 seconds',
94 'formatTimePeriod() rounding (<1h)'
100 'formatTimePeriod() rounding (<1h)'
104 [ 'noabbrevs' => true ],
105 '1 hour 0 minutes 0 seconds',
106 'formatTimePeriod() rounding (<1h)'
112 'formatTimePeriod() rounding (>=1h)'
116 [ 'noabbrevs' => true ],
117 '2 hours 0 minutes 0 seconds',
118 'formatTimePeriod() rounding (>=1h)'
124 'formatTimePeriod() rounding (>=1h), avoidseconds'
128 [ 'avoid' => 'avoidseconds', 'noabbrevs' => true ],
130 'formatTimePeriod() rounding (>=1h), avoidseconds'
136 'formatTimePeriod() rounding (>=1h), avoidminutes'
140 [ 'avoid' => 'avoidminutes', 'noabbrevs' => true ],
142 'formatTimePeriod() rounding (>=1h), avoidminutes'
148 'formatTimePeriod() rounding (=48h), avoidseconds'
152 [ 'avoid' => 'avoidseconds', 'noabbrevs' => true ],
153 '48 hours 0 minutes',
154 'formatTimePeriod() rounding (=48h), avoidseconds'
160 'formatTimePeriod() rounding (>48h), avoidhours'
164 [ 'avoid' => 'avoidhours', 'noabbrevs' => true ],
166 'formatTimePeriod() rounding (>48h), avoidhours'
172 'formatTimePeriod() rounding (>48h), avoidminutes'
176 [ 'avoid' => 'avoidminutes', 'noabbrevs' => true ],
178 'formatTimePeriod() rounding (>48h), avoidminutes'
184 'formatTimePeriod() rounding (>48h), avoidseconds'
188 [ 'avoid' => 'avoidseconds', 'noabbrevs' => true ],
189 '2 days 1 hour 0 minutes',
190 'formatTimePeriod() rounding (>48h), avoidseconds'
196 'formatTimePeriod() rounding (>48h), avoidminutes'
200 [ 'avoid' => 'avoidminutes', 'noabbrevs' => true ],
202 'formatTimePeriod() rounding (>48h), avoidminutes'
208 'formatTimePeriod() rounding (>48h), avoidseconds'
212 [ 'avoid' => 'avoidseconds', 'noabbrevs' => true ],
213 '3 days 0 hours 0 minutes',
214 'formatTimePeriod() rounding (>48h), avoidseconds'
220 'formatTimePeriod() rounding, (>48h), avoidseconds'
224 [ 'avoid' => 'avoidseconds', 'noabbrevs' => true ],
225 '2 days 0 hours 0 minutes',
226 'formatTimePeriod() rounding, (>48h), avoidseconds'
232 'formatTimePeriod() rounding, recursion, (>48h)'
236 [ 'noabbrevs' => true ],
237 '2 days 1 hour 1 minute 1 second',
238 'formatTimePeriod() rounding, recursion, (>48h)'
244 * @covers Language::truncateForDatabase
245 * @covers Language::truncateInternal
247 public function testTruncateForDatabase() {
250 $this->getLang()->truncateForDatabase( "1234567890", 0, 'XXX' ),
251 'truncate prefix, len 0, small ellipsis'
256 $this->getLang()->truncateForDatabase( "1234567890", 8, 'XXX' ),
257 'truncate prefix, small ellipsis'
262 $this->getLang()->truncateForDatabase( "123456789", 5, 'XXXXXXXXXXXXXXX' ),
263 'truncate prefix, large ellipsis'
268 $this->getLang()->truncateForDatabase( "1234567890", -8, 'XXX' ),
269 'truncate suffix, small ellipsis'
274 $this->getLang()->truncateForDatabase( "123456789", -5, 'XXXXXXXXXXXXXXX' ),
275 'truncate suffix, large ellipsis'
279 $this->getLang()->truncateForDatabase( "123 ", 9, 'XXX' ),
280 'truncate prefix, with spaces'
284 $this->getLang()->truncateForDatabase( "12345 8", 11, 'XXX' ),
285 'truncate prefix, with spaces and non-space ending'
289 $this->getLang()->truncateForDatabase( "1 234", -8, 'XXX' ),
290 'truncate suffix, with spaces'
294 $this->getLang()->truncateForDatabase( "1234567890", 5, 'XXX', false ),
295 'truncate without adjustment'
299 $this->getLang()->truncateForDatabase( "泰乐菌素123456789", 11, '...', false ),
300 'truncate does not chop Unicode characters in half'
304 $this->getLang()->truncateForDatabase( "\n泰乐菌素123456789", 12, '...', false ),
305 'truncate does not chop Unicode characters in half if there is a preceding newline'
310 * @dataProvider provideTruncateData
311 * @covers Language::truncateForVisual
312 * @covers Language::truncateInternal
314 public function testTruncateForVisual(
315 $expected, $string, $length, $ellipsis = '...', $adjustLength = true
319 $this->getLang()->truncateForVisual( $string, $length, $ellipsis, $adjustLength )
324 * @return array Format is ($expected, $string, $length, $ellipsis, $adjustLength)
326 public static function provideTruncateData() {
328 [ "XXX", "тестирам да ли ради", 0, "XXX" ],
329 [ "testnXXX", "testni scenarij", 8, "XXX" ],
330 [ "حالة اختبار", "حالة اختبار", 5, "XXXXXXXXXXXXXXX" ],
331 [ "XXXедент", "прецедент", -8, "XXX" ],
332 [ "XXപിൾ", "ആപ്പിൾ", -5, "XX" ],
333 [ "神秘XXX", "神秘 ", 9, "XXX" ],
334 [ "ΔημιουργXXX", "Δημιουργία Σύμπαντος", 11, "XXX" ],
335 [ "XXXの家です", "地球は私たちの唯 の家です", -8, "XXX" ],
336 [ "زندگیXXX", "زندگی زیباست", 6, "XXX", false ],
337 [ "ცხოვრება...", "ცხოვრება არის საოცარი", 8, "...", false ],
338 [ "\nທ່ານ...", "\nທ່ານບໍ່ຮູ້ຫນັງສື", 5, "...", false ],
343 * @dataProvider provideHTMLTruncateData
344 * @covers Language::truncateHTML
346 public function testTruncateHtml( $len, $ellipsis, $input, $expected ) {
350 $this->getLang()->truncateHtml( $input, $len, $ellipsis )
355 * @return array Format is ($len, $ellipsis, $input, $expected)
357 public static function provideHTMLTruncateData() {
359 [ 0, 'XXX', "1234567890", "XXX" ],
360 [ 8, 'XXX', "1234567890", "12345XXX" ],
361 [ 5, 'XXXXXXXXXXXXXXX', '1234567890', "1234567890" ],
363 '<p><span style="font-weight:bold;"></span></p>',
364 '<p><span style="font-weight:bold;"></span></p>',
367 '<p><span style="font-weight:bold;">123456789</span></p>',
368 '<p><span style="font-weight:bold;">***</span></p>',
371 '<p><span style="font-weight:bold;"> 23456789</span></p>',
372 '<p><span style="font-weight:bold;">***</span></p>',
375 '<p><span style="font-weight:bold;">123456789</span></p>',
376 '<p><span style="font-weight:bold;">***</span></p>',
379 '<p><span style="font-weight:bold;">123456789</span></p>',
380 '<p><span style="font-weight:bold;">1***</span></p>',
383 '<tt><span style="font-weight:bold;">123456789</span></tt>',
384 '<tt><span style="font-weight:bold;">12***</span></tt>',
387 '<p><a href="www.mediawiki.org">123456789</a></p>',
388 '<p><a href="www.mediawiki.org">123***</a></p>',
391 '<p><a href="www.mediawiki.org">12 456789</a></p>',
392 '<p><a href="www.mediawiki.org">12 ***</a></p>',
395 '<small><span style="font-weight:bold;">123<p id="#moo">456</p>789</span></small>',
396 '<small><span style="font-weight:bold;">123<p id="#moo">4***</p></span></small>',
399 '<div><span style="font-weight:bold;">123<span>4</span>56789</span></div>',
400 '<div><span style="font-weight:bold;">123<span>4</span>5***</span></div>',
403 '<p><table style="font-weight:bold;"><tr><td>123456789</td></tr></table></p>',
404 '<p><table style="font-weight:bold;"><tr><td>123456789</td></tr></table></p>',
407 '<p><font style="font-weight:bold;">123456789</font></p>',
408 '<p><font style="font-weight:bold;">123456789</font></p>',
414 * Test Language::isWellFormedLanguageTag()
415 * @dataProvider provideWellFormedLanguageTags
416 * @covers Language::isWellFormedLanguageTag
418 public function testWellFormedLanguageTag( $code, $message = '' ) {
420 Language
::isWellFormedLanguageTag( $code ),
421 "validating code $code $message"
426 * The test cases are based on the tests in the GaBuZoMeu parser
427 * written by Stéphane Bortzmeyer <bortzmeyer@nic.fr>
428 * and distributed as free software, under the GNU General Public Licence.
429 * http://www.bortzmeyer.org/gabuzomeu-parsing-language-tags.html
431 public static function provideWellFormedLanguageTags() {
433 [ 'fr', 'two-letter code' ],
434 [ 'fr-latn', 'two-letter code with lower case script code' ],
435 [ 'fr-Latn-FR', 'two-letter code with title case script code and uppercase country code' ],
436 [ 'fr-Latn-419', 'two-letter code with title case script code and region number' ],
437 [ 'fr-FR', 'two-letter code with uppercase' ],
438 [ 'ax-TZ', 'Not in the registry, but well-formed' ],
439 [ 'fr-shadok', 'two-letter code with variant' ],
440 [ 'fr-y-myext-myext2', 'non-x singleton' ],
441 [ 'fra-Latn', 'ISO 639 can be 3-letters' ],
442 [ 'fra', 'three-letter language code' ],
443 [ 'fra-FX', 'three-letter language code with country code' ],
444 [ 'i-klingon', 'grandfathered with singleton' ],
445 [ 'I-kLINgon', 'tags are case-insensitive...' ],
446 [ 'no-bok', 'grandfathered without singleton' ],
447 [ 'i-enochian', 'Grandfathered' ],
448 [ 'x-fr-CH', 'private use' ],
449 [ 'es-419', 'two-letter code with region number' ],
450 [ 'en-Latn-GB-boont-r-extended-sequence-x-private', 'weird, but well-formed' ],
451 [ 'ab-x-abc-x-abc', 'anything goes after x' ],
452 [ 'ab-x-abc-a-a', 'anything goes after x, including several non-x singletons' ],
453 [ 'i-default', 'grandfathered' ],
454 [ 'abcd-Latn', 'Language of 4 chars reserved for future use' ],
455 [ 'AaBbCcDd-x-y-any-x', 'Language of 5-8 chars, registered' ],
456 [ 'de-CH-1901', 'with country and year' ],
457 [ 'en-US-x-twain', 'with country and singleton' ],
458 [ 'zh-cmn', 'three-letter variant' ],
459 [ 'zh-cmn-Hant', 'three-letter variant and script' ],
460 [ 'zh-cmn-Hant-HK', 'three-letter variant, script and country' ],
461 [ 'xr-p-lze', 'Extension' ],
466 * Negative test for Language::isWellFormedLanguageTag()
467 * @dataProvider provideMalformedLanguageTags
468 * @covers Language::isWellFormedLanguageTag
470 public function testMalformedLanguageTag( $code, $message = '' ) {
472 Language
::isWellFormedLanguageTag( $code ),
473 "validating that code $code is a malformed language tag - $message"
478 * The test cases are based on the tests in the GaBuZoMeu parser
479 * written by Stéphane Bortzmeyer <bortzmeyer@nic.fr>
480 * and distributed as free software, under the GNU General Public Licence.
481 * http://www.bortzmeyer.org/gabuzomeu-parsing-language-tags.html
483 public static function provideMalformedLanguageTags() {
485 [ 'f', 'language too short' ],
486 [ 'f-Latn', 'language too short with script' ],
487 [ 'xr-lxs-qut', 'variants too short' ], # extlangS
488 [ 'fr-Latn-F', 'region too short' ],
489 [ 'a-value', 'language too short with region' ],
490 [ 'tlh-a-b-foo', 'valid three-letter with wrong variant' ],
493 'grandfathered but not registered: invalid, even if we only test well-formedness'
495 [ 'abcdefghi-012345678', 'numbers too long' ],
496 [ 'ab-abc-abc-abc-abc', 'invalid extensions' ],
497 [ 'ab-abcd-abc', 'invalid extensions' ],
498 [ 'ab-ab-abc', 'invalid extensions' ],
499 [ 'ab-123-abc', 'invalid extensions' ],
500 [ 'a-Hant-ZH', 'short language with valid extensions' ],
501 [ 'a1-Hant-ZH', 'invalid character in language' ],
502 [ 'ab-abcde-abc', 'invalid extensions' ],
503 [ 'ab-1abc-abc', 'invalid characters in extensions' ],
504 [ 'ab-ab-abcd', 'invalid order of extensions' ],
505 [ 'ab-123-abcd', 'invalid order of extensions' ],
506 [ 'ab-abcde-abcd', 'invalid extensions' ],
507 [ 'ab-1abc-abcd', 'invalid characters in extensions' ],
508 [ 'ab-a-b', 'extensions too short' ],
509 [ 'ab-a-x', 'extensions too short, even with singleton' ],
510 [ 'ab--ab', 'two separators' ],
511 [ 'ab-abc-', 'separator in the end' ],
512 [ '-ab-abc', 'separator in the beginning' ],
513 [ 'abcd-efg', 'language too long' ],
514 [ 'aabbccddE', 'tag too long' ],
515 [ 'pa_guru', 'A tag with underscore is invalid in strict mode' ],
516 [ 'de-f', 'subtag too short' ],
521 * Negative test for Language::isWellFormedLanguageTag()
522 * @covers Language::isWellFormedLanguageTag
524 public function testLenientLanguageTag() {
526 Language
::isWellFormedLanguageTag( 'pa_guru', true ),
527 'pa_guru is a well-formed language tag in lenient mode'
532 * Test too short timestamp
533 * @expectedException MWException
534 * @covers Language::sprintfDate
536 public function testSprintfDateTooShortTimestamp() {
537 $this->getLang()->sprintfDate( 'xiY', '1234567890123' );
541 * Test too long timestamp
542 * @expectedException MWException
543 * @covers Language::sprintfDate
545 public function testSprintfDateTooLongTimestamp() {
546 $this->getLang()->sprintfDate( 'xiY', '123456789012345' );
550 * Test too short timestamp
551 * @expectedException MWException
552 * @covers Language::sprintfDate
554 public function testSprintfDateNotAllDigitTimestamp() {
555 $this->getLang()->sprintfDate( 'xiY', '-1234567890123' );
559 * @dataProvider provideSprintfDateSamples
560 * @covers Language::sprintfDate
562 public function testSprintfDate( $format, $ts, $expected, $msg ) {
566 $this->getLang()->sprintfDate( $format, $ts, null, $ttl ),
567 "sprintfDate('$format', '$ts'): $msg"
570 $dt = new DateTime( $ts );
571 $lastValidTS = $dt->add( new DateInterval( 'PT' . ( $ttl - 1 ) . 'S' ) )->format( 'YmdHis' );
574 $this->getLang()->sprintfDate( $format, $lastValidTS, null ),
575 "sprintfDate('$format', '$ts'): TTL $ttl too high (output was different at $lastValidTS)"
578 // advance the time enough to make all of the possible outputs different (except possibly L)
579 $dt = new DateTime( $ts );
580 $newTS = $dt->add( new DateInterval( 'P1Y1M8DT13H1M1S' ) )->format( 'YmdHis' );
583 $this->getLang()->sprintfDate( $format, $newTS, null ),
584 "sprintfDate('$format', '$ts'): Missing TTL (output was different at $newTS)"
590 * sprintfDate should always use UTC when no zone is given.
591 * @dataProvider provideSprintfDateSamples
592 * @covers Language::sprintfDate
594 public function testSprintfDateNoZone( $format, $ts, $expected, $ignore, $msg ) {
595 $oldTZ = date_default_timezone_get();
596 $res = date_default_timezone_set( 'Asia/Seoul' );
598 $this->markTestSkipped( "Error setting Timezone" );
603 $this->getLang()->sprintfDate( $format, $ts ),
604 "sprintfDate('$format', '$ts'): $msg"
607 date_default_timezone_set( $oldTZ );
611 * sprintfDate should use passed timezone
612 * @dataProvider provideSprintfDateSamples
613 * @covers Language::sprintfDate
615 public function testSprintfDateTZ( $format, $ts, $ignore, $expected, $msg ) {
616 $tz = new DateTimeZone( 'Asia/Seoul' );
618 $this->markTestSkipped( "Error getting Timezone" );
623 $this->getLang()->sprintfDate( $format, $ts, $tz ),
624 "sprintfDate('$format', '$ts', 'Asia/Seoul'): $msg"
629 * sprintfDate should only calculate a TTL if the caller is going to use it.
630 * @covers Language::sprintfDate
632 public function testSprintfDateNoTtlIfNotNeeded() {
633 $noTtl = 'unused'; // Value used to represent that the caller didn't pass a variable in.
635 $this->getLang()->sprintfDate( 'YmdHis', wfTimestampNow(), null, $noTtl );
636 $this->getLang()->sprintfDate( 'YmdHis', wfTimestampNow(), null, $ttl );
641 'If the caller does not set the $ttl variable, do not compute it.'
643 $this->assertInternalType( 'int', $ttl, 'TTL should have been computed.' );
646 public static function provideSprintfDateSamples() {
651 '1390', // note because we're testing English locale we get Latin-standard digits
653 'Iranian calendar full year'
660 'Iranian calendar short year'
667 'ISO 8601 (week) year'
690 // What follows is mostly copied from
691 // https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions#.23time
718 'Month index, not zero pad'
725 'Month index. Zero pad'
746 'Genitive month name (same in EN)'
753 'Day of month (not zero pad)'
760 'Day of month (zero-pad)'
767 'Day of year (zero-indexed)'
774 'Day of week (abbrev)'
788 'Day of week (Mon=1, Sun=7)'
795 'Day of week (Sun=0, Sat=6)'
837 '12 hour, zero padded'
886 'Days in current month'
891 '2012-01-02T09:07:05+00:00',
892 '2012-01-02T09:07:05+09:00',
898 'Mon, 02 Jan 2012 09:07:05 +0000',
899 'Mon, 02 Jan 2012 09:07:05 +0900',
907 'Timezone identifier'
928 'Timezone offset with colon'
935 'Timezone abbreviation'
942 'Timezone offset in seconds'
970 'Hebrew number of days in month'
977 'Hebrew genitive month name (No difference in EN)'
1012 'nengo - last day of heisei'
1019 'nengo - first day of reiwa'
1026 'nengo - second year of reiwa'
1047 'Raw numerals (doesn\'t mean much in EN)'
1050 '[[Y "(yea"\\r)]] \\"xx\\"',
1052 '[[2012 (year)]] "x"',
1053 '[[2012 (year)]] "x"',
1061 * @dataProvider provideFormatSizes
1062 * @covers Language::formatSize
1064 public function testFormatSize( $size, $expected, $msg ) {
1065 $this->assertEquals(
1067 $this->getLang()->formatSize( $size ),
1068 "formatSize('$size'): $msg"
1072 public static function provideFormatSizes() {
1119 // How big!? THIS BIG!
1124 * @dataProvider provideFormatBitrate
1125 * @covers Language::formatBitrate
1127 public function testFormatBitrate( $bps, $expected, $msg ) {
1128 $this->assertEquals(
1130 $this->getLang()->formatBitrate( $bps ),
1131 "formatBitrate('$bps'): $msg"
1135 public static function provideFormatBitrate() {
1145 "999 bits per second"
1150 "1 kilobit per second"
1155 "1 megabit per second"
1160 "1 gigabit per second"
1165 "1 terabit per second"
1170 "1 petabit per second"
1175 "1 exabit per second"
1180 "1 zetabit per second"
1185 "1 yottabit per second"
1190 "1,000 yottabits per second"
1196 * @dataProvider provideFormatDuration
1197 * @covers Language::formatDuration
1199 public function testFormatDuration( $duration, $expected, $intervals = [] ) {
1200 $this->assertEquals(
1202 $this->getLang()->formatDuration( $duration, $intervals ),
1203 "formatDuration('$duration'): $expected"
1207 public static function provideFormatDuration() {
1246 // ( 365 + ( 24 * 3 + 25 ) / 400 ) * 86400 = 31556952
1247 ( 365 +
( 24 * 3 +
25 ) / 400.0 ) * 86400,
1280 '2 hours, 30 minutes and 1 second'
1284 '1 hour and 1 second'
1287 31556952 +
2 * 86400 +
9000,
1288 '1 year, 2 days, 2 hours and 30 minutes'
1291 42 * 1000 * 31556952 +
42,
1292 '42 millennia and 42 seconds'
1310 31556952 +
2 * 86400 +
9000,
1311 '1 year, 2 days and 150 minutes',
1312 [ 'years', 'days', 'minutes' ],
1317 [ 'years', 'days' ],
1320 31556952 +
2 * 86400 +
9000,
1321 '1 year, 2 days and 150 minutes',
1322 [ 'minutes', 'days', 'years' ],
1327 [ 'days', 'years' ],
1333 * @dataProvider provideCheckTitleEncodingData
1334 * @covers Language::checkTitleEncoding
1336 public function testCheckTitleEncoding( $s ) {
1337 $this->assertEquals(
1339 $this->getLang()->checkTitleEncoding( $s ),
1340 "checkTitleEncoding('$s')"
1344 public static function provideCheckTitleEncodingData() {
1345 // phpcs:disable Generic.Files.LineLength
1348 [ "United States of America" ], // 7bit ASCII
1349 [ rawurldecode( "S%C3%A9rie%20t%C3%A9l%C3%A9vis%C3%A9e" ) ],
1352 "Acteur%7CAlbert%20Robbins%7CAnglais%7CAnn%20Donahue%7CAnthony%20E.%20Zuiker%7CCarol%20Mendelsohn"
1355 // The following two data sets come from T38839. They fail if checkTitleEncoding uses a regexp to test for
1356 // valid UTF-8 encoding and the pcre.recursion_limit is low (like, say, 1024). They succeed if checkTitleEncoding
1357 // uses mb_check_encoding for its test.
1360 "Acteur%7CAlbert%20Robbins%7CAnglais%7CAnn%20Donahue%7CAnthony%20E.%20Zuiker%7CCarol%20Mendelsohn%7C"
1361 . "Catherine%20Willows%7CDavid%20Hodges%7CDavid%20Phillips%7CGil%20Grissom%7CGreg%20Sanders%7CHodges%7C"
1362 . "Internet%20Movie%20Database%7CJim%20Brass%7CLady%20Heather%7C"
1363 . "Les%20Experts%20(s%C3%A9rie%20t%C3%A9l%C3%A9vis%C3%A9e)%7CLes%20Experts%20:%20Manhattan%7C"
1364 . "Les%20Experts%20:%20Miami%7CListe%20des%20personnages%20des%20Experts%7C"
1365 . "Liste%20des%20%C3%A9pisodes%20des%20Experts%7CMod%C3%A8le%20discussion:Palette%20Les%20Experts%7C"
1366 . "Nick%20Stokes%7CPersonnage%20de%20fiction%7CPersonnage%20fictif%7CPersonnage%20de%20fiction%7C"
1367 . "Personnages%20r%C3%A9currents%20dans%20Les%20Experts%7CRaymond%20Langston%7CRiley%20Adams%7C"
1368 . "Saison%201%20des%20Experts%7CSaison%2010%20des%20Experts%7CSaison%2011%20des%20Experts%7C"
1369 . "Saison%2012%20des%20Experts%7CSaison%202%20des%20Experts%7CSaison%203%20des%20Experts%7C"
1370 . "Saison%204%20des%20Experts%7CSaison%205%20des%20Experts%7CSaison%206%20des%20Experts%7C"
1371 . "Saison%207%20des%20Experts%7CSaison%208%20des%20Experts%7CSaison%209%20des%20Experts%7C"
1372 . "Sara%20Sidle%7CSofia%20Curtis%7CS%C3%A9rie%20t%C3%A9l%C3%A9vis%C3%A9e%7CWallace%20Langham%7C"
1373 . "Warrick%20Brown%7CWendy%20Simms%7C%C3%89tats-Unis"
1378 "Mod%C3%A8le%3AArrondissements%20homonymes%7CMod%C3%A8le%3ABandeau%20standard%20pour%20page%20d'homonymie%7C"
1379 . "Mod%C3%A8le%3ABatailles%20homonymes%7CMod%C3%A8le%3ACantons%20homonymes%7C"
1380 . "Mod%C3%A8le%3ACommunes%20fran%C3%A7aises%20homonymes%7CMod%C3%A8le%3AFilms%20homonymes%7C"
1381 . "Mod%C3%A8le%3AGouvernements%20homonymes%7CMod%C3%A8le%3AGuerres%20homonymes%7CMod%C3%A8le%3AHomonymie%7C"
1382 . "Mod%C3%A8le%3AHomonymie%20bateau%7CMod%C3%A8le%3AHomonymie%20d'%C3%A9tablissements%20scolaires%20ou"
1383 . "%20universitaires%7CMod%C3%A8le%3AHomonymie%20d'%C3%AEles%7CMod%C3%A8le%3AHomonymie%20de%20clubs%20sportifs%7C"
1384 . "Mod%C3%A8le%3AHomonymie%20de%20comt%C3%A9s%7CMod%C3%A8le%3AHomonymie%20de%20monument%7C"
1385 . "Mod%C3%A8le%3AHomonymie%20de%20nom%20romain%7CMod%C3%A8le%3AHomonymie%20de%20parti%20politique%7C"
1386 . "Mod%C3%A8le%3AHomonymie%20de%20route%7CMod%C3%A8le%3AHomonymie%20dynastique%7C"
1387 . "Mod%C3%A8le%3AHomonymie%20vid%C3%A9oludique%7CMod%C3%A8le%3AHomonymie%20%C3%A9difice%20religieux%7C"
1388 . "Mod%C3%A8le%3AInternationalisation%7CMod%C3%A8le%3AIsom%C3%A9rie%7CMod%C3%A8le%3AParonymie%7C"
1389 . "Mod%C3%A8le%3APatronyme%7CMod%C3%A8le%3APatronyme%20basque%7CMod%C3%A8le%3APatronyme%20italien%7C"
1390 . "Mod%C3%A8le%3APatronymie%7CMod%C3%A8le%3APersonnes%20homonymes%7CMod%C3%A8le%3ASaints%20homonymes%7C"
1391 . "Mod%C3%A8le%3ATitres%20homonymes%7CMod%C3%A8le%3AToponymie%7CMod%C3%A8le%3AUnit%C3%A9s%20homonymes%7C"
1392 . "Mod%C3%A8le%3AVilles%20homonymes%7CMod%C3%A8le%3A%C3%89difices%20religieux%20homonymes"
1400 * @dataProvider provideRomanNumeralsData
1401 * @covers Language::romanNumeral
1403 public function testRomanNumerals( $num, $numerals ) {
1404 $this->assertEquals(
1406 Language
::romanNumeral( $num ),
1407 "romanNumeral('$num')"
1411 public static function provideRomanNumeralsData() {
1444 [ 1989, 'MCMLXXXIX' ],
1450 [ 7000, 'MMMMMMM' ],
1451 [ 8000, 'MMMMMMMM' ],
1452 [ 9000, 'MMMMMMMMM' ],
1453 [ 9999, 'MMMMMMMMMCMXCIX' ],
1454 [ 10000, 'MMMMMMMMMM' ],
1459 * @dataProvider provideHebrewNumeralsData
1460 * @covers Language::hebrewNumeral
1462 public function testHebrewNumeral( $num, $numerals ) {
1463 $this->assertEquals(
1465 Language
::hebrewNumeral( $num ),
1466 "hebrewNumeral('$num')"
1470 public static function provideHebrewNumeralsData() {
1513 [ 2000, "ב' אלפים" ],
1515 [ 3000, "ג' אלפים" ],
1516 [ 4000, "ד' אלפים" ],
1517 [ 4904, "ד'תתק\"ד" ],
1518 [ 5000, "ה' אלפים" ],
1519 [ 5680, "ה'תר\"ף" ],
1520 [ 5690, "ה'תר\"ץ" ],
1521 [ 5708, "ה'תש\"ח" ],
1522 [ 5720, "ה'תש\"ך" ],
1523 [ 5740, "ה'תש\"ם" ],
1524 [ 5750, "ה'תש\"ן" ],
1525 [ 5775, "ה'תשע\"ה" ],
1530 * @dataProvider providePluralData
1531 * @covers Language::convertPlural
1533 public function testConvertPlural( $expected, $number, $forms ) {
1534 $chosen = $this->getLang()->convertPlural( $number, $forms );
1535 $this->assertEquals( $expected, $chosen );
1538 public static function providePluralData() {
1539 // Params are: [expected text, number given, [the plural forms]]
1542 'singular', 'plural'
1544 [ 'explicit zero', 0, [
1545 '0=explicit zero', 'singular', 'plural'
1547 [ 'explicit one', 1, [
1548 'singular', 'plural', '1=explicit one',
1551 'singular', 'plural', '0=explicit zero',
1554 '0=explicit zero', '1=explicit one', 'singular', 'plural'
1556 [ 'explicit eleven', 11, [
1557 'singular', 'plural', '11=explicit eleven',
1560 'singular', 'plural', '11=explicit twelve',
1563 'singular', 'plural', '=explicit form',
1566 'kissa=kala', '1=2=3', 'other',
1569 '0=explicit zero', '1=explicit one',
1575 * @covers Language::embedBidi()
1577 public function testEmbedBidi() {
1578 $lre = "\u{202A}"; // U+202A LEFT-TO-RIGHT EMBEDDING
1579 $rle = "\u{202B}"; // U+202B RIGHT-TO-LEFT EMBEDDING
1580 $pdf = "\u{202C}"; // U+202C POP DIRECTIONAL FORMATTING
1581 $lang = $this->getLang();
1582 $this->assertEquals(
1584 $lang->embedBidi( '123' ),
1585 'embedBidi with neutral argument'
1587 $this->assertEquals(
1588 $lre . 'Ben_(WMF)' . $pdf,
1589 $lang->embedBidi( 'Ben_(WMF)' ),
1590 'embedBidi with LTR argument'
1592 $this->assertEquals(
1593 $rle . 'יהודי (מנוחין)' . $pdf,
1594 $lang->embedBidi( 'יהודי (מנוחין)' ),
1595 'embedBidi with RTL argument'
1600 * @covers Language::translateBlockExpiry()
1601 * @dataProvider provideTranslateBlockExpiry
1603 public function testTranslateBlockExpiry( $expectedData, $str, $now, $desc ) {
1604 $lang = $this->getLang();
1605 if ( is_array( $expectedData ) ) {
1606 list( $func, $arg ) = $expectedData;
1607 $expected = $lang->$func( $arg );
1609 $expected = $expectedData;
1611 $this->assertEquals( $expected, $lang->translateBlockExpiry( $str, null, $now ), $desc );
1614 public static function provideTranslateBlockExpiry() {
1616 [ '2 hours', '2 hours', 0, 'simple data from ipboptions' ],
1617 [ 'indefinite', 'infinite', 0, 'infinite from ipboptions' ],
1618 [ 'indefinite', 'infinity', 0, 'alternative infinite from ipboptions' ],
1619 [ 'indefinite', 'indefinite', 0, 'another alternative infinite from ipboptions' ],
1620 [ [ 'formatDuration', 1023 * 60 * 60 ], '1023 hours', 0, 'relative' ],
1621 [ [ 'formatDuration', -1023 ], '-1023 seconds', 0, 'negative relative' ],
1623 [ 'formatDuration', 1023 * 60 * 60 ],
1625 wfTimestamp( TS_UNIX
, '19910203040506' ),
1626 'relative with initial timestamp'
1628 [ [ 'formatDuration', 0 ], 'now', 0, 'now' ],
1630 [ 'timeanddate', '20120102070000' ],
1631 '2012-1-1 7:00 +1 day',
1633 'mixed, handled as absolute'
1635 [ [ 'timeanddate', '19910203040506' ], '1991-2-3 4:05:06', 0, 'absolute' ],
1636 [ [ 'timeanddate', '19700101000000' ], '1970-1-1 0:00:00', 0, 'absolute at epoch' ],
1637 [ [ 'timeanddate', '19691231235959' ], '1969-12-31 23:59:59', 0, 'time before epoch' ],
1639 [ 'timeanddate', '19910910000000' ],
1641 wfTimestamp( TS_UNIX
, '19910203040506' ),
1644 [ 'dummy', 'dummy', 0, 'return garbage as is' ],
1649 * @dataProvider provideFormatNum
1650 * @covers Language::formatNum
1652 public function testFormatNum(
1653 $translateNumerals, $langCode, $number, $nocommafy, $expected
1655 $this->setMwGlobals( [ 'wgTranslateNumerals' => $translateNumerals ] );
1656 $lang = Language
::factory( $langCode );
1657 $formattedNum = $lang->formatNum( $number, $nocommafy );
1658 $this->assertType( 'string', $formattedNum );
1659 $this->assertEquals( $expected, $formattedNum );
1662 public function provideFormatNum() {
1664 [ true, 'en', 100, false, '100' ],
1665 [ true, 'en', 101, true, '101' ],
1666 [ false, 'en', 103, false, '103' ],
1667 [ false, 'en', 104, true, '104' ],
1668 [ true, 'en', '105', false, '105' ],
1669 [ true, 'en', '106', true, '106' ],
1670 [ false, 'en', '107', false, '107' ],
1671 [ false, 'en', '108', true, '108' ],
1676 * @covers Language::parseFormattedNumber
1677 * @dataProvider parseFormattedNumberProvider
1679 public function testParseFormattedNumber( $langCode, $number ) {
1680 $lang = Language
::factory( $langCode );
1682 $localisedNum = $lang->formatNum( $number );
1683 $normalisedNum = $lang->parseFormattedNumber( $localisedNum );
1685 $this->assertEquals( $number, $normalisedNum );
1688 public function parseFormattedNumberProvider() {
1695 [ 'zh-classical', 7432 ]
1700 * @covers Language::commafy()
1701 * @dataProvider provideCommafyData
1703 public function testCommafy( $number, $numbersWithCommas ) {
1704 $this->assertEquals(
1706 $this->getLang()->commafy( $number ),
1707 "commafy('$number')"
1711 public static function provideCommafyData() {
1717 [ 10000, '10,000' ],
1718 [ 100000, '100,000' ],
1719 [ 1000000, '1,000,000' ],
1720 [ -1.0001, '-1.0001' ],
1721 [ 1.0001, '1.0001' ],
1722 [ 10.0001, '10.0001' ],
1723 [ 100.0001, '100.0001' ],
1724 [ 1000.0001, '1,000.0001' ],
1725 [ 10000.0001, '10,000.0001' ],
1726 [ 100000.0001, '100,000.0001' ],
1727 [ 1000000.0001, '1,000,000.0001' ],
1728 [ '200000000000000000000', '200,000,000,000,000,000,000' ],
1729 [ '-200000000000000000000', '-200,000,000,000,000,000,000' ],
1734 * @covers Language::listToText
1736 public function testListToText() {
1737 $lang = $this->getLang();
1738 $and = $lang->getMessageFromDB( 'and' );
1739 $s = $lang->getMessageFromDB( 'word-separator' );
1740 $c = $lang->getMessageFromDB( 'comma-separator' );
1742 $this->assertEquals( '', $lang->listToText( [] ) );
1743 $this->assertEquals( 'a', $lang->listToText( [ 'a' ] ) );
1744 $this->assertEquals( "a{$and}{$s}b", $lang->listToText( [ 'a', 'b' ] ) );
1745 $this->assertEquals( "a{$c}b{$and}{$s}c", $lang->listToText( [ 'a', 'b', 'c' ] ) );
1746 $this->assertEquals( "a{$c}b{$c}c{$and}{$s}d", $lang->listToText( [ 'a', 'b', 'c', 'd' ] ) );
1750 * @covers Language::clearCaches
1752 public function testClearCaches() {
1753 $languageClass = TestingAccessWrapper
::newFromClass( Language
::class );
1755 // Populate $mLangObjCache
1756 $lang = Language
::factory( 'en' );
1757 $this->assertNotCount( 0, Language
::$mLangObjCache );
1759 // Populate $fallbackLanguageCache
1760 Language
::getFallbacksIncludingSiteLanguage( 'en' );
1761 $this->assertNotCount( 0, $languageClass->fallbackLanguageCache
);
1763 // Populate $grammarTransformations
1764 $lang->getGrammarTransformations();
1765 $this->assertNotNull( $languageClass->grammarTransformations
);
1767 Language
::clearCaches();
1769 $this->assertCount( 0, Language
::$mLangObjCache );
1770 $this->assertCount( 0, $languageClass->fallbackLanguageCache
);
1771 $this->assertNull( $languageClass->grammarTransformations
);
1775 * @dataProvider provideGetParentLanguage
1776 * @covers Language::getParentLanguage
1778 public function testGetParentLanguage( $code, $expected, $comment ) {
1779 $lang = Language
::factory( $code );
1780 if ( is_null( $expected ) ) {
1781 $this->assertNull( $lang->getParentLanguage(), $comment );
1783 $this->assertEquals( $expected, $lang->getParentLanguage()->getCode(), $comment );
1787 public static function provideGetParentLanguage() {
1789 [ 'zh-cn', 'zh', 'zh is the parent language of zh-cn' ],
1790 [ 'zh', 'zh', 'zh is defined as the parent language of zh, '
1791 . 'because zh converter can convert zh-cn to zh' ],
1792 [ 'zh-invalid', null, 'do not be fooled by arbitrarily composed language codes' ],
1793 [ 'de-formal', null, 'de does not have converter' ],
1794 [ 'de', null, 'de does not have converter' ],
1799 * @dataProvider provideGetNamespaceAliases
1800 * @covers Language::getNamespaceAliases
1802 public function testGetNamespaceAliases( $languageCode, $subset ) {
1803 $language = Language
::factory( $languageCode );
1804 $aliases = $language->getNamespaceAliases();
1805 foreach ( $subset as $alias => $nsId ) {
1806 $this->assertEquals( $nsId, $aliases[$alias] );
1810 public static function provideGetNamespaceAliases() {
1811 // TODO: Add tests for NS_PROJECT_TALK and GenderNamespaces
1824 * @covers Language::hasVariant
1826 public function testHasVariant() {
1827 // See LanguageSrTest::testHasVariant() for additional tests
1828 $en = Language
::factory( 'en' );
1829 $this->assertTrue( $en->hasVariant( 'en' ), 'base is always a variant' );
1830 $this->assertFalse( $en->hasVariant( 'en-bogus' ), 'bogus en variant' );
1832 $bogus = Language
::factory( 'bogus' );
1833 $this->assertTrue( $bogus->hasVariant( 'bogus' ), 'base is always a variant' );
1837 * @covers Language::equals
1839 public function testEquals() {
1840 $en1 = Language
::factory( 'en' );
1841 $en2 = Language
::factory( 'en' );
1842 $en3 = new Language();
1843 $this->assertTrue( $en1->equals( $en2 ), 'en1 equals en2' );
1844 $this->assertTrue( $en2->equals( $en3 ), 'en2 equals en3' );
1845 $this->assertTrue( $en3->equals( $en1 ), 'en3 equals en1' );
1847 $fr = Language
::factory( 'fr' );
1848 $this->assertFalse( $en1->equals( $fr ), 'en not equals fr' );
1850 $ar1 = Language
::factory( 'ar' );
1851 $ar2 = new LanguageAr();
1852 $this->assertTrue( $ar1->equals( $ar2 ), 'ar equals ar' );
1856 * @dataProvider provideUcfirst
1857 * @covers Language::ucfirst
1859 public function testUcfirst( $orig, $expected, $desc, $overrides = false ) {
1860 $lang = new Language();
1861 if ( is_array( $overrides ) ) {
1862 $this->setMwGlobals( [ 'wgOverrideUcfirstCharacters' => $overrides ] );
1864 $this->assertSame( $lang->ucfirst( $orig ), $expected, $desc );
1867 public static function provideUcfirst() {
1869 [ 'alice', 'Alice', 'simple ASCII string', false ],
1870 [ 'århus', 'Århus', 'unicode string', false ],
1871 //overrides do not affect ASCII characters
1872 [ 'foo', 'Foo', 'ASCII is not overriden', [ 'f' => 'b' ] ],
1873 // but they do affect non-ascii ones
1874 [ 'èl', 'Ll' , 'Non-ASCII is overridden', [ 'è' => 'L' ] ],
1878 // The following methods are for LanguageNameUtilsTestTrait
1880 private function isSupportedLanguage( $code ) {
1881 return Language
::isSupportedLanguage( $code );
1884 private function isValidCode( $code ) {
1885 return Language
::isValidCode( $code );
1888 private function isValidBuiltInCode( $code ) {
1889 return Language
::isValidBuiltInCode( $code );
1892 private function isKnownLanguageTag( $code ) {
1893 return Language
::isKnownLanguageTag( $code );
1897 * Call getLanguageName() and getLanguageNames() using the Language static methods.
1899 * @param array $options To set globals for testing Language
1900 * @param string $expected
1901 * @param string $code
1902 * @param mixed ...$otherArgs Optionally, pass $inLanguage and/or $include.
1904 private function assertGetLanguageNames( array $options, $expected, $code, ...$otherArgs ) {
1906 foreach ( $options as $key => $val ) {
1907 $this->setMwGlobals( "wg$key", $val );
1909 $this->resetServices();
1911 $this->assertSame( $expected,
1912 Language
::fetchLanguageNames( ...$otherArgs )[strtolower( $code )] ??
'' );
1913 $this->assertSame( $expected, Language
::fetchLanguageName( $code, ...$otherArgs ) );
1916 private function getLanguageNames( ...$args ) {
1917 return Language
::fetchLanguageNames( ...$args );
1920 private function getLanguageName( ...$args ) {
1921 return Language
::fetchLanguageName( ...$args );
1924 private static function getFileName( ...$args ) {
1925 return Language
::getFileName( ...$args );
1928 private static function getMessagesFileName( $code ) {
1929 return Language
::getMessagesFileName( $code );
1932 private static function getJsonMessagesFileName( $code ) {
1933 return Language
::getJsonMessagesFileName( $code );
1937 * @todo This really belongs in the cldr extension's tests.
1939 * @covers MediaWiki\Languages\LanguageNameUtils::isKnownLanguageTag
1940 * @covers Language::isKnownLanguageTag
1942 public function testIsKnownLanguageTag_cldr() {
1943 if ( !class_exists( 'LanguageNames' ) ) {
1944 $this->markTestSkipped( 'The LanguageNames class is not available. '
1945 . 'The CLDR extension is probably not installed.' );
1948 // We need to restore the extension's hook that we removed.
1949 $this->setMwGlobals( 'wgHooks', $this->origHooks
);
1951 // "pal" is an ancient language, which probably will not appear in Names.php, but appears in
1953 $this->assertTrue( Language
::isKnownLanguageTag( 'pal' ) );