You are here

protected function BcTimestampNormalizerUnixTestTrait::formatExpectedTimestampItemValues in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/rest/tests/src/Functional/BcTimestampNormalizerUnixTestTrait.php \Drupal\Tests\rest\Functional\BcTimestampNormalizerUnixTestTrait::formatExpectedTimestampItemValues()

Formats a UNIX timestamp.

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

string|int The formatted RFC3339 date string or UNIX timestamp.

See also

\Drupal\serialization\Normalizer\TimestampItemNormalizer

17 calls to BcTimestampNormalizerUnixTestTrait::formatExpectedTimestampItemValues()
BlockContentResourceTestBase::getExpectedNormalizedEntity in core/modules/block_content/tests/src/Functional/Rest/BlockContentResourceTestBase.php
Returns the expected normalization of the entity.
EntitySerializationTest::testNormalize in core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php
Test the normalize function.
EntitySerializationTest::testSerialize in core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php
Test registered Serializer's entity serialization for core's formats.
EntityTestLabelResourceTestBase::getExpectedNormalizedEntity in core/modules/system/tests/modules/entity_test/tests/src/Functional/Rest/EntityTestLabelResourceTestBase.php
Returns the expected normalization of the entity.
EntityTestMapFieldResourceTestBase::getExpectedNormalizedEntity in core/modules/system/tests/modules/entity_test/tests/src/Functional/Rest/EntityTestMapFieldResourceTestBase.php
Returns the expected normalization of the entity.

... See full list

File

core/modules/rest/tests/src/Functional/BcTimestampNormalizerUnixTestTrait.php, line 24

Class

BcTimestampNormalizerUnixTestTrait
Trait for ResourceTestBase subclasses formatting expected timestamp data.

Namespace

Drupal\Tests\rest\Functional

Code

protected function formatExpectedTimestampItemValues($timestamp) {

  // If the setting is enabled, just return the timestamp as-is now.
  if ($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);

  // Per \Drupal\Core\TypedData\Plugin\DataType\Timestamp::getDateTime(), they
  // default to string representations in the UTC timezone.
  $date
    ->setTimezone(new \DateTimeZone('UTC'));

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