diff --git a/core/lib/Drupal/Core/TypedData/Type/DurationInterface.php b/core/lib/Drupal/Core/TypedData/Type/DurationInterface.php index a39c0d5..56d3b51 100644 --- a/core/lib/Drupal/Core/TypedData/Type/DurationInterface.php +++ b/core/lib/Drupal/Core/TypedData/Type/DurationInterface.php @@ -19,6 +19,8 @@ * * @return \DateInterval|null * A DateInterval object or NULL if there is no duration. + * + * @throws \Exception */ public function getDuration(); diff --git a/core/lib/Drupal/Core/TypedData/Type/TimeSpan.php b/core/lib/Drupal/Core/TypedData/Type/TimeSpan.php index c70a064..be540e1 100644 --- a/core/lib/Drupal/Core/TypedData/Type/TimeSpan.php +++ b/core/lib/Drupal/Core/TypedData/Type/TimeSpan.php @@ -22,7 +22,7 @@ class TimeSpan extends Integer implements DurationInterface { */ public function getDuration() { if ($this->value) { - return new DateInterval('PT' . $this->value . 'S'); + return new \DateInterval('PT' . $this->value . 'S'); } } diff --git a/core/lib/Drupal/Core/TypedData/Type/Timestamp.php b/core/lib/Drupal/Core/TypedData/Type/Timestamp.php index 86ac13c..74ae3b1 100644 --- a/core/lib/Drupal/Core/TypedData/Type/Timestamp.php +++ b/core/lib/Drupal/Core/TypedData/Type/Timestamp.php @@ -35,6 +35,6 @@ public function getDateTime() { * {@inheritdoc} */ public function setDateTime(DrupalDateTime $dateTime) { - $this->value = $dateTime->format('c'); + $this->value = $dateTime->getTimestamp(); } } diff --git a/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/PrimitiveTypeConstraintValidator.php b/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/PrimitiveTypeConstraintValidator.php index 0547991..3a522b6 100644 --- a/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/PrimitiveTypeConstraintValidator.php +++ b/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/PrimitiveTypeConstraintValidator.php @@ -54,10 +54,15 @@ public function validate($value, Constraint $constraint) { $valid = FALSE; } // @todo: Move those to separate constraint validators. - if ($typed_data instanceof DateTimeInterface && $typed_data->getDateTime()->hasErrors()) { - $valid = FALSE; - } - if ($typed_data instanceof DurationInterface && !($typed_data->getDuration() instanceof DateInterval)) { + try { + if ($typed_data instanceof DateTimeInterface && $typed_data->getDateTime()->hasErrors()) { + $valid = FALSE; + } + if ($typed_data instanceof DurationInterface && !($typed_data->getDuration() instanceof DateInterval)) { + $valid = FALSE; + } + } catch (\Exception $e) { + // Invalid durations or dates might throw exceptions. $valid = FALSE; } diff --git a/core/modules/datetime/lib/Drupal/datetime/Type/DateTimeItem.php b/core/modules/datetime/lib/Drupal/datetime/Type/DateTimeItem.php index 2fe97e4..78c1824 100644 --- a/core/modules/datetime/lib/Drupal/datetime/Type/DateTimeItem.php +++ b/core/modules/datetime/lib/Drupal/datetime/Type/DateTimeItem.php @@ -30,7 +30,7 @@ public function getPropertyDefinitions() { if (!isset(self::$propertyDefinitions)) { self::$propertyDefinitions['value'] = array( - 'type' => 'datetime', + 'type' => 'datetime_iso8601', 'label' => t('Date value'), ); } diff --git a/core/modules/system/lib/Drupal/system/Tests/TypedData/TypedDataTest.php b/core/modules/system/lib/Drupal/system/Tests/TypedData/TypedDataTest.php index ad81618..630d027 100644 --- a/core/modules/system/lib/Drupal/system/Tests/TypedData/TypedDataTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/TypedData/TypedDataTest.php @@ -182,10 +182,10 @@ public function testGetAndSet() { // DurationIso8601 type. $value = 'PT20S'; $typed_data = $this->createTypedData(array('type' => 'duration_iso8601'), $value); - $this->assertTrue($typed_data->getValue() === $value, 'DurationIso8601 value was fetched.'); + $this->assertIdentical($typed_data->getValue(), $value, 'DurationIso8601 value was fetched.'); $this->assertEqual($typed_data->validate()->count(), 0); $typed_data->setValue('P40D'); - $this->assertTrue($typed_data->getValue()->d == 40, 'DurationIso8601 value was changed and set by duration string.'); + $this->assertEqual($typed_data->getDuration()->d, 40, 'DurationIso8601 value was changed and set by duration string.'); $this->assertTrue(is_string($typed_data->getString()), 'DurationIso8601 value was converted to string'); $this->assertEqual($typed_data->validate()->count(), 0); $typed_data->setValue(NULL); @@ -197,17 +197,20 @@ public function testGetAndSet() { $typed_data = $this->createTypedData(array('type' => 'duration_iso8601'), 'PT20S'); $this->assertTrue($typed_data->getDuration() instanceof DateInterval); $typed_data->setDuration(new DateInterval('P40D')); - $this->assertEqual($typed_data->getValue(), 'P40D'); + // @todo: Should we make this "nicer"? + $this->assertEqual($typed_data->getValue(), 'P0Y0M40DT0H0M0S'); $typed_data->setValue(NULL); $this->assertNull($typed_data->getDuration()); // Time span type. - $value = new DateInterval(20); + $value = 20; $typed_data = $this->createTypedData(array('type' => 'timespan'), $value); - $this->assertTrue($typed_data->getValue() === $value, 'Time span value was fetched.'); + $this->assertIdentical($typed_data->getValue(), $value, 'Time span value was fetched.'); $this->assertEqual($typed_data->validate()->count(), 0); $typed_data->setValue(60 * 60 * 4); - $this->assertTrue($typed_data->getDuration()->h == 4, 'Time span was changed'); + // @todo: DateInterval does not recalculate the components, should the time + // span class take care of this? + $this->assertEqual($typed_data->getDuration()->s, 14400, 'Time span was changed'); $this->assertTrue(is_string($typed_data->getString()), 'Time span value was converted to string'); $this->assertEqual($typed_data->validate()->count(), 0); $typed_data->setValue(NULL); @@ -218,7 +221,7 @@ public function testGetAndSet() { // Check implementation of DurationInterface. $typed_data = $this->createTypedData(array('type' => 'timespan'), 20); $this->assertTrue($typed_data->getDuration() instanceof DateInterval); - $typed_data->setDuration(new DateInterval('P40H')); + $typed_data->setDuration(new DateInterval('PT4H')); $this->assertEqual($typed_data->getValue(), 60 * 60 * 4); $typed_data->setValue(NULL); $this->assertNull($typed_data->getDuration());