From 9ab2e6e7a10755b4b25dbcb3b841e2a3e3382713 Mon Sep 17 00:00:00 2001 From: Andrew Longosz Date: Thu, 23 Nov 2017 11:28:10 +0100 Subject: [PATCH] [7.0] EZP-28269: Fixed inconsistent creating of eztime fromTimestamp (#2159) * EZP-28269: Aligned Time::fromTimestamp with DateAndTime::fromTimestamp * EZP-28269: Fixed Unit Tests * EZP-28269: Fixed eztime integration tests * [Doc] EZP-28269: Updated 7.0 BC doc --- doc/bc/changes-7.0.md | 3 ++ .../Tests/FieldType/TimeIntegrationTest.php | 13 +++--- eZ/Publish/Core/FieldType/Tests/TimeTest.php | 46 +++++++++---------- eZ/Publish/Core/FieldType/Time/Value.php | 3 +- 4 files changed, 34 insertions(+), 31 deletions(-) diff --git a/doc/bc/changes-7.0.md b/doc/bc/changes-7.0.md index 387caef2d58..905e1fc4741 100644 --- a/doc/bc/changes-7.0.md +++ b/doc/bc/changes-7.0.md @@ -24,6 +24,9 @@ Changes affecting version compatibility with former or future versions. * "cache_pool_name" siteaccess setting has been removed & replaced by "cache_service_name" as the semantic is different. The new setting should contain the full service name of a symfony cache service, by default app_cache.app is used. +* The method `eZ\Publish\Core\FieldType\Time\Value::fromTimestamp` returns `Time\Value` without + taking into account the timezone. The reason for this change is consistency with the behavior of + `eZ\Publish\Core\FieldType\DateAndTime\Value::fromTimestamp`. ## Deprecations diff --git a/eZ/Publish/API/Repository/Tests/FieldType/TimeIntegrationTest.php b/eZ/Publish/API/Repository/Tests/FieldType/TimeIntegrationTest.php index c09104bc60c..32f4ccf5838 100644 --- a/eZ/Publish/API/Repository/Tests/FieldType/TimeIntegrationTest.php +++ b/eZ/Publish/API/Repository/Tests/FieldType/TimeIntegrationTest.php @@ -185,7 +185,7 @@ public function provideInvalidCreationFieldData() /** * Get update field externals data. * - * @return array + * @return TimeValue */ public function getValidUpdateFieldData() { @@ -206,9 +206,9 @@ public function assertUpdatedFieldDataLoadedCorrect(Field $field) $field->value ); - $dateTime = new DateTime(); + $dateTime = new DateTime('@12345678'); $expectedData = array( - 'time' => $dateTime->setTimestamp(12345678)->getTimestamp() - $dateTime->setTime(0, 0, 0)->getTimestamp(), + 'time' => $dateTime->getTimestamp() - $dateTime->setTime(0, 0, 0)->getTimestamp(), ); $this->assertPropertiesCorrect( $expectedData, @@ -305,12 +305,13 @@ public function assertCopiedFieldDataLoadedCorrectly(Field $field) */ public function provideToHashData() { - $dateTime = new DateTime(); + $timestamp = 123456; + $dateTime = new DateTime("@{$timestamp}"); return array( array( - TimeValue::fromTimestamp(123456), - $dateTime->setTimestamp(123456)->getTimestamp() - $dateTime->setTime(0, 0, 0)->getTimestamp(), + TimeValue::fromTimestamp($timestamp), + $dateTime->getTimestamp() - $dateTime->setTime(0, 0, 0)->getTimestamp(), ), ); } diff --git a/eZ/Publish/Core/FieldType/Tests/TimeTest.php b/eZ/Publish/Core/FieldType/Tests/TimeTest.php index 5d79691af80..0fe723626ec 100644 --- a/eZ/Publish/Core/FieldType/Tests/TimeTest.php +++ b/eZ/Publish/Core/FieldType/Tests/TimeTest.php @@ -140,44 +140,44 @@ public function provideInvalidInputForAcceptValue() public function provideValidInputForAcceptValue() { $dateTime = new DateTime(); - $secondsInDay = 24 * 60 * 60; + // change timezone to UTC (+00:00) to be able to calculate proper TimeValue + $timestamp = $dateTime->setTimezone(new \DateTimeZone('UTC'))->getTimestamp(); - return array( - array( + return [ + [ null, new TimeValue(), - ), - array( + ], + [ '2012-08-28 12:20', new TimeValue(44400), - ), - array( + ], + [ '2012-08-28 12:20 Europe/Berlin', new TimeValue(44400), - ), - array( + ], + [ '2012-08-28 12:20 Asia/Sakhalin', new TimeValue(44400), - ), - array( - // Set $dateTime to proper time for correct offset - $dateTime->setTimestamp(1372896001)->getTimestamp(), - // Correct for negative offset - new TimeValue(($secondsInDay + $dateTime->getOffset() + 1) % $secondsInDay), - ), - array( - TimeValue::fromTimestamp($timestamp = 1346149200), + ], + [ + // create new DateTime object from timestamp w/o taking into account server timezone + (new DateTime('@1372896001'))->getTimestamp(), + new TimeValue(1), + ], + [ + TimeValue::fromTimestamp($timestamp), new TimeValue( - $dateTime->setTimestamp($timestamp)->getTimestamp() - $dateTime->setTime(0, 0, 0)->getTimestamp() + $timestamp - $dateTime->setTime(0, 0, 0)->getTimestamp() ), - ), - array( + ], + [ clone $dateTime, new TimeValue( $dateTime->getTimestamp() - $dateTime->setTime(0, 0, 0)->getTimestamp() ), - ), - ); + ], + ]; } /** diff --git a/eZ/Publish/Core/FieldType/Time/Value.php b/eZ/Publish/Core/FieldType/Time/Value.php index 80f7c593cb7..71e91969c71 100644 --- a/eZ/Publish/Core/FieldType/Time/Value.php +++ b/eZ/Publish/Core/FieldType/Time/Value.php @@ -86,8 +86,7 @@ public static function fromString($timeString) public static function fromTimestamp($timestamp) { try { - $dateTime = new DateTime(); - $dateTime->setTimestamp($timestamp); + $dateTime = new DateTime("@{$timestamp}"); return static::fromDateTime($dateTime); } catch (Exception $e) {