function TimestampFormatterTest::testTimestampFormatter

Same name in other branches
  1. 9 core/modules/field/tests/src/Kernel/Timestamp/TimestampFormatterTest.php \Drupal\Tests\field\Kernel\Timestamp\TimestampFormatterTest::testTimestampFormatter()
  2. 8.9.x core/modules/field/tests/src/Kernel/Timestamp/TimestampFormatterTest.php \Drupal\Tests\field\Kernel\Timestamp\TimestampFormatterTest::testTimestampFormatter()
  3. 10 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 110

Class

TimestampFormatterTest
Tests the timestamp formatters.

Namespace

Drupal\Tests\field\Kernel\Timestamp

Code

public function testTimestampFormatter() : void {
    $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) {
        [
            $date_format,
            $custom_date_format,
            $timezone,
        ] = array_values($settings);
        if (empty($timezone)) {
            $timezone = NULL;
        }
        $value = \Drupal::time()->getRequestTime() - 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);
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.