3 class LanguageTest
extends LanguageClassesTestCase
{
5 * @covers Language::convertDoubleWidth
6 * @covers Language::normalizeForSearch
8 public function testLanguageConvertDoubleWidthToSingleWidth() {
10 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
11 $this->getLang()->normalizeForSearch(
12 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
14 'convertDoubleWidth() with the full alphabet and digits'
19 * @dataProvider provideFormattableTimes
20 * @covers Language::formatTimePeriod
22 public function testFormatTimePeriod( $seconds, $format, $expected, $desc ) {
23 $this->assertEquals( $expected, $this->getLang()->formatTimePeriod( $seconds, $format ), $desc );
26 public static function provideFormattableTimes() {
32 'formatTimePeriod() rounding (<10s)'
36 [ 'noabbrevs' => true ],
38 'formatTimePeriod() rounding (<10s)'
44 'formatTimePeriod() rounding (<10s)'
48 [ 'noabbrevs' => true ],
50 'formatTimePeriod() rounding (<10s)'
56 'formatTimePeriod() rounding (<60s)'
60 [ 'noabbrevs' => true ],
62 'formatTimePeriod() rounding (<60s)'
68 'formatTimePeriod() rounding (<1h)'
72 [ 'noabbrevs' => true ],
73 '2 minutes 0 seconds',
74 'formatTimePeriod() rounding (<1h)'
80 'formatTimePeriod() rounding (<1h)'
84 [ 'noabbrevs' => true ],
85 '1 hour 0 minutes 0 seconds',
86 'formatTimePeriod() rounding (<1h)'
92 'formatTimePeriod() rounding (>=1h)'
96 [ 'noabbrevs' => true ],
97 '2 hours 0 minutes 0 seconds',
98 'formatTimePeriod() rounding (>=1h)'
104 'formatTimePeriod() rounding (>=1h), avoidseconds'
108 [ 'avoid' => 'avoidseconds', 'noabbrevs' => true ],
110 'formatTimePeriod() rounding (>=1h), avoidseconds'
116 'formatTimePeriod() rounding (>=1h), avoidminutes'
120 [ 'avoid' => 'avoidminutes', 'noabbrevs' => true ],
122 'formatTimePeriod() rounding (>=1h), avoidminutes'
128 'formatTimePeriod() rounding (=48h), avoidseconds'
132 [ 'avoid' => 'avoidseconds', 'noabbrevs' => true ],
133 '48 hours 0 minutes',
134 'formatTimePeriod() rounding (=48h), avoidseconds'
140 'formatTimePeriod() rounding (>48h), avoidminutes'
144 [ 'avoid' => 'avoidminutes', 'noabbrevs' => true ],
146 'formatTimePeriod() rounding (>48h), avoidminutes'
152 'formatTimePeriod() rounding (>48h), avoidseconds'
156 [ 'avoid' => 'avoidseconds', 'noabbrevs' => true ],
157 '2 days 1 hour 0 minutes',
158 'formatTimePeriod() rounding (>48h), avoidseconds'
164 'formatTimePeriod() rounding (>48h), avoidminutes'
168 [ 'avoid' => 'avoidminutes', 'noabbrevs' => true ],
170 'formatTimePeriod() rounding (>48h), avoidminutes'
176 'formatTimePeriod() rounding (>48h), avoidseconds'
180 [ 'avoid' => 'avoidseconds', 'noabbrevs' => true ],
181 '3 days 0 hours 0 minutes',
182 'formatTimePeriod() rounding (>48h), avoidseconds'
188 'formatTimePeriod() rounding, (>48h), avoidseconds'
192 [ 'avoid' => 'avoidseconds', 'noabbrevs' => true ],
193 '2 days 0 hours 0 minutes',
194 'formatTimePeriod() rounding, (>48h), avoidseconds'
200 'formatTimePeriod() rounding, recursion, (>48h)'
204 [ 'noabbrevs' => true ],
205 '2 days 1 hour 1 minute 1 second',
206 'formatTimePeriod() rounding, recursion, (>48h)'
212 * @covers Language::truncateForDatabase
213 * @covers Language::truncateInternal
215 public function testTruncateForDatabase() {
218 $this->getLang()->truncateForDatabase( "1234567890", 0, 'XXX' ),
219 'truncate prefix, len 0, small ellipsis'
224 $this->getLang()->truncateForDatabase( "1234567890", 8, 'XXX' ),
225 'truncate prefix, small ellipsis'
230 $this->getLang()->truncateForDatabase( "123456789", 5, 'XXXXXXXXXXXXXXX' ),
231 'truncate prefix, large ellipsis'
236 $this->getLang()->truncateForDatabase( "1234567890", -8, 'XXX' ),
237 'truncate suffix, small ellipsis'
242 $this->getLang()->truncateForDatabase( "123456789", -5, 'XXXXXXXXXXXXXXX' ),
243 'truncate suffix, large ellipsis'
247 $this->getLang()->truncateForDatabase( "123 ", 9, 'XXX' ),
248 'truncate prefix, with spaces'
252 $this->getLang()->truncateForDatabase( "12345 8", 11, 'XXX' ),
253 'truncate prefix, with spaces and non-space ending'
257 $this->getLang()->truncateForDatabase( "1 234", -8, 'XXX' ),
258 'truncate suffix, with spaces'
262 $this->getLang()->truncateForDatabase( "1234567890", 5, 'XXX', false ),
263 'truncate without adjustment'
267 $this->getLang()->truncateForDatabase( "泰乐菌素123456789", 11, '...', false ),
268 'truncate does not chop Unicode characters in half'
272 $this->getLang()->truncateForDatabase( "\n泰乐菌素123456789", 12, '...', false ),
273 'truncate does not chop Unicode characters in half if there is a preceding newline'
278 * @dataProvider provideTruncateData
279 * @covers Language::truncateForVisual
280 * @covers Language::truncateInternal
282 public function testTruncateForVisual(
283 $expected, $string, $length, $ellipsis = '...', $adjustLength = true
287 $this->getLang()->truncateForVisual( $string, $length, $ellipsis, $adjustLength )
292 * @return array Format is ($expected, $string, $length, $ellipsis, $adjustLength)
294 public static function provideTruncateData() {
296 [ "XXX", "тестирам да ли ради", 0, "XXX" ],
297 [ "testnXXX", "testni scenarij", 8, "XXX" ],
298 [ "حالة اختبار", "حالة اختبار", 5, "XXXXXXXXXXXXXXX" ],
299 [ "XXXедент", "прецедент", -8, "XXX" ],
300 [ "XXപിൾ", "ആപ്പിൾ", -5, "XX" ],
301 [ "神秘XXX", "神秘 ", 9, "XXX" ],
302 [ "ΔημιουργXXX", "Δημιουργία Σύμπαντος", 11, "XXX" ],
303 [ "XXXの家です", "地球は私たちの唯 の家です", -8, "XXX" ],
304 [ "زندگیXXX", "زندگی زیباست", 6, "XXX", false ],
305 [ "ცხოვრება...", "ცხოვრება არის საოცარი", 8, "...", false ],
306 [ "\nທ່ານ...", "\nທ່ານບໍ່ຮູ້ຫນັງສື", 5, "...", false ],
311 * @dataProvider provideHTMLTruncateData
312 * @covers Language::truncateHTML
314 public function testTruncateHtml( $len, $ellipsis, $input, $expected ) {
318 $this->getLang()->truncateHtml( $input, $len, $ellipsis )
323 * @return array Format is ($len, $ellipsis, $input, $expected)
325 public static function provideHTMLTruncateData() {
327 [ 0, 'XXX', "1234567890", "XXX" ],
328 [ 8, 'XXX', "1234567890", "12345XXX" ],
329 [ 5, 'XXXXXXXXXXXXXXX', '1234567890', "1234567890" ],
331 '<p><span style="font-weight:bold;"></span></p>',
332 '<p><span style="font-weight:bold;"></span></p>',
335 '<p><span style="font-weight:bold;">123456789</span></p>',
336 '<p><span style="font-weight:bold;">***</span></p>',
339 '<p><span style="font-weight:bold;"> 23456789</span></p>',
340 '<p><span style="font-weight:bold;">***</span></p>',
343 '<p><span style="font-weight:bold;">123456789</span></p>',
344 '<p><span style="font-weight:bold;">***</span></p>',
347 '<p><span style="font-weight:bold;">123456789</span></p>',
348 '<p><span style="font-weight:bold;">1***</span></p>',
351 '<tt><span style="font-weight:bold;">123456789</span></tt>',
352 '<tt><span style="font-weight:bold;">12***</span></tt>',
355 '<p><a href="www.mediawiki.org">123456789</a></p>',
356 '<p><a href="www.mediawiki.org">123***</a></p>',
359 '<p><a href="www.mediawiki.org">12 456789</a></p>',
360 '<p><a href="www.mediawiki.org">12 ***</a></p>',
363 '<small><span style="font-weight:bold;">123<p id="#moo">456</p>789</span></small>',
364 '<small><span style="font-weight:bold;">123<p id="#moo">4***</p></span></small>',
367 '<div><span style="font-weight:bold;">123<span>4</span>56789</span></div>',
368 '<div><span style="font-weight:bold;">123<span>4</span>5***</span></div>',
371 '<p><table style="font-weight:bold;"><tr><td>123456789</td></tr></table></p>',
372 '<p><table style="font-weight:bold;"><tr><td>123456789</td></tr></table></p>',
375 '<p><font style="font-weight:bold;">123456789</font></p>',
376 '<p><font style="font-weight:bold;">123456789</font></p>',
382 * Test Language::isWellFormedLanguageTag()
383 * @dataProvider provideWellFormedLanguageTags
384 * @covers Language::isWellFormedLanguageTag
386 public function testWellFormedLanguageTag( $code, $message = '' ) {
388 Language
::isWellFormedLanguageTag( $code ),
389 "validating code $code $message"
394 * The test cases are based on the tests in the GaBuZoMeu parser
395 * written by Stéphane Bortzmeyer <bortzmeyer@nic.fr>
396 * and distributed as free software, under the GNU General Public Licence.
397 * http://www.bortzmeyer.org/gabuzomeu-parsing-language-tags.html
399 public static function provideWellFormedLanguageTags() {
401 [ 'fr', 'two-letter code' ],
402 [ 'fr-latn', 'two-letter code with lower case script code' ],
403 [ 'fr-Latn-FR', 'two-letter code with title case script code and uppercase country code' ],
404 [ 'fr-Latn-419', 'two-letter code with title case script code and region number' ],
405 [ 'fr-FR', 'two-letter code with uppercase' ],
406 [ 'ax-TZ', 'Not in the registry, but well-formed' ],
407 [ 'fr-shadok', 'two-letter code with variant' ],
408 [ 'fr-y-myext-myext2', 'non-x singleton' ],
409 [ 'fra-Latn', 'ISO 639 can be 3-letters' ],
410 [ 'fra', 'three-letter language code' ],
411 [ 'fra-FX', 'three-letter language code with country code' ],
412 [ 'i-klingon', 'grandfathered with singleton' ],
413 [ 'I-kLINgon', 'tags are case-insensitive...' ],
414 [ 'no-bok', 'grandfathered without singleton' ],
415 [ 'i-enochian', 'Grandfathered' ],
416 [ 'x-fr-CH', 'private use' ],
417 [ 'es-419', 'two-letter code with region number' ],
418 [ 'en-Latn-GB-boont-r-extended-sequence-x-private', 'weird, but well-formed' ],
419 [ 'ab-x-abc-x-abc', 'anything goes after x' ],
420 [ 'ab-x-abc-a-a', 'anything goes after x, including several non-x singletons' ],
421 [ 'i-default', 'grandfathered' ],
422 [ 'abcd-Latn', 'Language of 4 chars reserved for future use' ],
423 [ 'AaBbCcDd-x-y-any-x', 'Language of 5-8 chars, registered' ],
424 [ 'de-CH-1901', 'with country and year' ],
425 [ 'en-US-x-twain', 'with country and singleton' ],
426 [ 'zh-cmn', 'three-letter variant' ],
427 [ 'zh-cmn-Hant', 'three-letter variant and script' ],
428 [ 'zh-cmn-Hant-HK', 'three-letter variant, script and country' ],
429 [ 'xr-p-lze', 'Extension' ],
434 * Negative test for Language::isWellFormedLanguageTag()
435 * @dataProvider provideMalformedLanguageTags
436 * @covers Language::isWellFormedLanguageTag
438 public function testMalformedLanguageTag( $code, $message = '' ) {
440 Language
::isWellFormedLanguageTag( $code ),
441 "validating that code $code is a malformed language tag - $message"
446 * The test cases are based on the tests in the GaBuZoMeu parser
447 * written by Stéphane Bortzmeyer <bortzmeyer@nic.fr>
448 * and distributed as free software, under the GNU General Public Licence.
449 * http://www.bortzmeyer.org/gabuzomeu-parsing-language-tags.html
451 public static function provideMalformedLanguageTags() {
453 [ 'f', 'language too short' ],
454 [ 'f-Latn', 'language too short with script' ],
455 [ 'xr-lxs-qut', 'variants too short' ], # extlangS
456 [ 'fr-Latn-F', 'region too short' ],
457 [ 'a-value', 'language too short with region' ],
458 [ 'tlh-a-b-foo', 'valid three-letter with wrong variant' ],
461 'grandfathered but not registered: invalid, even if we only test well-formedness'
463 [ 'abcdefghi-012345678', 'numbers too long' ],
464 [ 'ab-abc-abc-abc-abc', 'invalid extensions' ],
465 [ 'ab-abcd-abc', 'invalid extensions' ],
466 [ 'ab-ab-abc', 'invalid extensions' ],
467 [ 'ab-123-abc', 'invalid extensions' ],
468 [ 'a-Hant-ZH', 'short language with valid extensions' ],
469 [ 'a1-Hant-ZH', 'invalid character in language' ],
470 [ 'ab-abcde-abc', 'invalid extensions' ],
471 [ 'ab-1abc-abc', 'invalid characters in extensions' ],
472 [ 'ab-ab-abcd', 'invalid order of extensions' ],
473 [ 'ab-123-abcd', 'invalid order of extensions' ],
474 [ 'ab-abcde-abcd', 'invalid extensions' ],
475 [ 'ab-1abc-abcd', 'invalid characters in extensions' ],
476 [ 'ab-a-b', 'extensions too short' ],
477 [ 'ab-a-x', 'extensions too short, even with singleton' ],
478 [ 'ab--ab', 'two separators' ],
479 [ 'ab-abc-', 'separator in the end' ],
480 [ '-ab-abc', 'separator in the beginning' ],
481 [ 'abcd-efg', 'language too long' ],
482 [ 'aabbccddE', 'tag too long' ],
483 [ 'pa_guru', 'A tag with underscore is invalid in strict mode' ],
484 [ 'de-f', 'subtag too short' ],
489 * Negative test for Language::isWellFormedLanguageTag()
490 * @covers Language::isWellFormedLanguageTag
492 public function testLenientLanguageTag() {
494 Language
::isWellFormedLanguageTag( 'pa_guru', true ),
495 'pa_guru is a well-formed language tag in lenient mode'
500 * Test Language::isValidBuiltInCode()
501 * @dataProvider provideLanguageCodes
502 * @covers Language::isValidBuiltInCode
504 public function testBuiltInCodeValidation( $code, $expected, $message = '' ) {
505 $this->assertEquals( $expected,
506 (bool)Language
::isValidBuiltInCode( $code ),
507 "validating code $code $message"
511 public static function provideLanguageCodes() {
513 [ 'fr', true, 'Two letters, minor case' ],
514 [ 'EN', false, 'Two letters, upper case' ],
515 [ 'tyv', true, 'Three letters' ],
516 [ 'be-tarask', true, 'With dash' ],
517 [ 'be-x-old', true, 'With extension (two dashes)' ],
518 [ 'be_tarask', false, 'Reject underscores' ],
523 * Test Language::isKnownLanguageTag()
524 * @dataProvider provideKnownLanguageTags
525 * @covers Language::isKnownLanguageTag
527 public function testKnownLanguageTag( $code, $message = '' ) {
529 (bool)Language
::isKnownLanguageTag( $code ),
530 "validating code $code - $message"
534 public static function provideKnownLanguageTags() {
536 [ 'fr', 'simple code' ],
537 [ 'bat-smg', 'an MW legacy tag' ],
538 [ 'sgs', 'an internal standard MW name, for which a legacy tag is used externally' ],
543 * @covers Language::isKnownLanguageTag
545 public function testKnownCldrLanguageTag() {
546 if ( !class_exists( 'LanguageNames' ) ) {
547 $this->markTestSkipped( 'The LanguageNames class is not available. '
548 . 'The CLDR extension is probably not installed.' );
552 (bool)Language
::isKnownLanguageTag( 'pal' ),
553 'validating code "pal" an ancient language, which probably will '
554 . 'not appear in Names.php, but appears in CLDR in English'
559 * Negative tests for Language::isKnownLanguageTag()
560 * @dataProvider provideUnKnownLanguageTags
561 * @covers Language::isKnownLanguageTag
563 public function testUnknownLanguageTag( $code, $message = '' ) {
565 (bool)Language
::isKnownLanguageTag( $code ),
566 "checking that code $code is invalid - $message"
570 public static function provideUnknownLanguageTags() {
572 [ 'mw', 'non-existent two-letter code' ],
573 [ 'foo"<bar', 'very invalid language code' ],
578 * Test too short timestamp
579 * @expectedException MWException
580 * @covers Language::sprintfDate
582 public function testSprintfDateTooShortTimestamp() {
583 $this->getLang()->sprintfDate( 'xiY', '1234567890123' );
587 * Test too long timestamp
588 * @expectedException MWException
589 * @covers Language::sprintfDate
591 public function testSprintfDateTooLongTimestamp() {
592 $this->getLang()->sprintfDate( 'xiY', '123456789012345' );
596 * Test too short timestamp
597 * @expectedException MWException
598 * @covers Language::sprintfDate
600 public function testSprintfDateNotAllDigitTimestamp() {
601 $this->getLang()->sprintfDate( 'xiY', '-1234567890123' );
605 * @dataProvider provideSprintfDateSamples
606 * @covers Language::sprintfDate
608 public function testSprintfDate( $format, $ts, $expected, $msg ) {
612 $this->getLang()->sprintfDate( $format, $ts, null, $ttl ),
613 "sprintfDate('$format', '$ts'): $msg"
616 $dt = new DateTime( $ts );
617 $lastValidTS = $dt->add( new DateInterval( 'PT' . ( $ttl - 1 ) . 'S' ) )->format( 'YmdHis' );
620 $this->getLang()->sprintfDate( $format, $lastValidTS, null ),
621 "sprintfDate('$format', '$ts'): TTL $ttl too high (output was different at $lastValidTS)"
624 // advance the time enough to make all of the possible outputs different (except possibly L)
625 $dt = new DateTime( $ts );
626 $newTS = $dt->add( new DateInterval( 'P1Y1M8DT13H1M1S' ) )->format( 'YmdHis' );
629 $this->getLang()->sprintfDate( $format, $newTS, null ),
630 "sprintfDate('$format', '$ts'): Missing TTL (output was different at $newTS)"
636 * sprintfDate should always use UTC when no zone is given.
637 * @dataProvider provideSprintfDateSamples
638 * @covers Language::sprintfDate
640 public function testSprintfDateNoZone( $format, $ts, $expected, $ignore, $msg ) {
641 $oldTZ = date_default_timezone_get();
642 $res = date_default_timezone_set( 'Asia/Seoul' );
644 $this->markTestSkipped( "Error setting Timezone" );
649 $this->getLang()->sprintfDate( $format, $ts ),
650 "sprintfDate('$format', '$ts'): $msg"
653 date_default_timezone_set( $oldTZ );
657 * sprintfDate should use passed timezone
658 * @dataProvider provideSprintfDateSamples
659 * @covers Language::sprintfDate
661 public function testSprintfDateTZ( $format, $ts, $ignore, $expected, $msg ) {
662 $tz = new DateTimeZone( 'Asia/Seoul' );
664 $this->markTestSkipped( "Error getting Timezone" );
669 $this->getLang()->sprintfDate( $format, $ts, $tz ),
670 "sprintfDate('$format', '$ts', 'Asia/Seoul'): $msg"
675 * sprintfDate should only calculate a TTL if the caller is going to use it.
676 * @covers Language::sprintfDate
678 public function testSprintfDateNoTtlIfNotNeeded() {
679 $noTtl = 'unused'; // Value used to represent that the caller didn't pass a variable in.
681 $this->getLang()->sprintfDate( 'YmdHis', wfTimestampNow(), null, $noTtl );
682 $this->getLang()->sprintfDate( 'YmdHis', wfTimestampNow(), null, $ttl );
687 'If the caller does not set the $ttl variable, do not compute it.'
689 $this->assertInternalType( 'int', $ttl, 'TTL should have been computed.' );
692 public static function provideSprintfDateSamples() {
697 '1390', // note because we're testing English locale we get Latin-standard digits
699 'Iranian calendar full year'
706 'Iranian calendar short year'
713 'ISO 8601 (week) year'
736 // What follows is mostly copied from
737 // https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions#.23time
764 'Month index, not zero pad'
771 'Month index. Zero pad'
792 'Genitive month name (same in EN)'
799 'Day of month (not zero pad)'
806 'Day of month (zero-pad)'
813 'Day of year (zero-indexed)'
820 'Day of week (abbrev)'
834 'Day of week (Mon=1, Sun=7)'
841 'Day of week (Sun=0, Sat=6)'
883 '12 hour, zero padded'
932 'Days in current month'
937 '2012-01-02T09:07:05+00:00',
938 '2012-01-02T09:07:05+09:00',
944 'Mon, 02 Jan 2012 09:07:05 +0000',
945 'Mon, 02 Jan 2012 09:07:05 +0900',
953 'Timezone identifier'
974 'Timezone offset with colon'
981 'Timezone abbreviation'
988 'Timezone offset in seconds'
1016 'Hebrew number of days in month'
1023 'Hebrew genitive month name (No difference in EN)'
1065 'Raw numerals (doesn\'t mean much in EN)'
1068 '[[Y "(yea"\\r)]] \\"xx\\"',
1070 '[[2012 (year)]] "x"',
1071 '[[2012 (year)]] "x"',
1079 * @dataProvider provideFormatSizes
1080 * @covers Language::formatSize
1082 public function testFormatSize( $size, $expected, $msg ) {
1083 $this->assertEquals(
1085 $this->getLang()->formatSize( $size ),
1086 "formatSize('$size'): $msg"
1090 public static function provideFormatSizes() {
1137 // How big!? THIS BIG!
1142 * @dataProvider provideFormatBitrate
1143 * @covers Language::formatBitrate
1145 public function testFormatBitrate( $bps, $expected, $msg ) {
1146 $this->assertEquals(
1148 $this->getLang()->formatBitrate( $bps ),
1149 "formatBitrate('$bps'): $msg"
1153 public static function provideFormatBitrate() {
1163 "999 bits per second"
1168 "1 kilobit per second"
1173 "1 megabit per second"
1178 "1 gigabit per second"
1183 "1 terabit per second"
1188 "1 petabit per second"
1193 "1 exabit per second"
1198 "1 zetabit per second"
1203 "1 yottabit per second"
1208 "1,000 yottabits per second"
1214 * @dataProvider provideFormatDuration
1215 * @covers Language::formatDuration
1217 public function testFormatDuration( $duration, $expected, $intervals = [] ) {
1218 $this->assertEquals(
1220 $this->getLang()->formatDuration( $duration, $intervals ),
1221 "formatDuration('$duration'): $expected"
1225 public static function provideFormatDuration() {
1264 // ( 365 + ( 24 * 3 + 25 ) / 400 ) * 86400 = 31556952
1265 ( 365 +
( 24 * 3 +
25 ) / 400.0 ) * 86400,
1298 '2 hours, 30 minutes and 1 second'
1302 '1 hour and 1 second'
1305 31556952 +
2 * 86400 +
9000,
1306 '1 year, 2 days, 2 hours and 30 minutes'
1309 42 * 1000 * 31556952 +
42,
1310 '42 millennia and 42 seconds'
1328 31556952 +
2 * 86400 +
9000,
1329 '1 year, 2 days and 150 minutes',
1330 [ 'years', 'days', 'minutes' ],
1335 [ 'years', 'days' ],
1338 31556952 +
2 * 86400 +
9000,
1339 '1 year, 2 days and 150 minutes',
1340 [ 'minutes', 'days', 'years' ],
1345 [ 'days', 'years' ],
1351 * @dataProvider provideCheckTitleEncodingData
1352 * @covers Language::checkTitleEncoding
1354 public function testCheckTitleEncoding( $s ) {
1355 $this->assertEquals(
1357 $this->getLang()->checkTitleEncoding( $s ),
1358 "checkTitleEncoding('$s')"
1362 public static function provideCheckTitleEncodingData() {
1363 // phpcs:disable Generic.Files.LineLength
1366 [ "United States of America" ], // 7bit ASCII
1367 [ rawurldecode( "S%C3%A9rie%20t%C3%A9l%C3%A9vis%C3%A9e" ) ],
1370 "Acteur%7CAlbert%20Robbins%7CAnglais%7CAnn%20Donahue%7CAnthony%20E.%20Zuiker%7CCarol%20Mendelsohn"
1373 // The following two data sets come from T38839. They fail if checkTitleEncoding uses a regexp to test for
1374 // valid UTF-8 encoding and the pcre.recursion_limit is low (like, say, 1024). They succeed if checkTitleEncoding
1375 // uses mb_check_encoding for its test.
1378 "Acteur%7CAlbert%20Robbins%7CAnglais%7CAnn%20Donahue%7CAnthony%20E.%20Zuiker%7CCarol%20Mendelsohn%7C"
1379 . "Catherine%20Willows%7CDavid%20Hodges%7CDavid%20Phillips%7CGil%20Grissom%7CGreg%20Sanders%7CHodges%7C"
1380 . "Internet%20Movie%20Database%7CJim%20Brass%7CLady%20Heather%7C"
1381 . "Les%20Experts%20(s%C3%A9rie%20t%C3%A9l%C3%A9vis%C3%A9e)%7CLes%20Experts%20:%20Manhattan%7C"
1382 . "Les%20Experts%20:%20Miami%7CListe%20des%20personnages%20des%20Experts%7C"
1383 . "Liste%20des%20%C3%A9pisodes%20des%20Experts%7CMod%C3%A8le%20discussion:Palette%20Les%20Experts%7C"
1384 . "Nick%20Stokes%7CPersonnage%20de%20fiction%7CPersonnage%20fictif%7CPersonnage%20de%20fiction%7C"
1385 . "Personnages%20r%C3%A9currents%20dans%20Les%20Experts%7CRaymond%20Langston%7CRiley%20Adams%7C"
1386 . "Saison%201%20des%20Experts%7CSaison%2010%20des%20Experts%7CSaison%2011%20des%20Experts%7C"
1387 . "Saison%2012%20des%20Experts%7CSaison%202%20des%20Experts%7CSaison%203%20des%20Experts%7C"
1388 . "Saison%204%20des%20Experts%7CSaison%205%20des%20Experts%7CSaison%206%20des%20Experts%7C"
1389 . "Saison%207%20des%20Experts%7CSaison%208%20des%20Experts%7CSaison%209%20des%20Experts%7C"
1390 . "Sara%20Sidle%7CSofia%20Curtis%7CS%C3%A9rie%20t%C3%A9l%C3%A9vis%C3%A9e%7CWallace%20Langham%7C"
1391 . "Warrick%20Brown%7CWendy%20Simms%7C%C3%89tats-Unis"
1396 "Mod%C3%A8le%3AArrondissements%20homonymes%7CMod%C3%A8le%3ABandeau%20standard%20pour%20page%20d'homonymie%7C"
1397 . "Mod%C3%A8le%3ABatailles%20homonymes%7CMod%C3%A8le%3ACantons%20homonymes%7C"
1398 . "Mod%C3%A8le%3ACommunes%20fran%C3%A7aises%20homonymes%7CMod%C3%A8le%3AFilms%20homonymes%7C"
1399 . "Mod%C3%A8le%3AGouvernements%20homonymes%7CMod%C3%A8le%3AGuerres%20homonymes%7CMod%C3%A8le%3AHomonymie%7C"
1400 . "Mod%C3%A8le%3AHomonymie%20bateau%7CMod%C3%A8le%3AHomonymie%20d'%C3%A9tablissements%20scolaires%20ou"
1401 . "%20universitaires%7CMod%C3%A8le%3AHomonymie%20d'%C3%AEles%7CMod%C3%A8le%3AHomonymie%20de%20clubs%20sportifs%7C"
1402 . "Mod%C3%A8le%3AHomonymie%20de%20comt%C3%A9s%7CMod%C3%A8le%3AHomonymie%20de%20monument%7C"
1403 . "Mod%C3%A8le%3AHomonymie%20de%20nom%20romain%7CMod%C3%A8le%3AHomonymie%20de%20parti%20politique%7C"
1404 . "Mod%C3%A8le%3AHomonymie%20de%20route%7CMod%C3%A8le%3AHomonymie%20dynastique%7C"
1405 . "Mod%C3%A8le%3AHomonymie%20vid%C3%A9oludique%7CMod%C3%A8le%3AHomonymie%20%C3%A9difice%20religieux%7C"
1406 . "Mod%C3%A8le%3AInternationalisation%7CMod%C3%A8le%3AIsom%C3%A9rie%7CMod%C3%A8le%3AParonymie%7C"
1407 . "Mod%C3%A8le%3APatronyme%7CMod%C3%A8le%3APatronyme%20basque%7CMod%C3%A8le%3APatronyme%20italien%7C"
1408 . "Mod%C3%A8le%3APatronymie%7CMod%C3%A8le%3APersonnes%20homonymes%7CMod%C3%A8le%3ASaints%20homonymes%7C"
1409 . "Mod%C3%A8le%3ATitres%20homonymes%7CMod%C3%A8le%3AToponymie%7CMod%C3%A8le%3AUnit%C3%A9s%20homonymes%7C"
1410 . "Mod%C3%A8le%3AVilles%20homonymes%7CMod%C3%A8le%3A%C3%89difices%20religieux%20homonymes"
1418 * @dataProvider provideRomanNumeralsData
1419 * @covers Language::romanNumeral
1421 public function testRomanNumerals( $num, $numerals ) {
1422 $this->assertEquals(
1424 Language
::romanNumeral( $num ),
1425 "romanNumeral('$num')"
1429 public static function provideRomanNumeralsData() {
1462 [ 1989, 'MCMLXXXIX' ],
1468 [ 7000, 'MMMMMMM' ],
1469 [ 8000, 'MMMMMMMM' ],
1470 [ 9000, 'MMMMMMMMM' ],
1471 [ 9999, 'MMMMMMMMMCMXCIX' ],
1472 [ 10000, 'MMMMMMMMMM' ],
1477 * @dataProvider provideHebrewNumeralsData
1478 * @covers Language::hebrewNumeral
1480 public function testHebrewNumeral( $num, $numerals ) {
1481 $this->assertEquals(
1483 Language
::hebrewNumeral( $num ),
1484 "hebrewNumeral('$num')"
1488 public static function provideHebrewNumeralsData() {
1531 [ 2000, "ב' אלפים" ],
1533 [ 3000, "ג' אלפים" ],
1534 [ 4000, "ד' אלפים" ],
1535 [ 4904, "ד'תתק\"ד" ],
1536 [ 5000, "ה' אלפים" ],
1537 [ 5680, "ה'תר\"ף" ],
1538 [ 5690, "ה'תר\"ץ" ],
1539 [ 5708, "ה'תש\"ח" ],
1540 [ 5720, "ה'תש\"ך" ],
1541 [ 5740, "ה'תש\"ם" ],
1542 [ 5750, "ה'תש\"ן" ],
1543 [ 5775, "ה'תשע\"ה" ],
1548 * @dataProvider providePluralData
1549 * @covers Language::convertPlural
1551 public function testConvertPlural( $expected, $number, $forms ) {
1552 $chosen = $this->getLang()->convertPlural( $number, $forms );
1553 $this->assertEquals( $expected, $chosen );
1556 public static function providePluralData() {
1557 // Params are: [expected text, number given, [the plural forms]]
1560 'singular', 'plural'
1562 [ 'explicit zero', 0, [
1563 '0=explicit zero', 'singular', 'plural'
1565 [ 'explicit one', 1, [
1566 'singular', 'plural', '1=explicit one',
1569 'singular', 'plural', '0=explicit zero',
1572 '0=explicit zero', '1=explicit one', 'singular', 'plural'
1574 [ 'explicit eleven', 11, [
1575 'singular', 'plural', '11=explicit eleven',
1578 'singular', 'plural', '11=explicit twelve',
1581 'singular', 'plural', '=explicit form',
1584 'kissa=kala', '1=2=3', 'other',
1587 '0=explicit zero', '1=explicit one',
1593 * @covers Language::embedBidi()
1595 public function testEmbedBidi() {
1596 $lre = "\xE2\x80\xAA"; // U+202A LEFT-TO-RIGHT EMBEDDING
1597 $rle = "\xE2\x80\xAB"; // U+202B RIGHT-TO-LEFT EMBEDDING
1598 $pdf = "\xE2\x80\xAC"; // U+202C POP DIRECTIONAL FORMATTING
1599 $lang = $this->getLang();
1600 $this->assertEquals(
1602 $lang->embedBidi( '123' ),
1603 'embedBidi with neutral argument'
1605 $this->assertEquals(
1606 $lre . 'Ben_(WMF)' . $pdf,
1607 $lang->embedBidi( 'Ben_(WMF)' ),
1608 'embedBidi with LTR argument'
1610 $this->assertEquals(
1611 $rle . 'יהודי (מנוחין)' . $pdf,
1612 $lang->embedBidi( 'יהודי (מנוחין)' ),
1613 'embedBidi with RTL argument'
1618 * @covers Language::translateBlockExpiry()
1619 * @dataProvider provideTranslateBlockExpiry
1621 public function testTranslateBlockExpiry( $expectedData, $str, $now, $desc ) {
1622 $lang = $this->getLang();
1623 if ( is_array( $expectedData ) ) {
1624 list( $func, $arg ) = $expectedData;
1625 $expected = $lang->$func( $arg );
1627 $expected = $expectedData;
1629 $this->assertEquals( $expected, $lang->translateBlockExpiry( $str, null, $now ), $desc );
1632 public static function provideTranslateBlockExpiry() {
1634 [ '2 hours', '2 hours', 0, 'simple data from ipboptions' ],
1635 [ 'indefinite', 'infinite', 0, 'infinite from ipboptions' ],
1636 [ 'indefinite', 'infinity', 0, 'alternative infinite from ipboptions' ],
1637 [ 'indefinite', 'indefinite', 0, 'another alternative infinite from ipboptions' ],
1638 [ [ 'formatDuration', 1023 * 60 * 60 ], '1023 hours', 0, 'relative' ],
1639 [ [ 'formatDuration', -1023 ], '-1023 seconds', 0, 'negative relative' ],
1641 [ 'formatDuration', 1023 * 60 * 60 ],
1643 wfTimestamp( TS_UNIX
, '19910203040506' ),
1644 'relative with initial timestamp'
1646 [ [ 'formatDuration', 0 ], 'now', 0, 'now' ],
1648 [ 'timeanddate', '20120102070000' ],
1649 '2012-1-1 7:00 +1 day',
1651 'mixed, handled as absolute'
1653 [ [ 'timeanddate', '19910203040506' ], '1991-2-3 4:05:06', 0, 'absolute' ],
1654 [ [ 'timeanddate', '19700101000000' ], '1970-1-1 0:00:00', 0, 'absolute at epoch' ],
1655 [ [ 'timeanddate', '19691231235959' ], '1969-12-31 23:59:59', 0, 'time before epoch' ],
1657 [ 'timeanddate', '19910910000000' ],
1659 wfTimestamp( TS_UNIX
, '19910203040506' ),
1662 [ 'dummy', 'dummy', 0, 'return garbage as is' ],
1667 * @dataProvider provideFormatNum
1668 * @covers Language::formatNum
1670 public function testFormatNum(
1671 $translateNumerals, $langCode, $number, $nocommafy, $expected
1673 $this->setMwGlobals( [ 'wgTranslateNumerals' => $translateNumerals ] );
1674 $lang = Language
::factory( $langCode );
1675 $formattedNum = $lang->formatNum( $number, $nocommafy );
1676 $this->assertType( 'string', $formattedNum );
1677 $this->assertEquals( $expected, $formattedNum );
1680 public function provideFormatNum() {
1682 [ true, 'en', 100, false, '100' ],
1683 [ true, 'en', 101, true, '101' ],
1684 [ false, 'en', 103, false, '103' ],
1685 [ false, 'en', 104, true, '104' ],
1686 [ true, 'en', '105', false, '105' ],
1687 [ true, 'en', '106', true, '106' ],
1688 [ false, 'en', '107', false, '107' ],
1689 [ false, 'en', '108', true, '108' ],
1694 * @covers Language::parseFormattedNumber
1695 * @dataProvider parseFormattedNumberProvider
1697 public function testParseFormattedNumber( $langCode, $number ) {
1698 $lang = Language
::factory( $langCode );
1700 $localisedNum = $lang->formatNum( $number );
1701 $normalisedNum = $lang->parseFormattedNumber( $localisedNum );
1703 $this->assertEquals( $number, $normalisedNum );
1706 public function parseFormattedNumberProvider() {
1713 [ 'zh-classical', 7432 ]
1718 * @covers Language::commafy()
1719 * @dataProvider provideCommafyData
1721 public function testCommafy( $number, $numbersWithCommas ) {
1722 $this->assertEquals(
1724 $this->getLang()->commafy( $number ),
1725 "commafy('$number')"
1729 public static function provideCommafyData() {
1735 [ 10000, '10,000' ],
1736 [ 100000, '100,000' ],
1737 [ 1000000, '1,000,000' ],
1738 [ -1.0001, '-1.0001' ],
1739 [ 1.0001, '1.0001' ],
1740 [ 10.0001, '10.0001' ],
1741 [ 100.0001, '100.0001' ],
1742 [ 1000.0001, '1,000.0001' ],
1743 [ 10000.0001, '10,000.0001' ],
1744 [ 100000.0001, '100,000.0001' ],
1745 [ 1000000.0001, '1,000,000.0001' ],
1746 [ '200000000000000000000', '200,000,000,000,000,000,000' ],
1747 [ '-200000000000000000000', '-200,000,000,000,000,000,000' ],
1752 * @covers Language::listToText
1754 public function testListToText() {
1755 $lang = $this->getLang();
1756 $and = $lang->getMessageFromDB( 'and' );
1757 $s = $lang->getMessageFromDB( 'word-separator' );
1758 $c = $lang->getMessageFromDB( 'comma-separator' );
1760 $this->assertEquals( '', $lang->listToText( [] ) );
1761 $this->assertEquals( 'a', $lang->listToText( [ 'a' ] ) );
1762 $this->assertEquals( "a{$and}{$s}b", $lang->listToText( [ 'a', 'b' ] ) );
1763 $this->assertEquals( "a{$c}b{$and}{$s}c", $lang->listToText( [ 'a', 'b', 'c' ] ) );
1764 $this->assertEquals( "a{$c}b{$c}c{$and}{$s}d", $lang->listToText( [ 'a', 'b', 'c', 'd' ] ) );
1768 * @dataProvider provideIsSupportedLanguage
1769 * @covers Language::isSupportedLanguage
1771 public function testIsSupportedLanguage( $code, $expected, $comment ) {
1772 $this->assertEquals( $expected, Language
::isSupportedLanguage( $code ), $comment );
1775 public static function provideIsSupportedLanguage() {
1777 [ 'en', true, 'is supported language' ],
1778 [ 'fi', true, 'is supported language' ],
1779 [ 'bunny', false, 'is not supported language' ],
1780 [ 'FI', false, 'is not supported language, input should be in lower case' ],
1785 * @dataProvider provideGetParentLanguage
1786 * @covers Language::getParentLanguage
1788 public function testGetParentLanguage( $code, $expected, $comment ) {
1789 $lang = Language
::factory( $code );
1790 if ( is_null( $expected ) ) {
1791 $this->assertNull( $lang->getParentLanguage(), $comment );
1793 $this->assertEquals( $expected, $lang->getParentLanguage()->getCode(), $comment );
1797 public static function provideGetParentLanguage() {
1799 [ 'zh-cn', 'zh', 'zh is the parent language of zh-cn' ],
1800 [ 'zh', 'zh', 'zh is defined as the parent language of zh, '
1801 . 'because zh converter can convert zh-cn to zh' ],
1802 [ 'zh-invalid', null, 'do not be fooled by arbitrarily composed language codes' ],
1803 [ 'de-formal', null, 'de does not have converter' ],
1804 [ 'de', null, 'de does not have converter' ],
1809 * @dataProvider provideGetNamespaceAliases
1810 * @covers Language::getNamespaceAliases
1812 public function testGetNamespaceAliases( $languageCode, $subset ) {
1813 $language = Language
::factory( $languageCode );
1814 $aliases = $language->getNamespaceAliases();
1815 foreach ( $subset as $alias => $nsId ) {
1816 $this->assertEquals( $nsId, $aliases[$alias] );
1820 public static function provideGetNamespaceAliases() {
1821 // TODO: Add tests for NS_PROJECT_TALK and GenderNamespaces
1834 * @covers Language::equals
1836 public function testEquals() {
1837 $en1 = new Language();
1838 $en1->setCode( 'en' );
1840 $en2 = Language
::factory( 'en' );
1841 $en2->setCode( 'en' );
1843 $this->assertTrue( $en1->equals( $en2 ), 'en equals en' );
1845 $fr = Language
::factory( 'fr' );
1846 $this->assertFalse( $en1->equals( $fr ), 'en not equals fr' );