Problem/Motivation

Making tests for the Entity Share module which uses the JSON:API, I found that filtering on a datetime field has unexpected results.

https://www.drupal.org/project/entity_share/issues/3082518#comment-13271362 (failing tests commented out)

Proposed resolution

Unknown

Remaining tasks

  1. Provide a failing test: DONE
  2. Found a solution

API changes

Unknown

Release notes snippet

Unknown

Issue fork drupal-3083561

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Grimreaper created an issue. See original summary.

Grimreaper’s picture

Assigned: Grimreaper » Unassigned
Issue summary: View changes
Status: Active » Needs review
FileSize
5.56 KB

Status: Needs review » Needs work

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

mglaman’s picture

Isn't this working as expected? You are querying by a timestamp and DateTimeItemInterface::DATETIME_STORAGE_FORMAT is Y-m-d\TH:i:s. Are you expecting the timestamp to be converted to a string format on query?

Looking at: https://www.drupal.org/docs/8/modules/jsonapi/filtering, it is conflicting.

Filtering on Date (Date only, no time)

filter[datefilter][condition][path]=field_test_date
filter[datefilter][condition][operator]=%3D
filter[datefilter][condition][value]=2019-06-27

Filtering on Date (with Date AND Time)

filter[datefilter][condition][path]=field_test_date
filter[datefilter][condition][operator]=%3D
filter[datefilter][condition][value]=1562004576

It shows that date and time should require a timestamp. But these docs are using =

mglaman’s picture

Status: Needs work » Needs review
FileSize
5.2 KB

Looks like the JSON API documentation needs to be updated. You cannot pass a timestamp anymore. You must pass a time string.

Grimreaper’s picture

Thanks @mglaman for the feedbacks.

If it is working with DateTimeItemInterface::DATETIME_STORAGE_FORMAT no problem for me.

As you said, the documentation needs to be updated in this case.

mglaman’s picture

Status: Needs review » Closed (works as designed)

I updated the documentation node. I figure we can close this, then.

Grimreaper’s picture

Status: Closed (works as designed) » Reviewed & tested by the community

Thanks.

Confirming it is working in tests in #3082518-9: Tests: filter, group and sort on channel.

Maybe keeping it opened to have more tests in JSON:API?

Wim Leers’s picture

Title: JSON:API: Filtering on a date time field has unexpected results » Add explicit test coverage for JSON:API filtering on a datetime field
Category: Bug report » Task
Status: Reviewed & tested by the community » Needs work
Issue tags: +API-First Initiative

I agree this would be a good addition to JSON:API's test coverage.

  1. +++ b/core/modules/jsonapi/tests/src/Functional/JsonApiFunctionalDateFieldTest.php
    @@ -0,0 +1,161 @@
    +    // TODO: Check exactly which node is present.
    ...
    +    // TODO: Check exactly which node is present.
    ...
    +    // TODO: Check exactly which node is present.
    ...
    +    // TODO: Check exactly which node is present.
    

    These still need to be implemented.

  2. I'm wondering if this test coverage should live in core/modules/datetime?

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

raman.b’s picture

Status: Needs work » Needs review
FileSize
5.74 KB
2.2 KB

1. Took some inspiration from core/modules/jsonapi/tests/src/Functional/JsonApiFunctionalTest.php and added assertions for checking the nodes.

2.

I'm wondering if this test coverage should live in core/modules/datetime?

Not certain about this one as the issue is still under jsonapi.module but would love to do the required changes if needed

Status: Needs review » Needs work

The last submitted patch, 12: 3083561-12.patch, failed testing. View results

raman.b’s picture

Status: Needs work » Needs review
FileSize
5.75 KB
669 bytes

Resolving deprecation notices

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

bbrala’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll

Great work, this seems like a good addition. I would need a reroll to 9.3.x i think. Could you do that? I'll review it again after.

bbrala’s picture

Status: Needs work » Reviewed & tested by the community
Issue tags: -Needs reroll

No it doesn't need a reroll. Well done.

quietone’s picture

Status: Reviewed & tested by the community » Needs work

Just some suggestions for improvements to the comments.

  1. +++ b/core/modules/jsonapi/tests/src/Functional/JsonApiFunctionalDateFieldTest.php
    @@ -0,0 +1,183 @@
    + * General functional test class.
    

    The comment should reflect that this is a test of the date field not a 'General' test.

  2. +++ b/core/modules/jsonapi/tests/src/Functional/JsonApiFunctionalDateFieldTest.php
    @@ -0,0 +1,183 @@
    +    $timestamp_node_1 = '5000000';
    +    $timestamp_node_2 = '6000000';
    +    $timestamp_node_3 = '7000000';
    

    The naming here is a bit confusing. $timestamp_node_2 is used for node 1 and node 2 and node 3. I think it would be easier to follow to remove the '_node_' part of the name.

  3. +++ b/core/modules/jsonapi/tests/src/Functional/JsonApiFunctionalDateFieldTest.php
    @@ -0,0 +1,183 @@
    +    // >.
    ...
    +    // >=.
    ...
    +    // <.
    ...
    +    // =<.
    

    These are rather cryptic. It would be easier to understand if this was in plain English.

ankithashetty’s picture

Status: Needs work » Needs review
FileSize
5.96 KB
3.42 KB

Addressed changes specified in #19 Not 100% sure about #19.3, gave it a try though. Thanks!

bbrala’s picture

Status: Needs review » Reviewed & tested by the community

Thanks for the changes (and for the feedback @quietone). Looking good.

Wim Leers’s picture

Nice work here!

  1. +++ b/core/modules/jsonapi/tests/src/Functional/JsonApiFunctionalDateFieldTest.php
    @@ -0,0 +1,183 @@
    + * JSON:API integration test for the "Date" field.
    

    I was gonna say "isn't it 'datetime'?" — but no:

     * @FieldType(
     *   id = "datetime",
     *   label = @Translation("Date"),
    …
     *   list_class = "\Drupal\datetime\Plugin\Field\FieldType\DateTimeFieldItemList",
     *   constraints = {"DateTimeFormat" = {}}
     * )
     */
    class DateTimeItem
    

    🙈

  2. +++ b/core/modules/jsonapi/tests/src/Functional/JsonApiFunctionalDateFieldTest.php
    @@ -0,0 +1,183 @@
    +    $field_config = FieldConfig::create([
    +      'field_name' => 'field_datetime',
    +      'label' => 'Date and time',
    +      'entity_type' => 'node',
    +      'bundle' => 'article',
    +      'required' => FALSE,
    +      'settings' => [],
    +      'description' => '',
    +    ]);
    +    $field_config->save();
    

    Übernit: 🤔 Why assign it to a variable if you're not going to use it?

  3. +++ b/core/modules/jsonapi/tests/src/Functional/JsonApiFunctionalDateFieldTest.php
    @@ -0,0 +1,183 @@
    +   * Test the GET method.
    

    Übernit: s/Test/Tests/

  4. +++ b/core/modules/jsonapi/tests/src/Functional/JsonApiFunctionalDateFieldTest.php
    @@ -0,0 +1,183 @@
    +          'operator' => '>',
    +          'value' => date(DateTimeItemInterface::DATETIME_STORAGE_FORMAT, $timestamp_greater_than_value),
    

    So this is the key thing that this is implicitly documenting through tests: you apparently have to use the storage-level datetime representation to get this to work.

    That makes sense from a Field API POV.

    It's not ideal from a JSON:API POV. But … we can still work to improve this in the future obviously :)

    Wonderful to have this explicitly documented & tested!

Keeping at RTBC.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 20: 3083561-20.patch, failed testing. View results

bbrala’s picture

Status: Needs work » Reviewed & tested by the community

Unrelated test failure.

Grimreaper’s picture

Hello,

I have created a MR for easier rebase and review.

And taking points 22.2 and 22.3 into account.

22.4: exactly!

Thanks for the review and the updates on the issue :)

alexpott’s picture

Version: 9.3.x-dev » 9.2.x-dev
Status: Reviewed & tested by the community » Fixed

Committed and pushed 623cc951b5 to 9.3.x and 7fb32c14ed to 9.2.x. Thanks!

Backported to 9.2.x as this is a test only change.

  • alexpott committed 623cc95 on 9.3.x
    Issue #3083561 by Grimreaper, raman.b, ankithashetty, mglaman, bbrala,...

  • alexpott committed 7fb32c1 on 9.2.x
    Issue #3083561 by Grimreaper, raman.b, ankithashetty, mglaman, bbrala,...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.