You are here

public function TimestampFormatterTest::testTimestampFormatter in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/field/tests/src/Kernel/Timestamp/TimestampFormatterTest.php \Drupal\Tests\field\Kernel\Timestamp\TimestampFormatterTest::testTimestampFormatter()

Tests TimestampFormatter.

File

core/modules/field/tests/src/Kernel/Timestamp/TimestampFormatterTest.php, line 108

Class

TimestampFormatterTest
Tests the timestamp formatters.

Namespace

Drupal\Tests\field\Kernel\Timestamp

Code

public function testTimestampFormatter() {
  $data = [];

  // Test standard formats.
  $date_formats = array_keys(\Drupal::entityTypeManager()
    ->getStorage('date_format')
    ->loadMultiple());
  foreach ($date_formats as $date_format) {
    $data[] = [
      'date_format' => $date_format,
      'custom_date_format' => '',
      'timezone' => '',
    ];
  }
  $data[] = [
    'date_format' => 'custom',
    'custom_date_format' => 'r',
    'timezone' => '',
  ];
  $data[] = [
    'date_format' => 'custom',
    'custom_date_format' => 'e',
    'timezone' => 'Asia/Tokyo',
  ];
  foreach ($data as $settings) {
    list($date_format, $custom_date_format, $timezone) = array_values($settings);
    if (empty($timezone)) {
      $timezone = NULL;
    }
    $value = REQUEST_TIME - 87654321;
    $expected = \Drupal::service('date.formatter')
      ->format($value, $date_format, $custom_date_format, $timezone);
    $component = $this->display
      ->getComponent($this->fieldName);
    $component['type'] = 'timestamp';
    $component['settings'] = $settings;
    $this->display
      ->setComponent($this->fieldName, $component);
    $entity = EntityTest::create([]);
    $entity->{$this->fieldName}->value = $value;
    $this
      ->renderEntityFields($entity, $this->display);
    $this
      ->assertRaw($expected);
  }
}