MediaWiki  REL1_19
LanguageTest.php
Go to the documentation of this file.
00001 <?php
00002 
00003 class LanguageTest extends MediaWikiTestCase {
00004 
00008         private $lang;
00009 
00010         function setUp() {
00011                 $this->lang = Language::factory( 'en' );
00012         }
00013         function tearDown() {
00014                 unset( $this->lang );
00015         }
00016 
00017         function testLanguageConvertDoubleWidthToSingleWidth() {
00018                 $this->assertEquals(
00019                         "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
00020                         $this->lang->normalizeForSearch(
00021                                 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
00022                         ),
00023                         'convertDoubleWidth() with the full alphabet and digits'
00024                 );
00025         }
00026 
00028         function testFormatTimePeriod( $seconds, $format, $expected, $desc ) {
00029                 $this->assertEquals( $expected, $this->lang->formatTimePeriod( $seconds, $format ), $desc );
00030         }
00031 
00032         function provideFormattableTimes() {
00033                 return array(
00034                         array( 9.45, array(), '9.5 s', 'formatTimePeriod() rounding (<10s)' ),
00035                         array( 9.45, array( 'noabbrevs' => true ), '9.5 seconds', 'formatTimePeriod() rounding (<10s)' ),
00036                         array( 9.95, array(), '10 s', 'formatTimePeriod() rounding (<10s)' ),
00037                         array( 9.95, array( 'noabbrevs' => true ), '10 seconds', 'formatTimePeriod() rounding (<10s)' ),
00038                         array( 59.55, array(), '1 min 0 s', 'formatTimePeriod() rounding (<60s)' ),
00039                         array( 59.55, array( 'noabbrevs' => true ), '1 minute 0 seconds', 'formatTimePeriod() rounding (<60s)' ),
00040                         array( 119.55, array(), '2 min 0 s', 'formatTimePeriod() rounding (<1h)' ),
00041                         array( 119.55, array( 'noabbrevs' => true ), '2 minutes 0 seconds', 'formatTimePeriod() rounding (<1h)' ),
00042                         array( 3599.55, array(), '1 h 0 min 0 s', 'formatTimePeriod() rounding (<1h)' ),
00043                         array( 3599.55, array( 'noabbrevs' => true ), '1 hour 0 minutes 0 seconds', 'formatTimePeriod() rounding (<1h)' ),
00044                         array( 7199.55, array(), '2 h 0 min 0 s', 'formatTimePeriod() rounding (>=1h)' ),
00045                         array( 7199.55, array( 'noabbrevs' => true ), '2 hours 0 minutes 0 seconds', 'formatTimePeriod() rounding (>=1h)' ),
00046                         array( 7199.55, 'avoidseconds', '2 h 0 min', 'formatTimePeriod() rounding (>=1h), avoidseconds' ),
00047                         array( 7199.55, array( 'avoid' => 'avoidseconds', 'noabbrevs' => true ), '2 hours 0 minutes', 'formatTimePeriod() rounding (>=1h), avoidseconds' ),
00048                         array( 7199.55, 'avoidminutes', '2 h 0 min', 'formatTimePeriod() rounding (>=1h), avoidminutes' ),
00049                         array( 7199.55, array( 'avoid' => 'avoidminutes', 'noabbrevs' => true ), '2 hours 0 minutes', 'formatTimePeriod() rounding (>=1h), avoidminutes' ),
00050                         array( 172799.55, 'avoidseconds', '48 h 0 min', 'formatTimePeriod() rounding (=48h), avoidseconds' ),
00051                         array( 172799.55, array( 'avoid' => 'avoidseconds', 'noabbrevs' => true ), '48 hours 0 minutes', 'formatTimePeriod() rounding (=48h), avoidseconds' ),
00052                         array( 259199.55, 'avoidminutes', '3 d 0 h', 'formatTimePeriod() rounding (>48h), avoidminutes' ),
00053                         array( 259199.55, array( 'avoid' => 'avoidminutes', 'noabbrevs' => true ), '3 days 0 hours', 'formatTimePeriod() rounding (>48h), avoidminutes' ),
00054                         array( 176399.55, 'avoidseconds', '2 d 1 h 0 min', 'formatTimePeriod() rounding (>48h), avoidseconds' ),
00055                         array( 176399.55, array( 'avoid' => 'avoidseconds', 'noabbrevs' => true ), '2 days 1 hour 0 minutes', 'formatTimePeriod() rounding (>48h), avoidseconds' ),
00056                         array( 176399.55, 'avoidminutes', '2 d 1 h', 'formatTimePeriod() rounding (>48h), avoidminutes' ),
00057                         array( 176399.55, array( 'avoid' => 'avoidminutes', 'noabbrevs' => true ), '2 days 1 hour', 'formatTimePeriod() rounding (>48h), avoidminutes' ),
00058                         array( 259199.55, 'avoidseconds', '3 d 0 h 0 min', 'formatTimePeriod() rounding (>48h), avoidseconds' ),
00059                         array( 259199.55, array( 'avoid' => 'avoidseconds', 'noabbrevs' => true ), '3 days 0 hours 0 minutes', 'formatTimePeriod() rounding (>48h), avoidseconds' ),
00060                         array( 172801.55, 'avoidseconds', '2 d 0 h 0 min', 'formatTimePeriod() rounding, (>48h), avoidseconds' ),
00061                         array( 172801.55, array( 'avoid' => 'avoidseconds', 'noabbrevs' => true ), '2 days 0 hours 0 minutes', 'formatTimePeriod() rounding, (>48h), avoidseconds' ),
00062                         array( 176460.55, array(), '2 d 1 h 1 min 1 s', 'formatTimePeriod() rounding, recursion, (>48h)' ),
00063                         array( 176460.55, array( 'noabbrevs' => true ), '2 days 1 hour 1 minute 1 second', 'formatTimePeriod() rounding, recursion, (>48h)' ),
00064                 );
00065 
00066         }
00067 
00068         function testTruncate() {
00069                 $this->assertEquals(
00070                         "XXX",
00071                         $this->lang->truncate( "1234567890", 0, 'XXX' ),
00072                         'truncate prefix, len 0, small ellipsis'
00073                 );
00074 
00075                 $this->assertEquals(
00076                         "12345XXX",
00077                         $this->lang->truncate( "1234567890", 8, 'XXX' ),
00078                         'truncate prefix, small ellipsis'
00079                 );
00080 
00081                 $this->assertEquals(
00082                         "123456789",
00083                         $this->lang->truncate( "123456789", 5, 'XXXXXXXXXXXXXXX' ),
00084                         'truncate prefix, large ellipsis'
00085                 );
00086 
00087                 $this->assertEquals(
00088                         "XXX67890",
00089                         $this->lang->truncate( "1234567890", -8, 'XXX' ),
00090                         'truncate suffix, small ellipsis'
00091                 );
00092 
00093                 $this->assertEquals(
00094                         "123456789",
00095                         $this->lang->truncate( "123456789", -5, 'XXXXXXXXXXXXXXX' ),
00096                         'truncate suffix, large ellipsis'
00097                 );
00098         }
00099 
00103         function testTruncateHtml( $len, $ellipsis, $input, $expected ) {
00104                 // Actual HTML...
00105                 $this->assertEquals(
00106                         $expected,
00107                         $this->lang->truncateHTML( $input, $len, $ellipsis )
00108                 );
00109         }
00110 
00114         function provideHTMLTruncateData() {
00115                 return array(
00116                         array( 0, 'XXX', "1234567890", "XXX" ),
00117                         array( 8, 'XXX', "1234567890", "12345XXX" ),
00118                         array( 5, 'XXXXXXXXXXXXXXX', '1234567890', "1234567890" ),
00119                         array( 2, '***',
00120                                 '<p><span style="font-weight:bold;"></span></p>',
00121                                 '<p><span style="font-weight:bold;"></span></p>',
00122                         ),
00123                         array( 2, '***',
00124                                 '<p><span style="font-weight:bold;">123456789</span></p>',
00125                                 '<p><span style="font-weight:bold;">***</span></p>',
00126                         ),
00127                         array( 2, '***',
00128                                 '<p><span style="font-weight:bold;">&nbsp;23456789</span></p>',
00129                                 '<p><span style="font-weight:bold;">***</span></p>',
00130                         ),
00131                         array( 3, '***',
00132                                 '<p><span style="font-weight:bold;">123456789</span></p>',
00133                                 '<p><span style="font-weight:bold;">***</span></p>',
00134                         ),
00135                         array( 4, '***',
00136                                 '<p><span style="font-weight:bold;">123456789</span></p>',
00137                                 '<p><span style="font-weight:bold;">1***</span></p>',
00138                         ),
00139                         array( 5, '***',
00140                                 '<tt><span style="font-weight:bold;">123456789</span></tt>',
00141                                 '<tt><span style="font-weight:bold;">12***</span></tt>',
00142                         ),
00143                         array( 6, '***',
00144                                 '<p><a href="www.mediawiki.org">123456789</a></p>',
00145                                 '<p><a href="www.mediawiki.org">123***</a></p>',
00146                         ),
00147                         array( 6, '***',
00148                                 '<p><a href="www.mediawiki.org">12&nbsp;456789</a></p>',
00149                                 '<p><a href="www.mediawiki.org">12&nbsp;***</a></p>',
00150                         ),
00151                         array( 7, '***',
00152                                 '<small><span style="font-weight:bold;">123<p id="#moo">456</p>789</span></small>',
00153                                 '<small><span style="font-weight:bold;">123<p id="#moo">4***</p></span></small>',
00154                         ),
00155                         array( 8, '***',
00156                                 '<div><span style="font-weight:bold;">123<span>4</span>56789</span></div>',
00157                                 '<div><span style="font-weight:bold;">123<span>4</span>5***</span></div>',
00158                         ),
00159                         array( 9, '***',
00160                                 '<p><table style="font-weight:bold;"><tr><td>123456789</td></tr></table></p>',
00161                                 '<p><table style="font-weight:bold;"><tr><td>123456789</td></tr></table></p>',
00162                         ),
00163                         array( 10, '***',
00164                                 '<p><font style="font-weight:bold;">123456789</font></p>',
00165                                 '<p><font style="font-weight:bold;">123456789</font></p>',
00166                         ),
00167                 );
00168         }
00169 
00174         function testBuiltInCodeValidation( $code, $message = '' ) {
00175                 $this->assertTrue(
00176                         (bool) Language::isValidBuiltInCode( $code ),
00177                         "validating code $code $message"
00178                 );
00179         }
00180 
00181         function testBuiltInCodeValidationRejectUnderscore() {
00182                 $this->assertFalse(
00183                         (bool) Language::isValidBuiltInCode( 'be_tarask' ),
00184                         "reject underscore in language code"
00185                 );
00186         }
00187 
00188         function provideLanguageCodes() {
00189                 return array(
00190                         array( 'fr'       , 'Two letters, minor case' ),
00191                         array( 'EN'       , 'Two letters, upper case' ),
00192                         array( 'tyv'      , 'Three letters' ),
00193                         array( 'tokipona'   , 'long language code' ),
00194                         array( 'be-tarask', 'With dash' ),
00195                         array( 'Zh-classical', 'Begin with upper case, dash' ),
00196                         array( 'Be-x-old', 'With extension (two dashes)' ),
00197                 );
00198         }
00199 
00203         function testSprintfDate( $format, $ts, $expected, $msg ) {
00204                 $this->assertEquals(
00205                         $expected,
00206                         $this->lang->sprintfDate( $format, $ts ),
00207                         "sprintfDate('$format', '$ts'): $msg"
00208                 );
00209         }
00214         function testSprintfDateTZ( $format, $ts, $expected, $msg ) {
00215                 $oldTZ = date_default_timezone_get();
00216                 $res = date_default_timezone_set( 'Asia/Seoul' );
00217                 if ( !$res ) {
00218                         $this->markTestSkipped( "Error setting Timezone" );
00219                 }
00220 
00221                 $this->assertEquals(
00222                         $expected,
00223                         $this->lang->sprintfDate( $format, $ts ),
00224                         "sprintfDate('$format', '$ts'): $msg"
00225                 );
00226 
00227                 date_default_timezone_set( $oldTZ );
00228         }
00229 
00230         function provideSprintfDateSamples() {
00231                 return array(
00232                         array(
00233                                 'xiY',
00234                                 '20111212000000',
00235                                 '1390', // note because we're testing English locale we get Latin-standard digits
00236                                 'Iranian calendar full year'
00237                         ),
00238                         array(
00239                                 'xiy',
00240                                 '20111212000000',
00241                                 '90',
00242                                 'Iranian calendar short year'
00243                         ),
00244                         array(
00245                                 'o',
00246                                 '20120101235000',
00247                                 '2011',
00248                                 'ISO 8601 (week) year'
00249                         ),
00250                         array(
00251                                 'W',
00252                                 '20120101235000',
00253                                 '52',
00254                                 'Week number'
00255                         ),
00256                         array(
00257                                 'W',
00258                                 '20120102235000',
00259                                 '1',
00260                                 'Week number'
00261                         ),
00262                         array(
00263                                 'o-\\WW-N',
00264                                 '20091231235000',
00265                                 '2009-W53-4',
00266                                 'leap week'
00267                         ),
00268                         // What follows is mostly copied from http://www.mediawiki.org/wiki/Help:Extension:ParserFunctions#.23time
00269                         array(
00270                                 'Y',
00271                                 '20120102090705',
00272                                 '2012',
00273                                 'Full year'
00274                         ),
00275                         array(
00276                                 'y',
00277                                 '20120102090705',
00278                                 '12',
00279                                 '2 digit year'
00280                         ),
00281                         array(
00282                                 'L',
00283                                 '20120102090705',
00284                                 '1',
00285                                 'Leap year'
00286                         ),
00287                         array(
00288                                 'n',
00289                                 '20120102090705',
00290                                 '1',
00291                                 'Month index, not zero pad'
00292                         ),
00293                         array(
00294                                 'N',
00295                                 '20120102090705',
00296                                 '01',
00297                                 'Month index. Zero pad'
00298                         ),
00299                         array(
00300                                 'M',
00301                                 '20120102090705',
00302                                 'Jan',
00303                                 'Month abbrev'
00304                         ),
00305                         array(
00306                                 'F',
00307                                 '20120102090705',
00308                                 'January',
00309                                 'Full month'
00310                         ),
00311                         array(
00312                                 'xg',
00313                                 '20120102090705',
00314                                 'January',
00315                                 'Genitive month name (same in EN)'
00316                         ),
00317                         array(
00318                                 'j',
00319                                 '20120102090705',
00320                                 '2',
00321                                 'Day of month (not zero pad)'
00322                         ),
00323                         array(
00324                                 'd',
00325                                 '20120102090705',
00326                                 '02',
00327                                 'Day of month (zero-pad)'
00328                         ),
00329                         array(
00330                                 'z',
00331                                 '20120102090705',
00332                                 '1',
00333                                 'Day of year (zero-indexed)'
00334                         ),
00335                         array(
00336                                 'D',
00337                                 '20120102090705',
00338                                 'Mon',
00339                                 'Day of week (abbrev)'
00340                         ),
00341                         array(
00342                                 'l',
00343                                 '20120102090705',
00344                                 'Monday',
00345                                 'Full day of week'
00346                         ),
00347                         array(
00348                                 'N',
00349                                 '20120101090705',
00350                                 '7',
00351                                 'Day of week (Mon=1, Sun=7)'
00352                         ),
00353                         array(
00354                                 'w',
00355                                 '20120101090705',
00356                                 '0',
00357                                 'Day of week (Sun=0, Sat=6)'
00358                         ),
00359                         array(
00360                                 'N',
00361                                 '20120102090705',
00362                                 '1',
00363                                 'Day of week'
00364                         ),
00365                         array(
00366                                 'a',
00367                                 '20120102090705',
00368                                 'am',
00369                                 'am vs pm'
00370                         ),
00371                         array(
00372                                 'A',
00373                                 '20120102120000',
00374                                 'PM',
00375                                 'AM vs PM'
00376                         ),
00377                         array(
00378                                 'a',
00379                                 '20120102000000',
00380                                 'am',
00381                                 'AM vs PM'
00382                         ),
00383                         array(
00384                                 'g',
00385                                 '20120102090705',
00386                                 '9',
00387                                 '12 hour, not Zero'
00388                         ),
00389                         array(
00390                                 'h',
00391                                 '20120102090705',
00392                                 '09',
00393                                 '12 hour, zero padded'
00394                         ),
00395                         array(
00396                                 'G',
00397                                 '20120102090705',
00398                                 '9',
00399                                 '24 hour, not zero'
00400                         ),
00401                         array(
00402                                 'H',
00403                                 '20120102090705',
00404                                 '09',
00405                                 '24 hour, zero'
00406                         ),
00407                         array(
00408                                 'H',
00409                                 '20120102110705',
00410                                 '11',
00411                                 '24 hour, zero'
00412                         ),
00413                         array(
00414                                 'i',
00415                                 '20120102090705',
00416                                 '07',
00417                                 'Minutes'
00418                         ),
00419                         array(
00420                                 's',
00421                                 '20120102090705',
00422                                 '05',
00423                                 'seconds'
00424                         ),
00425                         array(
00426                                 'U',
00427                                 '20120102090705',
00428                                 '1325495225',
00429                                 'unix time'
00430                         ),
00431                         array(
00432                                 't',
00433                                 '20120102090705',
00434                                 '31',
00435                                 'Days in current month'
00436                         ),
00437                         array(
00438                                 'c',
00439                                 '20120102090705',
00440                                 '2012-01-02T09:07:05+00:00',
00441                                 'ISO 8601 timestamp'
00442                         ),
00443                         array(
00444                                 'r',
00445                                 '20120102090705',
00446                                 'Mon, 02 Jan 2012 09:07:05 +0000',
00447                                 'RFC 5322'
00448                         ),
00449                         array(
00450                                 'xmj xmF xmn xmY',
00451                                 '20120102090705',
00452                                 '7 Safar 2 1433',
00453                                 'Islamic'
00454                         ),
00455                         array(
00456                                 'xij xiF xin xiY',
00457                                 '20120102090705',
00458                                 '12 Dey 10 1390',
00459                                 'Iranian'
00460                         ),
00461                         array(
00462                                 'xjj xjF xjn xjY',
00463                                 '20120102090705',
00464                                 '7 Tevet 4 5772',
00465                                 'Hebrew'
00466                         ),
00467                         array(
00468                                 'xjt',
00469                                 '20120102090705',
00470                                 '29',
00471                                 'Hebrew number of days in month'
00472                         ),
00473                         array(
00474                                 'xjx',
00475                                 '20120102090705',
00476                                 'Tevet',
00477                                 'Hebrew genitive month name (No difference in EN)'
00478                         ),
00479                         array(
00480                                 'xkY',
00481                                 '20120102090705',
00482                                 '2555',
00483                                 'Thai year'
00484                         ),
00485                         array(
00486                                 'xoY',
00487                                 '20120102090705',
00488                                 '101',
00489                                 'Minguo'
00490                         ),
00491                         array(
00492                                 'xtY',
00493                                 '20120102090705',
00494                                 '平成24',
00495                                 'nengo'
00496                         ),
00497                         array(
00498                                 'xrxkYY',
00499                                 '20120102090705',
00500                                 'MMDLV2012',
00501                                 'Roman numerals'
00502                         ),
00503                         array(
00504                                 'xhxjYY',
00505                                 '20120102090705',
00506                                 'ה\'תשע"ב2012',
00507                                 'Hebrew numberals'
00508                         ),
00509                         array(
00510                                 'xnY',
00511                                 '20120102090705',
00512                                 '2012',
00513                                 'Raw numerals (doesn\'t mean much in EN)'
00514                         ),
00515                         array(
00516                                 '[[Y "(yea"\\r)]] \\"xx\\"',
00517                                 '20120102090705',
00518                                 '[[2012 (year)]] "x"',
00519                                 'Various escaping'
00520                         ),
00521 
00522                 );
00523         }
00524 
00528         function testFormatSize( $size, $expected, $msg ) {
00529                 $this->assertEquals(
00530                         $expected,
00531                         $this->lang->formatSize( $size ),
00532                         "formatSize('$size'): $msg"
00533                 );
00534         }
00535 
00536         function provideFormatSizes() {
00537                 return array(
00538                         array(
00539                                 0,
00540                                 "0 B",
00541                                 "Zero bytes"
00542                         ),
00543                         array(
00544                                 1024,
00545                                 "1 KB",
00546                                 "1 kilobyte"
00547                         ),
00548                         array(
00549                                 1024 * 1024,
00550                                 "1 MB",
00551                                 "1,024 megabytes"
00552                         ),
00553                         array(
00554                                 1024 * 1024 * 1024,
00555                                 "1 GB",
00556                                 "1 gigabytes"
00557                         ),
00558                         array(
00559                                 pow( 1024, 4 ),
00560                                 "1 TB",
00561                                 "1 terabyte"
00562                         ),
00563                         array(
00564                                 pow( 1024, 5 ),
00565                                 "1 PB",
00566                                 "1 petabyte"
00567                         ),
00568                         array(
00569                                 pow( 1024, 6 ),
00570                                 "1 EB",
00571                                 "1,024 exabyte"
00572                         ),
00573                         array(
00574                                 pow( 1024, 7 ),
00575                                 "1 ZB",
00576                                 "1 zetabyte"
00577                         ),
00578                         array(
00579                                 pow( 1024, 8 ),
00580                                 "1 YB",
00581                                 "1 yottabyte"
00582                         ),
00583                         // How big!? THIS BIG!
00584                 );
00585         }
00586 
00590         function testFormatBitrate( $bps, $expected, $msg ) {
00591                 $this->assertEquals(
00592                         $expected,
00593                         $this->lang->formatBitrate( $bps ),
00594                         "formatBitrate('$bps'): $msg"
00595                 );
00596         }
00597 
00598         function provideFormatBitrate() {
00599                 return array(
00600                         array(
00601                                 0,
00602                                 "0 bps",
00603                                 "0 bits per second"
00604                         ),
00605                         array(
00606                                 999,
00607                                 "999 bps",
00608                                 "999 bits per second"
00609                         ),
00610                         array(
00611                                 1000,
00612                                 "1 kbps",
00613                                 "1 kilobit per second"
00614                         ),
00615                         array(
00616                                 1000 * 1000,
00617                                 "1 Mbps",
00618                                 "1 megabit per second"
00619                         ),
00620                         array(
00621                                 pow( 10, 9 ),
00622                                 "1 Gbps",
00623                                 "1 gigabit per second"
00624                         ),
00625                         array(
00626                                 pow( 10, 12 ),
00627                                 "1 Tbps",
00628                                 "1 terabit per second"
00629                         ),
00630                         array(
00631                                 pow( 10, 15 ),
00632                                 "1 Pbps",
00633                                 "1 petabit per second"
00634                         ),
00635                         array(
00636                                 pow( 10, 18 ),
00637                                 "1 Ebps",
00638                                 "1 exabit per second"
00639                         ),
00640                         array(
00641                                 pow( 10, 21 ),
00642                                 "1 Zbps",
00643                                 "1 zetabit per second"
00644                         ),
00645                         array(
00646                                 pow( 10, 24 ),
00647                                 "1 Ybps",
00648                                 "1 yottabit per second"
00649                         ),
00650                         array(
00651                                 pow( 10, 27 ),
00652                                 "1,000 Ybps",
00653                                 "1,000 yottabits per second"
00654                         ),
00655                 );
00656         }
00657 }