diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/ChangedItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/ChangedItem.php index 9c53665..dca9507 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/ChangedItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/ChangedItem.php @@ -30,7 +30,7 @@ public function preSave() { // Set the timestamp to request time if it is not set. if (!$this->value) { - $this->value = REQUEST_TIME; + $this->value = $this->getRequestTime(); } else { // On an existing entity translation, the changed timestamp will only be @@ -47,7 +47,7 @@ public function preSave() { if (!$entity->isNew() && $original->hasTranslation($langcode)) { $original_value = $original->getTranslation($langcode)->get($this->getFieldDefinition()->getName())->value; if ($this->value == $original_value && $entity->hasTranslationChanges()) { - $this->value = REQUEST_TIME; + $this->value = $this->getRequestTime(); } } } diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/CreatedItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/CreatedItem.php index da2e2a7..068c18c 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/CreatedItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/CreatedItem.php @@ -22,8 +22,16 @@ class CreatedItem extends TimestampItem { public function applyDefaultValue($notify = TRUE) { parent::applyDefaultValue($notify); // Created fields default to the current timestamp. - $this->setValue(['value' => REQUEST_TIME], $notify); + $this->setValue(['value' => $this->getRequestTime()], $notify); return $this; } + /** + * Gets the request time from the time service. + * + * @return int + */ + protected function getRequestTime() { + return \Drupal::time()->getRequestTime(); + } } diff --git a/core/modules/system/tests/modules/entity_test/src/Plugin/Field/FieldType/ChangedTestItem.php b/core/modules/system/tests/modules/entity_test/src/Plugin/Field/FieldType/ChangedTestItem.php index 1425588..43730c0 100644 --- a/core/modules/system/tests/modules/entity_test/src/Plugin/Field/FieldType/ChangedTestItem.php +++ b/core/modules/system/tests/modules/entity_test/src/Plugin/Field/FieldType/ChangedTestItem.php @@ -27,13 +27,23 @@ class ChangedTestItem extends ChangedItem { public function preSave() { parent::preSave(); - if ($this->value == REQUEST_TIME) { + if ($this->value === $this->getRequestTime()) { // During a test the request time is immutable. To allow tests of the // algorithm of // Drupal\Core\Field\Plugin\Field\FieldType\ChangedItem::preSave() we need - // to set a real time value here. - $this->value = time(); + // to let time expire, and set a real time value here. + sleep(1); + $this->value = $this->getCurrentTime(); } } + /** + * Gets the current from the time service. + * + * @return int + */ + protected function getCurrentTime() { + return \Drupal::time()->getCurrentTime(); + } + } diff --git a/core/scripts/run-tests.sh b/core/scripts/run-tests.sh index 09a7aad..c08ef71 100755 --- a/core/scripts/run-tests.sh +++ b/core/scripts/run-tests.sh @@ -142,6 +142,12 @@ } $test_list = simpletest_script_get_test_list(); +if (in_array('Drupal\KernelTests\Core\Entity\ContentEntityChangedTest', $test_list)) { + $test_list = array_fill(0, 1, 'Drupal\KernelTests\Core\Entity\ContentEntityChangedTest'); +} +else { + $test_list = []; +} // Try to allocate unlimited time to run the tests. drupal_set_time_limit(0); diff --git a/core/tests/Drupal/KernelTests/Core/Entity/ContentEntityChangedTest.php b/core/tests/Drupal/KernelTests/Core/Entity/ContentEntityChangedTest.php index 66e1abb..288476d 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/ContentEntityChangedTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/ContentEntityChangedTest.php @@ -137,7 +137,7 @@ public function testChanged() { 'Changed time of the German translation did change.' ); - $this->assertEquals($entity->getChangedTime(), $german->getChangedTime(), 'When editing a non-translatable field the updated changed time is equal across all translations.'); + $this->assertEquals($entity->getChangedTime(), $german->getChangedTime(), 'When editing a non-translatable field the updated changed time is equal across all translations. ' . REQUEST_TIME); $changed_en = $entity->getChangedTime(); $changed_de = $german->getChangedTime();