You are here

protected function NormalizerTestBase::formatExpectedTimestampItemValues in Replication 8

Formats a UNIX timestamp.

This is copied from \Drupal\Tests\rest\Functional\BcTimestampNormalizerUnixTestTrait in Drupal 8.4.x.

Depending on the 'bc_timestamp_normalizer_unix' setting. The return will be an RFC3339 date string or the same timestamp that was passed in.

Parameters

int $timestamp: The timestamp value to format.

Return value

array The formatted RFC3339 date string or UNIX timestamp.

See also

\Drupal\serialization\Normalizer\TimestampItemNormalizer

4 calls to NormalizerTestBase::formatExpectedTimestampItemValues()
ContentEntityNormalizerTest::testNormalizer in tests/src/Kernel/Normalizer/ContentEntityNormalizerTest.php
EntityReferenceItemNormalizerTest::testUserReferenceFieldNormalization in tests/src/Kernel/Normalizer/EntityReferenceItemNormalizerTest.php
Tests normalization of entity reference fields that reference users.
FileEntityNormalizerTest::testNormalizer in tests/src/Kernel/Normalizer/FileEntityNormalizerTest.php
Tests using entity fields of the file field type.
LinkItemNormalizerTest::testLinkFieldNormalization in tests/src/Kernel/Normalizer/LinkItemNormalizerTest.php
Tests field link normalization.

File

tests/src/Kernel/Normalizer/NormalizerTestBase.php, line 98

Class

NormalizerTestBase

Namespace

Drupal\Tests\replication\Kernel\Normalizer

Code

protected function formatExpectedTimestampItemValues($timestamp) {

  // Get the minor version only from the \Drupal::VERSION string.
  $minor_version = substr(\Drupal::VERSION, 0, 3);

  // If the setting is enabled, just return the timestamp as-is now.
  if (version_compare($minor_version, '8.4', '<') || $this
    ->config('serialization.settings')
    ->get('bc_timestamp_normalizer_unix')) {
    return [
      'value' => $timestamp,
    ];
  }

  // Otherwise, format the date string to the same that
  // \Drupal\serialization\Normalizer\TimestampItemNormalizer will produce.
  $date = new \DateTime();
  $date
    ->setTimestamp($timestamp);
  $date
    ->setTimezone(new \DateTimeZone('UTC'));

  // Format is also added to the expected return values.
  return [
    'value' => $date
      ->format(\DateTime::RFC3339),
    'format' => \DateTime::RFC3339,
  ];
}