Fix language code validation (from r82927)
authorAntoine Musso <hashar@users.mediawiki.org>
Thu, 3 Mar 2011 19:13:57 +0000 (19:13 +0000)
committerAntoine Musso <hashar@users.mediawiki.org>
Thu, 3 Mar 2011 19:13:57 +0000 (19:13 +0000)
A language code may contains the underscore character (be_tarask)
and might as well be upper case (FR).

Add tests for Language::isValidBuiltInCode() against some language codes

languages/Language.php
tests/phpunit/languages/LanguageTest.php

index 3fa1032..8b34944 100644 (file)
@@ -215,7 +215,7 @@ class Language {
         * internal customisation of MediaWiki, via Messages*.php.
         */
        public static function isValidBuiltInCode( $code ) {
-               return preg_match( '/^[a-z0-9-]*$/', $code );
+               return preg_match( '/^[a-z0-9-_]*$/i', $code );
        }
 
        /**
index efc2f0b..130f68d 100644 (file)
@@ -58,4 +58,27 @@ class LanguageTest extends MediaWikiTestCase {
                        'formatTimePeriod() rounding (>=1h)'
                );
        }
+
+       /**
+        * Test Language::isValidBuiltInCode()
+        * @dataProvider provideLanguageCodes
+        */
+       function testBuiltInCodeValidation( $code, $message = '' ) {
+               $this->assertTrue(
+                       (bool) Language::isValidBuiltInCode( $code ),
+                       "validating code $code $message"
+               );
+       }
+
+       function provideLanguageCodes() {
+               return array(
+                       array( 'fr'       , 'Two letters, minor case' ),
+                       array( 'EN'       , 'Two letters, upper case' ),
+                       array( 'tyv'      , 'Three letters' ),
+                       array( 'tokipona'   , 'long language code' ),
+                       array( 'be_tarask', 'With underscore' ),
+                       array( 'Zh_classical', 'Begin with upper case, underscore' ),
+                       array( 'Be_x_old', 'With extension (two underscores)' ),
+               );
+       }
 }