Make WordPress Core

Ticket #25002: mysql2date.php

File mysql2date.php, 3.7 KB (added by johnregan3, 13 years ago)

Unit Test for get_post_time() and get_post_modified_time()

Line 
1<?php
2
3/**
4 * Tests if get_post_time() and get_post_modified_time() return GMT(UTC) times correctly.
5 *
6 * get_post_time() and get_post_modified_time() have a parameter for GMT(UTC) time.
7 * However, they use mysqldate(), which does not take such a parameter.
8 * mysql2date() expects the default timezone to be set to GMT(UTC), but when it is not,
9 * get_post_time() and get_post_modified_time() do not correctly return GMT(UTC) time.
10 * To prove this, test_get_post_time() and test_get_post_modified_time() should return two
11 * identical variables, but they do not.
12 */
13
14class Mysql2date {
15
16 /**
17 * Test to see if get_post_modified_time returns GMT(UTC) date when requested.
18 *
19 * @return array Array of dates generated by get_post_time
20 */
21 public static function test_get_post_time( $post_id, $format ) {
22
23 //Ensure Default Time Zone is set to GMT(UTC), which is set/expected by WP.
24 date_default_timezone_set('UTC');
25
26 //Fetch post time with $gmt = true
27 $post_time_1 = get_post_time( $format, true, $post_id, false );
28
29 /**
30 * Now, fetch post time with the new non-GMT(UTC) timezone.
31 * In theory, it should be the same as $post_time_1
32 * because both set the $gmt parameter to true.
33 */
34 date_default_timezone_set('America/Chicago');
35
36 $post_time_2 = get_post_time( $format, true, $post_id, false );
37
38 //Reset the default timezone to UTC, or else a bunch of other tests will fail.
39 date_default_timezone_set('UTC');
40
41 return array( $post_time_1, $post_time_2 );
42
43 }
44
45
46
47 /**
48 * Test to see if get_post_modified_time returns GMT(UTC) date when requested.
49 *
50 * @return array Array of dates generated by get_post_modified_time
51 */
52 public static function test_get_post_modified_time( $post_id, $format ) {
53
54 //Ensure Default Time Zone is set to GMT/UTC, which is set/expected by WP.
55 date_default_timezone_set('UTC');
56
57 //Fetch post time with $gmt = true
58 $post_modified_time_1 = get_post_modified_time( $format, true, $post_id, false );
59
60 //Now, set default timezone to something non-GMT(UTC)
61 date_default_timezone_set('America/Chicago');
62
63 /**
64 * Now, fetch post time with the new non-GMT(UTC) timezone.
65 * In theory, it should be the same as $post_modified_time_1
66 * because both set the $gmt parameter to true.
67 */
68 $post_modified_time_2 = get_post_modified_time( $format, true, $post_id, false );
69
70 //Reset the default timezone to UTC, or else a bunch of other tests will fail.
71 date_default_timezone_set('UTC');
72
73 return array( $post_modified_time_1, $post_modified_time_2 );
74
75 }
76
77}
78
79
80/**
81 * Set Up Test
82 */
83
84class Tests_Mysql2date extends WP_UnitTestCase {
85
86 /**
87 * Establish testing variables
88 *
89 * @return array Array of Date format variables
90 */
91 public function provider() {
92
93 return array(
94 array( 'G' ),
95 array( 'U' ),
96 array( 'Y:m:d H:i' ),
97 );
98
99 }
100
101
102
103 /**
104 * Send date format to test_get_post_time() and check to see if times match
105 *
106 * @param string $format Reqested date format
107 * @dataProvider provider
108 */
109 public function test_mysql2date_get_post_time( $format ) {
110 $post_id = $this->factory->post->create();
111 $results = Mysql2date::test_get_post_time( $post_id, $format );
112 list( $expected_result, $result ) = $results;
113 $this->assertEquals( $expected_result, $result );
114
115 }
116
117
118
119 /**
120 * Send date format to test_get_post_modified_time() and check to see if times match
121 *
122 * @param string $format Reqested date format
123 * @dataProvider provider
124 */
125 public function test_mysql2date_get_post_modified_time( $format ) {
126 $post_id = $this->factory->post->create();
127 $results = Mysql2date::test_get_post_modified_time( $post_id, $format );
128 list( $expected_result, $result ) = $results;
129 $this->assertEquals( $expected_result, $result );
130
131 }
132
133}