-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathCombinedMicroformatsTest.php
More file actions
440 lines (394 loc) · 13.9 KB
/
CombinedMicroformatsTest.php
File metadata and controls
440 lines (394 loc) · 13.9 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
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
<?php
namespace Mf2\Parser\Test;
use Mf2\Parser;
use Mf2;
use Yoast\PHPUnitPolyfills\TestCases\TestCase;
use Yoast\PHPUnitPolyfills\Polyfills\AssertIsType;
/**
* Combined Microformats Test
*
* Tests the ability of Parser::parse() to handle nested microformats correctly.
* More info at https://microformats.org/wiki/microformats-2#combining_microformats
*
* @todo implement
*/
class CombinedMicroformatsTest extends TestCase {
use AssertIsType;
// phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
protected function set_up() {
date_default_timezone_set('Europe/London');
}
/**
* From https://microformats.org/wiki/microformats2#combining_microformats
*/
public function testHEventLocationHCard() {
// Note: The venue for IWC 2012 no longer exists so we switched
// this to Powell's since it's a long-term Portland institution
$input = '<div class="h-event">
<a class="p-name u-url" href="https://indieweb.org/2012">
IndieWebCamp 2012
</a>
from <time class="dt-start">2012-06-30</time>
to <time class="dt-end">2012-07-01</time> at
<span class="p-location h-card">
<a class="p-name p-org u-url" href="https://www.powells.com/">
Powell’s</a>, <span class="p-street-address">1005 W Burnside St.</span>, <span class="p-locality">Portland</span>, <abbr class="p-region" title="Oregon">OR</abbr>
</span>
</div>';
$expected = '{
"rels": {},
"rel-urls": {},
"items": [{
"type": ["h-event"],
"properties": {
"name": ["IndieWebCamp 2012"],
"url": ["https://indieweb.org/2012"],
"start": ["2012-06-30"],
"end": ["2012-07-01"],
"location": [{
"value": "Powell’s",
"type": ["h-card"],
"properties": {
"name": ["Powell’s"],
"org": ["Powell’s"],
"url": ["https://www.powells.com/"],
"street-address": ["1005 W Burnside St."],
"locality": ["Portland"],
"region": ["Oregon"]
}
}]
}
}]
}';
$parser = new Parser($input, '', true);
$output = $parser->parse();
$this->assertJsonStringEqualsJsonString(json_encode($output), $expected);
}
/**
* From https://microformats.org/wiki/microformats2#combining_microformats
*/
public function testHCardOrgPOrg() {
$input = '<div class="h-card">
<a class="p-name u-url"
href="http://blog.lizardwrangler.com/"
>Mitchell Baker</a>
(<span class="p-org">Mozilla Foundation</span>)
</div>';
$expected = '{
"rels": {},
"rel-urls": {},
"items": [{
"type": ["h-card"],
"properties": {
"name": ["Mitchell Baker"],
"url": ["http://blog.lizardwrangler.com/"],
"org": ["Mozilla Foundation"]
}
}]
}';
$parser = new Parser($input, '', true);
$output = $parser->parse();
$this->assertJsonStringEqualsJsonString(json_encode($output), $expected);
}
/**
* From https://microformats.org/wiki/microformats2#combining_microformats
*/
public function testHCardOrgHCard() {
$input = '<div class="h-card">
<a class="p-name u-url"
href="http://blog.lizardwrangler.com/"
>Mitchell Baker</a>
(<a class="p-org h-card"
href="http://mozilla.org/"
>Mozilla Foundation</a>)
</div>';
$expected = '{
"rels": {},
"rel-urls": {},
"items": [{
"type": ["h-card"],
"properties": {
"name": ["Mitchell Baker"],
"url": ["http://blog.lizardwrangler.com/"],
"org": [{
"value": "Mozilla Foundation",
"type": ["h-card"],
"properties": {
"name": ["Mozilla Foundation"],
"url": ["http://mozilla.org/"]
}
}]
}
}]
}';
$parser = new Parser($input, '', true);
$output = $parser->parse();
$this->assertJsonStringEqualsJsonString(json_encode($output), $expected);
}
/**
* From https://microformats.org/wiki/microformats2#combining_microformats
*/
public function testHCardPOrgHCardHOrg() {
$input = '<div class="h-card">
<a class="p-name u-url"
href="http://blog.lizardwrangler.com/"
>Mitchell Baker</a>
(<a class="p-org h-card h-org"
href="http://mozilla.org/"
>Mozilla Foundation</a>)
</div>';
$expected = '{
"rels": {},
"rel-urls": {},
"items": [{
"type": ["h-card"],
"properties": {
"name": ["Mitchell Baker"],
"url": ["http://blog.lizardwrangler.com/"],
"org": [{
"value": "Mozilla Foundation",
"type": ["h-card", "h-org"],
"properties": {
"name": ["Mozilla Foundation"],
"url": ["http://mozilla.org/"]
}
}]
}
}]
}';
$parser = new Parser($input, '', true);
$output = $parser->parse();
$this->assertJsonStringEqualsJsonString(json_encode($output), $expected);
}
/**
* From https://microformats.org/wiki/microformats2#combining_microformats
*/
public function testHCardChildHCard() {
$input = '<div class="h-card">
<a class="p-name u-url"
href="http://blog.lizardwrangler.com/">
Mitchell Baker</a>
(<a class="h-card h-org" href="http://mozilla.org/">
Mozilla Foundation</a>)
</div>';
$expected = '{
"rels": {},
"rel-urls": {},
"items": [{
"type": ["h-card"],
"properties": {
"name": ["Mitchell Baker"],
"url": ["http://blog.lizardwrangler.com/"]
},
"children": [{
"type": ["h-card","h-org"],
"properties": {
"name": ["Mozilla Foundation"],
"url": ["http://mozilla.org/"]
}
}]
}]
}';
$parser = new Parser($input, '', true);
$output = $parser->parse();
$this->assertJsonStringEqualsJsonString(json_encode($output), $expected);
}
/**
* Regression test for https://github.com/indieweb/php-mf2/issues/42
*
* This was occurring because mfPropertyNamesFromClass was only ever returning the first property name
* rather than all of them.
*/
public function testNestedMicroformatUnderMultipleProperties() {
$input = '<article class="h-entry"><div class="p-like-of p-in-reply-to h-cite"></div></article>';
$mf = Mf2\parse($input);
$this->assertCount(1, $mf['items'][0]['properties']['like-of']);
$this->assertCount(1, $mf['items'][0]['properties']['in-reply-to']);
}
/**
* Test microformats nested under e-* property classnames retain html: key in structure
*
* @see https://github.com/indieweb/php-mf2/issues/64
*/
public function testMicroformatsNestedUnderEPropertyClassnamesRetainHtmlKey() {
$input = '<div class="h-entry"><div class="h-card e-content"><p>Hello</p></div></div>';
$mf = Mf2\parse($input);
$this->assertArrayHasKey('value', $mf['items'][0]['properties']['content'][0]);
$this->assertEquals($mf['items'][0]['properties']['content'][0]['value'], 'Hello');
$this->assertArrayHasKey('html', $mf['items'][0]['properties']['content'][0]);
$this->assertEquals($mf['items'][0]['properties']['content'][0]['html'], '<p>Hello</p>');
}
/**
* Test microformats nested under u-* property classnames derive value: key from parsing as u-*
*/
public function testMicroformatsNestedUnderUPropertyClassnamesDeriveValueCorrectly() {
$input = '<div class="h-entry"><img class="u-url h-card" alt="This should not be the value" src="This should be the value" /></div>';
$mf = Mf2\parse($input);
$this->assertEquals($mf['items'][0]['properties']['url'][0]['value'], 'This should be the value', json_encode($mf['items'][0]['properties']['url'][0]));
}
public function testMicroformatsNestedUnderUPropertyClassnamesDeriveValueFromURL() {
$input = '<div class="h-entry">
<h1 class="p-name">Name</h1>
<p class="e-content">Hello World</p>
<ul>
<li class="u-comment h-cite">
<a class="u-author h-card" href="https://jane.example.com/">Jane Bloggs</a>
<p class="p-content p-name">lol</p>
<a class="u-url" href="https://example.com/post1234"><time class="dt-published">2015-07-12 12:03</time></a>
</li>
</ul>
</div>';
$expected = '{
"items": [{
"type": ["h-entry"],
"properties": {
"name": ["Name"],
"content": [{
"html": "Hello World",
"value": "Hello World"
}],
"comment": [{
"type": ["h-cite"],
"properties": {
"author": [{
"type": ["h-card"],
"properties": {
"name": ["Jane Bloggs"],
"url": ["https:\/\/jane.example.com\/"]
},
"value": "https:\/\/jane.example.com\/"
}],
"content": ["lol"],
"name": ["lol"],
"url": ["https:\/\/example.com\/post1234"],
"published": ["2015-07-12 12:03"]
},
"value": "https:\/\/example.com\/post1234"
}]
}
}],
"rels":[],
"rel-urls": []
}';
$mf = Mf2\parse($input);
$this->assertJsonStringEqualsJsonString(json_encode($mf), $expected);
$this->assertEquals($mf['items'][0]['properties']['comment'][0]['value'], 'https://example.com/post1234');
$this->assertEquals($mf['items'][0]['properties']['comment'][0]['properties']['author'][0]['value'], 'https://jane.example.com/');
}
public function testMicroformatsNestedUnderPPropertyClassnamesDeriveValueFromFirstPName() {
$input = '<div class="h-entry"><div class="p-author h-card">This post was written by <span class="p-name">Zoe</span>.</div></div>';
$mf = Mf2\parse($input);
$this->assertEquals($mf['items'][0]['properties']['author'][0]['value'], 'Zoe');
}
/**
* @see https://github.com/indieweb/php-mf2/issues/98
* @see https://github.com/microformats/tests/issues/58
*/
public function testNoValueForNestedMicroformatWithoutProperty() {
$input = '<div class="h-card" ><a class="h-card" href="jane.html">Jane Doe</a><p></p></div>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertArrayHasKey('children', $output['items'][0]);
$this->assertArrayNotHasKey('value', $output['items'][0]['children'][0]);
}
/**
* With the backcompat changes I worked on in this PR, I ran into a case where
* nested mf1 without properties were not added to the 'children' property properly.
* I fixed that but then wanted to ensure it worked beyond 1-level deep. This example
* is contrived, but lets me test to confirm 'children' is set correctly. - Gregor Morrill
*/
public function testNestedMf1() {
$input = '<div class="hentry"> <div class="vcard"><span class="fn">Jane Doe</span> and <div class="vcard"><span class="fn">John Doe</span></div> </div> </div>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertEmpty($output['items'][0]['properties']);
$this->assertArrayHasKey('children', $output['items'][0]);
$this->assertEquals('h-card', $output['items'][0]['children'][0]['type'][0]);
$this->assertEquals('Jane Doe', $output['items'][0]['children'][0]['properties']['name'][0]);
$child_mf = $output['items'][0]['children'][0];
$this->assertArrayHasKey('children', $child_mf);
$this->assertEquals('h-card', $child_mf['children'][0]['type'][0]);
$this->assertEquals('John Doe', $child_mf['children'][0]['properties']['name'][0]);
}
public function testNoUrlFromRelOnMf2() {
$input = <<< END
<div class="h-entry">
<p> <a href="/article" rel="bookmark" class="p-name">Title of Post</a> </p>
<div class="e-content"><p> This is the post </p> </div>
</div>
END;
$parser = new Parser($input);
$output = $parser->parse();
$this->assertArrayHasKey('name', $output['items'][0]['properties']);
$this->assertArrayHasKey('content', $output['items'][0]['properties']);
$this->assertArrayNotHasKey('url', $output['items'][0]['properties']);
}
/**
* Simplified h-entry with `p-location h-adr` from https://aaronparecki.com/2018/03/14/3/
* Whitespace cleaned up for easier test assertion
* @see https://github.com/indieweb/php-mf2/issues/151
*/
public function testNestedValuePProperty() {
$input = <<< END
<div class="h-entry">
<span class="p-location h-adr">
<span class="p-locality">Portland</span>, <span class="p-region">Oregon</span> <span class="weather"><span>•</span><i class="wi wi-night-alt-cloudy" title="Mostly Cloudy"></i> 44°F</span>
<data class="p-latitude" value="45.535623"></data>
<data class="p-longitude" value="-122.621209"></data>
</span>
</div>
END;
$parser = new Parser($input);
$output = $parser->parse();
$this->assertArrayHasKey('value', $output['items'][0]['properties']['location'][0]);
$this->assertEquals('Portland, Oregon • 44°F', $output['items'][0]['properties']['location'][0]['value']);
}
/**
* @see https://github.com/indieweb/php-mf2/issues/151
*/
public function testNestedValueDTProperty() {
$input = <<< END
<div class="h-entry">
<div class="dt-acme h-acme-object">1997-12-12</div>
</div>
END;
$parser = new Parser($input);
$output = $parser->parse();
$this->assertArrayHasKey('value', $output['items'][0]['properties']['acme'][0]);
$this->assertEquals('1997-12-12', $output['items'][0]['properties']['acme'][0]['value']);
}
/**
* rel=tag should not be upgraded within microformats2
* @see https://github.com/indieweb/php-mf2/issues/157
*/
public function testMf2DoesNotParseRelTag() {
$input = '<div class="h-entry">
<a rel="tag" href="/tags/tests">Tests</a>
</div>
<div class="h-review">
<a rel="tag" href="/tags/reviews">Reviews</a>
</div>
';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertArrayNotHasKey('category', $output['items'][0]['properties']);
$this->assertArrayNotHasKey('category', $output['items'][1]['properties']);
}
/**
* JSON-mode should return an empty stdClass when a microformat has no properties.
* @see https://github.com/indieweb/php-mf2/issues/171
*/
public function testEmptyPropertiesObjectInJSONMode() {
$input = '<div class="h-feed"><div class="h-entry"></div></div>';
// Try in JSON-mode: expect the raw PHP to be an stdClass instance that serializes to an empty object.
$parser = new Parser($input, null, true);
$output = $parser->parse();
$this->assertInstanceOf('\stdClass', $output['items'][0]['properties']);
$this->assertSame('{}', json_encode($output['items'][0]['properties']));
// Repeat in non-JSON-mode: expect the raw PHP to be an array. Check that its serialization is not what we need for mf2 JSON.
$parser = new Parser($input, null, false);
$output = $parser->parse();
$this->assertIsArray($output['items'][0]['properties']);
$this->assertSame('[]', json_encode($output['items'][0]['properties']));
}
}