Merge "Add input checks for Language::sprintfDate()"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Mon, 29 Apr 2013 08:50:08 +0000 (08:50 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Mon, 29 Apr 2013 08:50:08 +0000 (08:50 +0000)
languages/Language.php
tests/phpunit/languages/LanguageTest.php

index ff87f89..b9201d7 100644 (file)
@@ -1078,6 +1078,7 @@ class Language {
         * @param $zone DateTimeZone: Timezone of $ts
         * @todo handling of "o" format character for Iranian, Hebrew, Hijri & Thai?
         *
+        * @throws MWException
         * @return string
         */
        function sprintfDate( $format, $ts, DateTimeZone $zone = null ) {
@@ -1093,6 +1094,15 @@ class Language {
                $thai = false;
                $minguo = false;
                $tenno = false;
+
+               if ( strlen( $ts ) !== 14 ) {
+                       throw new MWException( __METHOD__ . ": The timestamp $ts should have 14 characters" );
+               }
+
+               if ( !ctype_digit( $ts ) ) {
+                       throw new MWException( __METHOD__ . ": The timestamp $ts should be a number" );
+               }
+
                for ( $p = 0; $p < strlen( $format ); $p++ ) {
                        $num = false;
                        $code = $format[$p];
index 80f76a5..54f7753 100644 (file)
@@ -1,7 +1,6 @@
 <?php
 
 class LanguageTest extends LanguageClassesTestCase {
-
        function testLanguageConvertDoubleWidthToSingleWidth() {
                $this->assertEquals(
                        "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
@@ -498,6 +497,30 @@ class LanguageTest extends LanguageClassesTestCase {
                );
        }
 
+       /**
+        * Test too short timestamp
+        * @expectedException MWException
+        */
+       function testSprintfDateTooShortTimestamp() {
+               $this->getLang()->sprintfDate( 'xiY', '1234567890123' );
+       }
+
+       /**
+        * Test too long timestamp
+        * @expectedException MWException
+        */
+       function testSprintfDateTooLongTimestamp() {
+               $this->getLang()->sprintfDate( 'xiY', '123456789012345' );
+       }
+
+       /**
+        * Test too short timestamp
+        * @expectedException MWException
+        */
+       function testSprintfDateNotAllDigitTimestamp() {
+               $this->getLang()->sprintfDate( 'xiY', '-1234567890123' );
+       }
+
        /**
         * @dataProvider provideSprintfDateSamples
         */