forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwpDate.php
More file actions
177 lines (149 loc) · 4.14 KB
/
wpDate.php
File metadata and controls
177 lines (149 loc) · 4.14 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/**
* @group date
* @group datetime
*
* @covers ::wp_date
*/
class Tests_Date_wpDate extends WP_UnitTestCase {
/** @var WP_Locale */
private $wp_locale_original;
public function set_up() {
global $wp_locale;
parent::set_up();
$this->wp_locale_original = clone $wp_locale;
}
public function tear_down() {
global $wp_locale;
$wp_locale = $this->wp_locale_original;
parent::tear_down();
}
/**
* @ticket 28636
*/
public function test_should_return_false_on_invalid_timestamp() {
$this->assertFalse( wp_date( DATE_RFC3339, 'invalid' ) );
}
/**
* @ticket 48319
*/
public function test_should_not_escape_localized_numbers() {
global $wp_locale;
$wp_locale->month = array( 10 => '10月' );
$utc = new DateTimeZone( 'UTC' );
$datetime = new DateTimeImmutable( '2019-10-17', $utc );
$this->assertSame( '10月', wp_date( 'F', $datetime->getTimestamp(), $utc ) );
}
/**
* @ticket 48319
*/
public function test_should_keep_localized_slashes() {
global $wp_locale;
$string = 'A \ B';
$wp_locale->month = array( 10 => $string );
$utc = new DateTimeZone( 'UTC' );
$datetime = new DateTimeImmutable( '2019-10-17', $utc );
$this->assertSame( $string, wp_date( 'F', $datetime->getTimestamp(), $utc ) );
}
/**
* Tests that the date is formatted with no timestamp provided.
*
* @ticket 53485
*/
public function test_should_format_date_with_no_timestamp() {
$utc = new DateTimeZone( 'UTC' );
$this->assertSame( (string) time(), wp_date( 'U', null, $utc ) );
}
/**
* Tests that the date is formatted with no timezone provided.
*
* @ticket 53485
*/
public function test_should_format_date_with_no_timezone() {
$utc = new DateTimeZone( 'UTC' );
$datetime = new DateTimeImmutable( '2019-10-17', $utc );
$this->assertSame( 'October', wp_date( 'F', $datetime->getTimestamp() ) );
}
/**
* Tests that the format is set correctly.
*
* @ticket 53485
*
* @dataProvider data_should_format_date
*
* @param string $expected The expected result.
* @param string $format The date format.
*/
public function test_should_format_date( $expected, $format ) {
$utc = new DateTimeZone( 'UTC' );
$datetime = new DateTimeImmutable( '2019-10-17', $utc );
$this->assertSame( $expected, wp_date( $format, $datetime->getTimestamp(), $utc ) );
}
/**
* Data provider.
*
* @return array
*/
public function data_should_format_date() {
return array(
'Swatch Internet Time' => array(
'expected' => '041',
'format' => 'B',
),
'Ante meridiem and Post meridiem (uppercase)' => array(
'expected' => 'AM',
'format' => 'A',
),
'Ante meridiem and Post meridiem (uppercase) and escaped "A"' => array(
'expected' => 'A AM',
'format' => '\\A A',
),
'Ante meridiem and Post meridiem (lowercase)' => array(
'expected' => 'am',
'format' => 'a',
),
'Month' => array(
'expected' => 'October',
'format' => 'F',
),
'Month (abbreviated' => array(
'expected' => 'Oct',
'format' => 'M',
),
'Weekday' => array(
'expected' => 'Thursday',
'format' => 'l',
),
'Weekday (abbreviated)' => array(
'expected' => 'Thu',
'format' => 'D',
),
);
}
/**
* Tests that the date is formatted when
* `$wp_locale->month` and `$wp_locale->weekday` are empty.
*
* @ticket 53485
*/
public function test_should_format_date_with_empty_wp_locale_month_and_weekday() {
global $wp_locale;
$utc = new DateTimeZone( 'UTC' );
$datetime = new DateTimeImmutable( '2019-10-17', $utc );
$wp_locale->month = array();
$wp_locale->weekday = array();
$actual = wp_date( 'F', $datetime->getTimestamp(), $utc );
$this->assertSame( 'October', $actual );
}
/**
* Tests the wp_date filter.
*
* @ticket 53485
*/
public function test_should_apply_filters_for_wp_date() {
$ma = new MockAction();
add_filter( 'wp_date', array( &$ma, 'filter' ) );
wp_date( '' );
$this->assertSame( 1, $ma->get_call_count() );
}
}