wordpress-develop/tests/phpunit/tests/date/mysql2date.php
Tonya Mork 40ac5de838 Coding Standards: Add visibility to methods in tests/phpunit/tests/.
Adds a `public` visibility to test fixtures, tests, data providers, and callbacks methods.

Adds a `private` visibility to helper methods within test classes.

Renames callbacks and helpers that previously started with a `_` prefix. Why? For consistency and to leverage using the method visibility. Further naming standardizations is beyond the scope of this commit.

Props costdev, jrf, hellofromTonya.
Fixes #54177.

git-svn-id: https://develop.svn.wordpress.org/trunk@52010 602fd350-edb4-49c9-b593-d223f7449a82
2021-11-04 15:22:47 +00:00

90 lines
2.8 KiB
PHP

<?php
/**
* @group date
* @group datetime
* @covers ::mysql2date
*/
class Tests_Date_mysql2date extends WP_UnitTestCase {
public function tear_down() {
// phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set
date_default_timezone_set( 'UTC' );
parent::tear_down();
}
/**
* @ticket 28310
*/
public function test_mysql2date_returns_false_with_no_date() {
$this->assertFalse( mysql2date( 'F j, Y H:i:s', '' ) );
}
/**
* @ticket 28310
*/
public function test_mysql2date_returns_gmt_or_unix_timestamp() {
$this->assertSame( 441013392, mysql2date( 'G', '1983-12-23 07:43:12' ) );
$this->assertSame( 441013392, mysql2date( 'U', '1983-12-23 07:43:12' ) );
}
/**
* @ticket 28992
*/
public function test_mysql2date_should_format_time() {
$timezone = 'Europe/Kiev';
update_option( 'timezone_string', $timezone );
$datetime = new DateTime( 'now', new DateTimeZone( $timezone ) );
$rfc3339 = $datetime->format( DATE_RFC3339 );
$mysql = $datetime->format( 'Y-m-d H:i:s' );
$this->assertSame( $rfc3339, mysql2date( DATE_RFC3339, $mysql ) );
$this->assertSame( $rfc3339, mysql2date( DATE_RFC3339, $mysql, false ) );
}
/**
* @ticket 28992
*/
public function test_mysql2date_should_format_time_with_changed_time_zone() {
$timezone = 'Europe/Kiev';
// phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set
date_default_timezone_set( $timezone );
update_option( 'timezone_string', $timezone );
$datetime = new DateTime( 'now', new DateTimeZone( $timezone ) );
$rfc3339 = $datetime->format( DATE_RFC3339 );
$mysql = $datetime->format( 'Y-m-d H:i:s' );
$this->assertSame( $rfc3339, mysql2date( DATE_RFC3339, $mysql ) );
$this->assertSame( $rfc3339, mysql2date( DATE_RFC3339, $mysql, false ) );
}
/**
* @ticket 28992
*/
public function test_mysql2date_should_return_wp_timestamp() {
$timezone = 'Europe/Kiev';
update_option( 'timezone_string', $timezone );
$datetime = new DateTime( 'now', new DateTimeZone( $timezone ) );
$wp_timestamp = $datetime->getTimestamp() + $datetime->getOffset();
$mysql = $datetime->format( 'Y-m-d H:i:s' );
$this->assertSame( $wp_timestamp, mysql2date( 'U', $mysql, false ) );
$this->assertSame( $wp_timestamp, mysql2date( 'G', $mysql, false ) );
}
/**
* @ticket 28992
*/
public function test_mysql2date_should_return_unix_timestamp_for_gmt_time() {
$timezone = 'Europe/Kiev';
update_option( 'timezone_string', $timezone );
$datetime = new DateTime( 'now', new DateTimeZone( 'UTC' ) );
$timestamp = $datetime->getTimestamp();
$mysql = $datetime->format( 'Y-m-d H:i:s' );
$this->assertSame( $timestamp, mysql2date( 'U', $mysql, false ) );
$this->assertSame( $timestamp, mysql2date( 'G', $mysql, false ) );
}
}