From 91517ce620987304bad718bb3c847f4f378b5b1c Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Fri, 1 Aug 2014 18:39:22 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@29344 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/functions.php | 2 +- src/wp-includes/general-template.php | 33 ++++-- tests/phpunit/tests/post.php | 157 +++++++++++++++++++++++++++ 3 files changed, 183 insertions(+), 9 deletions(-) diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index 6ee29a84e9..0a8e1a0ccc 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -21,7 +21,7 @@ require( ABSPATH . WPINC . '/option.php' ); * @param string $format Format of the date to return. * @param string $date Date string to convert. * @param bool $translate Whether the return date should be translated. Default true. - * @return string|int Formatted date string, or Unix timestamp. + * @return string|int|bool Formatted date string or Unix timestamp. False if $date is empty. */ function mysql2date( $format, $date, $translate = true ) { if ( empty( $date ) ) diff --git a/src/wp-includes/general-template.php b/src/wp-includes/general-template.php index 8233871da4..61665ad5af 100644 --- a/src/wp-includes/general-template.php +++ b/src/wp-includes/general-template.php @@ -1725,11 +1725,15 @@ function the_date( $d = '', $before = '', $after = '', $echo = true ) { * * @param string $d Optional. PHP date format defaults to the date_format option if not specified. * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default current post. - * @return string Date the current post was written. + * @return string|bool Date the current post was written. False on failure. */ function get_the_date( $d = '', $post = null ) { $post = get_post( $post ); + if ( ! $post ) { + return false; + } + if ( '' == $d ) { $the_date = mysql2date( get_option( 'date_format' ), $post->post_date ); } else { @@ -1839,11 +1843,15 @@ function the_time( $d = '' ) { * was written. Either 'G', 'U', or php date format defaults * to the value specified in the time_format option. Default empty. * @param int|WP_Post $post WP_Post object or ID. Default is global $post object. - * @return string|int Formatted date string, or Unix timestamp. + * @return string|int|bool Formatted date string or Unix timestamp. False on failure. */ function get_the_time( $d = '', $post = null ) { $post = get_post($post); + if ( ! $post ) { + return false; + } + if ( '' == $d ) $the_time = get_post_time(get_option('time_format'), false, $post, true); else @@ -1873,11 +1881,15 @@ function get_the_time( $d = '', $post = null ) { * @param bool $gmt Optional. Whether to retrieve the GMT time. Default false. * @param int|WP_Post $post WP_Post object or ID. Default is global $post object. * @param bool $translate Whether to translate the time string. Default false. - * @return string|int Formatted date string, or Unix timestamp. + * @return string|int|bool Formatted date string or Unix timestamp. False on failure. */ function get_post_time( $d = 'U', $gmt = false, $post = null, $translate = false ) { $post = get_post($post); + if ( ! $post ) { + return false; + } + if ( $gmt ) $time = $post->post_date_gmt; else @@ -1951,15 +1963,20 @@ function get_the_modified_time($d = '') { * * @since 2.0.0 * - * @param string $d Optional, default is 'U'. Either 'G', 'U', or php date format. - * @param bool $gmt Optional, default is false. Whether to return the gmt time. - * @param int|object $post Optional, default is global post object. A post_id or post object - * @param bool $translate Optional, default is false. Whether to translate the result - * @return string Returns timestamp + * @param string $d Optional. Format to use for retrieving the time the post + * was modified. Either 'G', 'U', or php date format. Default 'U'. + * @param bool $gmt Optional. Whether to retrieve the GMT time. Default false. + * @param int|WP_Post $post WP_Post object or ID. Default is global $post object. + * @param bool $translate Whether to translate the time string. Default false. + * @return string|int|bool Formatted date string or Unix timestamp. False on failure. */ function get_post_modified_time( $d = 'U', $gmt = false, $post = null, $translate = false ) { $post = get_post($post); + if ( ! $post ) { + return false; + } + if ( $gmt ) $time = $post->post_modified_gmt; else diff --git a/tests/phpunit/tests/post.php b/tests/phpunit/tests/post.php index 50cc7b1530..b1cb4303b3 100644 --- a/tests/phpunit/tests/post.php +++ b/tests/phpunit/tests/post.php @@ -874,6 +874,163 @@ class Tests_Post extends WP_UnitTestCase { $this->assertEquals( 'March 1, 2014', get_the_date( 'F j, Y', $post_id ) ); } + /** + * @ticket 28310 + */ + function test_get_the_date_returns_false_with_null_post() { + $this->assertFalse( get_the_date() ); + } + + /** + * @ticket 28310 + */ + function test_get_the_date_returns_false_with_format_and_null_post() { + $this->assertFalse( get_the_date( 'F j, Y h:i:s' ) ); + } + + /** + * @ticket 28310 + */ + function test_get_the_date_returns_false_with_post_that_is_not_found() { + $this->assertFalse( get_the_date( '', 9 ) ); + } + + /** + * @ticket 28310 + */ + function test_get_the_date_returns_false_with_format_and_post_that_is_not_found() { + $this->assertFalse( get_the_date( 'F j, Y h:i:s', 9 ) ); + } + + /** + * @ticket 28310 + */ + function test_get_the_time_with_id_returns_correct_time() { + $post_id = $this->factory->post->create( array( 'post_date' => '2014-03-01 16:35:00' ) ); + $this->assertEquals( '16:35:00', get_the_time( 'H:i:s', $post_id ) ); + } + + /** + * @ticket 28310 + */ + function test_get_the_time_returns_false_with_null_post() { + $this->assertFalse( get_the_time() ); + } + + /** + * @ticket 28310 + */ + function test_get_the_time_returns_false_with_format_and_null_post() { + $this->assertFalse( get_the_time( 'h:i:s' ) ); + } + + /** + * @ticket 28310 + */ + function test_get_the_time_returns_false_with_post_that_is_not_found() { + $this->assertFalse( get_the_time( '', 9 ) ); + } + + /** + * @ticket 28310 + */ + function test_get_the_time_returns_false_with_format_and_post_that_is_not_found() { + $this->assertFalse( get_the_time( 'h:i:s', 9 ) ); + } + + /** + * @ticket 28310 + */ + function test_get_post_time_with_id_returns_correct_time() { + $post_id = $this->factory->post->create( array( 'post_date' => '2014-03-01 16:35:00' ) ); + $this->assertEquals( '16:35:00', get_post_time( 'H:i:s', false, $post_id ) ); + } + + /** + * @ticket 28310 + */ + function test_get_post_time_returns_false_with_null_post() { + $this->assertFalse( get_post_time() ); + } + + /** + * @ticket 28310 + */ + function test_get_post_time_returns_false_with_format_and_null_post() { + $this->assertFalse( get_post_time( 'h:i:s' ) ); + } + + /** + * @ticket 28310 + */ + function test_get_post_time_returns_false_with_post_that_is_not_found() { + $this->assertFalse( get_post_time( '', false, 9 ) ); + } + + /** + * @ticket 28310 + */ + function test_get_post_time_returns_false_with_format_and_post_that_is_not_found() { + $this->assertFalse( get_post_time( 'h:i:s', false, 9 ) ); + } + + /** + * @ticket 28310 + */ + function test_get_post_modified_time_with_id_returns_correct_time() { + $post_id = $this->factory->post->create( array( 'post_date' => '2014-03-01 16:35:00' ) ); + $this->assertEquals( '16:35:00', get_post_modified_time( 'H:i:s', false, $post_id ) ); + } + + /** + * @ticket 28310 + */ + function test_get_post_modified_time_returns_false_with_null_post() { + $this->assertFalse( get_post_modified_time() ); + } + + /** + * @ticket 28310 + */ + function test_get_post_modified_time_returns_false_with_format_and_null_post() { + $this->assertFalse( get_post_modified_time( 'h:i:s' ) ); + } + + /** + * @ticket 28310 + */ + function test_get_post_modified_time_returns_false_with_post_that_is_not_found() { + $this->assertFalse( get_post_modified_time( '', false, 9 ) ); + } + + /** + * @ticket 28310 + */ + function test_get_post_modified_time_returns_false_with_format_and_post_that_is_not_found() { + $this->assertFalse( get_post_modified_time( 'h:i:s', false, 9 ) ); + } + + /** + * @ticket 28310 + */ + function test_mysql2date_returns_false_with_no_date() { + $this->assertFalse( mysql2date( 'F j, Y H:i:s', '' ) ); + } + + /** + * @ticket 28310 + */ + function test_mysql2date_returns_gmt_unix_timestamp() { + $this->assertEquals( '441013392', mysql2date( 'G', '1983-12-23 07:43:12' ) ); + } + + /** + * @ticket 28310 + */ + function test_mysql2date_returns_unix_timestamp() { + $this->assertEquals( '441013392', mysql2date( 'U', '1983-12-23 07:43:12' ) ); + } + /** * @ticket 25566 */