From b06f82293bdb3d20a7768544386745e2cfb040f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20W=C3=B3js?= Date: Sun, 17 Dec 2017 13:05:00 +0100 Subject: [PATCH] Fix EZP-28513: Current date populated as the default value for Date field type should be the absolute value --- eZ/Publish/Core/FieldType/Date/Type.php | 9 ++++++++- .../Legacy/Content/FieldValue/Converter/DateConverter.php | 6 ++---- .../Legacy/Tests/Content/FieldValue/Converter/DateTest.php | 6 +++--- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/eZ/Publish/Core/FieldType/Date/Type.php b/eZ/Publish/Core/FieldType/Date/Type.php index 5798cb59eb..26f4a3d6c3 100644 --- a/eZ/Publish/Core/FieldType/Date/Type.php +++ b/eZ/Publish/Core/FieldType/Date/Type.php @@ -136,7 +136,10 @@ protected function getSortInfo(BaseValue $value) /** * Converts an $hash to the Value defined by the field type. * - * @param mixed $hash Null or associative array containing timestamp and optionally date in RFC850 format. + * @param mixed $hash Null or associative array containing one of the following (first value found in the order below is picked): + * 'rfc850': Date in RFC 850 format (DateTime::RFC850) + * 'timestring': Date in parseable string format supported by DateTime (e.g. 'now', '+3 days') + * 'timestamp': Unix timestamp * * @return \eZ\Publish\Core\FieldType\Date\Value $value */ @@ -150,6 +153,10 @@ public function fromHash($hash) return Value::fromString($hash['rfc850']); } + if (isset($hash['timestring']) && is_string($hash['timestring'])) { + return Value::fromString($hash['timestring']); + } + return Value::fromTimestamp((int)$hash['timestamp']); } diff --git a/eZ/Publish/Core/Persistence/Legacy/Content/FieldValue/Converter/DateConverter.php b/eZ/Publish/Core/Persistence/Legacy/Content/FieldValue/Converter/DateConverter.php index c916b2a0b8..b0fff263a3 100644 --- a/eZ/Publish/Core/Persistence/Legacy/Content/FieldValue/Converter/DateConverter.php +++ b/eZ/Publish/Core/Persistence/Legacy/Content/FieldValue/Converter/DateConverter.php @@ -15,7 +15,6 @@ use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition; use eZ\Publish\Core\FieldType\Date\Type as DateType; use eZ\Publish\Core\FieldType\FieldSettings; -use DateTime; /** * Date field value converter class. @@ -93,11 +92,10 @@ public function toFieldDefinition(StorageFieldDefinition $storageDef, FieldDefin // Building default value switch ($fieldDef->fieldTypeConstraints->fieldSettings['defaultType']) { case DateType::DEFAULT_CURRENT_DATE: - $dateTime = new DateTime(); - $dateTime->setTime(0, 0, 0); $data = array( - 'timestamp' => $dateTime->getTimestamp(), + 'timestamp' => time(), // @deprecated timestamp is no longer used and will be removed in a future version. 'rfc850' => null, + 'timestring' => 'now', ); break; default: diff --git a/eZ/Publish/Core/Persistence/Legacy/Tests/Content/FieldValue/Converter/DateTest.php b/eZ/Publish/Core/Persistence/Legacy/Tests/Content/FieldValue/Converter/DateTest.php index fe75f4ec67..d931fe3f3b 100644 --- a/eZ/Publish/Core/Persistence/Legacy/Tests/Content/FieldValue/Converter/DateTest.php +++ b/eZ/Publish/Core/Persistence/Legacy/Tests/Content/FieldValue/Converter/DateTest.php @@ -157,8 +157,7 @@ public function testToFieldDefinitionDefaultEmpty() */ public function testToFieldDefinitionDefaultCurrentDate() { - $dateTime = new DateTime(); - $timestamp = $dateTime->setTime(0, 0, 0)->getTimestamp(); + $timestamp = time(); $fieldDef = new PersistenceFieldDefinition(); $storageDef = new StorageFieldDefinition( array( @@ -168,8 +167,9 @@ public function testToFieldDefinitionDefaultCurrentDate() $this->converter->toFieldDefinition($storageDef, $fieldDef); self::assertInternalType('array', $fieldDef->defaultValue->data); - self::assertCount(2, $fieldDef->defaultValue->data); + self::assertCount(3, $fieldDef->defaultValue->data); self::assertNull($fieldDef->defaultValue->data['rfc850']); self::assertSame($timestamp, $fieldDef->defaultValue->data['timestamp']); + self::assertSame('now', $fieldDef->defaultValue->data['timestring']); } }