-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathMicroformatsTestSuiteTest.php
More file actions
206 lines (184 loc) · 7.03 KB
/
Copy pathMicroformatsTestSuiteTest.php
File metadata and controls
206 lines (184 loc) · 7.03 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
<?php
namespace Mf2\Parser\Test;
use Yoast\PHPUnitPolyfills\TestCases\TestCase;
final class TestSuiteParser extends \Mf2\Parser
{
/** Actually textContent from before the whitespace normalisation merge (e8da04f93d548d26287a8980eca4216639cbc61d) */
public function textContent(\DOMElement $el, $dummy=false) {
$excludeTags = array('noframe', 'noscript', 'script', 'style', 'frames', 'frameset');
if (isset($el->tagName) and in_array(strtolower($el->tagName), $excludeTags)) {
return '';
}
$this->_resolveChildUrls($el);
$clonedEl = $el->cloneNode(true);
foreach ($this->xpath->query('.//img', $clonedEl) as $imgEl) {
if ($imgEl->hasAttribute('alt')) {
$replacement = $imgEl->getAttribute('alt');
} else if ($imgEl->hasAttribute('src')) {
$replacement = ' ' . $imgEl->getAttribute('src') . ' ';
} else {
$replacement = ''; // Bye bye IMG element.
}
$newNode = $this->doc->createTextNode($replacement);
$imgEl->parentNode->replaceChild($newNode, $imgEl);
}
foreach ($excludeTags as $tagName) {
foreach ($this->xpath->query(".//{$tagName}", $clonedEl) as $elToRemove) {
$elToRemove->parentNode->removeChild($elToRemove);
}
}
return \Mf2\unicodeTrim($clonedEl->textContent);
}
// Hack. Old textContent requires "resolveChildUrls", but that method is private.
private $__resolveChildUrls = null;
private function _resolveChildUrls(\DOMElement $element) {
if (null === $this->__resolveChildUrls) {
$reflectUpon = new \ReflectionClass($this);
$this->__resolveChildUrls = $reflectUpon->getMethod('resolveChildUrls');
$this->__resolveChildUrls->setAccessible(true);
}
return $this->__resolveChildUrls->invoke($this, $element);
}
}
class MicroformatsTestSuiteTest extends TestCase
{
const MF1_KNOWN_FAILURES = [
"hreview/vcard",
"hentry/summarycontent",
"hfeed/simple",
"hnews/all",
"hnews/minimum",
"hcalendar/time",
"hresume/work",
"hresume/education",
"hresume/affiliation",
"includes/heventitemref",
"includes/hyperlink",
"includes/table",
"includes/hcarditemref",
"includes/object",
"hcard/single",
"hcard/name",
"hcard/multiple",
];
/**
* @dataProvider mf1TestsProvider
* @group microformats/tests/mf1
*/
public function testMf1FromTestSuite($input, $expectedOutput, $name, $knownFailure)
{
if ($knownFailure) {
$this->markTestSkipped('Not yet implemented');
}
$parser = new TestSuiteParser($input, 'http://example.com/');
$this->assertEquals(
$this->makeComparible(json_decode($expectedOutput, true)),
$this->makeComparible(json_decode(json_encode($parser->parse()), true))
);
}
/**
* @dataProvider mf2TestsProvider
* @group microformats/tests/mf2
*/
public function testMf2FromTestSuite($input, $expectedOutput, $test)
{
if ($test === 'h-event/time') {
$this->markTestIncomplete('This test does not match because we implement a proposed spec: https://github.com/microformats/microformats2-parsing/issues/4#issuecomment-373457720.');
}
$parser = new TestSuiteParser($input, 'http://example.com/');
$this->assertEquals(
$this->makeComparible(json_decode($expectedOutput, true)),
$this->makeComparible(json_decode(json_encode($parser->parse()), true))
);
}
/**
* @dataProvider mixedTestsProvider
* @group microformats/tests/mixed
*/
public function testMixedFromTestSuite($input, $expectedOutput)
{
$parser = new TestSuiteParser($input, 'http://example.com/');
$this->assertEquals(
$this->makeComparible(json_decode($expectedOutput, true)),
$this->makeComparible(json_decode(json_encode($parser->parse()), true))
);
}
/**
* To make arrays coming from JSON more easily comparible by PHPUnit:
* * We sort arrays by key, normalising them, because JSON objects are unordered.
* * We json_encode strings, and cut the starting and ending ", so PHPUnit better
* shows whitespace characters like tabs and newlines.
**/
public function makeComparible($array)
{
ksort($array);
foreach ($array as $key => $value) {
if (gettype($value) === 'array') {
$array[$key] = $this->makeComparible($value);
} else if (gettype($value) === 'string') {
$array[$key] = substr(json_encode($value), 1, -1);
}
}
return $array;
}
/**
* Data provider lists all tests from a specific directory in mf2/tests.
**/
public function htmlAndJsonProvider($subFolder = '')
{
// Get the actual wanted subfolder.
$subFolder = '/mf2/tests/tests' . $subFolder;
// Ripped out of the test-suite.php code:
$finder = new \RegexIterator(
new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(
dirname(__FILE__) . '/../../vendor/' . $subFolder,
\RecursiveDirectoryIterator::SKIP_DOTS
)),
'/^.+\.html$/i',
\RecursiveRegexIterator::GET_MATCH
);
// Build the array of separate tests:
$tests = array();
foreach ($finder as $key => $value) {
$dir = realpath(pathinfo($key, PATHINFO_DIRNAME));
$testname = substr($dir, strpos($dir, $subFolder) + strlen($subFolder) + 1) . '/' . pathinfo($key, PATHINFO_FILENAME);
$test = pathinfo($key, PATHINFO_BASENAME);
$result = pathinfo($key, PATHINFO_FILENAME) . '.json';
if (is_file($dir . '/' . $result)) {
$tests[$testname] = array(
'input' => file_get_contents($dir . '/' . $test),
'expectedOutput' => file_get_contents($dir . '/' . $result),
'name' => $testname
);
}
}
return $tests;
}
/**
* Following three functions are the actual dataProviders used by the test methods.
*/
public function mf1TestsProvider()
{
$data = $this->htmlAndJsonProvider('/microformats-v1');
$keys = array_keys($data);
$values = array_values($data);
return array_combine(
$keys,
array_map(
function($case, $name) {
return $case + ['knownFailure' => in_array($name, self::MF1_KNOWN_FAILURES)];
},
$values,
$keys
)
);
}
public function mf2TestsProvider()
{
return $this->htmlAndJsonProvider('/microformats-v2');
}
public function mixedTestsProvider()
{
return $this->htmlAndJsonProvider('/microformats-mixed');
}
}