forked from timber/timber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-timber-dates.php
358 lines (307 loc) · 10.9 KB
/
test-timber-dates.php
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
<?php
use Timber\DateTimeHelper;
use Timber\Timber;
/**
* Class TestTimberDates
*
* @group called-post-constructor
* @group Timber\Date
*/
class TestTimberDates extends Timber_UnitTestCase
{
public function testDate()
{
$pid = $this->factory->post->create();
$post = Timber::get_post($pid);
$twig = 'I am from {{post.date}}';
$str = Timber::compile_string($twig, [
'post' => $post,
]);
$this->assertEquals('I am from ' . date('F j, Y'), $str);
}
public function testTimeAgoFuture()
{
$str = DateTimeHelper::time_ago('2016-12-01 02:00:00', '2016-11-30, 02:00:00');
$this->assertEquals('1 day from now', $str);
}
public function testTimeAgoPast()
{
$str = DateTimeHelper::time_ago('2016-11-29 02:00:00', '2016-11-30, 02:00:00');
$this->assertEquals('1 day ago', $str);
}
public function testTimeAgoWithPostDate()
{
$pid = $this->factory->post->create([
'post_date' => '2016-07-07 02:03:00',
]);
$post = Timber::get_post($pid);
$str = DateTimeHelper::time_ago($post->date(), '2016-11-30, 02:00:00');
$this->assertEquals('5 months ago', $str);
}
public function testTimeAgoWithPostDateAndCurrent()
{
$pid = $this->factory->post->create([
'post_date' => '2016-07-07 02:03:00',
]);
$post = Timber::get_post($pid);
$str = DateTimeHelper::time_ago($post->date());
$this->assertEquals(
sprintf('%s ago', human_time_diff(strtotime($post->post_date))),
$str
);
}
public function testTimeAgoWithPostDateTwigFilter()
{
$pid = $this->factory->post->create([
'post_date' => '2016-07-07 02:03:00',
]);
$post = Timber::get_post($pid);
$current_ago = DateTimeHelper::time_ago($post->date, time());
$str = Timber::compile_string(
'{{ post.date|time_ago }}',
[
'post' => $post,
]
);
$this->assertEquals($current_ago, $str);
}
/**
* @ticket https://github.com/timber/timber/issues/2737
* @return void
*/
public function testTimeAgoWithPostDateTwigFilterTimezoneAustralia()
{
$timezone_backup = get_option('timezone_string');
// Set timezone to Australia/Adelaide.
update_option('timezone_string', 'Australia/Adelaide');
$current_time = current_datetime();
// Subtract 3 hours to get a time in the past.
$post_date = $current_time->sub(new \DateInterval('PT3H'));
$post_id = $this->factory->post->create([
'post_date' => $post_date->format('Y-m-d H:i:s'),
]);
$post = Timber::get_post($post_id);
$diff1 = Timber::compile_string(
"{{ post.date('U')|time_ago }}",
[
'post' => $post,
]
);
$diff2 = Timber::compile_string(
"{{ post.date(constant('DATE_ATOM'))|time_ago }}",
[
'post' => $post,
]
);
$diff3 = Timber::compile_string(
"{{ post.date('Y-m-d H:i:s')|time_ago }}",
[
'post' => $post,
]
);
$this->assertEquals('3 hours ago', $diff1);
$this->assertEquals('3 hours ago', $diff2);
$this->assertEquals('3 hours ago', $diff3);
update_option('timezone_string', $timezone_backup);
}
public function testTimeAgoLabels()
{
$past = DateTimeHelper::time_ago('2016-11-29 02:00:00', '2016-11-30, 02:00:00', 'prePast %s afterPast');
$future = DateTimeHelper::time_ago('2016-12-01 02:00:00', '2016-11-30, 02:00:00', null, 'preFuture %s afterFuture');
$this->assertEquals('prePast 1 day afterPast', $past);
$this->assertEquals('preFuture 1 day afterFuture', $future);
}
public function testTime()
{
$pid = $this->factory->post->create([
'post_date' => '2016-07-07 02:03:00',
]);
$post = Timber::get_post($pid);
$twig = 'Posted at {{post.time}}';
$str = Timber::compile_string($twig, [
'post' => $post,
]);
$this->assertEquals('Posted at 2:03 am', $str);
}
public function testPostDisplayDate()
{
$pid = $this->factory->post->create();
$post = Timber::get_post($pid);
$twig = 'I am from {{post.date}}';
$str = Timber::compile_string($twig, [
'post' => $post,
]);
$this->assertEquals('I am from ' . date(get_option('date_format')), $str);
}
public function testPostDisplayDateTimezoneDifference()
{
// Switch timezone.
update_option('timezone_string', 'America/Los_Angeles');
$date_format = DATE_ATOM;
$timezone = new DateTimeZone('Australia/Sydney');
$pid = $this->factory->post->create([
'post_date' => '2016-07-07 02:03:00',
]);
$post = Timber::get_post($pid);
$twig = "{{ post.date(date_format) }}";
$str = Timber::compile_string($twig, [
'post' => $post,
'date_format' => $date_format,
'timezone' => $timezone,
]);
$this->assertEquals('2016-07-07T02:03:00-07:00', $str);
}
public function testPostDate()
{
$pid = $this->factory->post->create();
$post = Timber::get_post($pid);
$twig = 'I am from {{post.post_date}}';
$str = Timber::compile_string($twig, [
'post' => $post,
]);
$this->assertEquals('I am from ' . $post->post_date, $str);
}
public function testPostDateWithDateFilter()
{
$pid = $this->factory->post->create();
$post = Timber::get_post($pid);
$twig = 'I am from {{post.post_date|date}}';
$str = Timber::compile_string($twig, [
'post' => $post,
]);
$this->assertEquals('I am from ' . date('F j, Y'), $str);
}
public function testPostDateFunctionWithDateFilter()
{
$post_id = $this->factory->post->create([
'post_date' => '2016-07-07 02:03:00',
]);
$post = Timber::get_post($post_id);
$template = "{{ post.date|date('j. F Y') }}";
$result = Timber::compile_string($template, [
'post' => $post,
]);
$this->assertEquals($post->date('j. F Y'), $result);
}
public function testModifiedDate()
{
$date = date('F j, Y @ g:i a');
$pid = $this->factory->post->create();
$post = Timber::get_post($pid);
$twig = "I was modified {{ post.modified_date('F j, Y @ g:i a') }}";
$str = Timber::compile_string($twig, [
'post' => $post,
]);
$this->assertEquals('I was modified ' . $date, $str);
}
public function testModifiedDateFilter()
{
$pid = $this->factory->post->create();
$post = Timber::get_post($pid);
add_filter('get_the_modified_date', function ($the_date) {
return 'foobar';
});
$twig = "I was modified {{post.modified_date('F j, Y @ g:i a')}}";
$str = Timber::compile_string($twig, [
'post' => $post,
]);
$this->assertEquals('I was modified foobar', $str);
}
public function testModifiedTime()
{
$date = date('F j, Y @ g:i a');
$pid = $this->factory->post->create();
$post = Timber::get_post($pid);
$twig = "I was modified {{post.modified_time('F j, Y @ g:i a')}}";
$str = Timber::compile_string($twig, [
'post' => $post,
]);
$this->assertEquals('I was modified ' . $date, $str);
}
public function testInternationalTime()
{
$date = new DateTime('2015-09-28 05:00:00', new DateTimeZone('Europe/Amsterdam'));
$twig = "{{'" . $date->format('g:i') . "'|date('g:i')}}";
$str = Timber::compile_string($twig);
$this->assertEquals('5:00', $str);
}
public function testModifiedTimeFilter()
{
$pid = $this->factory->post->create();
$post = Timber::get_post($pid);
add_filter('get_the_modified_time', function ($the_date) {
return 'foobar';
});
$twig = "I was modified {{post.modified_time('F j, Y @ g:i a')}}";
$str = Timber::compile_string($twig, [
'post' => $post,
]);
$this->assertEquals('I was modified foobar', $str);
}
public function testACFDate()
{
$twig = "Thing is on {{'20150928'|date('M j, Y')}}";
$str = Timber::compile_string($twig);
$this->assertEquals('Thing is on Sep 28, 2015', $str);
}
public function testUnixDate()
{
$twig = "Thing is on {{'1446127859'|date('M j, Y')}}";
$str = Timber::compile_string($twig);
$this->assertEquals('Thing is on Oct 29, 2015', $str);
}
public function testUnixDateEdgeCase()
{
$twig = "Thing is on {{'1457395200'|date('M j, Y')}}";
$str = Timber::compile_string($twig);
$this->assertEquals('Thing is on Mar 8, 2016', $str);
}
public function testEightDigitsString()
{
$twig = "Thing is on {{'20160505'|date('M j, Y')}}";
$str = Timber::compile_string($twig);
$this->assertEquals('Thing is on May 5, 2016', $str);
}
public function testEightDigits()
{
$twig = "Thing is on {{20160505|date('M j, Y')}}";
$str = Timber::compile_string($twig);
$this->assertEquals('Thing is on May 5, 2016', $str);
}
public function testSeventiesDates()
{
$twig = "Nixon was re-elected on {{'89942400'|date('M j, Y')}}, long may he reign!";
$str = Timber::compile_string($twig);
$this->assertEquals('Nixon was re-elected on Nov 7, 1972, long may he reign!', $str);
}
public function testDateNowFunctionTwig()
{
$twig = "{{ date('now')|date('F j, Y @ g:i a') }}";
$str = Timber::compile_string($twig);
$this->assertEquals(wp_date('F j, Y @ g:i a'), $str);
}
public function testTimeAgoFutureTranslated()
{
$this->switch_to_locale('de_DE');
$str = DateTimeHelper::time_ago('2016-12-01 20:00:00', '2016-11-30, 20:00:00');
$this->assertEquals('1 Tag ab jetzt', $str);
restore_current_locale();
}
public function testTimeAgoPastTranslated()
{
$this->switch_to_locale('de_DE');
$str = DateTimeHelper::time_ago('2016-11-29 20:00:00', '2016-11-30, 20:00:00');
$this->assertEquals('vor 1 Tag', $str);
restore_current_locale();
}
public function testPostDateWithFilter()
{
$pid = $this->factory->post->create();
$post = Timber::get_post($pid);
$twig = 'I am from {{post.post_date|date}}';
$str = Timber::compile_string($twig, [
'post' => $post,
]);
$this->assertEquals('I am from ' . date('F j, Y'), $str);
}
}