Date/Time: Use wp_timezone() in WP_Date_Query::build_mysql_datetime() to address timezone issues.

Improve unit test coverage.

Props Rarst, Biranit, birgire, jave.web, SergeyBiryukov.
Fixes #41782.

git-svn-id: https://develop.svn.wordpress.org/trunk@45876 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2019-08-22 03:10:05 +00:00
parent 8d5e4fed4d
commit 4e129d1506
2 changed files with 77 additions and 30 deletions

View File

@ -853,7 +853,7 @@ class WP_Date_Query {
* *
* You can pass an array of values (year, month, etc.) with missing parameter values being defaulted to * You can pass an array of values (year, month, etc.) with missing parameter values being defaulted to
* either the maximum or minimum values (controlled by the $default_to parameter). Alternatively you can * either the maximum or minimum values (controlled by the $default_to parameter). Alternatively you can
* pass a string that will be run through strtotime(). * pass a string that will be passed to date_create().
* *
* @since 3.7.0 * @since 3.7.0
* *
@ -865,8 +865,6 @@ class WP_Date_Query {
* @return string|false A MySQL format date/time or false on failure * @return string|false A MySQL format date/time or false on failure
*/ */
public function build_mysql_datetime( $datetime, $default_to_max = false ) { public function build_mysql_datetime( $datetime, $default_to_max = false ) {
$now = current_time( 'timestamp' );
if ( ! is_array( $datetime ) ) { if ( ! is_array( $datetime ) ) {
/* /*
@ -907,15 +905,23 @@ class WP_Date_Query {
// If no match is found, we don't support default_to_max. // If no match is found, we don't support default_to_max.
if ( ! is_array( $datetime ) ) { if ( ! is_array( $datetime ) ) {
// @todo Timezone issues here possibly $wp_timezone = wp_timezone();
return gmdate( 'Y-m-d H:i:s', strtotime( $datetime, $now ) );
// Assume local timezone if not provided.
$dt = date_create( $datetime, $wp_timezone );
if ( false === $dt ) {
return gmdate( 'Y-m-d H:i:s', false );
}
return $dt->setTimezone( $wp_timezone )->format( 'Y-m-d H:i:s' );
} }
} }
$datetime = array_map( 'absint', $datetime ); $datetime = array_map( 'absint', $datetime );
if ( ! isset( $datetime['year'] ) ) { if ( ! isset( $datetime['year'] ) ) {
$datetime['year'] = gmdate( 'Y', $now ); $datetime['year'] = current_time( 'Y' );
} }
if ( ! isset( $datetime['month'] ) ) { if ( ! isset( $datetime['month'] ) ) {

View File

@ -509,40 +509,81 @@ class Tests_WP_Date_Query extends WP_UnitTestCase {
$this->assertSame( $expected, $found ); $this->assertSame( $expected, $found );
} }
public function test_build_mysql_datetime_default_to_max_true() { /**
* @ticket 41782
*
* @dataProvider mysql_datetime_input_provider
*
* @param array|string $datetime Array or string date input.
* @param string $expected Expected built result.
* @param bool $default_to_max Flag to default missing values to max.
*/
public function test_build_mysql_datetime( $datetime, $expected, $default_to_max = false ) {
$q = new WP_Date_Query( array() ); $q = new WP_Date_Query( array() );
$found = $q->build_mysql_datetime( $found = $q->build_mysql_datetime( $datetime, $default_to_max );
array(
'year' => 2011, $message = "Expected {$expected}, got {$found}";
), $this->assertEquals( strtotime( $expected ), strtotime( $found ), $message, 10 );
true
);
$this->assertSame( '2011-12-31 23:59:59', $found );
} }
public function test_build_mysql_datetime_default_to_max_false() { public function mysql_datetime_input_provider() {
$q = new WP_Date_Query( array() ); return array(
array( '2019-06-04T08:18:24+03:00', '2019-06-04 05:18:24' ),
$found = $q->build_mysql_datetime( array( '2019-06-04T05:18:24+00:00', '2019-06-04 05:18:24' ),
array( array( array(), current_time( 'Y' ) . '-01-01 00:00:00' ),
'year' => 2011, array( array( 'year' => 2011 ), '2011-12-31 23:59:59', true ),
), array( array( 'year' => 2011 ), '2011-01-01 00:00:00' ),
false array( '2011', '2011-01-01 00:00:00' ),
array( '2011-02', '2011-02-01 00:00:00' ),
array( '2011-02-03', '2011-02-03 00:00:00' ),
array( '2011-02-03 13:30', '2011-02-03 13:30:00' ),
array( '2011-02-03 13:30:35', '2011-02-03 13:30:35' ),
); );
$this->assertSame( '2011-01-01 00:00:00', $found );
} }
public function test_build_mysql_datetime_default_to_max_default_to_false() { /**
* @ticket 41782
*
* @dataProvider mysql_datetime_input_provider_custom_timezone
*
* @param array|string $datetime Array or string date input.
* @param string $expected Expected built result.
* @param bool $default_to_max Flag to default missing values to max.
*/
public function test_build_mysql_datetime_with_custom_timezone( $datetime, $expected, $default_to_max = false ) {
update_option( 'timezone_string', 'Europe/Kiev' );
$q = new WP_Date_Query( array() ); $q = new WP_Date_Query( array() );
$found = $q->build_mysql_datetime( $found = $q->build_mysql_datetime( $datetime, $default_to_max );
array(
'year' => 2011, $message = "Expected {$expected}, got {$found}";
), $this->assertEquals( strtotime( $expected ), strtotime( $found ), $message, 10 );
false
}
public function mysql_datetime_input_provider_custom_timezone() {
return array(
array( '2019-06-04T08:18:24+03:00', '2019-06-04 08:18:24' ),
array( '2019-06-04T05:18:24+00:00', '2019-06-04 08:18:24' ),
); );
$this->assertSame( '2011-01-01 00:00:00', $found ); }
/**
* @ticket 41782
*/
public function test_build_mysql_datetime_with_relative_date() {
update_option( 'timezone_string', 'Europe/Kiev' );
$q = new WP_Date_Query( array() );
$yesterday = new DateTimeImmutable( '-1 day', wp_timezone() );
$expected = $yesterday->format( 'Y-m-d H:i:s' );
$found = $q->build_mysql_datetime( '-1 day' );
$message = "Expected {$expected}, got {$found}";
$this->assertEquals( strtotime( $expected ), strtotime( $found ), $message, 10 );
} }
public function test_build_time_query_insufficient_time_values() { public function test_build_time_query_insufficient_time_values() {