public function JsonApiFunctionalDateFieldTest::testRead in Drupal 10
Tests the GET method.
File
- core/
modules/ jsonapi/ tests/ src/ Functional/ JsonApiFunctionalDateFieldTest.php, line 56
Class
- JsonApiFunctionalDateFieldTest
- JSON:API integration test for the "Date" field.
Namespace
Drupal\Tests\jsonapi\FunctionalCode
public function testRead() {
/** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
$date_formatter = $this->container
->get('date.formatter');
$timestamp_1 = '5000000';
$timestamp_2 = '6000000';
$timestamp_3 = '7000000';
// Expected: node 1.
$timestamp_smaller_than_value = $timestamp_2;
// Expected: node 1 and node 2.
$timestamp_smaller_than_or_equal_value = $timestamp_2;
// Expected: node 3.
$timestamp_greater_than_value = $timestamp_2;
// Expected: node 2 and node 3.
$timestamp_greater_than_or_equal_value = $timestamp_2;
$node_1 = $this
->createNode([
'type' => 'article',
'uuid' => 'es_test_1',
'status' => NodeInterface::PUBLISHED,
'field_datetime' => $date_formatter
->format($timestamp_1, 'custom', DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
]);
$node_2 = $this
->createNode([
'type' => 'article',
'uuid' => 'es_test_2',
'status' => NodeInterface::PUBLISHED,
'field_datetime' => $date_formatter
->format($timestamp_2, 'custom', DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
]);
$node_3 = $this
->createNode([
'type' => 'article',
'uuid' => 'es_test_3',
'status' => NodeInterface::PUBLISHED,
'field_datetime' => $date_formatter
->format($timestamp_3, 'custom', DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
]);
// Checks whether the date is greater than the given timestamp.
$filter = [
'filter_datetime' => [
'condition' => [
'path' => 'field_datetime',
'operator' => '>',
'value' => date(DateTimeItemInterface::DATETIME_STORAGE_FORMAT, $timestamp_greater_than_value),
],
],
];
$output = Json::decode($this
->drupalGet('/jsonapi/node/article', [
'query' => [
'filter' => $filter,
],
]));
$this
->assertSession()
->statusCodeEquals(200);
$output_uuids = array_map(function ($result) {
return $result['id'];
}, $output['data']);
$this
->assertCount(1, $output_uuids);
$this
->assertSame([
$node_3
->uuid(),
], $output_uuids);
// Checks whether the date is greater than or equal to the given timestamp.
$filter = [
'filter_datetime' => [
'condition' => [
'path' => 'field_datetime',
'operator' => '>=',
'value' => date(DateTimeItemInterface::DATETIME_STORAGE_FORMAT, $timestamp_greater_than_or_equal_value),
],
],
];
$output = Json::decode($this
->drupalGet('/jsonapi/node/article', [
'query' => [
'filter' => $filter,
],
]));
$this
->assertSession()
->statusCodeEquals(200);
$output_uuids = array_map(function ($result) {
return $result['id'];
}, $output['data']);
$this
->assertCount(2, $output_uuids);
$this
->assertSame([
$node_2
->uuid(),
$node_3
->uuid(),
], $output_uuids);
// Checks whether the date is less than the given timestamp.
$filter = [
'filter_datetime' => [
'condition' => [
'path' => 'field_datetime',
'operator' => '<',
'value' => date(DateTimeItemInterface::DATETIME_STORAGE_FORMAT, $timestamp_smaller_than_value),
],
],
];
$output = Json::decode($this
->drupalGet('/jsonapi/node/article', [
'query' => [
'filter' => $filter,
],
]));
$this
->assertSession()
->statusCodeEquals(200);
$output_uuids = array_map(function ($result) {
return $result['id'];
}, $output['data']);
$this
->assertCount(1, $output_uuids);
$this
->assertSame([
$node_1
->uuid(),
], $output_uuids);
// Checks whether the date is less than or equal to the given timestamp.
$filter = [
'filter_datetime' => [
'condition' => [
'path' => 'field_datetime',
'operator' => '<=',
'value' => date(DateTimeItemInterface::DATETIME_STORAGE_FORMAT, $timestamp_smaller_than_or_equal_value),
],
],
];
$output = Json::decode($this
->drupalGet('/jsonapi/node/article', [
'query' => [
'filter' => $filter,
],
]));
$this
->assertSession()
->statusCodeEquals(200);
$output_uuids = array_map(function ($result) {
return $result['id'];
}, $output['data']);
$this
->assertCount(2, $output_uuids);
$this
->assertSame([
$node_1
->uuid(),
$node_2
->uuid(),
], $output_uuids);
}