Make WordPress Core

Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#28310 closed defect (bug) (fixed)

Accessing properties of null returned from get_post()

Reported by: garyj's profile GaryJ Owned by: wonderboymusic's profile 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)

28310.diff (3.0 KB) - added by GaryJ 10 years ago.
28310.2.diff (3.3 KB) - added by SergeyBiryukov 10 years ago.
28310.3.diff (3.9 KB) - added by SergeyBiryukov 10 years ago.
28310.4.diff (3.9 KB) - added by tollmanz 10 years ago.
Refreshed patch to apply cleanly
28310.5.diff (8.1 KB) - added by tollmanz 10 years ago.
Patch updated with unit tests covering each of the functions relevant to the ticket.

Download all attachments as: .zip

Change History (18)

@GaryJ
10 years ago

This ticket was mentioned in IRC in #wordpress-dev by GaryJ. View the logs.


10 years ago

#2 @SergeyBiryukov
10 years ago

  • Keywords has-patch added
  • Milestone changed from Awaiting Review to 4.0

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() and get_post_modified_time().

#3 @DrewAPicture
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: @SergeyBiryukov
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 @GaryJ
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 @DrewAPicture
10 years ago

  • Keywords needs-refresh punt added

Patch needs a refresh. Might need some tests for at least mysql2date() too.

Last edited 10 years ago by DrewAPicture (previous) (diff)

@tollmanz
10 years ago

Refreshed patch to apply cleanly

@tollmanz
10 years ago

Patch updated with unit tests covering each of the functions relevant to the ticket.

#8 @DrewAPicture
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?

#10 @wonderboymusic
10 years ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 29344:

Clarify that get_the_date(), get_the_time(), get_post_time() and get_post_modified_time() should return false when get_post() is null.

Adds unit tests.

Props GaryJ, SergeyBiryukov, tollmanz.
Fixes #28310.

#11 @SergeyBiryukov
10 years ago

In 29351:

Consolidate some of the test functions added in [29344].

see #28310.

#12 @nacin
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.

#13 @SergeyBiryukov
10 years ago

I agree in general, it just seemed inconsistent with the rest of the file and a bit redundant for these particular tests.

I've grouped them by the function being tested, and made sure the test functions still have accurate names.

Note: See TracTickets for help on using tickets.