function JsonApiPatchRegressionTest::testPatchingDateTimeFieldsFromIssue3021194
Same name in other branches
- 11.x core/modules/jsonapi/tests/src/Functional/JsonApiPatchRegressionTest.php \Drupal\Tests\jsonapi\Functional\JsonApiPatchRegressionTest::testPatchingDateTimeFieldsFromIssue3021194()
Ensures PATCHing datetime (both date-only & date+time) fields is possible.
See also
https://www.drupal.org/project/drupal/issues/3021194
File
-
core/
modules/ jsonapi/ tests/ src/ Functional/ JsonApiPatchRegressionTest.php, line 209
Class
- JsonApiPatchRegressionTest
- JSON:API regression tests.
Namespace
Drupal\Tests\jsonapi\FunctionalCode
public function testPatchingDateTimeFieldsFromIssue3021194() : void {
$this->config('jsonapi.settings')
->set('read_only', FALSE)
->save(TRUE);
// 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_DATE,
],
])->save();
FieldConfig::create([
'field_name' => 'when',
'entity_type' => 'node',
'bundle' => 'page',
])->save();
FieldStorageConfig::create([
'field_name' => 'when_exactly',
'type' => 'datetime',
'entity_type' => 'node',
'settings' => [
'datetime_type' => DateTimeItem::DATETIME_TYPE_DATETIME,
],
])->save();
FieldConfig::create([
'field_name' => 'when_exactly',
'entity_type' => 'node',
'bundle' => 'page',
])->save();
// Create data.
$page = Node::create([
'title' => 'Stegosaurus',
'type' => 'page',
'when' => [
'value' => '2018-12-19',
],
'when_exactly' => [
'value' => '2018-12-19T17:00:00',
],
]);
$page->save();
// Test.
$user = $this->drupalCreateUser([
'access content',
'edit any page content',
]);
$request_options = [
RequestOptions::AUTH => [
$user->getAccountName(),
$user->pass_raw,
],
RequestOptions::HEADERS => [
'Content-Type' => 'application/vnd.api+json',
'Accept' => 'application/vnd.api+json',
],
];
$node_url = Url::fromUri('internal:/jsonapi/node/page/' . $page->uuid());
$response = $this->request('GET', $node_url, $request_options);
$document = $this->getDocumentFromResponse($response);
$this->assertSame(200, $response->getStatusCode());
$this->assertSame('2018-12-19', $document['data']['attributes']['when']);
$this->assertSame('2018-12-20T04:00:00+11:00', $document['data']['attributes']['when_exactly']);
$document['data']['attributes']['when'] = '2018-12-20';
$document['data']['attributes']['when_exactly'] = '2018-12-19T19:00:00+01:00';
$request_options = $request_options + [
RequestOptions::JSON => $document,
];
$response = $this->request('PATCH', $node_url, $request_options);
$document = $this->getDocumentFromResponse($response);
$this->assertSame(200, $response->getStatusCode());
$this->assertSame('2018-12-20', $document['data']['attributes']['when']);
$this->assertSame('2018-12-20T05:00:00+11:00', $document['data']['attributes']['when_exactly']);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.