| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @group date |
| 5 | * @group datetime |
| 6 | */ |
| 7 | class Tests_Date_I18n extends WP_UnitTestCase { |
| 8 | public function test_should_format_date() { |
| 9 | $expected = date( 'Y-m-d H:i:s' ); |
| 10 | $this->assertEquals( $expected, date_i18n( 'Y-m-d H:i:s' ) ); |
| 11 | } |
| 12 | |
| 13 | public function test_should_use_custom_timestamp() { |
| 14 | $expected = '2012-12-01 00:00:00'; |
| 15 | |
| 16 | $this->assertEquals( $expected, date_i18n( 'Y-m-d H:i:s', strtotime( '2012-12-01 00:00:00' ) ) ); |
| 17 | } |
| 18 | |
| 19 | public function test_date_should_be_in_gmt() { |
| 20 | $expected = date( 'Y-m-d H:i:s' ); |
| 21 | |
| 22 | $this->assertEquals( $expected, date_i18n( 'Y-m-d H:i:s', false, true ) ); |
| 23 | } |
| 24 | |
| 25 | public function test_custom_timestamp_ignores_gmt_setting() { |
| 26 | $expected = '2012-12-01 00:00:00'; |
| 27 | |
| 28 | $this->assertEquals( $expected, date_i18n( 'Y-m-d H:i:s', strtotime( '2012-12-01 00:00:00' ), false ) ); |
| 29 | } |
| 30 | |
| 31 | public function test_custom_timezone_setting() { |
| 32 | update_option( 'timezone_string', 'Europe/London' ); |
| 33 | $expected = date( 'Y-m-d H:i:s', strtotime( date( 'Y-m-d H:i:s' ) ) + HOUR_IN_SECONDS ); |
| 34 | |
| 35 | $this->assertEquals( $expected, date_i18n( 'Y-m-d H:i:s' ) ); |
| 36 | } |
| 37 | |
| 38 | public function test_date_should_be_in_gmt_with_custom_timezone_setting() { |
| 39 | update_option( 'timezone_string', 'Europe/London' ); |
| 40 | $expected = date( 'Y-m-d H:i:s' ); |
| 41 | |
| 42 | $this->assertNotEquals( date_i18n( 'Y-m-d H:i:s', false, false ), date_i18n( 'Y-m-d H:i:s', false, true ) ); |
| 43 | $this->assertEquals( $expected, date_i18n( 'Y-m-d H:i:s', false, true ) ); |
| 44 | } |
| 45 | |
| 46 | public function test_date_should_be_in_gmt_with_custom_timezone_setting_and_timestamp() { |
| 47 | update_option( 'timezone_string', 'Europe/London' ); |
| 48 | |
| 49 | $expected = '2012-12-01 00:00:00'; |
| 50 | |
| 51 | $this->assertEquals( $expected, date_i18n( 'Y-m-d H:i:s', strtotime( '2012-12-01 00:00:00' ), false ) ); |
| 52 | $this->assertEquals( $expected, date_i18n( 'Y-m-d H:i:s', strtotime( '2012-12-01 00:00:00' ), true ) ); |
| 53 | } |
| 54 | |
| 55 | public function test_adjusts_format_based_on_locale() { |
| 56 | $original_locale = $GLOBALS['wp_locale']; |
| 57 | /* @var WP_Locale $locale */ |
| 58 | $locale = clone $GLOBALS['wp_locale']; |
| 59 | |
| 60 | $locale->weekday[6] = 'Saturday_Translated'; |
| 61 | $locale->weekday_abbrev[ 'Saturday_Translated' ] = 'Sat_Translated'; |
| 62 | $locale->month[12] = 'December_Translated'; |
| 63 | $locale->month_abbrev[ 'December_Translated' ] = 'Dec_Translated'; |
| 64 | $locale->meridiem['am'] = 'am_Translated'; |
| 65 | $locale->meridiem['AM'] = 'AM_Translated'; |
| 66 | |
| 67 | $GLOBALS['wp_locale'] = $locale; |
| 68 | |
| 69 | $expected = 'Saturday_Translated (Sat_Translated) 01 December_Translated (Dec_Translated) 00:00:00 am_Translated AM_Translated'; |
| 70 | $actual = date_i18n( 'l (D) d F (M) H:i:s a A', strtotime( '2012-12-01 00:00:00' ), false ); |
| 71 | |
| 72 | // Restore original locale. |
| 73 | $GLOBALS['wp_locale'] = $original_locale; |
| 74 | |
| 75 | $this->assertEquals( $expected, $actual ); |
| 76 | } |
| 77 | |
| 78 | public function test_adjusts_format_based_on_timezone_string() { |
| 79 | update_option( 'timezone_string', 'Europe/Zurich' ); |
| 80 | |
| 81 | $this->assertEquals( '2012-12-01 00:00:00 CEST +02:00 Europe/Zurich', date_i18n( 'Y-m-d H:i:s T P e', strtotime( '2012-12-01 00:00:00' ), false ) ); |
| 82 | } |
| 83 | } |