Add input checks for Language::sprintfDate()
authorSiebrand Mazeland <s.mazeland@xs4all.nl>
Thu, 25 Apr 2013 07:58:39 +0000 (09:58 +0200)
committerSiebrand Mazeland <s.mazeland@xs4all.nl>
Fri, 26 Apr 2013 08:05:18 +0000 (10:05 +0200)
Check if the timestamp has a length of 14 characters and if it is numeric.
Throw an exception otherwise. Includes tests.

Bug: 47629
Change-Id: I9a4fd0af88cf20c2a6bd72fd7048743466c1600f

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

index 9651f3d..7ec37a1 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 d5dbfb2..26bb2f3 100644 (file)
@@ -1,7 +1,6 @@
 <?php
 
 class LanguageTest extends LanguageClassesTestCase {
-
        function testLanguageConvertDoubleWidthToSingleWidth() {
                $this->assertEquals(
                        "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
@@ -499,6 +498,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
         */