-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathRelTest.php
218 lines (189 loc) · 9.21 KB
/
RelTest.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
<?php
/**
* Tests of the parsing methods within mf2\Parser
*/
namespace Mf2\Parser\Test;
use Mf2;
use Mf2\Parser;
use Yoast\PHPUnitPolyfills\TestCases\TestCase;
class RelTest extends TestCase {
public function testRelValueOnLinkTag() {
$input = '<link rel="webmention" href="https://example.com/webmention">';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertArrayHasKey('webmention', $output['rels']);
$this->assertEquals('https://example.com/webmention', $output['rels']['webmention'][0]);
}
public function testRelValueOnATag() {
$input = '<a rel="webmention" href="https://example.com/webmention">webmention me</a>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertArrayHasKey('webmention', $output['rels']);
$this->assertEquals('https://example.com/webmention', $output['rels']['webmention'][0]);
}
public function testRelValueOnAreaTag() {
$input = '<map><area rel="webmention" href="https://example.com/webmention"/></map>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertArrayHasKey('webmention', $output['rels']);
$this->assertEquals('https://example.com/webmention', $output['rels']['webmention'][0]);
}
public function testRelValueOrder() {
$input = '<map><area rel="webmention" href="https://example.com/area"/></map>
<a rel="webmention" href="https://example.com/a">webmention me</a>
<link rel="webmention" href="https://example.com/link">';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertArrayHasKey('webmention', $output['rels']);
$this->assertEquals('https://example.com/area', $output['rels']['webmention'][0]);
$this->assertEquals('https://example.com/a', $output['rels']['webmention'][1]);
$this->assertEquals('https://example.com/link', $output['rels']['webmention'][2]);
}
public function testRelValueOrder2() {
$input = '<map><area rel="webmention" href="https://example.com/area"/></map>
<link rel="webmention" href="https://example.com/link">
<a rel="webmention" href="https://example.com/a">webmention me</a>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertArrayHasKey('webmention', $output['rels']);
$this->assertEquals('https://example.com/area', $output['rels']['webmention'][0]);
$this->assertEquals('https://example.com/link', $output['rels']['webmention'][1]);
$this->assertEquals('https://example.com/a', $output['rels']['webmention'][2]);
}
public function testRelValueOrder3() {
$input = '<html>
<head>
<link rel="webmention" href="https://example.com/link">
</head>
<body>
<a rel="webmention" href="https://example.com/a">webmention me</a>
<map><area rel="webmention" href="https://example.com/area"/></map>
</body>
</html>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertArrayHasKey('webmention', $output['rels']);
$this->assertEquals('https://example.com/link', $output['rels']['webmention'][0]);
$this->assertEquals('https://example.com/a', $output['rels']['webmention'][1]);
$this->assertEquals('https://example.com/area', $output['rels']['webmention'][2]);
}
public function testRelValueOnBTag() {
$input = '<b rel="webmention" href="https://example.com/webmention">this makes no sense</b>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertArrayNotHasKey('webmention', $output['rels']);
}
public function testEnableAlternatesFlagTrue() {
$input = '<a rel="author" href="https://example.com/a">author a</a>
<a rel="author" href="https://example.com/b">author b</a>
<a rel="in-reply-to" href="https://example.com/1">post 1</a>
<a rel="in-reply-to" href="https://example.com/2">post 2</a>
<a rel="alternate home"
href="https://example.com/fr"
media="handheld"
hreflang="fr">French mobile homepage</a>';
$parser = new Parser($input);
$parser->enableAlternates = true;
$output = $parser->parse();
$this->assertArrayHasKey('alternates', $output);
}
public function testEnableAlternatesFlagFalse() {
$input = '<a rel="author" href="https://example.com/a">author a</a>
<a rel="author" href="https://example.com/b">author b</a>
<a rel="in-reply-to" href="https://example.com/1">post 1</a>
<a rel="in-reply-to" href="https://example.com/2">post 2</a>
<a rel="alternate home"
href="https://example.com/fr"
media="handheld"
hreflang="fr">French mobile homepage</a>';
$parser = new Parser($input);
$parser->enableAlternates = false;
$output = $parser->parse();
$this->assertArrayNotHasKey('alternates', $output);
}
/**
* @see https://github.com/indieweb/php-mf2/issues/112
* @see https://microformats.org/wiki/microformats2-parsing#rel_parse_examples
*/
public function testRelURLs() {
$input = '<a rel="author" href="https://example.com/a">author a</a>
<a rel="author" href="https://example.com/b">author b</a>
<a rel="in-reply-to" href="https://example.com/1">post 1</a>
<a rel="in-reply-to" href="https://example.com/2">post 2</a>
<a rel="alternate home"
href="https://example.com/fr"
media="handheld"
hreflang="fr">French mobile homepage</a>
<link rel="alternate" type="application/atom+xml" href="https://example.com/articles.atom" title="Atom Feed" />';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertArrayHasKey('rels', $output);
$this->assertCount(4, $output['rels']);
$this->assertArrayHasKey('author', $output['rels']);
$this->assertArrayHasKey('in-reply-to', $output['rels']);
$this->assertArrayHasKey('alternate', $output['rels']);
$this->assertArrayHasKey('home', $output['rels']);
$this->assertArrayHasKey('rel-urls', $output);
$this->assertCount(6, $output['rel-urls']);
$this->assertArrayHasKey('https://example.com/a', $output['rel-urls']);
$this->assertArrayHasKey('https://example.com/b', $output['rel-urls']);
$this->assertArrayHasKey('https://example.com/1', $output['rel-urls']);
$this->assertArrayHasKey('https://example.com/2', $output['rel-urls']);
$this->assertArrayHasKey('https://example.com/fr', $output['rel-urls']);
$this->assertArrayHasKey('https://example.com/articles.atom', $output['rel-urls']);
$this->assertArrayHasKey('rels', $output['rel-urls']['https://example.com/a']);
$this->assertArrayHasKey('text', $output['rel-urls']['https://example.com/a']);
$this->assertArrayHasKey('rels', $output['rel-urls']['https://example.com/b']);
$this->assertArrayHasKey('text', $output['rel-urls']['https://example.com/b']);
$this->assertArrayHasKey('rels', $output['rel-urls']['https://example.com/1']);
$this->assertArrayHasKey('text', $output['rel-urls']['https://example.com/1']);
$this->assertArrayHasKey('rels', $output['rel-urls']['https://example.com/2']);
$this->assertArrayHasKey('text', $output['rel-urls']['https://example.com/2']);
$this->assertArrayHasKey('rels', $output['rel-urls']['https://example.com/fr']);
$this->assertArrayHasKey('text', $output['rel-urls']['https://example.com/fr']);
$this->assertArrayHasKey('media', $output['rel-urls']['https://example.com/fr']);
$this->assertArrayHasKey('hreflang', $output['rel-urls']['https://example.com/fr']);
$this->assertArrayHasKey('title', $output['rel-urls']['https://example.com/articles.atom']);
$this->assertArrayHasKey('type', $output['rel-urls']['https://example.com/articles.atom']);
$this->assertArrayHasKey('rels', $output['rel-urls']['https://example.com/articles.atom']);
}
/**
* @see https://github.com/microformats/microformats2-parsing/issues/29
* @see https://github.com/microformats/microformats2-parsing/issues/30
*/
public function testRelURLsRelsUniqueAndSorted() {
$input = '<a href="#" rel="me bookmark"></a>
<a href="#" rel="bookmark archived"></a>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertEquals($output['rel-urls']['#']['rels'], array('archived', 'bookmark', 'me'));
}
public function testRelURLsInfoMergesCorrectly() {
$input = '<a href="#" rel="a">This nodeValue</a>
<a href="#" rel="a" hreflang="en">Not this nodeValue</a>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertEquals($output['rel-urls']['#']['hreflang'], 'en');
$this->assertArrayNotHasKey('media', $output['rel-urls']['#']);
$this->assertArrayNotHasKey('title', $output['rel-urls']['#']);
$this->assertArrayNotHasKey('type', $output['rel-urls']['#']);
$this->assertEquals($output['rel-urls']['#']['text'], 'This nodeValue');
}
public function testRelURLsNoDuplicates() {
$input = '<a href="#a" rel="a"></a>
<a href="#b" rel="a"></a>
<a href="#a" rel="a"></a>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertEquals($output['rels']['a'], array('#a', '#b'));
}
public function testRelURLsFalsyTextVSEmpty() {
$input = '<a href="#a" rel="a">0</a>
<a href="#b" rel="b"></a>';
$parser = new Parser($input);
$output = $parser->parse();
$this->assertArrayHasKey('text', $output['rel-urls']['#a']);
$this->assertEquals($output['rel-urls']['#a']['text'], '0');
$this->assertArrayNotHasKey('text', $output['rel-urls']['#b']);
}
}