You are here

public function JsonApiRegressionTest::testPatchingDateTimeNormalizedWrongTimeZoneIssue3021194 in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/jsonapi/tests/src/Functional/JsonApiRegressionTest.php \Drupal\Tests\jsonapi\Functional\JsonApiRegressionTest::testPatchingDateTimeNormalizedWrongTimeZoneIssue3021194()
  2. 9 core/modules/jsonapi/tests/src/Functional/JsonApiRegressionTest.php \Drupal\Tests\jsonapi\Functional\JsonApiRegressionTest::testPatchingDateTimeNormalizedWrongTimeZoneIssue3021194()

Ensures datetime fields are normalized using the correct timezone.

See also

https://www.drupal.org/project/drupal/issues/2999438

File

core/modules/jsonapi/tests/src/Functional/JsonApiRegressionTest.php, line 669

Class

JsonApiRegressionTest
JSON:API regression tests.

Namespace

Drupal\Tests\jsonapi\Functional

Code

public function testPatchingDateTimeNormalizedWrongTimeZoneIssue3021194() {

  // Set up data model.
  $this
    ->assertTrue($this->container
    ->get('module_installer')
    ->install([
    'datetime',
  ], TRUE), 'Installed modules.');
  $this
    ->drupalCreateContentType([
    'type' => 'page',
  ]);
  $this
    ->rebuildAll();
  FieldStorageConfig::create([
    'field_name' => 'when',
    'type' => 'datetime',
    'entity_type' => 'node',
    'settings' => [
      'datetime_type' => DateTimeItem::DATETIME_TYPE_DATETIME,
    ],
  ])
    ->save();
  FieldConfig::create([
    'field_name' => 'when',
    'entity_type' => 'node',
    'bundle' => 'page',
  ])
    ->save();

  // Create data.
  $page = Node::create([
    'title' => 'Stegosaurus',
    'type' => 'page',
    'when' => [
      'value' => '2018-09-16T12:00:00',
    ],
  ]);
  $page
    ->save();

  // Test.
  $user = $this
    ->drupalCreateUser([
    'access content',
  ]);
  $response = $this
    ->request('GET', Url::fromUri('internal:/jsonapi/node/page/' . $page
    ->uuid()), [
    RequestOptions::AUTH => [
      $user
        ->getAccountName(),
      $user->pass_raw,
    ],
  ]);
  $this
    ->assertSame(200, $response
    ->getStatusCode());
  $doc = Json::decode((string) $response
    ->getBody());
  $this
    ->assertSame('2018-09-16T22:00:00+10:00', $doc['data']['attributes']['when']);
}