forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetPostTime.php
More file actions
140 lines (120 loc) · 4.69 KB
/
getPostTime.php
File metadata and controls
140 lines (120 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
/**
* @group date
* @group datetime
* @group post
*
* @covers ::get_post_time
* @covers ::get_post_modified_time
*/
class Tests_Date_GetPostTime extends WP_UnitTestCase {
/**
* Cleans up.
*/
public function tear_down() {
// Reset the timezone option to the default value.
update_option( 'timezone_string', '' );
parent::tear_down();
}
/**
* @ticket 28310
*/
public function test_get_post_time_returns_correct_time_with_post_id() {
$post_id = self::factory()->post->create( array( 'post_date' => '2014-03-01 16:35:00' ) );
$this->assertSame( '16:35:00', get_post_time( 'H:i:s', false, $post_id ) );
}
/**
* @ticket 28310
*/
public function test_get_post_time_returns_false_with_null_or_non_existing_post() {
$this->assertFalse( get_post_time() );
$this->assertFalse( get_post_time( 'h:i:s' ) );
$this->assertFalse( get_post_time( '', false, 9 ) );
$this->assertFalse( get_post_time( 'h:i:s', false, 9 ) );
}
/**
* @ticket 28310
*/
public function test_get_post_modified_time_returns_correct_time_with_post_id() {
$post_id = self::factory()->post->create( array( 'post_date' => '2014-03-01 16:35:00' ) );
$this->assertSame( '16:35:00', get_post_modified_time( 'H:i:s', false, $post_id ) );
}
/**
* @ticket 28310
*/
public function test_get_post_modified_time_returns_false_with_null_or_non_existing_post() {
$this->assertFalse( get_post_modified_time() );
$this->assertFalse( get_post_modified_time( 'h:i:s' ) );
$this->assertFalse( get_post_modified_time( '', false, 9 ) );
$this->assertFalse( get_post_modified_time( 'h:i:s', false, 9 ) );
}
/**
* @ticket 25002
*/
public function test_should_return_wp_timestamp() {
$timezone = 'Europe/Helsinki';
update_option( 'timezone_string', $timezone );
$datetime = new DateTimeImmutable( 'now', new DateTimeZone( $timezone ) );
$mysql = $datetime->format( 'Y-m-d H:i:s' );
$timestamp = $datetime->getTimestamp();
$wp_timestamp = $datetime->getTimestamp() + $datetime->getOffset();
$post_id = self::factory()->post->create(
array(
'post_date' => $mysql,
'post_modified' => $mysql,
)
);
$this->assertSame( $wp_timestamp, get_post_time( 'U', false, $post_id ) );
$this->assertSame( $wp_timestamp, get_post_time( 'G', false, $post_id ) );
$this->assertSame( $timestamp, get_post_time( 'U', true, $post_id ) );
$this->assertSame( $timestamp, get_post_time( 'G', true, $post_id ) );
$this->assertSame( $wp_timestamp, get_post_modified_time( 'U', false, $post_id ) );
$this->assertSame( $wp_timestamp, get_post_modified_time( 'G', false, $post_id ) );
$this->assertSame( $timestamp, get_post_modified_time( 'U', true, $post_id ) );
$this->assertSame( $timestamp, get_post_modified_time( 'G', true, $post_id ) );
}
/**
* @ticket 25002
*/
public function test_should_return_time() {
$timezone = 'Europe/Helsinki';
update_option( 'timezone_string', $timezone );
$datetime = new DateTimeImmutable( 'now', new DateTimeZone( $timezone ) );
$mysql = $datetime->format( 'Y-m-d H:i:s' );
$rfc3339 = $datetime->format( DATE_RFC3339 );
$rfc3339_utc = $datetime->setTimezone( new DateTimeZone( 'UTC' ) )->format( DATE_RFC3339 );
$post_id = self::factory()->post->create(
array(
'post_date' => $mysql,
'post_modified' => $mysql,
)
);
$this->assertSame( $rfc3339, get_post_time( DATE_RFC3339, false, $post_id ) );
$this->assertSame( $rfc3339_utc, get_post_time( DATE_RFC3339, true, $post_id ) );
$this->assertSame( $rfc3339, get_post_time( DATE_RFC3339, false, $post_id, true ) );
$this->assertSame( $rfc3339_utc, get_post_time( DATE_RFC3339, true, $post_id, true ) );
$this->assertSame( $rfc3339, get_post_modified_time( DATE_RFC3339, false, $post_id ) );
$this->assertSame( $rfc3339_utc, get_post_modified_time( DATE_RFC3339, true, $post_id ) );
$this->assertSame( $rfc3339, get_post_modified_time( DATE_RFC3339, false, $post_id, true ) );
$this->assertSame( $rfc3339_utc, get_post_modified_time( DATE_RFC3339, true, $post_id, true ) );
}
/**
* @ticket 48384
*/
public function test_should_keep_utc_time_on_timezone_change() {
$timezone = 'UTC';
update_option( 'timezone_string', $timezone );
$datetime = new DateTimeImmutable( 'now', new DateTimeZone( $timezone ) );
$mysql = $datetime->format( 'Y-m-d H:i:s' );
$rfc3339 = $datetime->format( DATE_RFC3339 );
$post_id = self::factory()->post->create(
array(
'post_date' => $mysql,
'post_modified' => $mysql,
)
);
update_option( 'timezone_string', 'Europe/Helsinki' );
$this->assertSame( $rfc3339, get_post_time( DATE_RFC3339, true, $post_id ) );
$this->assertSame( $rfc3339, get_post_modified_time( DATE_RFC3339, true, $post_id ) );
}
}