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), avoidminutes'
146 [ 'avoid' => 'avoidminutes', 'noabbrevs' => true ],
148 'formatTimePeriod() rounding (>48h), avoidminutes'
154 'formatTimePeriod() rounding (>48h), avoidseconds'
158 [ 'avoid' => 'avoidseconds', 'noabbrevs' => true ],
159 '2 days 1 hour 0 minutes',
160 'formatTimePeriod() rounding (>48h), avoidseconds'
166 'formatTimePeriod() rounding (>48h), avoidminutes'
170 [ 'avoid' => 'avoidminutes', 'noabbrevs' => true ],
172 'formatTimePeriod() rounding (>48h), avoidminutes'
178 'formatTimePeriod() rounding (>48h), avoidseconds'
182 [ 'avoid' => 'avoidseconds', 'noabbrevs' => true ],
183 '3 days 0 hours 0 minutes',
184 'formatTimePeriod() rounding (>48h), avoidseconds'
190 'formatTimePeriod() rounding, (>48h), avoidseconds'
194 [ 'avoid' => 'avoidseconds', 'noabbrevs' => true ],
195 '2 days 0 hours 0 minutes',
196 'formatTimePeriod() rounding, (>48h), avoidseconds'
202 'formatTimePeriod() rounding, recursion, (>48h)'
206 [ 'noabbrevs' => true ],
207 '2 days 1 hour 1 minute 1 second',
208 'formatTimePeriod() rounding, recursion, (>48h)'
214 * @covers Language::truncateForDatabase
215 * @covers Language::truncateInternal
217 public function testTruncateForDatabase() {
220 $this->getLang()->truncateForDatabase( "1234567890", 0, 'XXX' ),
221 'truncate prefix, len 0, small ellipsis'
226 $this->getLang()->truncateForDatabase( "1234567890", 8, 'XXX' ),
227 'truncate prefix, small ellipsis'
232 $this->getLang()->truncateForDatabase( "123456789", 5, 'XXXXXXXXXXXXXXX' ),
233 'truncate prefix, large ellipsis'
238 $this->getLang()->truncateForDatabase( "1234567890", -8, 'XXX' ),
239 'truncate suffix, small ellipsis'
244 $this->getLang()->truncateForDatabase( "123456789", -5, 'XXXXXXXXXXXXXXX' ),
245 'truncate suffix, large ellipsis'
249 $this->getLang()->truncateForDatabase( "123 ", 9, 'XXX' ),
250 'truncate prefix, with spaces'
254 $this->getLang()->truncateForDatabase( "12345 8", 11, 'XXX' ),
255 'truncate prefix, with spaces and non-space ending'
259 $this->getLang()->truncateForDatabase( "1 234", -8, 'XXX' ),
260 'truncate suffix, with spaces'
264 $this->getLang()->truncateForDatabase( "1234567890", 5, 'XXX', false ),
265 'truncate without adjustment'
269 $this->getLang()->truncateForDatabase( "泰乐菌素123456789", 11, '...', false ),
270 'truncate does not chop Unicode characters in half'
274 $this->getLang()->truncateForDatabase( "\n泰乐菌素123456789", 12, '...', false ),
275 'truncate does not chop Unicode characters in half if there is a preceding newline'
280 * @dataProvider provideTruncateData
281 * @covers Language::truncateForVisual
282 * @covers Language::truncateInternal
284 public function testTruncateForVisual(
285 $expected, $string, $length, $ellipsis = '...', $adjustLength = true
289 $this->getLang()->truncateForVisual( $string, $length, $ellipsis, $adjustLength )
294 * @return array Format is ($expected, $string, $length, $ellipsis, $adjustLength)
296 public static function provideTruncateData() {
298 [ "XXX", "тестирам да ли ради", 0, "XXX" ],
299 [ "testnXXX", "testni scenarij", 8, "XXX" ],
300 [ "حالة اختبار", "حالة اختبار", 5, "XXXXXXXXXXXXXXX" ],
301 [ "XXXедент", "прецедент", -8, "XXX" ],
302 [ "XXപിൾ", "ആപ്പിൾ", -5, "XX" ],
303 [ "神秘XXX", "神秘 ", 9, "XXX" ],
304 [ "ΔημιουργXXX", "Δημιουργία Σύμπαντος", 11, "XXX" ],
305 [ "XXXの家です", "地球は私たちの唯 の家です", -8, "XXX" ],
306 [ "زندگیXXX", "زندگی زیباست", 6, "XXX", false ],
307 [ "ცხოვრება...", "ცხოვრება არის საოცარი", 8, "...", false ],
308 [ "\nທ່ານ...", "\nທ່ານບໍ່ຮູ້ຫນັງສື", 5, "...", false ],
313 * @dataProvider provideHTMLTruncateData
314 * @covers Language::truncateHTML
316 public function testTruncateHtml( $len, $ellipsis, $input, $expected ) {
320 $this->getLang()->truncateHtml( $input, $len, $ellipsis )
325 * @return array Format is ($len, $ellipsis, $input, $expected)
327 public static function provideHTMLTruncateData() {
329 [ 0, 'XXX', "1234567890", "XXX" ],
330 [ 8, 'XXX', "1234567890", "12345XXX" ],
331 [ 5, 'XXXXXXXXXXXXXXX', '1234567890', "1234567890" ],
333 '<p><span style="font-weight:bold;"></span></p>',
334 '<p><span style="font-weight:bold;"></span></p>',
337 '<p><span style="font-weight:bold;">123456789</span></p>',
338 '<p><span style="font-weight:bold;">***</span></p>',
341 '<p><span style="font-weight:bold;"> 23456789</span></p>',
342 '<p><span style="font-weight:bold;">***</span></p>',
345 '<p><span style="font-weight:bold;">123456789</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;">1***</span></p>',
353 '<tt><span style="font-weight:bold;">123456789</span></tt>',
354 '<tt><span style="font-weight:bold;">12***</span></tt>',
357 '<p><a href="www.mediawiki.org">123456789</a></p>',
358 '<p><a href="www.mediawiki.org">123***</a></p>',
361 '<p><a href="www.mediawiki.org">12 456789</a></p>',
362 '<p><a href="www.mediawiki.org">12 ***</a></p>',
365 '<small><span style="font-weight:bold;">123<p id="#moo">456</p>789</span></small>',
366 '<small><span style="font-weight:bold;">123<p id="#moo">4***</p></span></small>',
369 '<div><span style="font-weight:bold;">123<span>4</span>56789</span></div>',
370 '<div><span style="font-weight:bold;">123<span>4</span>5***</span></div>',
373 '<p><table style="font-weight:bold;"><tr><td>123456789</td></tr></table></p>',
374 '<p><table style="font-weight:bold;"><tr><td>123456789</td></tr></table></p>',
377 '<p><font style="font-weight:bold;">123456789</font></p>',
378 '<p><font style="font-weight:bold;">123456789</font></p>',
384 * Test Language::isWellFormedLanguageTag()
385 * @dataProvider provideWellFormedLanguageTags
386 * @covers Language::isWellFormedLanguageTag
388 public function testWellFormedLanguageTag( $code, $message = '' ) {
390 Language
::isWellFormedLanguageTag( $code ),
391 "validating code $code $message"
396 * The test cases are based on the tests in the GaBuZoMeu parser
397 * written by Stéphane Bortzmeyer <bortzmeyer@nic.fr>
398 * and distributed as free software, under the GNU General Public Licence.
399 * http://www.bortzmeyer.org/gabuzomeu-parsing-language-tags.html
401 public static function provideWellFormedLanguageTags() {
403 [ 'fr', 'two-letter code' ],
404 [ 'fr-latn', 'two-letter code with lower case script code' ],
405 [ 'fr-Latn-FR', 'two-letter code with title case script code and uppercase country code' ],
406 [ 'fr-Latn-419', 'two-letter code with title case script code and region number' ],
407 [ 'fr-FR', 'two-letter code with uppercase' ],
408 [ 'ax-TZ', 'Not in the registry, but well-formed' ],
409 [ 'fr-shadok', 'two-letter code with variant' ],
410 [ 'fr-y-myext-myext2', 'non-x singleton' ],
411 [ 'fra-Latn', 'ISO 639 can be 3-letters' ],
412 [ 'fra', 'three-letter language code' ],
413 [ 'fra-FX', 'three-letter language code with country code' ],
414 [ 'i-klingon', 'grandfathered with singleton' ],
415 [ 'I-kLINgon', 'tags are case-insensitive...' ],
416 [ 'no-bok', 'grandfathered without singleton' ],
417 [ 'i-enochian', 'Grandfathered' ],
418 [ 'x-fr-CH', 'private use' ],
419 [ 'es-419', 'two-letter code with region number' ],
420 [ 'en-Latn-GB-boont-r-extended-sequence-x-private', 'weird, but well-formed' ],
421 [ 'ab-x-abc-x-abc', 'anything goes after x' ],
422 [ 'ab-x-abc-a-a', 'anything goes after x, including several non-x singletons' ],
423 [ 'i-default', 'grandfathered' ],
424 [ 'abcd-Latn', 'Language of 4 chars reserved for future use' ],
425 [ 'AaBbCcDd-x-y-any-x', 'Language of 5-8 chars, registered' ],
426 [ 'de-CH-1901', 'with country and year' ],
427 [ 'en-US-x-twain', 'with country and singleton' ],
428 [ 'zh-cmn', 'three-letter variant' ],
429 [ 'zh-cmn-Hant', 'three-letter variant and script' ],
430 [ 'zh-cmn-Hant-HK', 'three-letter variant, script and country' ],
431 [ 'xr-p-lze', 'Extension' ],
436 * Negative test for Language::isWellFormedLanguageTag()
437 * @dataProvider provideMalformedLanguageTags
438 * @covers Language::isWellFormedLanguageTag
440 public function testMalformedLanguageTag( $code, $message = '' ) {
442 Language
::isWellFormedLanguageTag( $code ),
443 "validating that code $code is a malformed language tag - $message"
448 * The test cases are based on the tests in the GaBuZoMeu parser
449 * written by Stéphane Bortzmeyer <bortzmeyer@nic.fr>
450 * and distributed as free software, under the GNU General Public Licence.
451 * http://www.bortzmeyer.org/gabuzomeu-parsing-language-tags.html
453 public static function provideMalformedLanguageTags() {
455 [ 'f', 'language too short' ],
456 [ 'f-Latn', 'language too short with script' ],
457 [ 'xr-lxs-qut', 'variants too short' ], # extlangS
458 [ 'fr-Latn-F', 'region too short' ],
459 [ 'a-value', 'language too short with region' ],
460 [ 'tlh-a-b-foo', 'valid three-letter with wrong variant' ],
463 'grandfathered but not registered: invalid, even if we only test well-formedness'
465 [ 'abcdefghi-012345678', 'numbers too long' ],
466 [ 'ab-abc-abc-abc-abc', 'invalid extensions' ],
467 [ 'ab-abcd-abc', 'invalid extensions' ],
468 [ 'ab-ab-abc', 'invalid extensions' ],
469 [ 'ab-123-abc', 'invalid extensions' ],
470 [ 'a-Hant-ZH', 'short language with valid extensions' ],
471 [ 'a1-Hant-ZH', 'invalid character in language' ],
472 [ 'ab-abcde-abc', 'invalid extensions' ],
473 [ 'ab-1abc-abc', 'invalid characters in extensions' ],
474 [ 'ab-ab-abcd', 'invalid order of extensions' ],
475 [ 'ab-123-abcd', 'invalid order of extensions' ],
476 [ 'ab-abcde-abcd', 'invalid extensions' ],
477 [ 'ab-1abc-abcd', 'invalid characters in extensions' ],
478 [ 'ab-a-b', 'extensions too short' ],
479 [ 'ab-a-x', 'extensions too short, even with singleton' ],
480 [ 'ab--ab', 'two separators' ],
481 [ 'ab-abc-', 'separator in the end' ],
482 [ '-ab-abc', 'separator in the beginning' ],
483 [ 'abcd-efg', 'language too long' ],
484 [ 'aabbccddE', 'tag too long' ],
485 [ 'pa_guru', 'A tag with underscore is invalid in strict mode' ],
486 [ 'de-f', 'subtag too short' ],
491 * Negative test for Language::isWellFormedLanguageTag()
492 * @covers Language::isWellFormedLanguageTag
494 public function testLenientLanguageTag() {
496 Language
::isWellFormedLanguageTag( 'pa_guru', true ),
497 'pa_guru is a well-formed language tag in lenient mode'
502 * Test Language::isValidBuiltInCode()
503 * @dataProvider provideLanguageCodes
504 * @covers Language::isValidBuiltInCode
506 public function testBuiltInCodeValidation( $code, $expected, $message = '' ) {
507 $this->assertEquals( $expected,
508 (bool)Language
::isValidBuiltInCode( $code ),
509 "validating code $code $message"
513 public static function provideLanguageCodes() {
515 [ 'fr', true, 'Two letters, minor case' ],
516 [ 'EN', false, 'Two letters, upper case' ],
517 [ 'tyv', true, 'Three letters' ],
518 [ 'be-tarask', true, 'With dash' ],
519 [ 'be-x-old', true, 'With extension (two dashes)' ],
520 [ 'be_tarask', false, 'Reject underscores' ],
525 * Test Language::isKnownLanguageTag()
526 * @dataProvider provideKnownLanguageTags
527 * @covers Language::isKnownLanguageTag
529 public function testKnownLanguageTag( $code, $message = '' ) {
531 (bool)Language
::isKnownLanguageTag( $code ),
532 "validating code $code - $message"
536 public static function provideKnownLanguageTags() {
538 [ 'fr', 'simple code' ],
539 [ 'bat-smg', 'an MW legacy tag' ],
540 [ 'sgs', 'an internal standard MW name, for which a legacy tag is used externally' ],
545 * @covers Language::isKnownLanguageTag
547 public function testKnownCldrLanguageTag() {
548 if ( !class_exists( 'LanguageNames' ) ) {
549 $this->markTestSkipped( 'The LanguageNames class is not available. '
550 . 'The CLDR extension is probably not installed.' );
554 (bool)Language
::isKnownLanguageTag( 'pal' ),
555 'validating code "pal" an ancient language, which probably will '
556 . 'not appear in Names.php, but appears in CLDR in English'
561 * Negative tests for Language::isKnownLanguageTag()
562 * @dataProvider provideUnKnownLanguageTags
563 * @covers Language::isKnownLanguageTag
565 public function testUnknownLanguageTag( $code, $message = '' ) {
567 (bool)Language
::isKnownLanguageTag( $code ),
568 "checking that code $code is invalid - $message"
572 public static function provideUnknownLanguageTags() {
574 [ 'mw', 'non-existent two-letter code' ],
575 [ 'foo"<bar', 'very invalid language code' ],
580 * Test too short timestamp
581 * @expectedException MWException
582 * @covers Language::sprintfDate
584 public function testSprintfDateTooShortTimestamp() {
585 $this->getLang()->sprintfDate( 'xiY', '1234567890123' );
589 * Test too long timestamp
590 * @expectedException MWException
591 * @covers Language::sprintfDate
593 public function testSprintfDateTooLongTimestamp() {
594 $this->getLang()->sprintfDate( 'xiY', '123456789012345' );
598 * Test too short timestamp
599 * @expectedException MWException
600 * @covers Language::sprintfDate
602 public function testSprintfDateNotAllDigitTimestamp() {
603 $this->getLang()->sprintfDate( 'xiY', '-1234567890123' );
607 * @dataProvider provideSprintfDateSamples
608 * @covers Language::sprintfDate
610 public function testSprintfDate( $format, $ts, $expected, $msg ) {
614 $this->getLang()->sprintfDate( $format, $ts, null, $ttl ),
615 "sprintfDate('$format', '$ts'): $msg"
618 $dt = new DateTime( $ts );
619 $lastValidTS = $dt->add( new DateInterval( 'PT' . ( $ttl - 1 ) . 'S' ) )->format( 'YmdHis' );
622 $this->getLang()->sprintfDate( $format, $lastValidTS, null ),
623 "sprintfDate('$format', '$ts'): TTL $ttl too high (output was different at $lastValidTS)"
626 // advance the time enough to make all of the possible outputs different (except possibly L)
627 $dt = new DateTime( $ts );
628 $newTS = $dt->add( new DateInterval( 'P1Y1M8DT13H1M1S' ) )->format( 'YmdHis' );
631 $this->getLang()->sprintfDate( $format, $newTS, null ),
632 "sprintfDate('$format', '$ts'): Missing TTL (output was different at $newTS)"
638 * sprintfDate should always use UTC when no zone is given.
639 * @dataProvider provideSprintfDateSamples
640 * @covers Language::sprintfDate
642 public function testSprintfDateNoZone( $format, $ts, $expected, $ignore, $msg ) {
643 $oldTZ = date_default_timezone_get();
644 $res = date_default_timezone_set( 'Asia/Seoul' );
646 $this->markTestSkipped( "Error setting Timezone" );
651 $this->getLang()->sprintfDate( $format, $ts ),
652 "sprintfDate('$format', '$ts'): $msg"
655 date_default_timezone_set( $oldTZ );
659 * sprintfDate should use passed timezone
660 * @dataProvider provideSprintfDateSamples
661 * @covers Language::sprintfDate
663 public function testSprintfDateTZ( $format, $ts, $ignore, $expected, $msg ) {
664 $tz = new DateTimeZone( 'Asia/Seoul' );
666 $this->markTestSkipped( "Error getting Timezone" );
671 $this->getLang()->sprintfDate( $format, $ts, $tz ),
672 "sprintfDate('$format', '$ts', 'Asia/Seoul'): $msg"
677 * sprintfDate should only calculate a TTL if the caller is going to use it.
678 * @covers Language::sprintfDate
680 public function testSprintfDateNoTtlIfNotNeeded() {
681 $noTtl = 'unused'; // Value used to represent that the caller didn't pass a variable in.
683 $this->getLang()->sprintfDate( 'YmdHis', wfTimestampNow(), null, $noTtl );
684 $this->getLang()->sprintfDate( 'YmdHis', wfTimestampNow(), null, $ttl );
689 'If the caller does not set the $ttl variable, do not compute it.'
691 $this->assertInternalType( 'int', $ttl, 'TTL should have been computed.' );
694 public static function provideSprintfDateSamples() {
699 '1390', // note because we're testing English locale we get Latin-standard digits
701 'Iranian calendar full year'
708 'Iranian calendar short year'
715 'ISO 8601 (week) year'
738 // What follows is mostly copied from
739 // https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions#.23time
766 'Month index, not zero pad'
773 'Month index. Zero pad'
794 'Genitive month name (same in EN)'
801 'Day of month (not zero pad)'
808 'Day of month (zero-pad)'
815 'Day of year (zero-indexed)'
822 'Day of week (abbrev)'
836 'Day of week (Mon=1, Sun=7)'
843 'Day of week (Sun=0, Sat=6)'
885 '12 hour, zero padded'
934 'Days in current month'
939 '2012-01-02T09:07:05+00:00',
940 '2012-01-02T09:07:05+09:00',
946 'Mon, 02 Jan 2012 09:07:05 +0000',
947 'Mon, 02 Jan 2012 09:07:05 +0900',
955 'Timezone identifier'
976 'Timezone offset with colon'
983 'Timezone abbreviation'
990 'Timezone offset in seconds'
1018 'Hebrew number of days in month'
1025 'Hebrew genitive month name (No difference in EN)'
1074 'Raw numerals (doesn\'t mean much in EN)'
1077 '[[Y "(yea"\\r)]] \\"xx\\"',
1079 '[[2012 (year)]] "x"',
1080 '[[2012 (year)]] "x"',
1088 * @dataProvider provideFormatSizes
1089 * @covers Language::formatSize
1091 public function testFormatSize( $size, $expected, $msg ) {
1092 $this->assertEquals(
1094 $this->getLang()->formatSize( $size ),
1095 "formatSize('$size'): $msg"
1099 public static function provideFormatSizes() {
1146 // How big!? THIS BIG!
1151 * @dataProvider provideFormatBitrate
1152 * @covers Language::formatBitrate
1154 public function testFormatBitrate( $bps, $expected, $msg ) {
1155 $this->assertEquals(
1157 $this->getLang()->formatBitrate( $bps ),
1158 "formatBitrate('$bps'): $msg"
1162 public static function provideFormatBitrate() {
1172 "999 bits per second"
1177 "1 kilobit per second"
1182 "1 megabit per second"
1187 "1 gigabit per second"
1192 "1 terabit per second"
1197 "1 petabit per second"
1202 "1 exabit per second"
1207 "1 zetabit per second"
1212 "1 yottabit per second"
1217 "1,000 yottabits per second"
1223 * @dataProvider provideFormatDuration
1224 * @covers Language::formatDuration
1226 public function testFormatDuration( $duration, $expected, $intervals = [] ) {
1227 $this->assertEquals(
1229 $this->getLang()->formatDuration( $duration, $intervals ),
1230 "formatDuration('$duration'): $expected"
1234 public static function provideFormatDuration() {
1273 // ( 365 + ( 24 * 3 + 25 ) / 400 ) * 86400 = 31556952
1274 ( 365 +
( 24 * 3 +
25 ) / 400.0 ) * 86400,
1307 '2 hours, 30 minutes and 1 second'
1311 '1 hour and 1 second'
1314 31556952 +
2 * 86400 +
9000,
1315 '1 year, 2 days, 2 hours and 30 minutes'
1318 42 * 1000 * 31556952 +
42,
1319 '42 millennia and 42 seconds'
1337 31556952 +
2 * 86400 +
9000,
1338 '1 year, 2 days and 150 minutes',
1339 [ 'years', 'days', 'minutes' ],
1344 [ 'years', 'days' ],
1347 31556952 +
2 * 86400 +
9000,
1348 '1 year, 2 days and 150 minutes',
1349 [ 'minutes', 'days', 'years' ],
1354 [ 'days', 'years' ],
1360 * @dataProvider provideCheckTitleEncodingData
1361 * @covers Language::checkTitleEncoding
1363 public function testCheckTitleEncoding( $s ) {
1364 $this->assertEquals(
1366 $this->getLang()->checkTitleEncoding( $s ),
1367 "checkTitleEncoding('$s')"
1371 public static function provideCheckTitleEncodingData() {
1372 // phpcs:disable Generic.Files.LineLength
1375 [ "United States of America" ], // 7bit ASCII
1376 [ rawurldecode( "S%C3%A9rie%20t%C3%A9l%C3%A9vis%C3%A9e" ) ],
1379 "Acteur%7CAlbert%20Robbins%7CAnglais%7CAnn%20Donahue%7CAnthony%20E.%20Zuiker%7CCarol%20Mendelsohn"
1382 // The following two data sets come from T38839. They fail if checkTitleEncoding uses a regexp to test for
1383 // valid UTF-8 encoding and the pcre.recursion_limit is low (like, say, 1024). They succeed if checkTitleEncoding
1384 // uses mb_check_encoding for its test.
1387 "Acteur%7CAlbert%20Robbins%7CAnglais%7CAnn%20Donahue%7CAnthony%20E.%20Zuiker%7CCarol%20Mendelsohn%7C"
1388 . "Catherine%20Willows%7CDavid%20Hodges%7CDavid%20Phillips%7CGil%20Grissom%7CGreg%20Sanders%7CHodges%7C"
1389 . "Internet%20Movie%20Database%7CJim%20Brass%7CLady%20Heather%7C"
1390 . "Les%20Experts%20(s%C3%A9rie%20t%C3%A9l%C3%A9vis%C3%A9e)%7CLes%20Experts%20:%20Manhattan%7C"
1391 . "Les%20Experts%20:%20Miami%7CListe%20des%20personnages%20des%20Experts%7C"
1392 . "Liste%20des%20%C3%A9pisodes%20des%20Experts%7CMod%C3%A8le%20discussion:Palette%20Les%20Experts%7C"
1393 . "Nick%20Stokes%7CPersonnage%20de%20fiction%7CPersonnage%20fictif%7CPersonnage%20de%20fiction%7C"
1394 . "Personnages%20r%C3%A9currents%20dans%20Les%20Experts%7CRaymond%20Langston%7CRiley%20Adams%7C"
1395 . "Saison%201%20des%20Experts%7CSaison%2010%20des%20Experts%7CSaison%2011%20des%20Experts%7C"
1396 . "Saison%2012%20des%20Experts%7CSaison%202%20des%20Experts%7CSaison%203%20des%20Experts%7C"
1397 . "Saison%204%20des%20Experts%7CSaison%205%20des%20Experts%7CSaison%206%20des%20Experts%7C"
1398 . "Saison%207%20des%20Experts%7CSaison%208%20des%20Experts%7CSaison%209%20des%20Experts%7C"
1399 . "Sara%20Sidle%7CSofia%20Curtis%7CS%C3%A9rie%20t%C3%A9l%C3%A9vis%C3%A9e%7CWallace%20Langham%7C"
1400 . "Warrick%20Brown%7CWendy%20Simms%7C%C3%89tats-Unis"
1405 "Mod%C3%A8le%3AArrondissements%20homonymes%7CMod%C3%A8le%3ABandeau%20standard%20pour%20page%20d'homonymie%7C"
1406 . "Mod%C3%A8le%3ABatailles%20homonymes%7CMod%C3%A8le%3ACantons%20homonymes%7C"
1407 . "Mod%C3%A8le%3ACommunes%20fran%C3%A7aises%20homonymes%7CMod%C3%A8le%3AFilms%20homonymes%7C"
1408 . "Mod%C3%A8le%3AGouvernements%20homonymes%7CMod%C3%A8le%3AGuerres%20homonymes%7CMod%C3%A8le%3AHomonymie%7C"
1409 . "Mod%C3%A8le%3AHomonymie%20bateau%7CMod%C3%A8le%3AHomonymie%20d'%C3%A9tablissements%20scolaires%20ou"
1410 . "%20universitaires%7CMod%C3%A8le%3AHomonymie%20d'%C3%AEles%7CMod%C3%A8le%3AHomonymie%20de%20clubs%20sportifs%7C"
1411 . "Mod%C3%A8le%3AHomonymie%20de%20comt%C3%A9s%7CMod%C3%A8le%3AHomonymie%20de%20monument%7C"
1412 . "Mod%C3%A8le%3AHomonymie%20de%20nom%20romain%7CMod%C3%A8le%3AHomonymie%20de%20parti%20politique%7C"
1413 . "Mod%C3%A8le%3AHomonymie%20de%20route%7CMod%C3%A8le%3AHomonymie%20dynastique%7C"
1414 . "Mod%C3%A8le%3AHomonymie%20vid%C3%A9oludique%7CMod%C3%A8le%3AHomonymie%20%C3%A9difice%20religieux%7C"
1415 . "Mod%C3%A8le%3AInternationalisation%7CMod%C3%A8le%3AIsom%C3%A9rie%7CMod%C3%A8le%3AParonymie%7C"
1416 . "Mod%C3%A8le%3APatronyme%7CMod%C3%A8le%3APatronyme%20basque%7CMod%C3%A8le%3APatronyme%20italien%7C"
1417 . "Mod%C3%A8le%3APatronymie%7CMod%C3%A8le%3APersonnes%20homonymes%7CMod%C3%A8le%3ASaints%20homonymes%7C"
1418 . "Mod%C3%A8le%3ATitres%20homonymes%7CMod%C3%A8le%3AToponymie%7CMod%C3%A8le%3AUnit%C3%A9s%20homonymes%7C"
1419 . "Mod%C3%A8le%3AVilles%20homonymes%7CMod%C3%A8le%3A%C3%89difices%20religieux%20homonymes"
1427 * @dataProvider provideRomanNumeralsData
1428 * @covers Language::romanNumeral
1430 public function testRomanNumerals( $num, $numerals ) {
1431 $this->assertEquals(
1433 Language
::romanNumeral( $num ),
1434 "romanNumeral('$num')"
1438 public static function provideRomanNumeralsData() {
1471 [ 1989, 'MCMLXXXIX' ],
1477 [ 7000, 'MMMMMMM' ],
1478 [ 8000, 'MMMMMMMM' ],
1479 [ 9000, 'MMMMMMMMM' ],
1480 [ 9999, 'MMMMMMMMMCMXCIX' ],
1481 [ 10000, 'MMMMMMMMMM' ],
1486 * @dataProvider provideHebrewNumeralsData
1487 * @covers Language::hebrewNumeral
1489 public function testHebrewNumeral( $num, $numerals ) {
1490 $this->assertEquals(
1492 Language
::hebrewNumeral( $num ),
1493 "hebrewNumeral('$num')"
1497 public static function provideHebrewNumeralsData() {
1540 [ 2000, "ב' אלפים" ],
1542 [ 3000, "ג' אלפים" ],
1543 [ 4000, "ד' אלפים" ],
1544 [ 4904, "ד'תתק\"ד" ],
1545 [ 5000, "ה' אלפים" ],
1546 [ 5680, "ה'תר\"ף" ],
1547 [ 5690, "ה'תר\"ץ" ],
1548 [ 5708, "ה'תש\"ח" ],
1549 [ 5720, "ה'תש\"ך" ],
1550 [ 5740, "ה'תש\"ם" ],
1551 [ 5750, "ה'תש\"ן" ],
1552 [ 5775, "ה'תשע\"ה" ],
1557 * @dataProvider providePluralData
1558 * @covers Language::convertPlural
1560 public function testConvertPlural( $expected, $number, $forms ) {
1561 $chosen = $this->getLang()->convertPlural( $number, $forms );
1562 $this->assertEquals( $expected, $chosen );
1565 public static function providePluralData() {
1566 // Params are: [expected text, number given, [the plural forms]]
1569 'singular', 'plural'
1571 [ 'explicit zero', 0, [
1572 '0=explicit zero', 'singular', 'plural'
1574 [ 'explicit one', 1, [
1575 'singular', 'plural', '1=explicit one',
1578 'singular', 'plural', '0=explicit zero',
1581 '0=explicit zero', '1=explicit one', 'singular', 'plural'
1583 [ 'explicit eleven', 11, [
1584 'singular', 'plural', '11=explicit eleven',
1587 'singular', 'plural', '11=explicit twelve',
1590 'singular', 'plural', '=explicit form',
1593 'kissa=kala', '1=2=3', 'other',
1596 '0=explicit zero', '1=explicit one',
1602 * @covers Language::embedBidi()
1604 public function testEmbedBidi() {
1605 $lre = "\u{202A}"; // U+202A LEFT-TO-RIGHT EMBEDDING
1606 $rle = "\u{202B}"; // U+202B RIGHT-TO-LEFT EMBEDDING
1607 $pdf = "\u{202C}"; // U+202C POP DIRECTIONAL FORMATTING
1608 $lang = $this->getLang();
1609 $this->assertEquals(
1611 $lang->embedBidi( '123' ),
1612 'embedBidi with neutral argument'
1614 $this->assertEquals(
1615 $lre . 'Ben_(WMF)' . $pdf,
1616 $lang->embedBidi( 'Ben_(WMF)' ),
1617 'embedBidi with LTR argument'
1619 $this->assertEquals(
1620 $rle . 'יהודי (מנוחין)' . $pdf,
1621 $lang->embedBidi( 'יהודי (מנוחין)' ),
1622 'embedBidi with RTL argument'
1627 * @covers Language::translateBlockExpiry()
1628 * @dataProvider provideTranslateBlockExpiry
1630 public function testTranslateBlockExpiry( $expectedData, $str, $now, $desc ) {
1631 $lang = $this->getLang();
1632 if ( is_array( $expectedData ) ) {
1633 list( $func, $arg ) = $expectedData;
1634 $expected = $lang->$func( $arg );
1636 $expected = $expectedData;
1638 $this->assertEquals( $expected, $lang->translateBlockExpiry( $str, null, $now ), $desc );
1641 public static function provideTranslateBlockExpiry() {
1643 [ '2 hours', '2 hours', 0, 'simple data from ipboptions' ],
1644 [ 'indefinite', 'infinite', 0, 'infinite from ipboptions' ],
1645 [ 'indefinite', 'infinity', 0, 'alternative infinite from ipboptions' ],
1646 [ 'indefinite', 'indefinite', 0, 'another alternative infinite from ipboptions' ],
1647 [ [ 'formatDuration', 1023 * 60 * 60 ], '1023 hours', 0, 'relative' ],
1648 [ [ 'formatDuration', -1023 ], '-1023 seconds', 0, 'negative relative' ],
1650 [ 'formatDuration', 1023 * 60 * 60 ],
1652 wfTimestamp( TS_UNIX
, '19910203040506' ),
1653 'relative with initial timestamp'
1655 [ [ 'formatDuration', 0 ], 'now', 0, 'now' ],
1657 [ 'timeanddate', '20120102070000' ],
1658 '2012-1-1 7:00 +1 day',
1660 'mixed, handled as absolute'
1662 [ [ 'timeanddate', '19910203040506' ], '1991-2-3 4:05:06', 0, 'absolute' ],
1663 [ [ 'timeanddate', '19700101000000' ], '1970-1-1 0:00:00', 0, 'absolute at epoch' ],
1664 [ [ 'timeanddate', '19691231235959' ], '1969-12-31 23:59:59', 0, 'time before epoch' ],
1666 [ 'timeanddate', '19910910000000' ],
1668 wfTimestamp( TS_UNIX
, '19910203040506' ),
1671 [ 'dummy', 'dummy', 0, 'return garbage as is' ],
1676 * @dataProvider provideFormatNum
1677 * @covers Language::formatNum
1679 public function testFormatNum(
1680 $translateNumerals, $langCode, $number, $nocommafy, $expected
1682 $this->setMwGlobals( [ 'wgTranslateNumerals' => $translateNumerals ] );
1683 $lang = Language
::factory( $langCode );
1684 $formattedNum = $lang->formatNum( $number, $nocommafy );
1685 $this->assertType( 'string', $formattedNum );
1686 $this->assertEquals( $expected, $formattedNum );
1689 public function provideFormatNum() {
1691 [ true, 'en', 100, false, '100' ],
1692 [ true, 'en', 101, true, '101' ],
1693 [ false, 'en', 103, false, '103' ],
1694 [ false, 'en', 104, true, '104' ],
1695 [ true, 'en', '105', false, '105' ],
1696 [ true, 'en', '106', true, '106' ],
1697 [ false, 'en', '107', false, '107' ],
1698 [ false, 'en', '108', true, '108' ],
1703 * @covers Language::parseFormattedNumber
1704 * @dataProvider parseFormattedNumberProvider
1706 public function testParseFormattedNumber( $langCode, $number ) {
1707 $lang = Language
::factory( $langCode );
1709 $localisedNum = $lang->formatNum( $number );
1710 $normalisedNum = $lang->parseFormattedNumber( $localisedNum );
1712 $this->assertEquals( $number, $normalisedNum );
1715 public function parseFormattedNumberProvider() {
1722 [ 'zh-classical', 7432 ]
1727 * @covers Language::commafy()
1728 * @dataProvider provideCommafyData
1730 public function testCommafy( $number, $numbersWithCommas ) {
1731 $this->assertEquals(
1733 $this->getLang()->commafy( $number ),
1734 "commafy('$number')"
1738 public static function provideCommafyData() {
1744 [ 10000, '10,000' ],
1745 [ 100000, '100,000' ],
1746 [ 1000000, '1,000,000' ],
1747 [ -1.0001, '-1.0001' ],
1748 [ 1.0001, '1.0001' ],
1749 [ 10.0001, '10.0001' ],
1750 [ 100.0001, '100.0001' ],
1751 [ 1000.0001, '1,000.0001' ],
1752 [ 10000.0001, '10,000.0001' ],
1753 [ 100000.0001, '100,000.0001' ],
1754 [ 1000000.0001, '1,000,000.0001' ],
1755 [ '200000000000000000000', '200,000,000,000,000,000,000' ],
1756 [ '-200000000000000000000', '-200,000,000,000,000,000,000' ],
1761 * @covers Language::listToText
1763 public function testListToText() {
1764 $lang = $this->getLang();
1765 $and = $lang->getMessageFromDB( 'and' );
1766 $s = $lang->getMessageFromDB( 'word-separator' );
1767 $c = $lang->getMessageFromDB( 'comma-separator' );
1769 $this->assertEquals( '', $lang->listToText( [] ) );
1770 $this->assertEquals( 'a', $lang->listToText( [ 'a' ] ) );
1771 $this->assertEquals( "a{$and}{$s}b", $lang->listToText( [ 'a', 'b' ] ) );
1772 $this->assertEquals( "a{$c}b{$and}{$s}c", $lang->listToText( [ 'a', 'b', 'c' ] ) );
1773 $this->assertEquals( "a{$c}b{$c}c{$and}{$s}d", $lang->listToText( [ 'a', 'b', 'c', 'd' ] ) );
1777 * @covers Language::clearCaches
1779 public function testClearCaches() {
1780 $languageClass = TestingAccessWrapper
::newFromClass( Language
::class );
1782 // Populate $dataCache
1783 Language
::getLocalisationCache()->getItem( 'zh', 'mainpage' );
1784 $oldCacheObj = Language
::$dataCache;
1785 $this->assertNotCount( 0,
1786 TestingAccessWrapper
::newFromObject( Language
::$dataCache )->loadedItems
);
1788 // Populate $mLangObjCache
1789 $lang = Language
::factory( 'en' );
1790 $this->assertNotCount( 0, Language
::$mLangObjCache );
1792 // Populate $fallbackLanguageCache
1793 Language
::getFallbacksIncludingSiteLanguage( 'en' );
1794 $this->assertNotCount( 0, $languageClass->fallbackLanguageCache
);
1796 // Populate $grammarTransformations
1797 $lang->getGrammarTransformations();
1798 $this->assertNotNull( $languageClass->grammarTransformations
);
1800 // Populate $languageNameCache
1801 Language
::fetchLanguageNames();
1802 $this->assertNotNull( $languageClass->languageNameCache
);
1804 Language
::clearCaches();
1806 $this->assertNotSame( $oldCacheObj, Language
::$dataCache );
1807 $this->assertCount( 0,
1808 TestingAccessWrapper
::newFromObject( Language
::$dataCache )->loadedItems
);
1809 $this->assertCount( 0, Language
::$mLangObjCache );
1810 $this->assertCount( 0, $languageClass->fallbackLanguageCache
);
1811 $this->assertNull( $languageClass->grammarTransformations
);
1812 $this->assertNull( $languageClass->languageNameCache
);
1816 * @dataProvider provideIsSupportedLanguage
1817 * @covers Language::isSupportedLanguage
1819 public function testIsSupportedLanguage( $code, $expected, $comment ) {
1820 $this->assertEquals( $expected, Language
::isSupportedLanguage( $code ), $comment );
1823 public static function provideIsSupportedLanguage() {
1825 [ 'en', true, 'is supported language' ],
1826 [ 'fi', true, 'is supported language' ],
1827 [ 'bunny', false, 'is not supported language' ],
1828 [ 'FI', false, 'is not supported language, input should be in lower case' ],
1833 * @dataProvider provideGetParentLanguage
1834 * @covers Language::getParentLanguage
1836 public function testGetParentLanguage( $code, $expected, $comment ) {
1837 $lang = Language
::factory( $code );
1838 if ( is_null( $expected ) ) {
1839 $this->assertNull( $lang->getParentLanguage(), $comment );
1841 $this->assertEquals( $expected, $lang->getParentLanguage()->getCode(), $comment );
1845 public static function provideGetParentLanguage() {
1847 [ 'zh-cn', 'zh', 'zh is the parent language of zh-cn' ],
1848 [ 'zh', 'zh', 'zh is defined as the parent language of zh, '
1849 . 'because zh converter can convert zh-cn to zh' ],
1850 [ 'zh-invalid', null, 'do not be fooled by arbitrarily composed language codes' ],
1851 [ 'de-formal', null, 'de does not have converter' ],
1852 [ 'de', null, 'de does not have converter' ],
1857 * @dataProvider provideGetNamespaceAliases
1858 * @covers Language::getNamespaceAliases
1860 public function testGetNamespaceAliases( $languageCode, $subset ) {
1861 $language = Language
::factory( $languageCode );
1862 $aliases = $language->getNamespaceAliases();
1863 foreach ( $subset as $alias => $nsId ) {
1864 $this->assertEquals( $nsId, $aliases[$alias] );
1868 public static function provideGetNamespaceAliases() {
1869 // TODO: Add tests for NS_PROJECT_TALK and GenderNamespaces
1882 * @covers Language::equals
1884 public function testEquals() {
1885 $en1 = Language
::factory( 'en' );
1886 $en2 = Language
::factory( 'en' );
1887 $en3 = new Language();
1888 $this->assertTrue( $en1->equals( $en2 ), 'en1 equals en2' );
1889 $this->assertTrue( $en2->equals( $en3 ), 'en2 equals en3' );
1890 $this->assertTrue( $en3->equals( $en1 ), 'en3 equals en1' );
1892 $fr = Language
::factory( 'fr' );
1893 $this->assertFalse( $en1->equals( $fr ), 'en not equals fr' );
1895 $ar1 = Language
::factory( 'ar' );
1896 $ar2 = new LanguageAr();
1897 $this->assertTrue( $ar1->equals( $ar2 ), 'ar equals ar' );