#28310 closed defect (bug) (fixed)
Accessing properties of null returned from get_post()
Reported by: | GaryJ | Owned by: | wonderboymusic |
---|---|---|---|
Milestone: | 4.0 | Priority: | normal |
Severity: | normal | Version: | 4.0 |
Component: | Posts, Post Types | Keywords: | has-patch has-unit-tests punt |
Focuses: | Cc: |
Description
get_the_date()
, get_the_time()
, get_post_time()
and get_post_modified_time()
all call get_post()
and then proceed to assume that what was returned was an object, and not the null
that it could have been. When null is returned, Notices are thrown of "Trying to get property of non-object'.
My use case is unit testing the filter hook of a shortcode function which would usually get the post date. The unit test doesn't create a post, and not should it, since the ability for WP to return the right post date is not being tested. The PHP Notice generated from core however makes the unit test into a failure, when it would otherwise have passed the filter aspect of the test just fine.
In real terms, if a post date or time can't be determined, since the post can't be determined, then returning an empty string must be better than generating a Notice?
Attachments (5)
Change History (18)
This ticket was mentioned in IRC in #wordpress-dev by GaryJ. View the logs.
10 years ago
#3
@
10 years ago
- Keywords needs-unit-tests added
28310.2.diff looks good to me. Seems like we're both adding and clarifying a return condition though. Should we not also have new or improved unit tests for get_the_date()
, get_the_time()
, get_post_time()
, and get_post_modified_time()
?
#4
follow-up:
↓ 5
@
10 years ago
These functions all currently return false on failure (that's what mysql2date()
returns if $date
is empty).
I think we should keep that and just avoid a PHP notice. See 28310.3.diff.
Also noticed that some functions do mention int
in the return value in case of a Unix timestamp, and some don't. Leaving a note here to create a separate ticket for that.
#5
in reply to:
↑ 4
@
10 years ago
Replying to SergeyBiryukov:
These functions all currently return false on failure (that's what
mysql2date()
returns if$date
is empty).
I think we should keep that and just avoid a PHP notice. See 28310.3.diff.
I'd like to see the number of types returned from functions reduced to one (return an empty string instead of false), not increased to three, but that's likely a different ticket.
This ticket was mentioned in IRC in #wordpress-dev by DrewAPicture. View the logs.
10 years ago
#7
@
10 years ago
- Keywords needs-refresh punt added
Patch needs a refresh. Might need some tests for at least mysql2date()
too.
#8
@
10 years ago
- Keywords has-unit-tests added; needs-unit-tests needs-refresh removed
- Owner set to wonderboymusic
- Status changed from new to reviewing
@wonderboymusic, could you give a yea or nay on this for 4.0?
#12
@
10 years ago
The tests pre-[29351] is actually the preferred design of tests. Tests should be atomic and simple. Otherwise when one fails, the rest don't execute either, and you end up fixing one test to find additional failures. Also, the function names self-described exactly what was being tested.
single_post_title() and single_term_title() do verify if they have a valid object to work with, and I think it makes sense to follow the pattern here.
28310.2.diff also synchronizes parameter docs between
get_post_time()
andget_post_modified_time()
.