-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathParseDTTest.php
553 lines (467 loc) · 18.9 KB
/
ParseDTTest.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
<?php
/**
* Tests of the parsing methods within mf2\Parser
*/
namespace Mf2\Parser\Test;
use Mf2\Parser;
use Yoast\PHPUnitPolyfills\TestCases\TestCase;
class ParseDTTest extends TestCase {
protected function set_up() {
date_default_timezone_set('Europe/London');
}
// Note that value-class tests for dt-* attributes are stored elsewhere, as there are so many of the bloody things
/**
* @group parseDT
*/
public function testParseDTHandlesImg() {
$input = '<div class="h-event"><img class="dt-start" alt="2012-08-05T14:50"></div>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertArrayHasKey('start', $output['items'][0]['properties']);
$this->assertEquals('2012-08-05T14:50', $output['items'][0]['properties']['start'][0]);
}
/**
* @group parseDT
*/
public function testParseDTHandlesDataValueAttr() {
$input = '<div class="h-event"><data class="dt-start" value="2012-08-05T14:50"></div>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertArrayHasKey('start', $output['items'][0]['properties']);
$this->assertEquals('2012-08-05T14:50', $output['items'][0]['properties']['start'][0]);
}
/**
* @group parseDT
*/
public function testParseDTHandlesDataInnerHTML() {
$input = '<div class="h-event"><data class="dt-start">2012-08-05T14:50</data></div>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertArrayHasKey('start', $output['items'][0]['properties']);
$this->assertEquals('2012-08-05T14:50', $output['items'][0]['properties']['start'][0]);
}
/**
* @group parseDT
*/
public function testParseDTHandlesAbbrValueAttr() {
$input = '<div class="h-event"><abbr class="dt-start" title="2012-08-05T14:50"></div>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertArrayHasKey('start', $output['items'][0]['properties']);
$this->assertEquals('2012-08-05T14:50', $output['items'][0]['properties']['start'][0]);
}
/**
* @group parseDT
*/
public function testParseDTHandlesAbbrInnerHTML() {
$input = '<div class="h-event"><abbr class="dt-start">2012-08-05T14:50</abbr></div>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertArrayHasKey('start', $output['items'][0]['properties']);
$this->assertEquals('2012-08-05T14:50', $output['items'][0]['properties']['start'][0]);
}
/**
* @group parseDT
*/
public function testParseDTHandlesTimeDatetimeAttr() {
$input = '<div class="h-event"><time class="dt-start" datetime="2012-08-05T14:50"></div>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertArrayHasKey('start', $output['items'][0]['properties']);
$this->assertEquals('2012-08-05T14:50', $output['items'][0]['properties']['start'][0]);
}
/**
* @group parseDT
*/
public function testParseDTHandlesTimeDatetimeAttrWithZ() {
$input = '<div class="h-event"><time class="dt-start" datetime="2012-08-05T14:50:00Z"></div>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertArrayHasKey('start', $output['items'][0]['properties']);
$this->assertEquals('2012-08-05T14:50:00Z', $output['items'][0]['properties']['start'][0]);
}
/**
* @group parseDT
*/
public function testParseDTHandlesTimeDatetimeAttrWithTZOffset() {
$input = '<div class="h-event"><time class="dt-start" datetime="2012-08-05T14:50:00-0700"></div>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertArrayHasKey('start', $output['items'][0]['properties']);
$this->assertEquals('2012-08-05T14:50:00-0700', $output['items'][0]['properties']['start'][0]);
}
/**
* @group parseDT
*/
public function testParseDTHandlesTimeDatetimeAttrWithTZOffset2() {
$input = '<div class="h-event"><time class="dt-start" datetime="2012-08-05T14:50:00-07:00"></div>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertArrayHasKey('start', $output['items'][0]['properties']);
$this->assertEquals('2012-08-05T14:50:00-07:00', $output['items'][0]['properties']['start'][0]);
}
/**
* @group parseDT
*/
public function testParseDTHandlesTimeInnerHTML() {
$input = '<div class="h-event"><time class="dt-start">2012-08-05T14:50</time></div>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertArrayHasKey('start', $output['items'][0]['properties']);
$this->assertEquals('2012-08-05T14:50', $output['items'][0]['properties']['start'][0]);
}
/**
* @group parseDT
*/
public function testParseDTHandlesInsDelDatetime() {
$input = '<div class="h-event"><ins class="dt-start" datetime="2012-08-05T14:50"></ins><del class="dt-end" datetime="2012-08-05T18:00"></del></div>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertArrayHasKey('start', $output['items'][0]['properties']);
$this->assertArrayHasKey('end', $output['items'][0]['properties']);
$this->assertEquals('2012-08-05T14:50', $output['items'][0]['properties']['start'][0]);
$this->assertEquals('2012-08-05T18:00', $output['items'][0]['properties']['end'][0]);
}
/**
* @group parseDT
* @group valueClass
*/
public function testYYYY_MM_DD__HH_MM() {
$input = '<div class="h-event"><span class="dt-start"><span class="value">2012-10-07</span> at <span class="value">21:18</span></span></div>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertArrayHasKey('start', $output['items'][0]['properties']);
$this->assertEquals('2012-10-07 21:18', $output['items'][0]['properties']['start'][0]);
}
/**
* @group parseDT
* @group valueClass
*/
public function testAbbrYYYY_MM_DD__HH_MM() {
$input = '<div class="h-event"><span class="dt-start"><abbr class="value" title="2012-10-07">some day</abbr> at <span class="value">21:18</span></span></div>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertArrayHasKey('start', $output['items'][0]['properties']);
$this->assertEquals('2012-10-07 21:18', $output['items'][0]['properties']['start'][0]);
}
/**
* @group parseDT
* @group valueClass
*/
public function testYYYY_MM_DD__HHpm() {
$input = '<div class="h-event"><span class="dt-start"><span class="value">2012-10-07</span> at <span class="value">9pm</span></span></div>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertArrayHasKey('start', $output['items'][0]['properties']);
$this->assertEquals('2012-10-07 21:00', $output['items'][0]['properties']['start'][0]);
}
/**
* @group parseDT
* @group valueClass
*/
public function testYYYY_MM_DD__HH_MMpm() {
$input = '<div class="h-event"><span class="dt-start"><span class="value">2012-10-07</span> at <span class="value">9:00pm</span></span></div>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertArrayHasKey('start', $output['items'][0]['properties']);
$this->assertEquals('2012-10-07 21:00', $output['items'][0]['properties']['start'][0]);
}
/**
* @group parseDT
* @group valueClass
*/
public function testYYYY_MM_DD__HH_MM_SSpm() {
$input = '<div class="h-event"><span class="dt-start"><span class="value">2012-10-07</span> at <span class="value">9:00:00pm</span></span></div>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertArrayHasKey('start', $output['items'][0]['properties']);
$this->assertEquals('2012-10-07 21:00:00', $output['items'][0]['properties']['start'][0]);
}
/**
* This test name refers to the value-class used within the dt-end.
* @group parseDT
* @group valueClass
*/
public function testImpliedDTEndWithValueClass() {
$input = '<div class="h-event"> <span class="dt-start"><span class="value">2014-06-04</span> at <span class="value">18:30</span> <span class="dt-end"><span class="value">19:30</span></span></span> </div>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertArrayHasKey('start', $output['items'][0]['properties']);
$this->assertArrayHasKey('end', $output['items'][0]['properties']);
$this->assertEquals('2014-06-04 18:30', $output['items'][0]['properties']['start'][0]);
$this->assertEquals('2014-06-04 19:30', $output['items'][0]['properties']['end'][0]);
}
/**
* This test name refers to the lack of value-class within the dt-end.
* @group parseDT
* @group valueClass
*/
public function testImpliedDTEndWithoutValueClass() {
$input = '<div class="h-event"> <span class="dt-start"><span class="value">2014-06-05</span> at <span class="value">18:31</span> <span class="dt-end">19:31</span></span> </div>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertArrayHasKey('start', $output['items'][0]['properties']);
$this->assertArrayHasKey('end', $output['items'][0]['properties']);
$this->assertEquals('2014-06-05 18:31', $output['items'][0]['properties']['start'][0]);
$this->assertEquals('2014-06-05 19:31', $output['items'][0]['properties']['end'][0]);
}
/**
* @see https://github.com/indieweb/php-mf2/pull/46
* @group parseDT
* @group valueClass
*/
public function testImpliedDTEndUsingNonValueClassDTStart() {
$input = '<div class="h-event"> <time class="dt-start">2014-06-05T18:31</time> until <span class="dt-end">19:31</span></span> </div>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertArrayHasKey('start', $output['items'][0]['properties']);
$this->assertArrayHasKey('end', $output['items'][0]['properties']);
$this->assertEquals('2014-06-05T18:31', $output['items'][0]['properties']['start'][0]);
$this->assertEquals('2014-06-05 19:31', $output['items'][0]['properties']['end'][0]);
}
/**
* @group parseDT
* @group valueClass
*/
public function testDTStartOnly() {
$input = '<div class="h-event"> <span class="dt-start"><span class="value">2014-06-06</span> at <span class="value">18:32</span> </span> </div>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertArrayHasKey('start', $output['items'][0]['properties']);
$this->assertEquals('2014-06-06 18:32', $output['items'][0]['properties']['start'][0]);
$this->assertArrayNotHasKey('end', $output['items'][0]['properties']);
}
/**
* @group parseDT
* @group valueClass
*/
public function testDTStartDateOnly() {
$input = '<div class="h-event"> <span class="dt-start"><span class="value">2014-06-07</span> </span> </div>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertArrayHasKey('start', $output['items'][0]['properties']);
$this->assertEquals('2014-06-07', $output['items'][0]['properties']['start'][0]);
}
/**
* TZ offsets normalized only for VCP.
* This behavior is implied from "However the colons ":" separating the hours and minutes of any timezone offset are optional and discouraged in order to make it less likely that a timezone offset will be confused for a time."
* @see https://microformats.org/wiki/index.php?title=value-class-pattern&oldid=66473##However+the+colons
*/
public function testNormalizeTZOffsetVCP() {
$input = '<div class="h-event">
<span class="dt-start"> <time class="value" datetime="2017-05-27">May 27</time>, from
<time class="value">20:57-07:00</time> </span>
</div>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertEquals('2017-05-27 20:57-0700', $output['items'][0]['properties']['start'][0]);
}
/**
* TZ offsets *not* normalized for non-VCP dates
*/
public function testNoNormalizeTZOffset() {
$input = '<div class="h-entry"> <time class="dt-start" datetime="2018-03-13 15:30-07:00">March 13, 2018 3:30PM</time> </div>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertEquals('2018-03-13 15:30-07:00', $output['items'][0]['properties']['start'][0]);
}
/**
* @see https://github.com/indieweb/php-mf2/issues/115
*/
public function testDoNotAddT() {
$input = '<div class="h-entry">
<span class="dt-start">
<time class="value" datetime="2009-06-26">26 July</time>, from
<time class="value">19:00:00-08:00</time>
</span>
</div>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertEquals('2009-06-26 19:00:00-0800', $output['items'][0]['properties']['start'][0]);
}
/**
* @see https://github.com/indieweb/php-mf2/issues/115
*/
public function testPreserrveTIfAuthored() {
$input = '<div class="h-entry"> <time class="dt-start" datetime="2009-06-26T19:01-08:00">26 July, 7:01pm</time> </div>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertEquals('2009-06-26T19:01-08:00', $output['items'][0]['properties']['start'][0]);
}
/**
* @see https://github.com/indieweb/php-mf2/issues/126
*/
public function testDtVCPTimezone() {
$input = '<div class="h-event">
<span class="e-summary">HomebrewWebsiteClub Berlin</span> will be next on
<span class="dt-start">
<span class="value">2017-05-31</span>, from
<span class="value">19:00</span> (UTC<span class="value">+02:00</span>)
</span> to <span class="dt-end">21:00</span>.</div>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertEquals('2017-05-31 19:00+0200', $output['items'][0]['properties']['start'][0]);
$this->assertEquals('2017-05-31 21:00+0200', $output['items'][0]['properties']['end'][0]);
}
/**
* @see https://github.com/indieweb/php-mf2/issues/126
*/
public function testDtVCPTimezoneShort() {
$input = '<div class="h-event">
<span class="e-summary">HomebrewWebsiteClub Berlin</span> will be next on
<span class="dt-start">
<span class="value">2017-05-31</span>, from
<span class="value">19:00</span> (UTC<span class="value">+2</span>)
</span> to <span class="dt-end">21:00</span>.</div>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertEquals('2017-05-31 19:00+0200', $output['items'][0]['properties']['start'][0]);
$this->assertEquals('2017-05-31 21:00+0200', $output['items'][0]['properties']['end'][0]);
}
/**
* @see https://github.com/indieweb/php-mf2/issues/126
*/
public function testDtVCPTimezoneNoLeadingZero() {
$input = '<div class="h-event">
<span class="dt-start">
<span class="value">2017-06-17</span>
<span class="value">22:00-700</span>
</span>
<span class="dt-end">
<span class="value">2017-06-17</span>
<span class="value">23:00-700</span>
</span>
</div>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertEquals('2017-06-17 22:00-0700', $output['items'][0]['properties']['start'][0]);
$this->assertEquals('2017-06-17 23:00-0700', $output['items'][0]['properties']['end'][0]);
}
/**
* @see https://github.com/microformats/microformats2-parsing/issues/4
*/
public function testImplyTimezoneFromStart() {
$input = '<div class="h-event"> <time class="dt-start" datetime="2014-09-11 13:30-0700">13:30</time> to <time class="dt-end">15:30</time> </div>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertEquals('2014-09-11 13:30-0700', $output['items'][0]['properties']['start'][0]);
$this->assertEquals('2014-09-11 15:30-0700', $output['items'][0]['properties']['end'][0]);
}
/**
* @see https://github.com/microformats/microformats2-parsing/issues/4
*/
public function testImplyTimezoneFromEnd() {
$input = '<div class="h-event"> <time class="dt-start" datetime="2014-09-11 13:30">13:30</time> to <time class="dt-end">15:30-0700</time> </div>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertEquals('2014-09-11 13:30-0700', $output['items'][0]['properties']['start'][0]);
$this->assertEquals('2014-09-11 15:30-0700', $output['items'][0]['properties']['end'][0]);
}
/**
*
*/
public function testAMPMWithPeriods() {
$input = '<div class="h-event">
<span class="dt-start">
<span class="value">2017-06-11</span>
<span class="value">10:00P.M.</span>
</span>
<span class="dt-end">
<span class="value">2017-06-12</span>
<span class="value">02:00a.m.</span>
</span>
</div>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertEquals('2017-06-11 22:00', $output['items'][0]['properties']['start'][0]);
$this->assertEquals('2017-06-12 02:00', $output['items'][0]['properties']['end'][0]);
}
/**
*
*/
public function testAMPMWithoutPeriods() {
$input = '<div class="h-event">
<span class="dt-start">
<span class="value">2017-06-17</span>
<span class="value">10:30pm</span>
</span>
<span class="dt-end">
<span class="value">2017-06-18</span>
<span class="value">02:30AM</span>
</span>
</div>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertEquals('2017-06-17 22:30', $output['items'][0]['properties']['start'][0]);
$this->assertEquals('2017-06-18 02:30', $output['items'][0]['properties']['end'][0]);
}
/**
*
*/
public function testDtVCPTimeAndTimezone() {
$input = '<div class="h-event">
<span class="dt-start">
<span class="value">2017-06-17</span>
<span class="value">13:30-07:00</span>
</span>
<span class="dt-end">
<span class="value">2017-06-17</span>
<span class="value">15:30-0700</span>
</span>
</div>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertEquals('2017-06-17 13:30-0700', $output['items'][0]['properties']['start'][0]);
$this->assertEquals('2017-06-17 15:30-0700', $output['items'][0]['properties']['end'][0]);
}
/**
* @see https://github.com/indieweb/php-mf2/issues/147
*/
public function testDtVCPMultipleDatesAndTimezones() {
$input = '<div class="h-event">
<h1 class="p-name">Multiple date and time values</h1>
<p> When:
<span class="dt-start">
<span class="value" title="June 1, 2014">2014-06-01</span>
<span class="value" title="June 1, 3014">3014-06-01</span>
<span class="value" title="12:30">12:30</span>
(UTC<span class="value">-06:00</span>)
<span class="value" title="23:00">23:00</span>
(UTC<span class="value">+01:00</span>)
–
<span class="dt-end">19:30</span>
</p>
</div>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertEquals('2014-06-01 12:30-0600', $output['items'][0]['properties']['start'][0]);
$this->assertEquals('2014-06-01 19:30-0600', $output['items'][0]['properties']['end'][0]);
}
/**
* @see https://github.com/indieweb/php-mf2/issues/149
*/
public function testDtWithoutYear() {
$input = '<div class="h-card"> <time class="dt-bday" datetime="--12-28"></time> </div>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertEquals('--12-28', $output['items'][0]['properties']['bday'][0]);
}
/**
* @see https://github.com/indieweb/php-mf2/issues/167
* @see https://github.com/microformats/mf2py/blob/master/test/examples/datetimes.html
*/
public function testNormalizeOrdinalDate() {
$input = '<div class="h-event">
<h1 class="p-name">Ordinal date</h1>
<p> When:
<span class="dt-start">
<span class="value">2016-062</span>
<span class="value">12:30AM</span>
(UTC<span class="value">-06:00</span>)
</p>
</div>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertEquals('2016-03-02 12:30-0600', $output['items'][0]['properties']['start'][0]);
}
}