getCreatedTime returns string instead of integer

Created on 7 June 2024, about 1 year ago

Problem/Motivation

The phpdocs of getCreatedTime mention that the timestamp will be returned as integer. While it is currently returning a string.
This leads to bugs when you do strict comparison with the changed time which is correctly returning an integer.

Steps to reproduce

if ($comment->getCreatedTime() !== $comment->getChangedTime()) {
  // Statement is always true
}

Proposed resolution

Convert the value to int, just like all other timestamp methods.

πŸ› Bug report
Status

Active

Version

10.4 ✨

Component
CommentΒ  β†’

Last updated 3 days ago

Created by

πŸ‡§πŸ‡ͺBelgium nils.destoop

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @nils.destoop
  • Status changed to Needs review about 1 year ago
  • πŸ‡§πŸ‡ͺBelgium nils.destoop

    Updated the logic to correctly return an integer.

  • πŸ‡§πŸ‡ͺBelgium nils.destoop

    nils.destoop β†’ changed the visibility of the branch 3453210-getcreatedtime-returns-string to hidden.

  • Pipeline finished with Failed
    about 1 year ago
    Total: 631s
    #193626
  • Status changed to Needs work about 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Think maybe the docs should be updated? You can see the change causes test failures so there is backwards compatibility concerns with making this change.

  • Status changed to Needs review about 1 year ago
  • πŸ‡§πŸ‡ͺBelgium nils.destoop

    The docs that need to be updated is the quick fix, but imo a dirty fix. It makes no sense to return an int for getChangedTime and a string for getCreatedTime.
    I agree that there could. be backwards compatibility issues, but a simple release note can warn users for this.

    The failing test that I just fixed also shows how illogical it is:

    before:

        $this->assertSame('1421727536', $comment->getCreatedTime());
        $this->assertSame(1421727536, $comment->getChangedTime());
    

    after:

        $this->assertSame(1421727536, $comment->getCreatedTime());
        $this->assertSame(1421727536, $comment->getChangedTime());
    
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Right but about the BC concern this could immediately start breaking someone's code with no warning.

  • Pipeline finished with Failed
    about 1 year ago
    Total: 652s
    #197287
  • Status changed to Needs work about 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Discussed in slack with @catch, for reference https://drupal.slack.com/archives/C04CHUX484T/p1718289507191399

    Created a CR just in case.

    But does have a legit failure.

  • πŸ‡ΊπŸ‡ΈUnited States cmlara

    Setting parent issue to 🌱 [META] Stronger Typing for Entity API Active .

  • πŸ‡¨πŸ‡¦Canada Liam Morland Ontario, CA πŸ‡¨πŸ‡¦

    liam morland β†’ made their first commit to this issue’s fork.

  • πŸ‡¨πŸ‡¦Canada Liam Morland Ontario, CA πŸ‡¨πŸ‡¦

    Test failure fixed.

  • Pipeline finished with Success
    8 months ago
    Total: 626s
    #346381
  • πŸ‡ΊπŸ‡ΈUnited States mfb San Francisco

    Looks good to me.

    When we made this fix for File entity - https://www.drupal.org/node/3262371 β†’ - I assumed that adding type declarations would need to happen in a major version, so I didn't work on it in that issue.

    Tagging "needs followup" since there are other cases of getCreatedTime() and getRevisionCreationTime() that need to be fixed

  • πŸ‡ΊπŸ‡ΈUnited States mfb San Francisco

    By the way, for comment entities, created time cannot *really* be null, as that column has a NOT NULL specification, so you cannot save a comment with a null created time; when you create a new comment entity, the created time is automatically initialized to the current time. So I'm not sure why or in what circumstances this method would return null, aside from someone forcibly setting the created time to null..

  • πŸ‡¨πŸ‡¦Canada Liam Morland Ontario, CA πŸ‡¨πŸ‡¦

    I grepped Drupal core for "function getCreatedTime" and fixed all of them. All except File are documented in their interfaces to return int, so I have it doing that.

    I did not add any type declarations.

  • Pipeline finished with Success
    8 months ago
    Total: 452s
    #347100
  • πŸ‡ΊπŸ‡ΈUnited States mfb San Francisco

    @liam morland File entity allows NULL created time, and thus databases may actually contain NULL for that column. That's why I changed FileInterface to return int|null.

    I believe most entities have a NOT NULL created column, but it's worth double checking them.

  • πŸ‡¨πŸ‡¦Canada Liam Morland Ontario, CA πŸ‡¨πŸ‡¦

    OK, in every other case, the interface already said that only int is allowed.

  • πŸ‡ΊπŸ‡ΈUnited States mfb San Francisco

    @liam morland I was referring to how we need to look at the data model. For example, Media and Workspace entity, like File entity, allow a NULL created time to be stored in the database. So we need to allow the method to return NULL in these cases, and those interfaces need to be adjusted to match.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    It's a pain but will need to add some backwards coverage. πŸ› The method ContentEntityBase::getLoadedRevisionId() should not return string values Needs review is a good example.

  • Pipeline finished with Failed
    7 months ago
    #355378
  • Pipeline finished with Success
    7 months ago
    Total: 813s
    #355389
  • πŸ‡ΊπŸ‡ΈUnited States mfb San Francisco
    • Also fix RevisionLogEntityTrait::getRevisionCreationTime() to return integers, as it is documented to
    • Adjust MediaInterface::getCreatedTime, NodeInterface::getRevisionCreationTime() and RevisionLogInterface::getRevisionCreationTime() to allow NULL return value, as the data model allows NULL values
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Believe #18 still holds true. If someone is checking if this is a string it’ll immediately break. So still think some BC coverage is needed

  • πŸ‡ΊπŸ‡ΈUnited States mfb San Francisco

    @smustgrave Can you explain what you mean by BC coverage?

    I believe all we need here is a change record (and some reviews to sanity check this :)

    This is a bugfix because these methods are already documented to return an integer.

    This is analogous to similar bug fixes I made for changed time - πŸ› EntityChangedTrait return type mismatch RTBC - and File created time - πŸ› Fix type hints in FileInterface to align with reality Fixed - where all we needed were change records.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    If you look at the issue in #18 essentially a deprecation had to be added to return a string

  • πŸ‡ΊπŸ‡ΈUnited States mfb San Francisco

    @smustgrave is there a scenario where a deprecation would be needed here? We didn't need a deprecation for EntityChangedTrait or File created time.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    May be low. But you had to fix tests to match this change. Possible someone was doing the same on their contrib?

  • πŸ‡ΊπŸ‡ΈUnited States mfb San Francisco

    This is why a change record is needed. Basically the same test changes were made when we fixed changed time - now these two fields will finally match.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    I’ll leave in review but think this could be a breaking change

  • Status changed to Needs review 5 months ago
  • First commit to issue fork.
  • Pipeline finished with Success
    5 months ago
    Total: 498s
    #402954
  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    Can we get the change record updated here to reflect that this isn't just for comments?

    Because this has the potential to break contrib tests (as seen by the test changes needed for core) I feel like this is too disruptive for a patch release and would need to be minor release only

    Thanks for working on this πŸŽ‰

  • πŸ‡ΊπŸ‡ΈUnited States mfb San Francisco

    Updated the change record

  • Pipeline finished with Success
    4 months ago
    Total: 810s
    #451989
  • πŸ‡ΊπŸ‡ΈUnited States mfb San Francisco

    Ready for review - this removes some potential edge case behavior changes where null would have been cast to 0.

    But, the methods are not documented to return null. So, I think either a deprecation should be triggered if null is returned, or the documentation could be changed to allow null, despite the data model not allowing null.

  • Pipeline finished with Failed
    4 months ago
    Total: 632s
    #452210
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Shouldn't the todos point to an issue

  • Status changed to Needs work 3 days ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Can we please update the todos to the issue they’re suppose to be solved in please

  • πŸ‡¨πŸ‡¦Canada Liam Morland Ontario, CA πŸ‡¨πŸ‡¦

    Is there an existing issue about those?

  • πŸ‡ΊπŸ‡ΈUnited States mfb San Francisco

    We didn't make another issue AFAIK. The current state of the patch is "for review" to figure out next steps.

  • Pipeline finished with Success
    3 days ago
    Total: 2402s
    #538270
  • πŸ‡¨πŸ‡¦Canada Liam Morland Ontario, CA πŸ‡¨πŸ‡¦
  • πŸ‡¨πŸ‡¦Canada Liam Morland Ontario, CA πŸ‡¨πŸ‡¦
  • πŸ‡ΊπŸ‡ΈUnited States mfb San Francisco

    @liam morland did you see my comments in #30? I feel like it would be helpful if this issue could decide to either trigger a deprecation if null is returned in cases where it is not documented to be possible, or change the documentation to allow null, despite the data model not allowing null.

    In other words, my @todo was something to figure out in this issue, not a future issue.

  • πŸ‡¬πŸ‡§United Kingdom oily Greater London

    I have had a look at the CR and the MR and recent comments. I note there are 2x todos, 1 in User.php and 1 in Workspace.php. It would seem a follow-up issue could deal with those? Since the 2x timestamps involved in the todos are unusual and need to be handled differently since we are allowing safe handling of NULL values for timestamp in this issue but these 2x todos are when NULL is not currently supposed to be allowed.

    Worth taking a look at the phpdocs for the 2x timestamps to see why they should not return NULL and if should throw an exception or how null values are to be dealt with.

  • πŸ‡¨πŸ‡¦Canada Liam Morland Ontario, CA πŸ‡¨πŸ‡¦

    @#37: The current issue is pretty straight-forward: Return int instead of string. Deciding what to do about the nulls is a bigger topic. So, I think it makes sense to surface that issue and put it into its own issue.

  • πŸ‡¬πŸ‡§United Kingdom oily Greater London

    #39 +1. Are we ready for RTBTC? We have test coverage in place, CR seems done.. Pipeline green?

  • πŸ‡¬πŸ‡§United Kingdom oily Greater London

    oily β†’ changed the visibility of the branch 11.x to hidden.

  • Pipeline finished with Success
    3 days ago
    Total: 4224s
    #538318
Production build 0.71.5 2024