diff --git a/core/modules/datetime/src/Tests/DateTimeFieldTest.php b/core/modules/datetime/src/Tests/DateTimeFieldTest.php index 0cb226b..bcf40b6 100644 --- a/core/modules/datetime/src/Tests/DateTimeFieldTest.php +++ b/core/modules/datetime/src/Tests/DateTimeFieldTest.php @@ -195,22 +195,25 @@ function testDateField() { // has the same interval. Since the database always stores UTC, and the // interval will use this, force the test date to use UTC and not the local // or user timezome. + $timestamp = REQUEST_TIME - 87654321; $entity = entity_load('entity_test', $id); $field_name = $this->fieldStorage->getName(); - $date = DrupalDateTime::createFromTimestamp(REQUEST_TIME - 87654321, 'UTC'); + $date = DrupalDateTime::createFromTimestamp($timestamp, 'UTC'); $entity->{$field_name}->value = $date->format($date_format); $entity->save(); $this->displayOptions['type'] = 'datetime_time_ago'; $this->displayOptions['settings'] = array( - 'future_format' => '@time in the future', - 'past_format' => '@time in the past', + 'future_format' => '@interval in the future', + 'past_format' => '@interval in the past', 'granularity' => 3, ); entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full') ->setComponent($field_name, $this->displayOptions) ->save(); - $expected = '2 years 9 months 1 week in the past'; + $expected = SafeMarkup::format($this->displayOptions['settings']['past_format'], [ + '@interval' => \Drupal::service('date.formatter')->formatTimeDiffSince($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]) + ]); $this->renderTestEntity($id); $this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', array('%expected' => $expected))); @@ -219,16 +222,19 @@ function testDateField() { // has the same interval. Since the database always stores UTC, and the // interval will use this, force the test date to use UTC and not the local // or user timezome. + $timestamp = REQUEST_TIME + 87654321; $entity = entity_load('entity_test', $id); $field_name = $this->fieldStorage->getName(); - $date = DrupalDateTime::createFromTimestamp(REQUEST_TIME + 87654321, 'UTC'); + $date = DrupalDateTime::createFromTimestamp($timestamp, 'UTC'); $entity->{$field_name}->value = $date->format($date_format); $entity->save(); entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full') ->setComponent($field_name, $this->displayOptions) ->save(); - $expected = '2 years 9 months 1 week in the future'; + $expected = SafeMarkup::format($this->displayOptions['settings']['future_format'], [ + '@interval' => \Drupal::service('date.formatter')->formatTimeDiffUntil($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]) + ]); $this->renderTestEntity($id); $this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', array('%expected' => $expected))); } @@ -328,22 +334,25 @@ function testDatetimeField() { // has the same interval. Since the database always stores UTC, and the // interval will use this, force the test date to use UTC and not the local // or user timezome. + $timestamp = REQUEST_TIME - 87654321; $entity = entity_load('entity_test', $id); $field_name = $this->fieldStorage->getName(); - $date = DrupalDateTime::createFromTimestamp(REQUEST_TIME - 87654321, 'UTC'); + $date = DrupalDateTime::createFromTimestamp($timestamp, 'UTC'); $entity->{$field_name}->value = $date->format(DATETIME_DATETIME_STORAGE_FORMAT); $entity->save(); $this->displayOptions['type'] = 'datetime_time_ago'; $this->displayOptions['settings'] = array( - 'future_format' => '@time from now', - 'past_format' => '@time earlier', + 'future_format' => '@interval from now', + 'past_format' => '@interval earlier', 'granularity' => 3, ); entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full') ->setComponent($field_name, $this->displayOptions) ->save(); - $expected = '2 years 9 months 1 week earlier'; + $expected = SafeMarkup::format($this->displayOptions['settings']['past_format'], [ + '@interval' => \Drupal::service('date.formatter')->formatTimeDiffSince($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]) + ]); $this->renderTestEntity($id); $this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', array('%expected' => $expected))); @@ -352,16 +361,19 @@ function testDatetimeField() { // has the same interval. Since the database always stores UTC, and the // interval will use this, force the test date to use UTC and not the local // or user timezome. + $timestamp = REQUEST_TIME + 87654321; $entity = entity_load('entity_test', $id); $field_name = $this->fieldStorage->getName(); - $date = DrupalDateTime::createFromTimestamp(REQUEST_TIME + 87654321, 'UTC'); + $date = DrupalDateTime::createFromTimestamp($timestamp, 'UTC'); $entity->{$field_name}->value = $date->format(DATETIME_DATETIME_STORAGE_FORMAT); $entity->save(); entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full') ->setComponent($field_name, $this->displayOptions) ->save(); - $expected = '2 years 9 months 1 week from now'; + $expected = SafeMarkup::format($this->displayOptions['settings']['future_format'], [ + '@interval' => \Drupal::service('date.formatter')->formatTimeDiffUntil($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]) + ]); $this->renderTestEntity($id); $this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', array('%expected' => $expected))); } diff --git a/core/modules/field/src/Tests/Timestamp/TimestampFormatterTest.php b/core/modules/field/src/Tests/Timestamp/TimestampFormatterTest.php index 6aa1588..8ef98d6 100644 --- a/core/modules/field/src/Tests/Timestamp/TimestampFormatterTest.php +++ b/core/modules/field/src/Tests/Timestamp/TimestampFormatterTest.php @@ -147,8 +147,8 @@ protected function testTimestampAgoFormatter() { foreach (array(1,2,3,4,5,6) as $granularity) { $data[] = [ - 'future_format' => '@time hence', - 'past_format' => '@time ago', + 'future_format' => '@interval hence', + 'past_format' => '@interval ago', 'granularity' => $granularity, ]; } @@ -161,7 +161,7 @@ protected function testTimestampAgoFormatter() { // Test a timestamp in the past $value = $request_time - 87654321; - $expected = SafeMarkup::format($past_format, ['@time' => \Drupal::service('date.formatter')->formatTimeDiffSince($value, ['granularity' => $granularity])]); + $expected = SafeMarkup::format($past_format, ['@interval' => \Drupal::service('date.formatter')->formatTimeDiffSince($value, ['granularity' => $granularity])]); $component = $this->display->getComponent($this->fieldName); $component['type'] = 'timestamp_ago'; @@ -176,7 +176,7 @@ protected function testTimestampAgoFormatter() { // Test a timestamp in the future $value = $request_time + 87654321; - $expected = SafeMarkup::format($future_format, ['@time' => \Drupal::service('date.formatter')->formatTimeDiffUntil($value, ['granularity' => $granularity])]); + $expected = SafeMarkup::format($future_format, ['@interval' => \Drupal::service('date.formatter')->formatTimeDiffUntil($value, ['granularity' => $granularity])]); $component = $this->display->getComponent($this->fieldName); $component['type'] = 'timestamp_ago';