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)
1  2 
languages/Language.php
tests/phpunit/languages/LanguageTest.php

diff --combined languages/Language.php
@@@ -1078,6 -1078,7 +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 ) {
                $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];
                }
                $start = substr( $str, 0, strlen( $str ) - 2 );
                $end = substr( $str, strlen( $str ) - 2 );
 -              switch( $end ) {
 +              switch ( $end ) {
                        case 'כ':
                                $str = $start . 'ך';
                                break;
@@@ -1,7 -1,6 +1,6 @@@
  <?php
  
  class LanguageTest extends LanguageClassesTestCase {
        function testLanguageConvertDoubleWidthToSingleWidth() {
                $this->assertEquals(
                        "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
                                'formatTimePeriod() rounding, recursion, (>48h)'
                        ),
                );
 -
        }
  
        function testTruncate() {
                );
        }
  
+       /**
+        * 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
         */