wordpress-develop/tests/phpunit/tests/date/dateI18n.php
Gary Pendergast 9efcfbbf3f Tests: Fix the tests for the c and r formats in date_i18n().
To test the `date_i18n()` output correctly, the tests added in [43434] need to set a non-UTC timezone.

Props Rarst.
Fixes #20973.



git-svn-id: https://develop.svn.wordpress.org/trunk@44710 602fd350-edb4-49c9-b593-d223f7449a82
2019-01-29 01:19:30 +00:00

111 lines
4.0 KiB
PHP

<?php
/**
* @group date
* @group datetime
*/
class Tests_Date_I18n extends WP_UnitTestCase {
public function test_should_format_date() {
$this->assertEquals( strtotime( date( 'Y-m-d H:i:s' ) ), strtotime( date_i18n( 'Y-m-d H:i:s' ) ), 'The dates should be equal', 2 );
}
public function test_should_use_custom_timestamp() {
$this->assertEquals( '2012-12-01 00:00:00', date_i18n( 'Y-m-d H:i:s', strtotime( '2012-12-01 00:00:00' ) ) );
}
public function test_date_should_be_in_gmt() {
$this->assertEquals( strtotime( date( DATE_RFC3339 ) ), strtotime( date_i18n( DATE_RFC3339, false, true ) ), 'The dates should be equal', 2 );
}
public function test_custom_timestamp_ignores_gmt_setting() {
$this->assertEquals( '2012-12-01 00:00:00', date_i18n( 'Y-m-d H:i:s', strtotime( '2012-12-01 00:00:00' ) ) );
}
public function test_custom_timezone_setting() {
update_option( 'timezone_string', 'America/Regina' );
$this->assertEquals( strtotime( date( 'Y-m-d H:i:s', time() + get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) ), strtotime( date_i18n( 'Y-m-d H:i:s' ) ), 'The dates should be equal', 2 );
}
public function test_date_should_be_in_gmt_with_custom_timezone_setting() {
update_option( 'timezone_string', 'America/Regina' );
$this->assertEquals( strtotime( date( DATE_RFC3339 ) ), strtotime( date_i18n( DATE_RFC3339, false, true ) ), 'The dates should be equal', 2 );
}
public function test_date_should_be_in_gmt_with_custom_timezone_setting_and_timestamp() {
update_option( 'timezone_string', 'America/Regina' );
$this->assertEquals( '2012-12-01 00:00:00', date_i18n( 'Y-m-d H:i:s', strtotime( '2012-12-01 00:00:00' ) ) );
}
public function test_adjusts_format_based_on_locale() {
$original_locale = $GLOBALS['wp_locale'];
/* @var WP_Locale $locale */
$locale = clone $GLOBALS['wp_locale'];
$locale->weekday[6] = 'Saturday_Translated';
$locale->weekday_abbrev['Saturday_Translated'] = 'Sat_Translated';
$locale->month[12] = 'December_Translated';
$locale->month_abbrev['December_Translated'] = 'Dec_Translated';
$locale->meridiem['am'] = 'am_Translated';
$locale->meridiem['AM'] = 'AM_Translated';
$GLOBALS['wp_locale'] = $locale;
$expected = 'Saturday_Translated (Sat_Translated) 01 December_Translated (Dec_Translated) 00:00:00 am_Translated AM_Translated';
$actual = date_i18n( 'l (D) d F (M) H:i:s a A', strtotime( '2012-12-01 00:00:00' ) );
// Restore original locale.
$GLOBALS['wp_locale'] = $original_locale;
$this->assertEquals( $expected, $actual );
}
public function test_adjusts_format_based_on_timezone_string() {
update_option( 'timezone_string', 'America/Regina' );
$this->assertEquals( '2012-12-01 00:00:00 CST -06:00 America/Regina', date_i18n( 'Y-m-d H:i:s T P e', strtotime( '2012-12-01 00:00:00' ) ) );
}
/**
* @ticket 34835
*/
public function test_gmt_offset_should_output_correct_timezone() {
$timezone_formats = 'P I O T Z e';
$timezone_string = 'America/Regina';
$datetimezone = new DateTimeZone( $timezone_string );
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' ) );
$this->assertEquals( $datetime->format( $timezone_formats ), date_i18n( $timezone_formats ) );
}
/**
* @dataProvider data_formats
* @ticket 20973
*/
public function test_date_i18n_handles_shorthand_formats( $short, $full ) {
update_option( 'timezone_string', 'America/Regina' );
$this->assertEquals( strtotime( date_i18n( $full ) ), strtotime( date_i18n( $short ) ), 'The dates should be equal', 2 );
$this->assertEquals( $short, date_i18n( '\\' . $short ) );
}
public function data_formats() {
return array(
array(
'c',
'Y-m-d\TH:i:sP',
),
array(
'r',
'D, d M Y H:i:s O',
),
);
}
}