3 use Wikimedia\TestingAccessWrapper
;
5 class LanguageTest
extends LanguageClassesTestCase
{
7 * @covers Language::convertDoubleWidth
8 * @covers Language::normalizeForSearch
10 public function testLanguageConvertDoubleWidthToSingleWidth() {
12 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
13 $this->getLang()->normalizeForSearch(
14 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
16 'convertDoubleWidth() with the full alphabet and digits'
21 * @dataProvider provideFormattableTimes
22 * @covers Language::formatTimePeriod
24 public function testFormatTimePeriod( $seconds, $format, $expected, $desc ) {
25 $this->assertEquals( $expected, $this->getLang()->formatTimePeriod( $seconds, $format ), $desc );
28 public static function provideFormattableTimes() {
34 'formatTimePeriod() rounding (<10s)'
38 [ 'noabbrevs' => true ],
40 'formatTimePeriod() rounding (<10s)'
46 'formatTimePeriod() rounding (<10s)'
50 [ 'noabbrevs' => true ],
52 'formatTimePeriod() rounding (<10s)'
58 'formatTimePeriod() rounding (<60s)'
62 [ 'noabbrevs' => true ],
64 'formatTimePeriod() rounding (<60s)'
70 'formatTimePeriod() rounding (<1h)'
74 [ 'noabbrevs' => true ],
75 '2 minutes 0 seconds',
76 'formatTimePeriod() rounding (<1h)'
82 'formatTimePeriod() rounding (<1h)'
86 [ 'noabbrevs' => true ],
87 '1 hour 0 minutes 0 seconds',
88 'formatTimePeriod() rounding (<1h)'
94 'formatTimePeriod() rounding (>=1h)'
98 [ 'noabbrevs' => true ],
99 '2 hours 0 minutes 0 seconds',
100 'formatTimePeriod() rounding (>=1h)'
106 'formatTimePeriod() rounding (>=1h), avoidseconds'
110 [ 'avoid' => 'avoidseconds', 'noabbrevs' => true ],
112 'formatTimePeriod() rounding (>=1h), avoidseconds'
118 'formatTimePeriod() rounding (>=1h), avoidminutes'
122 [ 'avoid' => 'avoidminutes', 'noabbrevs' => true ],
124 'formatTimePeriod() rounding (>=1h), avoidminutes'
130 'formatTimePeriod() rounding (=48h), avoidseconds'
134 [ 'avoid' => 'avoidseconds', 'noabbrevs' => true ],
135 '48 hours 0 minutes',
136 'formatTimePeriod() rounding (=48h), avoidseconds'
142 'formatTimePeriod() rounding (>48h), avoidhours'
146 [ 'avoid' => 'avoidhours', 'noabbrevs' => true ],
148 'formatTimePeriod() rounding (>48h), avoidhours'
154 'formatTimePeriod() rounding (>48h), avoidminutes'
158 [ 'avoid' => 'avoidminutes', 'noabbrevs' => true ],
160 'formatTimePeriod() rounding (>48h), avoidminutes'
166 'formatTimePeriod() rounding (>48h), avoidseconds'
170 [ 'avoid' => 'avoidseconds', 'noabbrevs' => true ],
171 '2 days 1 hour 0 minutes',
172 'formatTimePeriod() rounding (>48h), avoidseconds'
178 'formatTimePeriod() rounding (>48h), avoidminutes'
182 [ 'avoid' => 'avoidminutes', 'noabbrevs' => true ],
184 'formatTimePeriod() rounding (>48h), avoidminutes'
190 'formatTimePeriod() rounding (>48h), avoidseconds'
194 [ 'avoid' => 'avoidseconds', 'noabbrevs' => true ],
195 '3 days 0 hours 0 minutes',
196 'formatTimePeriod() rounding (>48h), avoidseconds'
202 'formatTimePeriod() rounding, (>48h), avoidseconds'
206 [ 'avoid' => 'avoidseconds', 'noabbrevs' => true ],
207 '2 days 0 hours 0 minutes',
208 'formatTimePeriod() rounding, (>48h), avoidseconds'
214 'formatTimePeriod() rounding, recursion, (>48h)'
218 [ 'noabbrevs' => true ],
219 '2 days 1 hour 1 minute 1 second',
220 'formatTimePeriod() rounding, recursion, (>48h)'
226 * @covers Language::truncateForDatabase
227 * @covers Language::truncateInternal
229 public function testTruncateForDatabase() {
232 $this->getLang()->truncateForDatabase( "1234567890", 0, 'XXX' ),
233 'truncate prefix, len 0, small ellipsis'
238 $this->getLang()->truncateForDatabase( "1234567890", 8, 'XXX' ),
239 'truncate prefix, small ellipsis'
244 $this->getLang()->truncateForDatabase( "123456789", 5, 'XXXXXXXXXXXXXXX' ),
245 'truncate prefix, large ellipsis'
250 $this->getLang()->truncateForDatabase( "1234567890", -8, 'XXX' ),
251 'truncate suffix, small ellipsis'
256 $this->getLang()->truncateForDatabase( "123456789", -5, 'XXXXXXXXXXXXXXX' ),
257 'truncate suffix, large ellipsis'
261 $this->getLang()->truncateForDatabase( "123 ", 9, 'XXX' ),
262 'truncate prefix, with spaces'
266 $this->getLang()->truncateForDatabase( "12345 8", 11, 'XXX' ),
267 'truncate prefix, with spaces and non-space ending'
271 $this->getLang()->truncateForDatabase( "1 234", -8, 'XXX' ),
272 'truncate suffix, with spaces'
276 $this->getLang()->truncateForDatabase( "1234567890", 5, 'XXX', false ),
277 'truncate without adjustment'
281 $this->getLang()->truncateForDatabase( "泰乐菌素123456789", 11, '...', false ),
282 'truncate does not chop Unicode characters in half'
286 $this->getLang()->truncateForDatabase( "\n泰乐菌素123456789", 12, '...', false ),
287 'truncate does not chop Unicode characters in half if there is a preceding newline'
292 * @dataProvider provideTruncateData
293 * @covers Language::truncateForVisual
294 * @covers Language::truncateInternal
296 public function testTruncateForVisual(
297 $expected, $string, $length, $ellipsis = '...', $adjustLength = true
301 $this->getLang()->truncateForVisual( $string, $length, $ellipsis, $adjustLength )
306 * @return array Format is ($expected, $string, $length, $ellipsis, $adjustLength)
308 public static function provideTruncateData() {
310 [ "XXX", "тестирам да ли ради", 0, "XXX" ],
311 [ "testnXXX", "testni scenarij", 8, "XXX" ],
312 [ "حالة اختبار", "حالة اختبار", 5, "XXXXXXXXXXXXXXX" ],
313 [ "XXXедент", "прецедент", -8, "XXX" ],
314 [ "XXപിൾ", "ആപ്പിൾ", -5, "XX" ],
315 [ "神秘XXX", "神秘 ", 9, "XXX" ],
316 [ "ΔημιουργXXX", "Δημιουργία Σύμπαντος", 11, "XXX" ],
317 [ "XXXの家です", "地球は私たちの唯 の家です", -8, "XXX" ],
318 [ "زندگیXXX", "زندگی زیباست", 6, "XXX", false ],
319 [ "ცხოვრება...", "ცხოვრება არის საოცარი", 8, "...", false ],
320 [ "\nທ່ານ...", "\nທ່ານບໍ່ຮູ້ຫນັງສື", 5, "...", false ],
325 * @dataProvider provideHTMLTruncateData
326 * @covers Language::truncateHTML
328 public function testTruncateHtml( $len, $ellipsis, $input, $expected ) {
332 $this->getLang()->truncateHtml( $input, $len, $ellipsis )
337 * @return array Format is ($len, $ellipsis, $input, $expected)
339 public static function provideHTMLTruncateData() {
341 [ 0, 'XXX', "1234567890", "XXX" ],
342 [ 8, 'XXX', "1234567890", "12345XXX" ],
343 [ 5, 'XXXXXXXXXXXXXXX', '1234567890', "1234567890" ],
345 '<p><span style="font-weight:bold;"></span></p>',
346 '<p><span style="font-weight:bold;"></span></p>',
349 '<p><span style="font-weight:bold;">123456789</span></p>',
350 '<p><span style="font-weight:bold;">***</span></p>',
353 '<p><span style="font-weight:bold;"> 23456789</span></p>',
354 '<p><span style="font-weight:bold;">***</span></p>',
357 '<p><span style="font-weight:bold;">123456789</span></p>',
358 '<p><span style="font-weight:bold;">***</span></p>',
361 '<p><span style="font-weight:bold;">123456789</span></p>',
362 '<p><span style="font-weight:bold;">1***</span></p>',
365 '<tt><span style="font-weight:bold;">123456789</span></tt>',
366 '<tt><span style="font-weight:bold;">12***</span></tt>',
369 '<p><a href="www.mediawiki.org">123456789</a></p>',
370 '<p><a href="www.mediawiki.org">123***</a></p>',
373 '<p><a href="www.mediawiki.org">12 456789</a></p>',
374 '<p><a href="www.mediawiki.org">12 ***</a></p>',
377 '<small><span style="font-weight:bold;">123<p id="#moo">456</p>789</span></small>',
378 '<small><span style="font-weight:bold;">123<p id="#moo">4***</p></span></small>',
381 '<div><span style="font-weight:bold;">123<span>4</span>56789</span></div>',
382 '<div><span style="font-weight:bold;">123<span>4</span>5***</span></div>',
385 '<p><table style="font-weight:bold;"><tr><td>123456789</td></tr></table></p>',
386 '<p><table style="font-weight:bold;"><tr><td>123456789</td></tr></table></p>',
389 '<p><font style="font-weight:bold;">123456789</font></p>',
390 '<p><font style="font-weight:bold;">123456789</font></p>',
396 * Test Language::isWellFormedLanguageTag()
397 * @dataProvider provideWellFormedLanguageTags
398 * @covers Language::isWellFormedLanguageTag
400 public function testWellFormedLanguageTag( $code, $message = '' ) {
402 Language
::isWellFormedLanguageTag( $code ),
403 "validating code $code $message"
408 * The test cases are based on the tests in the GaBuZoMeu parser
409 * written by Stéphane Bortzmeyer <bortzmeyer@nic.fr>
410 * and distributed as free software, under the GNU General Public Licence.
411 * http://www.bortzmeyer.org/gabuzomeu-parsing-language-tags.html
413 public static function provideWellFormedLanguageTags() {
415 [ 'fr', 'two-letter code' ],
416 [ 'fr-latn', 'two-letter code with lower case script code' ],
417 [ 'fr-Latn-FR', 'two-letter code with title case script code and uppercase country code' ],
418 [ 'fr-Latn-419', 'two-letter code with title case script code and region number' ],
419 [ 'fr-FR', 'two-letter code with uppercase' ],
420 [ 'ax-TZ', 'Not in the registry, but well-formed' ],
421 [ 'fr-shadok', 'two-letter code with variant' ],
422 [ 'fr-y-myext-myext2', 'non-x singleton' ],
423 [ 'fra-Latn', 'ISO 639 can be 3-letters' ],
424 [ 'fra', 'three-letter language code' ],
425 [ 'fra-FX', 'three-letter language code with country code' ],
426 [ 'i-klingon', 'grandfathered with singleton' ],
427 [ 'I-kLINgon', 'tags are case-insensitive...' ],
428 [ 'no-bok', 'grandfathered without singleton' ],
429 [ 'i-enochian', 'Grandfathered' ],
430 [ 'x-fr-CH', 'private use' ],
431 [ 'es-419', 'two-letter code with region number' ],
432 [ 'en-Latn-GB-boont-r-extended-sequence-x-private', 'weird, but well-formed' ],
433 [ 'ab-x-abc-x-abc', 'anything goes after x' ],
434 [ 'ab-x-abc-a-a', 'anything goes after x, including several non-x singletons' ],
435 [ 'i-default', 'grandfathered' ],
436 [ 'abcd-Latn', 'Language of 4 chars reserved for future use' ],
437 [ 'AaBbCcDd-x-y-any-x', 'Language of 5-8 chars, registered' ],
438 [ 'de-CH-1901', 'with country and year' ],
439 [ 'en-US-x-twain', 'with country and singleton' ],
440 [ 'zh-cmn', 'three-letter variant' ],
441 [ 'zh-cmn-Hant', 'three-letter variant and script' ],
442 [ 'zh-cmn-Hant-HK', 'three-letter variant, script and country' ],
443 [ 'xr-p-lze', 'Extension' ],
448 * Negative test for Language::isWellFormedLanguageTag()
449 * @dataProvider provideMalformedLanguageTags
450 * @covers Language::isWellFormedLanguageTag
452 public function testMalformedLanguageTag( $code, $message = '' ) {
454 Language
::isWellFormedLanguageTag( $code ),
455 "validating that code $code is a malformed language tag - $message"
460 * The test cases are based on the tests in the GaBuZoMeu parser
461 * written by Stéphane Bortzmeyer <bortzmeyer@nic.fr>
462 * and distributed as free software, under the GNU General Public Licence.
463 * http://www.bortzmeyer.org/gabuzomeu-parsing-language-tags.html
465 public static function provideMalformedLanguageTags() {
467 [ 'f', 'language too short' ],
468 [ 'f-Latn', 'language too short with script' ],
469 [ 'xr-lxs-qut', 'variants too short' ], # extlangS
470 [ 'fr-Latn-F', 'region too short' ],
471 [ 'a-value', 'language too short with region' ],
472 [ 'tlh-a-b-foo', 'valid three-letter with wrong variant' ],
475 'grandfathered but not registered: invalid, even if we only test well-formedness'
477 [ 'abcdefghi-012345678', 'numbers too long' ],
478 [ 'ab-abc-abc-abc-abc', 'invalid extensions' ],
479 [ 'ab-abcd-abc', 'invalid extensions' ],
480 [ 'ab-ab-abc', 'invalid extensions' ],
481 [ 'ab-123-abc', 'invalid extensions' ],
482 [ 'a-Hant-ZH', 'short language with valid extensions' ],
483 [ 'a1-Hant-ZH', 'invalid character in language' ],
484 [ 'ab-abcde-abc', 'invalid extensions' ],
485 [ 'ab-1abc-abc', 'invalid characters in extensions' ],
486 [ 'ab-ab-abcd', 'invalid order of extensions' ],
487 [ 'ab-123-abcd', 'invalid order of extensions' ],
488 [ 'ab-abcde-abcd', 'invalid extensions' ],
489 [ 'ab-1abc-abcd', 'invalid characters in extensions' ],
490 [ 'ab-a-b', 'extensions too short' ],
491 [ 'ab-a-x', 'extensions too short, even with singleton' ],
492 [ 'ab--ab', 'two separators' ],
493 [ 'ab-abc-', 'separator in the end' ],
494 [ '-ab-abc', 'separator in the beginning' ],
495 [ 'abcd-efg', 'language too long' ],
496 [ 'aabbccddE', 'tag too long' ],
497 [ 'pa_guru', 'A tag with underscore is invalid in strict mode' ],
498 [ 'de-f', 'subtag too short' ],
503 * Negative test for Language::isWellFormedLanguageTag()
504 * @covers Language::isWellFormedLanguageTag
506 public function testLenientLanguageTag() {
508 Language
::isWellFormedLanguageTag( 'pa_guru', true ),
509 'pa_guru is a well-formed language tag in lenient mode'
514 * Test Language::isValidBuiltInCode()
515 * @dataProvider provideLanguageCodes
516 * @covers Language::isValidBuiltInCode
518 public function testBuiltInCodeValidation( $code, $expected, $message = '' ) {
519 $this->assertEquals( $expected,
520 (bool)Language
::isValidBuiltInCode( $code ),
521 "validating code $code $message"
525 public static function provideLanguageCodes() {
527 [ 'fr', true, 'Two letters, minor case' ],
528 [ 'EN', false, 'Two letters, upper case' ],
529 [ 'tyv', true, 'Three letters' ],
530 [ 'be-tarask', true, 'With dash' ],
531 [ 'be-x-old', true, 'With extension (two dashes)' ],
532 [ 'be_tarask', false, 'Reject underscores' ],
537 * Test Language::isKnownLanguageTag()
538 * @dataProvider provideKnownLanguageTags
539 * @covers Language::isKnownLanguageTag
541 public function testKnownLanguageTag( $code, $message = '' ) {
543 (bool)Language
::isKnownLanguageTag( $code ),
544 "validating code $code - $message"
548 public static function provideKnownLanguageTags() {
550 [ 'fr', 'simple code' ],
551 [ 'bat-smg', 'an MW legacy tag' ],
552 [ 'sgs', 'an internal standard MW name, for which a legacy tag is used externally' ],
557 * @covers Language::isKnownLanguageTag
559 public function testKnownCldrLanguageTag() {
560 if ( !class_exists( 'LanguageNames' ) ) {
561 $this->markTestSkipped( 'The LanguageNames class is not available. '
562 . 'The CLDR extension is probably not installed.' );
566 (bool)Language
::isKnownLanguageTag( 'pal' ),
567 'validating code "pal" an ancient language, which probably will '
568 . 'not appear in Names.php, but appears in CLDR in English'
573 * Negative tests for Language::isKnownLanguageTag()
574 * @dataProvider provideUnKnownLanguageTags
575 * @covers Language::isKnownLanguageTag
577 public function testUnknownLanguageTag( $code, $message = '' ) {
579 (bool)Language
::isKnownLanguageTag( $code ),
580 "checking that code $code is invalid - $message"
584 public static function provideUnknownLanguageTags() {
586 [ 'mw', 'non-existent two-letter code' ],
587 [ 'foo"<bar', 'very invalid language code' ],
592 * Test too short timestamp
593 * @expectedException MWException
594 * @covers Language::sprintfDate
596 public function testSprintfDateTooShortTimestamp() {
597 $this->getLang()->sprintfDate( 'xiY', '1234567890123' );
601 * Test too long timestamp
602 * @expectedException MWException
603 * @covers Language::sprintfDate
605 public function testSprintfDateTooLongTimestamp() {
606 $this->getLang()->sprintfDate( 'xiY', '123456789012345' );
610 * Test too short timestamp
611 * @expectedException MWException
612 * @covers Language::sprintfDate
614 public function testSprintfDateNotAllDigitTimestamp() {
615 $this->getLang()->sprintfDate( 'xiY', '-1234567890123' );
619 * @dataProvider provideSprintfDateSamples
620 * @covers Language::sprintfDate
622 public function testSprintfDate( $format, $ts, $expected, $msg ) {
626 $this->getLang()->sprintfDate( $format, $ts, null, $ttl ),
627 "sprintfDate('$format', '$ts'): $msg"
630 $dt = new DateTime( $ts );
631 $lastValidTS = $dt->add( new DateInterval( 'PT' . ( $ttl - 1 ) . 'S' ) )->format( 'YmdHis' );
634 $this->getLang()->sprintfDate( $format, $lastValidTS, null ),
635 "sprintfDate('$format', '$ts'): TTL $ttl too high (output was different at $lastValidTS)"
638 // advance the time enough to make all of the possible outputs different (except possibly L)
639 $dt = new DateTime( $ts );
640 $newTS = $dt->add( new DateInterval( 'P1Y1M8DT13H1M1S' ) )->format( 'YmdHis' );
643 $this->getLang()->sprintfDate( $format, $newTS, null ),
644 "sprintfDate('$format', '$ts'): Missing TTL (output was different at $newTS)"
650 * sprintfDate should always use UTC when no zone is given.
651 * @dataProvider provideSprintfDateSamples
652 * @covers Language::sprintfDate
654 public function testSprintfDateNoZone( $format, $ts, $expected, $ignore, $msg ) {
655 $oldTZ = date_default_timezone_get();
656 $res = date_default_timezone_set( 'Asia/Seoul' );
658 $this->markTestSkipped( "Error setting Timezone" );
663 $this->getLang()->sprintfDate( $format, $ts ),
664 "sprintfDate('$format', '$ts'): $msg"
667 date_default_timezone_set( $oldTZ );
671 * sprintfDate should use passed timezone
672 * @dataProvider provideSprintfDateSamples
673 * @covers Language::sprintfDate
675 public function testSprintfDateTZ( $format, $ts, $ignore, $expected, $msg ) {
676 $tz = new DateTimeZone( 'Asia/Seoul' );
678 $this->markTestSkipped( "Error getting Timezone" );
683 $this->getLang()->sprintfDate( $format, $ts, $tz ),
684 "sprintfDate('$format', '$ts', 'Asia/Seoul'): $msg"
689 * sprintfDate should only calculate a TTL if the caller is going to use it.
690 * @covers Language::sprintfDate
692 public function testSprintfDateNoTtlIfNotNeeded() {
693 $noTtl = 'unused'; // Value used to represent that the caller didn't pass a variable in.
695 $this->getLang()->sprintfDate( 'YmdHis', wfTimestampNow(), null, $noTtl );
696 $this->getLang()->sprintfDate( 'YmdHis', wfTimestampNow(), null, $ttl );
701 'If the caller does not set the $ttl variable, do not compute it.'
703 $this->assertInternalType( 'int', $ttl, 'TTL should have been computed.' );
706 public static function provideSprintfDateSamples() {
711 '1390', // note because we're testing English locale we get Latin-standard digits
713 'Iranian calendar full year'
720 'Iranian calendar short year'
727 'ISO 8601 (week) year'
750 // What follows is mostly copied from
751 // https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions#.23time
778 'Month index, not zero pad'
785 'Month index. Zero pad'
806 'Genitive month name (same in EN)'
813 'Day of month (not zero pad)'
820 'Day of month (zero-pad)'
827 'Day of year (zero-indexed)'
834 'Day of week (abbrev)'
848 'Day of week (Mon=1, Sun=7)'
855 'Day of week (Sun=0, Sat=6)'
897 '12 hour, zero padded'
946 'Days in current month'
951 '2012-01-02T09:07:05+00:00',
952 '2012-01-02T09:07:05+09:00',
958 'Mon, 02 Jan 2012 09:07:05 +0000',
959 'Mon, 02 Jan 2012 09:07:05 +0900',
967 'Timezone identifier'
988 'Timezone offset with colon'
995 'Timezone abbreviation'
1002 'Timezone offset in seconds'
1030 'Hebrew number of days in month'
1037 'Hebrew genitive month name (No difference in EN)'
1072 'nengo - last day of heisei'
1079 'nengo - first day of reiwa'
1086 'nengo - second year of reiwa'
1107 'Raw numerals (doesn\'t mean much in EN)'
1110 '[[Y "(yea"\\r)]] \\"xx\\"',
1112 '[[2012 (year)]] "x"',
1113 '[[2012 (year)]] "x"',
1121 * @dataProvider provideFormatSizes
1122 * @covers Language::formatSize
1124 public function testFormatSize( $size, $expected, $msg ) {
1125 $this->assertEquals(
1127 $this->getLang()->formatSize( $size ),
1128 "formatSize('$size'): $msg"
1132 public static function provideFormatSizes() {
1179 // How big!? THIS BIG!
1184 * @dataProvider provideFormatBitrate
1185 * @covers Language::formatBitrate
1187 public function testFormatBitrate( $bps, $expected, $msg ) {
1188 $this->assertEquals(
1190 $this->getLang()->formatBitrate( $bps ),
1191 "formatBitrate('$bps'): $msg"
1195 public static function provideFormatBitrate() {
1205 "999 bits per second"
1210 "1 kilobit per second"
1215 "1 megabit per second"
1220 "1 gigabit per second"
1225 "1 terabit per second"
1230 "1 petabit per second"
1235 "1 exabit per second"
1240 "1 zetabit per second"
1245 "1 yottabit per second"
1250 "1,000 yottabits per second"
1256 * @dataProvider provideFormatDuration
1257 * @covers Language::formatDuration
1259 public function testFormatDuration( $duration, $expected, $intervals = [] ) {
1260 $this->assertEquals(
1262 $this->getLang()->formatDuration( $duration, $intervals ),
1263 "formatDuration('$duration'): $expected"
1267 public static function provideFormatDuration() {
1306 // ( 365 + ( 24 * 3 + 25 ) / 400 ) * 86400 = 31556952
1307 ( 365 +
( 24 * 3 +
25 ) / 400.0 ) * 86400,
1340 '2 hours, 30 minutes and 1 second'
1344 '1 hour and 1 second'
1347 31556952 +
2 * 86400 +
9000,
1348 '1 year, 2 days, 2 hours and 30 minutes'
1351 42 * 1000 * 31556952 +
42,
1352 '42 millennia and 42 seconds'
1370 31556952 +
2 * 86400 +
9000,
1371 '1 year, 2 days and 150 minutes',
1372 [ 'years', 'days', 'minutes' ],
1377 [ 'years', 'days' ],
1380 31556952 +
2 * 86400 +
9000,
1381 '1 year, 2 days and 150 minutes',
1382 [ 'minutes', 'days', 'years' ],
1387 [ 'days', 'years' ],
1393 * @dataProvider provideCheckTitleEncodingData
1394 * @covers Language::checkTitleEncoding
1396 public function testCheckTitleEncoding( $s ) {
1397 $this->assertEquals(
1399 $this->getLang()->checkTitleEncoding( $s ),
1400 "checkTitleEncoding('$s')"
1404 public static function provideCheckTitleEncodingData() {
1405 // phpcs:disable Generic.Files.LineLength
1408 [ "United States of America" ], // 7bit ASCII
1409 [ rawurldecode( "S%C3%A9rie%20t%C3%A9l%C3%A9vis%C3%A9e" ) ],
1412 "Acteur%7CAlbert%20Robbins%7CAnglais%7CAnn%20Donahue%7CAnthony%20E.%20Zuiker%7CCarol%20Mendelsohn"
1415 // The following two data sets come from T38839. They fail if checkTitleEncoding uses a regexp to test for
1416 // valid UTF-8 encoding and the pcre.recursion_limit is low (like, say, 1024). They succeed if checkTitleEncoding
1417 // uses mb_check_encoding for its test.
1420 "Acteur%7CAlbert%20Robbins%7CAnglais%7CAnn%20Donahue%7CAnthony%20E.%20Zuiker%7CCarol%20Mendelsohn%7C"
1421 . "Catherine%20Willows%7CDavid%20Hodges%7CDavid%20Phillips%7CGil%20Grissom%7CGreg%20Sanders%7CHodges%7C"
1422 . "Internet%20Movie%20Database%7CJim%20Brass%7CLady%20Heather%7C"
1423 . "Les%20Experts%20(s%C3%A9rie%20t%C3%A9l%C3%A9vis%C3%A9e)%7CLes%20Experts%20:%20Manhattan%7C"
1424 . "Les%20Experts%20:%20Miami%7CListe%20des%20personnages%20des%20Experts%7C"
1425 . "Liste%20des%20%C3%A9pisodes%20des%20Experts%7CMod%C3%A8le%20discussion:Palette%20Les%20Experts%7C"
1426 . "Nick%20Stokes%7CPersonnage%20de%20fiction%7CPersonnage%20fictif%7CPersonnage%20de%20fiction%7C"
1427 . "Personnages%20r%C3%A9currents%20dans%20Les%20Experts%7CRaymond%20Langston%7CRiley%20Adams%7C"
1428 . "Saison%201%20des%20Experts%7CSaison%2010%20des%20Experts%7CSaison%2011%20des%20Experts%7C"
1429 . "Saison%2012%20des%20Experts%7CSaison%202%20des%20Experts%7CSaison%203%20des%20Experts%7C"
1430 . "Saison%204%20des%20Experts%7CSaison%205%20des%20Experts%7CSaison%206%20des%20Experts%7C"
1431 . "Saison%207%20des%20Experts%7CSaison%208%20des%20Experts%7CSaison%209%20des%20Experts%7C"
1432 . "Sara%20Sidle%7CSofia%20Curtis%7CS%C3%A9rie%20t%C3%A9l%C3%A9vis%C3%A9e%7CWallace%20Langham%7C"
1433 . "Warrick%20Brown%7CWendy%20Simms%7C%C3%89tats-Unis"
1438 "Mod%C3%A8le%3AArrondissements%20homonymes%7CMod%C3%A8le%3ABandeau%20standard%20pour%20page%20d'homonymie%7C"
1439 . "Mod%C3%A8le%3ABatailles%20homonymes%7CMod%C3%A8le%3ACantons%20homonymes%7C"
1440 . "Mod%C3%A8le%3ACommunes%20fran%C3%A7aises%20homonymes%7CMod%C3%A8le%3AFilms%20homonymes%7C"
1441 . "Mod%C3%A8le%3AGouvernements%20homonymes%7CMod%C3%A8le%3AGuerres%20homonymes%7CMod%C3%A8le%3AHomonymie%7C"
1442 . "Mod%C3%A8le%3AHomonymie%20bateau%7CMod%C3%A8le%3AHomonymie%20d'%C3%A9tablissements%20scolaires%20ou"
1443 . "%20universitaires%7CMod%C3%A8le%3AHomonymie%20d'%C3%AEles%7CMod%C3%A8le%3AHomonymie%20de%20clubs%20sportifs%7C"
1444 . "Mod%C3%A8le%3AHomonymie%20de%20comt%C3%A9s%7CMod%C3%A8le%3AHomonymie%20de%20monument%7C"
1445 . "Mod%C3%A8le%3AHomonymie%20de%20nom%20romain%7CMod%C3%A8le%3AHomonymie%20de%20parti%20politique%7C"
1446 . "Mod%C3%A8le%3AHomonymie%20de%20route%7CMod%C3%A8le%3AHomonymie%20dynastique%7C"
1447 . "Mod%C3%A8le%3AHomonymie%20vid%C3%A9oludique%7CMod%C3%A8le%3AHomonymie%20%C3%A9difice%20religieux%7C"
1448 . "Mod%C3%A8le%3AInternationalisation%7CMod%C3%A8le%3AIsom%C3%A9rie%7CMod%C3%A8le%3AParonymie%7C"
1449 . "Mod%C3%A8le%3APatronyme%7CMod%C3%A8le%3APatronyme%20basque%7CMod%C3%A8le%3APatronyme%20italien%7C"
1450 . "Mod%C3%A8le%3APatronymie%7CMod%C3%A8le%3APersonnes%20homonymes%7CMod%C3%A8le%3ASaints%20homonymes%7C"
1451 . "Mod%C3%A8le%3ATitres%20homonymes%7CMod%C3%A8le%3AToponymie%7CMod%C3%A8le%3AUnit%C3%A9s%20homonymes%7C"
1452 . "Mod%C3%A8le%3AVilles%20homonymes%7CMod%C3%A8le%3A%C3%89difices%20religieux%20homonymes"
1460 * @dataProvider provideRomanNumeralsData
1461 * @covers Language::romanNumeral
1463 public function testRomanNumerals( $num, $numerals ) {
1464 $this->assertEquals(
1466 Language
::romanNumeral( $num ),
1467 "romanNumeral('$num')"
1471 public static function provideRomanNumeralsData() {
1504 [ 1989, 'MCMLXXXIX' ],
1510 [ 7000, 'MMMMMMM' ],
1511 [ 8000, 'MMMMMMMM' ],
1512 [ 9000, 'MMMMMMMMM' ],
1513 [ 9999, 'MMMMMMMMMCMXCIX' ],
1514 [ 10000, 'MMMMMMMMMM' ],
1519 * @dataProvider provideHebrewNumeralsData
1520 * @covers Language::hebrewNumeral
1522 public function testHebrewNumeral( $num, $numerals ) {
1523 $this->assertEquals(
1525 Language
::hebrewNumeral( $num ),
1526 "hebrewNumeral('$num')"
1530 public static function provideHebrewNumeralsData() {
1573 [ 2000, "ב' אלפים" ],
1575 [ 3000, "ג' אלפים" ],
1576 [ 4000, "ד' אלפים" ],
1577 [ 4904, "ד'תתק\"ד" ],
1578 [ 5000, "ה' אלפים" ],
1579 [ 5680, "ה'תר\"ף" ],
1580 [ 5690, "ה'תר\"ץ" ],
1581 [ 5708, "ה'תש\"ח" ],
1582 [ 5720, "ה'תש\"ך" ],
1583 [ 5740, "ה'תש\"ם" ],
1584 [ 5750, "ה'תש\"ן" ],
1585 [ 5775, "ה'תשע\"ה" ],
1590 * @dataProvider providePluralData
1591 * @covers Language::convertPlural
1593 public function testConvertPlural( $expected, $number, $forms ) {
1594 $chosen = $this->getLang()->convertPlural( $number, $forms );
1595 $this->assertEquals( $expected, $chosen );
1598 public static function providePluralData() {
1599 // Params are: [expected text, number given, [the plural forms]]
1602 'singular', 'plural'
1604 [ 'explicit zero', 0, [
1605 '0=explicit zero', 'singular', 'plural'
1607 [ 'explicit one', 1, [
1608 'singular', 'plural', '1=explicit one',
1611 'singular', 'plural', '0=explicit zero',
1614 '0=explicit zero', '1=explicit one', 'singular', 'plural'
1616 [ 'explicit eleven', 11, [
1617 'singular', 'plural', '11=explicit eleven',
1620 'singular', 'plural', '11=explicit twelve',
1623 'singular', 'plural', '=explicit form',
1626 'kissa=kala', '1=2=3', 'other',
1629 '0=explicit zero', '1=explicit one',
1635 * @covers Language::embedBidi()
1637 public function testEmbedBidi() {
1638 $lre = "\u{202A}"; // U+202A LEFT-TO-RIGHT EMBEDDING
1639 $rle = "\u{202B}"; // U+202B RIGHT-TO-LEFT EMBEDDING
1640 $pdf = "\u{202C}"; // U+202C POP DIRECTIONAL FORMATTING
1641 $lang = $this->getLang();
1642 $this->assertEquals(
1644 $lang->embedBidi( '123' ),
1645 'embedBidi with neutral argument'
1647 $this->assertEquals(
1648 $lre . 'Ben_(WMF)' . $pdf,
1649 $lang->embedBidi( 'Ben_(WMF)' ),
1650 'embedBidi with LTR argument'
1652 $this->assertEquals(
1653 $rle . 'יהודי (מנוחין)' . $pdf,
1654 $lang->embedBidi( 'יהודי (מנוחין)' ),
1655 'embedBidi with RTL argument'
1660 * @covers Language::translateBlockExpiry()
1661 * @dataProvider provideTranslateBlockExpiry
1663 public function testTranslateBlockExpiry( $expectedData, $str, $now, $desc ) {
1664 $lang = $this->getLang();
1665 if ( is_array( $expectedData ) ) {
1666 list( $func, $arg ) = $expectedData;
1667 $expected = $lang->$func( $arg );
1669 $expected = $expectedData;
1671 $this->assertEquals( $expected, $lang->translateBlockExpiry( $str, null, $now ), $desc );
1674 public static function provideTranslateBlockExpiry() {
1676 [ '2 hours', '2 hours', 0, 'simple data from ipboptions' ],
1677 [ 'indefinite', 'infinite', 0, 'infinite from ipboptions' ],
1678 [ 'indefinite', 'infinity', 0, 'alternative infinite from ipboptions' ],
1679 [ 'indefinite', 'indefinite', 0, 'another alternative infinite from ipboptions' ],
1680 [ [ 'formatDuration', 1023 * 60 * 60 ], '1023 hours', 0, 'relative' ],
1681 [ [ 'formatDuration', -1023 ], '-1023 seconds', 0, 'negative relative' ],
1683 [ 'formatDuration', 1023 * 60 * 60 ],
1685 wfTimestamp( TS_UNIX
, '19910203040506' ),
1686 'relative with initial timestamp'
1688 [ [ 'formatDuration', 0 ], 'now', 0, 'now' ],
1690 [ 'timeanddate', '20120102070000' ],
1691 '2012-1-1 7:00 +1 day',
1693 'mixed, handled as absolute'
1695 [ [ 'timeanddate', '19910203040506' ], '1991-2-3 4:05:06', 0, 'absolute' ],
1696 [ [ 'timeanddate', '19700101000000' ], '1970-1-1 0:00:00', 0, 'absolute at epoch' ],
1697 [ [ 'timeanddate', '19691231235959' ], '1969-12-31 23:59:59', 0, 'time before epoch' ],
1699 [ 'timeanddate', '19910910000000' ],
1701 wfTimestamp( TS_UNIX
, '19910203040506' ),
1704 [ 'dummy', 'dummy', 0, 'return garbage as is' ],
1709 * @dataProvider provideFormatNum
1710 * @covers Language::formatNum
1712 public function testFormatNum(
1713 $translateNumerals, $langCode, $number, $nocommafy, $expected
1715 $this->setMwGlobals( [ 'wgTranslateNumerals' => $translateNumerals ] );
1716 $lang = Language
::factory( $langCode );
1717 $formattedNum = $lang->formatNum( $number, $nocommafy );
1718 $this->assertType( 'string', $formattedNum );
1719 $this->assertEquals( $expected, $formattedNum );
1722 public function provideFormatNum() {
1724 [ true, 'en', 100, false, '100' ],
1725 [ true, 'en', 101, true, '101' ],
1726 [ false, 'en', 103, false, '103' ],
1727 [ false, 'en', 104, true, '104' ],
1728 [ true, 'en', '105', false, '105' ],
1729 [ true, 'en', '106', true, '106' ],
1730 [ false, 'en', '107', false, '107' ],
1731 [ false, 'en', '108', true, '108' ],
1736 * @covers Language::parseFormattedNumber
1737 * @dataProvider parseFormattedNumberProvider
1739 public function testParseFormattedNumber( $langCode, $number ) {
1740 $lang = Language
::factory( $langCode );
1742 $localisedNum = $lang->formatNum( $number );
1743 $normalisedNum = $lang->parseFormattedNumber( $localisedNum );
1745 $this->assertEquals( $number, $normalisedNum );
1748 public function parseFormattedNumberProvider() {
1755 [ 'zh-classical', 7432 ]
1760 * @covers Language::commafy()
1761 * @dataProvider provideCommafyData
1763 public function testCommafy( $number, $numbersWithCommas ) {
1764 $this->assertEquals(
1766 $this->getLang()->commafy( $number ),
1767 "commafy('$number')"
1771 public static function provideCommafyData() {
1777 [ 10000, '10,000' ],
1778 [ 100000, '100,000' ],
1779 [ 1000000, '1,000,000' ],
1780 [ -1.0001, '-1.0001' ],
1781 [ 1.0001, '1.0001' ],
1782 [ 10.0001, '10.0001' ],
1783 [ 100.0001, '100.0001' ],
1784 [ 1000.0001, '1,000.0001' ],
1785 [ 10000.0001, '10,000.0001' ],
1786 [ 100000.0001, '100,000.0001' ],
1787 [ 1000000.0001, '1,000,000.0001' ],
1788 [ '200000000000000000000', '200,000,000,000,000,000,000' ],
1789 [ '-200000000000000000000', '-200,000,000,000,000,000,000' ],
1794 * @covers Language::listToText
1796 public function testListToText() {
1797 $lang = $this->getLang();
1798 $and = $lang->getMessageFromDB( 'and' );
1799 $s = $lang->getMessageFromDB( 'word-separator' );
1800 $c = $lang->getMessageFromDB( 'comma-separator' );
1802 $this->assertEquals( '', $lang->listToText( [] ) );
1803 $this->assertEquals( 'a', $lang->listToText( [ 'a' ] ) );
1804 $this->assertEquals( "a{$and}{$s}b", $lang->listToText( [ 'a', 'b' ] ) );
1805 $this->assertEquals( "a{$c}b{$and}{$s}c", $lang->listToText( [ 'a', 'b', 'c' ] ) );
1806 $this->assertEquals( "a{$c}b{$c}c{$and}{$s}d", $lang->listToText( [ 'a', 'b', 'c', 'd' ] ) );
1810 * @covers Language::clearCaches
1812 public function testClearCaches() {
1813 $languageClass = TestingAccessWrapper
::newFromClass( Language
::class );
1815 // Populate $dataCache
1816 Language
::getLocalisationCache()->getItem( 'zh', 'mainpage' );
1817 $oldCacheObj = Language
::$dataCache;
1818 $this->assertNotCount( 0,
1819 TestingAccessWrapper
::newFromObject( Language
::$dataCache )->loadedItems
);
1821 // Populate $mLangObjCache
1822 $lang = Language
::factory( 'en' );
1823 $this->assertNotCount( 0, Language
::$mLangObjCache );
1825 // Populate $fallbackLanguageCache
1826 Language
::getFallbacksIncludingSiteLanguage( 'en' );
1827 $this->assertNotCount( 0, $languageClass->fallbackLanguageCache
);
1829 // Populate $grammarTransformations
1830 $lang->getGrammarTransformations();
1831 $this->assertNotNull( $languageClass->grammarTransformations
);
1833 // Populate $languageNameCache
1834 Language
::fetchLanguageNames();
1835 $this->assertNotNull( $languageClass->languageNameCache
);
1837 Language
::clearCaches();
1839 $this->assertNotSame( $oldCacheObj, Language
::$dataCache );
1840 $this->assertCount( 0,
1841 TestingAccessWrapper
::newFromObject( Language
::$dataCache )->loadedItems
);
1842 $this->assertCount( 0, Language
::$mLangObjCache );
1843 $this->assertCount( 0, $languageClass->fallbackLanguageCache
);
1844 $this->assertNull( $languageClass->grammarTransformations
);
1845 $this->assertNull( $languageClass->languageNameCache
);
1849 * @dataProvider provideIsSupportedLanguage
1850 * @covers Language::isSupportedLanguage
1852 public function testIsSupportedLanguage( $code, $expected, $comment ) {
1853 $this->assertEquals( $expected, Language
::isSupportedLanguage( $code ), $comment );
1856 public static function provideIsSupportedLanguage() {
1858 [ 'en', true, 'is supported language' ],
1859 [ 'fi', true, 'is supported language' ],
1860 [ 'bunny', false, 'is not supported language' ],
1861 [ 'FI', false, 'is not supported language, input should be in lower case' ],
1866 * @dataProvider provideGetParentLanguage
1867 * @covers Language::getParentLanguage
1869 public function testGetParentLanguage( $code, $expected, $comment ) {
1870 $lang = Language
::factory( $code );
1871 if ( is_null( $expected ) ) {
1872 $this->assertNull( $lang->getParentLanguage(), $comment );
1874 $this->assertEquals( $expected, $lang->getParentLanguage()->getCode(), $comment );
1878 public static function provideGetParentLanguage() {
1880 [ 'zh-cn', 'zh', 'zh is the parent language of zh-cn' ],
1881 [ 'zh', 'zh', 'zh is defined as the parent language of zh, '
1882 . 'because zh converter can convert zh-cn to zh' ],
1883 [ 'zh-invalid', null, 'do not be fooled by arbitrarily composed language codes' ],
1884 [ 'de-formal', null, 'de does not have converter' ],
1885 [ 'de', null, 'de does not have converter' ],
1890 * @dataProvider provideGetNamespaceAliases
1891 * @covers Language::getNamespaceAliases
1893 public function testGetNamespaceAliases( $languageCode, $subset ) {
1894 $language = Language
::factory( $languageCode );
1895 $aliases = $language->getNamespaceAliases();
1896 foreach ( $subset as $alias => $nsId ) {
1897 $this->assertEquals( $nsId, $aliases[$alias] );
1901 public static function provideGetNamespaceAliases() {
1902 // TODO: Add tests for NS_PROJECT_TALK and GenderNamespaces
1915 * @covers Language::hasVariant
1917 public function testHasVariant() {
1918 // See LanguageSrTest::testHasVariant() for additional tests
1919 $en = Language
::factory( 'en' );
1920 $this->assertTrue( $en->hasVariant( 'en' ), 'base is always a variant' );
1921 $this->assertFalse( $en->hasVariant( 'en-bogus' ), 'bogus en variant' );
1923 $bogus = Language
::factory( 'bogus' );
1924 $this->assertTrue( $bogus->hasVariant( 'bogus' ), 'base is always a variant' );
1928 * @covers Language::equals
1930 public function testEquals() {
1931 $en1 = Language
::factory( 'en' );
1932 $en2 = Language
::factory( 'en' );
1933 $en3 = new Language();
1934 $this->assertTrue( $en1->equals( $en2 ), 'en1 equals en2' );
1935 $this->assertTrue( $en2->equals( $en3 ), 'en2 equals en3' );
1936 $this->assertTrue( $en3->equals( $en1 ), 'en3 equals en1' );
1938 $fr = Language
::factory( 'fr' );
1939 $this->assertFalse( $en1->equals( $fr ), 'en not equals fr' );
1941 $ar1 = Language
::factory( 'ar' );
1942 $ar2 = new LanguageAr();
1943 $this->assertTrue( $ar1->equals( $ar2 ), 'ar equals ar' );
1947 * @dataProvider provideUcfirst
1948 * @covers Language::ucfirst
1950 public function testUcfirst( $orig, $expected, $desc, $overrides = false ) {
1951 $lang = new Language();
1952 if ( is_array( $overrides ) ) {
1953 $this->setMwGlobals( [ 'wgOverrideUcfirstCharacters' => $overrides ] );
1955 $this->assertSame( $lang->ucfirst( $orig ), $expected, $desc );
1958 public static function provideUcfirst() {
1960 [ 'alice', 'Alice', 'simple ASCII string', false ],
1961 [ 'århus', 'Århus', 'unicode string', false ],
1962 //overrides do not affect ASCII characters
1963 [ 'foo', 'Foo', 'ASCII is not overriden', [ 'f' => 'b' ] ],
1964 // but they do affect non-ascii ones
1965 [ 'èl', 'Ll' , 'Non-ASCII is overridden', [ 'è' => 'L' ] ],