Skip to content

Instantly share code, notes, and snippets.

@johnregan3
Created August 9, 2013 15:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnregan3/6194589 to your computer and use it in GitHub Desktop.
Save johnregan3/6194589 to your computer and use it in GitHub Desktop.
WordPress Unit Test for get_post_time(), get_post_modified_time() and mysql2date()
<?php
/**
* Tests if get_post_time() and get_post_modified_time() return GMT(UTC) times correctly.
*
* get_post_time() and get_post_modified_time() have a parameter for GMT(UTC) time.
* However, they use mysqldate(), which does not take such a parameter.
* mysql2date() expects the default timezone to be set to GMT(UTC), but when it is not,
* get_post_time() and get_post_modified_time() do not correctly return GMT(UTC) time.
* To prove this, test_get_post_time() and test_get_post_modified_time() should return two
* identical variables, but they do not.
*/
class Mysql2date {
/**
* Test to see if get_post_modified_time returns GMT(UTC) date when requested.
*
* @return array Array of dates generated by get_post_time
*/
public static function test_get_post_time( $post_id, $format ) {
//Ensure Default Time Zone is set to GMT(UTC), which is set/expected by WP.
date_default_timezone_set('UTC');
//Fetch post time with $gmt = true
$post_time_1 = get_post_time( $format, true, $post_id, false );
/**
* Now, fetch post time with the new non-GMT(UTC) timezone.
* In theory, it should be the same as $post_time_1
* because both set the $gmt parameter to true.
*/
date_default_timezone_set('America/Chicago');
$post_time_2 = get_post_time( $format, true, $post_id, false );
//Reset the default timezone to UTC, or else a bunch of other tests will fail.
date_default_timezone_set('UTC');
return array( $post_time_1, $post_time_2 );
}
/**
* Test to see if get_post_modified_time returns GMT(UTC) date when requested.
*
* @return array Array of dates generated by get_post_modified_time
*/
public static function test_get_post_modified_time( $post_id, $format ) {
//Ensure Default Time Zone is set to GMT/UTC, which is set/expected by WP.
date_default_timezone_set('UTC');
//Fetch post time with $gmt = true
$post_modified_time_1 = get_post_modified_time( $format, true, $post_id, false );
//Now, set default timezone to something non-GMT(UTC)
date_default_timezone_set('America/Chicago');
/**
* Now, fetch post time with the new non-GMT(UTC) timezone.
* In theory, it should be the same as $post_modified_time_1
* because both set the $gmt parameter to true.
*/
$post_modified_time_2 = get_post_modified_time( $format, true, $post_id, false );
//Reset the default timezone to UTC, or else a bunch of other tests will fail.
date_default_timezone_set('UTC');
return array( $post_modified_time_1, $post_modified_time_2 );
}
}
/**
* Set Up Test
*/
class Tests_Mysql2date extends WP_UnitTestCase {
/**
* Establish testing variables
*
* @return array Array of Date format variables
*/
public function provider() {
return array(
array( 'G' ),
array( 'U' ),
array( 'Y:m:d H:i' ),
);
}
/**
* Send date format to test_get_post_time() and check to see if times match
*
* @param string $format Reqested date format
* @dataProvider provider
*/
public function test_mysql2date_get_post_time( $format ) {
$post_id = $this->factory->post->create();
$results = Mysql2date::test_get_post_time( $post_id, $format );
list( $expected_result, $result ) = $results;
$this->assertEquals( $expected_result, $result );
}
/**
* Send date format to test_get_post_modified_time() and check to see if times match
*
* @param string $format Reqested date format
* @dataProvider provider
*/
public function test_mysql2date_get_post_modified_time( $format ) {
$post_id = $this->factory->post->create();
$results = Mysql2date::test_get_post_modified_time( $post_id, $format );
list( $expected_result, $result ) = $results;
$this->assertEquals( $expected_result, $result );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment