Bläddra i källkod

ZF-11588: handle string dates and dates beyond unix epoch

- Utilize DateTime instead of strtotime in tests as well as
  Zend_XmlRpc_Value_DateTime

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@24291 44c647ce-9c0f-0410-b52a-842ac1e357ba
matthew 14 år sedan
förälder
incheckning
8c234a7880
2 ändrade filer med 25 tillägg och 12 borttagningar
  1. 3 3
      library/Zend/XmlRpc/Value/DateTime.php
  2. 22 9
      tests/Zend/XmlRpc/ValueTest.php

+ 3 - 3
library/Zend/XmlRpc/Value/DateTime.php

@@ -69,13 +69,13 @@ class Zend_XmlRpc_Value_DateTime extends Zend_XmlRpc_Value_Scalar
         } elseif (is_numeric($value)) { // The value is numeric, we make sure it is an integer
             $this->_value = date($this->_phpFormatString, (int)$value);
         } else {
-            $timestamp = strtotime($value);
-            if ($timestamp === false || $timestamp == -1) { // cannot convert the value to a timestamp
+            $timestamp = new DateTime($value);
+            if ($timestamp === false) { // cannot convert the value to a timestamp
                 require_once 'Zend/XmlRpc/Value/Exception.php';
                 throw new Zend_XmlRpc_Value_Exception('Cannot convert given value \''. $value .'\' to a timestamp');
             }
 
-            $this->_value = date($this->_phpFormatString, $timestamp); // Convert the timestamp to iso8601 format
+            $this->_value = $timestamp->format($this->_phpFormatString); // Convert the timestamp to iso8601 format
         }
     }
 

+ 22 - 9
tests/Zend/XmlRpc/ValueTest.php

@@ -47,6 +47,8 @@ require_once 'Zend/Date.php';
  */
 class Zend_XmlRpc_ValueTest extends PHPUnit_Framework_TestCase
 {
+    public $xmlRpcDateFormat = 'Ymd\\TH:i:s';
+
     // Boolean
     public function testFactoryAutodetectsBoolean()
     {
@@ -575,8 +577,8 @@ class Zend_XmlRpc_ValueTest extends PHPUnit_Framework_TestCase
 
         $this->assertXmlRpcType('dateTime', $val);
 
-        $expected = '1997-07-16T19:20+01:00';
-        $this->assertSame(strtotime($native), strtotime($val->getValue()));
+        $expected = new Datetime($native);
+        $this->assertSame($expected->format($this->xmlRpcDateFormat), $val->getValue());
     }
 
     public function testMarshalDateTimeFromNativeStringProducesIsoOutput()
@@ -587,17 +589,14 @@ class Zend_XmlRpc_ValueTest extends PHPUnit_Framework_TestCase
 
         $this->assertXmlRpcType('dateTime', $val);
 
-        $expected = date('c', strtotime($native));
-        $expected = substr($expected, 0, strlen($expected) - 6);
-        $expected = str_replace('-', '', $expected);
+        $expected = new DateTime($native);
         $received = $val->getValue();
-        $this->assertEquals($expected, $received);
+        $this->assertEquals($expected->format($this->xmlRpcDateFormat), $received);
     }
 
     public function testMarshalDateTimeFromInvalidString()
     {
-        $this->setExpectedException('Zend_XmlRpc_Value_Exception',
-            "Cannot convert given value 'foobarbaz' to a timestamp");
+        $this->setExpectedException('Exception', "foobarbaz");
         Zend_XmlRpc_Value::getXmlRpcValue('foobarbaz', Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME);
     }
 
@@ -612,6 +611,17 @@ class Zend_XmlRpc_ValueTest extends PHPUnit_Framework_TestCase
     }
 
     /**
+     * @group ZF-11588
+     */
+    public function testMarshalDateTimeBeyondUnixEpochFromNativeStringPassedToConstructor()
+    {
+        $native = '2040-01-01T00:00:00';
+        $value  = new Zend_XmlRpc_Value_DateTime($native);
+        $expected = new DateTime($native);
+        $this->assertSame($expected->format($this->xmlRpcDateFormat), $value->getValue());
+    }
+
+    /**
      * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
      */
     public function testMarshalDateTimeFromXmlRpc(Zend_XmlRpc_Generator_GeneratorAbstract $generator)
@@ -625,7 +635,10 @@ class Zend_XmlRpc_ValueTest extends PHPUnit_Framework_TestCase
 
         $this->assertXmlRpcType('dateTime', $val);
         $this->assertEquals('dateTime.iso8601', $val->getType());
-        $this->assertSame(strtotime($iso8601), strtotime($val->getValue()));
+
+        $expected = new DateTime($iso8601);
+        $this->assertSame($expected->format($this->xmlRpcDateFormat), $val->getValue());
+
         $this->assertEquals($this->wrapXml($xml), $val->saveXml());
     }