Date/Time: Restore the previous behavior of date_i18n() where invalid input would result in current time.

Make `wp_date()` return `false` on invalid timestamp input, for consistency with upstream PHP `date()` function.

Props Rarst.
Fixes #28636.

git-svn-id: https://develop.svn.wordpress.org/trunk@45914 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov
2019-08-29 23:17:30 +00:00
parent 07ec966f17
commit 56d4e7fb86
6 changed files with 42 additions and 6 deletions

View File

@@ -4,7 +4,7 @@
* @group date
* @group datetime
*/
class Tests_Date_CurrentTime extends WP_UnitTestCase {
class Tests_Date_Current_Time extends WP_UnitTestCase {
/**
* @ticket 37440

View File

@@ -5,6 +5,20 @@
* @group datetime
*/
class Tests_Date_I18n extends WP_UnitTestCase {
/**
* @ticket 28636
*/
public function test_should_return_current_time_on_invalid_timestamp() {
$timezone = 'Europe/Kiev';
update_option( 'timezone_string', $timezone );
$datetime = new DateTime( 'now', new DateTimeZone( $timezone ) );
$wp_timestamp = $datetime->getTimestamp() + $datetime->getOffset();
$this->assertEquals( $wp_timestamp, date_i18n( 'U', 'invalid' ), '', 5 );
}
public function test_should_format_date() {
$this->assertEquals( strtotime( gmdate( 'Y-m-d H:i:s' ) ), strtotime( date_i18n( 'Y-m-d H:i:s' ) ), 'The dates should be equal', 2 );
}
@@ -74,6 +88,7 @@ class Tests_Date_I18n extends WP_UnitTestCase {
update_option( 'timezone_string', '' );
$offset = $datetimezone->getOffset( new DateTime() ) / 3600;
update_option( 'gmt_offset', $offset );
$datetime = new DateTime( 'now', $datetimezone );
$datetime = new DateTime( $datetime->format( 'P' ) );

View File

@@ -4,7 +4,7 @@
* @group date
* @group datetime
*/
class Tests_Date_TheDate extends WP_UnitTestCase {
class Tests_Date_The_Date extends WP_UnitTestCase {
/** @var array $hooks_called Count of hooks called. */
protected $hooks_called = array(

View File

@@ -0,0 +1,15 @@
<?php
/**
* @group date
* @group datetime
*/
class Tests_Date_WP_Date extends WP_UnitTestCase {
/**
* @ticket 28636
*/
public function test_should_return_false_on_invalid_timestamp() {
$this->assertFalse( wp_date( DATE_RFC3339, 'invalid' ) );
}
}

View File

@@ -4,7 +4,7 @@
* @group date
* @group datetime
*/
class Tests_WP_Timezone extends WP_UnitTestCase {
class Tests_Date_WP_Timezone extends WP_UnitTestCase {
/**
* @ticket 24730