<?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 );

	}

}