3 class LanguageTest
extends MediaWikiTestCase
{
11 $this->lang
= Language
::factory( 'en' );
17 function testLanguageConvertDoubleWidthToSingleWidth() {
19 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
20 $this->lang
->normalizeForSearch(
21 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
23 'convertDoubleWidth() with the full alphabet and digits'
27 /** @dataProvider provideFormattableTimes */
28 function testFormatTimePeriod( $seconds, $format, $expected, $desc ) {
29 $this->assertEquals( $expected, $this->lang
->formatTimePeriod( $seconds, $format ), $desc );
32 function provideFormattableTimes() {
34 array( 9.45, array(), '9.5s', 'formatTimePeriod() rounding (<10s)' ),
35 array( 9.45, array( 'noabbrevs' => true ), '9.5 seconds', 'formatTimePeriod() rounding (<10s)' ),
36 array( 9.95, array(), '10s', 'formatTimePeriod() rounding (<10s)' ),
37 array( 9.95, array( 'noabbrevs' => true ), '10 seconds', 'formatTimePeriod() rounding (<10s)' ),
38 array( 59.55, array(), '1m 0s', 'formatTimePeriod() rounding (<60s)' ),
39 array( 59.55, array( 'noabbrevs' => true ), '1 minute 0 seconds', 'formatTimePeriod() rounding (<60s)' ),
40 array( 119.55, array(), '2m 0s', 'formatTimePeriod() rounding (<1h)' ),
41 array( 119.55, array( 'noabbrevs' => true ), '2 minutes 0 seconds', 'formatTimePeriod() rounding (<1h)' ),
42 array( 3599.55, array(), '1h 0m 0s', 'formatTimePeriod() rounding (<1h)' ),
43 array( 3599.55, array( 'noabbrevs' => true ), '1 hour 0 minutes 0 seconds', 'formatTimePeriod() rounding (<1h)' ),
44 array( 7199.55, array(), '2h 0m 0s', 'formatTimePeriod() rounding (>=1h)' ),
45 array( 7199.55, array( 'noabbrevs' => true ), '2 hours 0 minutes 0 seconds', 'formatTimePeriod() rounding (>=1h)' ),
46 array( 7199.55, 'avoidseconds', '2h 0m', 'formatTimePeriod() rounding (>=1h), avoidseconds' ),
47 array( 7199.55, array( 'avoid' => 'avoidseconds', 'noabbrevs' => true ), '2 hours 0 minutes', 'formatTimePeriod() rounding (>=1h), avoidseconds' ),
48 array( 7199.55, 'avoidminutes', '2h 0m', 'formatTimePeriod() rounding (>=1h), avoidminutes' ),
49 array( 7199.55, array( 'avoid' => 'avoidminutes', 'noabbrevs' => true ), '2 hours 0 minutes', 'formatTimePeriod() rounding (>=1h), avoidminutes' ),
50 array( 172799.55, 'avoidseconds', '48h 0m', 'formatTimePeriod() rounding (=48h), avoidseconds' ),
51 array( 172799.55, array( 'avoid' => 'avoidseconds', 'noabbrevs' => true ), '48 hours 0 minutes', 'formatTimePeriod() rounding (=48h), avoidseconds' ),
52 array( 259199.55, 'avoidminutes', '3d 0h', 'formatTimePeriod() rounding (>48h), avoidminutes' ),
53 array( 259199.55, array( 'avoid' => 'avoidminutes', 'noabbrevs' => true ), '3 days 0 hours', 'formatTimePeriod() rounding (>48h), avoidminutes' ),
54 array( 176399.55, 'avoidseconds', '2d 1h 0m', 'formatTimePeriod() rounding (>48h), avoidseconds' ),
55 array( 176399.55, array( 'avoid' => 'avoidseconds', 'noabbrevs' => true ), '2 days 1 hour 0 minutes', 'formatTimePeriod() rounding (>48h), avoidseconds' ),
56 array( 176399.55, 'avoidminutes', '2d 1h', 'formatTimePeriod() rounding (>48h), avoidminutes' ),
57 array( 176399.55, array( 'avoid' => 'avoidminutes', 'noabbrevs' => true ), '2 days 1 hour', 'formatTimePeriod() rounding (>48h), avoidminutes' ),
58 array( 259199.55, 'avoidseconds', '3d 0h 0m', 'formatTimePeriod() rounding (>48h), avoidseconds' ),
59 array( 259199.55, array( 'avoid' => 'avoidseconds', 'noabbrevs' => true ), '3 days 0 hours 0 minutes', 'formatTimePeriod() rounding (>48h), avoidseconds' ),
60 array( 172801.55, 'avoidseconds', '2d 0h 0m', 'formatTimePeriod() rounding, (>48h), avoidseconds' ),
61 array( 172801.55, array( 'avoid' => 'avoidseconds', 'noabbrevs' => true ), '2 days 0 hours 0 minutes', 'formatTimePeriod() rounding, (>48h), avoidseconds' ),
62 array( 176460.55, array(), '2d 1h 1m 1s', 'formatTimePeriod() rounding, recursion, (>48h)' ),
63 array( 176460.55, array( 'noabbrevs' => true ), '2 days 1 hour 1 minute 1 second', 'formatTimePeriod() rounding, recursion, (>48h)' ),
68 function testTruncate() {
71 $this->lang
->truncate( "1234567890", 0, 'XXX' ),
72 'truncate prefix, len 0, small ellipsis'
77 $this->lang
->truncate( "1234567890", 8, 'XXX' ),
78 'truncate prefix, small ellipsis'
83 $this->lang
->truncate( "123456789", 5, 'XXXXXXXXXXXXXXX' ),
84 'truncate prefix, large ellipsis'
89 $this->lang
->truncate( "1234567890", -8, 'XXX' ),
90 'truncate suffix, small ellipsis'
95 $this->lang
->truncate( "123456789", -5, 'XXXXXXXXXXXXXXX' ),
96 'truncate suffix, large ellipsis'
101 * @dataProvider provideHTMLTruncateData()
103 function testTruncateHtml( $len, $ellipsis, $input, $expected ) {
107 $this->lang
->truncateHTML( $input, $len, $ellipsis )
112 * Array format is ($len, $ellipsis, $input, $expected)
114 function provideHTMLTruncateData() {
116 array( 0, 'XXX', "1234567890", "XXX" ),
117 array( 8, 'XXX', "1234567890", "12345XXX" ),
118 array( 5, 'XXXXXXXXXXXXXXX', '1234567890', "1234567890" ),
120 '<p><span style="font-weight:bold;"></span></p>',
121 '<p><span style="font-weight:bold;"></span></p>',
124 '<p><span style="font-weight:bold;">123456789</span></p>',
125 '<p><span style="font-weight:bold;">***</span></p>',
128 '<p><span style="font-weight:bold;"> 23456789</span></p>',
129 '<p><span style="font-weight:bold;">***</span></p>',
132 '<p><span style="font-weight:bold;">123456789</span></p>',
133 '<p><span style="font-weight:bold;">***</span></p>',
136 '<p><span style="font-weight:bold;">123456789</span></p>',
137 '<p><span style="font-weight:bold;">1***</span></p>',
140 '<tt><span style="font-weight:bold;">123456789</span></tt>',
141 '<tt><span style="font-weight:bold;">12***</span></tt>',
144 '<p><a href="www.mediawiki.org">123456789</a></p>',
145 '<p><a href="www.mediawiki.org">123***</a></p>',
148 '<p><a href="www.mediawiki.org">12 456789</a></p>',
149 '<p><a href="www.mediawiki.org">12 ***</a></p>',
152 '<small><span style="font-weight:bold;">123<p id="#moo">456</p>789</span></small>',
153 '<small><span style="font-weight:bold;">123<p id="#moo">4***</p></span></small>',
156 '<div><span style="font-weight:bold;">123<span>4</span>56789</span></div>',
157 '<div><span style="font-weight:bold;">123<span>4</span>5***</span></div>',
160 '<p><table style="font-weight:bold;"><tr><td>123456789</td></tr></table></p>',
161 '<p><table style="font-weight:bold;"><tr><td>123456789</td></tr></table></p>',
164 '<p><font style="font-weight:bold;">123456789</font></p>',
165 '<p><font style="font-weight:bold;">123456789</font></p>',
171 * Test Language::isValidBuiltInCode()
172 * @dataProvider provideLanguageCodes
174 function testBuiltInCodeValidation( $code, $message = '' ) {
176 (bool) Language
::isValidBuiltInCode( $code ),
177 "validating code $code $message"
181 function testBuiltInCodeValidationRejectUnderscore() {
183 (bool) Language
::isValidBuiltInCode( 'be_tarask' ),
184 "reject underscore in language code"
188 function provideLanguageCodes() {
190 array( 'fr' , 'Two letters, minor case' ),
191 array( 'EN' , 'Two letters, upper case' ),
192 array( 'tyv' , 'Three letters' ),
193 array( 'tokipona' , 'long language code' ),
194 array( 'be-tarask', 'With dash' ),
195 array( 'Zh-classical', 'Begin with upper case, dash' ),
196 array( 'Be-x-old', 'With extension (two dashes)' ),
201 * @dataProvider provideSprintfDateSamples
203 function testSprintfDate( $format, $ts, $expected, $msg ) {
206 $this->lang
->sprintfDate( $format, $ts ),
207 "sprintfDate('$format', '$ts'): $msg"
211 * bug 33454. sprintfDate should always use UTC.
212 * @dataProvider provideSprintfDateSamples
214 function testSprintfDateTZ( $format, $ts, $expected, $msg ) {
215 $oldTZ = date_default_timezone_get();
216 $res = date_default_timezone_set( 'Asia/Seoul' );
218 $this->markTestSkipped( "Error setting Timezone" );
223 $this->lang
->sprintfDate( $format, $ts ),
224 "sprintfDate('$format', '$ts'): $msg"
227 date_default_timezone_set( $oldTZ );
230 function provideSprintfDateSamples() {
235 '1390', // note because we're testing English locale we get Latin-standard digits
236 'Iranian calendar full year'
242 'Iranian calendar short year'
248 'ISO 8601 (week) year'
268 // What follows is mostly copied from http://www.mediawiki.org/wiki/Help:Extension:ParserFunctions#.23time
291 'Month index, not zero pad'
297 'Month index. Zero pad'
315 'Genitive month name (same in EN)'
321 'Day of month (not zero pad)'
327 'Day of month (zero-pad)'
333 'Day of year (zero-indexed)'
339 'Day of week (abbrev)'
351 'Day of week (Mon=1, Sun=7)'
357 'Day of week (Sun=0, Sat=6)'
393 '12 hour, zero padded'
435 'Days in current month'
440 '2012-01-02T09:07:05+00:00',
446 'Mon, 02 Jan 2012 09:07:05 +0000',
471 'Hebrew number of days in month'
477 'Hebrew genitive month name (No difference in EN)'
513 'Raw numerals (doesn\'t mean much in EN)'
516 '[[Y "(yea"\\r)]] \\"xx\\"',
518 '[[2012 (year)]] "x"',
526 * @dataProvider provideFormatSizes
528 function testFormatSize( $size, $expected, $msg ) {
531 $this->lang
->formatSize( $size ),
532 "formatSize('$size'): $msg"
536 function provideFormatSizes() {
583 // How big!? THIS BIG!
588 * @dataProvider provideFormatBitrate
590 function testFormatBitrate( $bps, $expected, $msg ) {
593 $this->lang
->formatBitrate( $bps ),
594 "formatBitrate('$bps'): $msg"
598 function provideFormatBitrate() {
608 "999 bits per second"
613 "1 kilobit per second"
618 "1 megabit per second"
623 "1 gigabit per second"
628 "1 terabit per second"
633 "1 petabit per second"
638 "1 exabit per second"
643 "1 zetabit per second"
648 "1 yottabit per second"
653 "1,000 yottabits per second"
661 * @dataProvider provideFormatDuration
663 function testFormatDuration( $duration, $expected, $intervals = array() ) {
666 $this->lang
->formatDuration( $duration, $intervals ),
667 "formatDuration('$duration'): $expected"
671 function provideFormatDuration() {
710 365.25 * 86400, // 365.25 * 86400 = 31557600
743 '2 hours, 30 minutes and 1 second'
747 '1 hour and 1 second'
750 31557600 +
2 * 86400 +
9000,
751 '1 year, 2 days, 2 hours and 30 minutes'
754 42 * 1000 * 31557600 +
42,
755 '42 millennia and 42 seconds'
773 31557600 +
2 * 86400 +
9000,
774 '1 year, 2 days and 150 minutes',
775 array( 'years', 'days', 'minutes' ),
780 array( 'years', 'days' ),
783 31557600 +
2 * 86400 +
9000,
784 '1 year, 2 days and 150 minutes',
785 array( 'minutes', 'days', 'years' ),
790 array( 'days', 'years' ),