Skip to content

Commit 103b34e

Browse files
authored
Merge pull request #255 from gRegorLove/dev/issue155
Fix hreview backcompat
2 parents 2753c22 + 774a6f8 commit 103b34e

File tree

4 files changed

+91
-14
lines changed

4 files changed

+91
-14
lines changed

Mf2/Parser.php

+21-5
Original file line numberDiff line numberDiff line change
@@ -1637,12 +1637,31 @@ public function backcompat(DOMElement $el, $context = '', $isParentMf2 = false)
16371637
foreach ( $item_and_hproduct as $tempEl ) {
16381638
if ( !$this->hasRootMf2($tempEl) ) {
16391639
$this->addMfClasses($tempEl, 'p-item h-product');
1640-
$this->backcompat($tempEl, 'vevent');
1640+
$this->backcompat($tempEl, 'hproduct');
16411641
$this->addUpgraded($tempEl, array('item', 'hproduct'));
16421642
}
16431643
}
16441644
}
16451645

1646+
$rel_self_bookmark = $this->xpath->query('.//*[contains(concat(" ", normalize-space(@rel), " "), " self ") and contains(concat(" ", normalize-space(@rel), " "), " bookmark ")]', $el);
1647+
1648+
if ( $rel_self_bookmark->length ) {
1649+
foreach ( $rel_self_bookmark as $tempEl ) {
1650+
$this->addMfClasses($tempEl, 'u-url');
1651+
$this->addUpgraded($tempEl, array('self', 'bookmark'));
1652+
}
1653+
}
1654+
1655+
$reviewer_nodes = $this->xpath->query('.//*[contains(concat(" ", normalize-space(@class), " "), " reviewer ")]', $el);
1656+
1657+
if ( $reviewer_nodes->length ) {
1658+
foreach ( $reviewer_nodes as $tempEl ) {
1659+
if ( !$this->hasRootMf2($tempEl) ) {
1660+
$this->addMfClasses($tempEl, 'p-author h-card');
1661+
}
1662+
}
1663+
}
1664+
16461665
$this->upgradeRelTagToCategory($el);
16471666
break;
16481667

@@ -2058,10 +2077,7 @@ public function query($expression, $context = null) {
20582077
'replace' => 'p-item h-item',
20592078
'context' => 'item'
20602079
),
2061-
'reviewer' => array(
2062-
'replace' => 'p-author h-card',
2063-
'context' => 'vcard',
2064-
),
2080+
# reviewer: see backcompat()
20652081
'dtreviewed' => array(
20662082
'replace' => 'dt-published'
20672083
),

tests/Mf2/ClassicMicroformatsTest.php

+57-2
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,16 @@ public function testParsesFBerrimanClassicHEntry() {
113113
$this->assertContains('txjs', $e['properties']['category']);
114114
}
115115

116-
public function testParsesSnarfedOrgArticleCorrectly() {
116+
/**
117+
* This test appears to have never made assertions. When initially
118+
* added it was only printing out the parsed results, probably for
119+
* debugging. Left for reference in case assertions need to be added.
120+
* @see https://github.com/microformats/php-mf2/commit/5fd2c961447f305f50f73e17bd8decf7ec77fa1d#diff-45371c4a595b936f718ab44eb3ff35c326e00a01f2f5cb3d327f34d03750b872
121+
*/
122+
/*public function testParsesSnarfedOrgArticleCorrectly() {
117123
$input = file_get_contents(__DIR__ . '/snarfed.org.html');
118124
$result = Mf2\parse($input, 'http://snarfed.org/2013-10-23_oauth-dropins');
119-
}
125+
}*/
120126

121127
public function testParsesHProduct() {
122128
$input = <<<'EOT'
@@ -761,6 +767,7 @@ public function testParsesClassicHreview() {
761767
</div>
762768
<p>Visit date: <span>April 2005</span></p>
763769
<p>Food eaten: <span>Florentine crepe</span></p>
770+
<p>Permanent link for review: <a rel="self bookmark" href="http://example.com/crepe">http://example.com/crepe</a></p>
764771
</div>
765772
END;
766773
$parser = new Parser($input);
@@ -946,6 +953,54 @@ public function testHReviewRelTag() {
946953
$this->assertContains('Garçon', $output['items'][0]['properties']['category']);
947954
}
948955

956+
public function testHReviewItemVevent()
957+
{
958+
$input = '<div class="hreview">
959+
<span><span class="rating">5</span> out of 5 stars</span>
960+
<span class="item vevent">
961+
<span class="summary">IndieWebCamp 2014</span> -
962+
<a href="https://indieweb.org/2014" class="url">indieweb.org/2014</a>
963+
</span>
964+
</div>';
965+
$parser = new Parser($input);
966+
$output = $parser->parse();
967+
968+
$this->assertArrayHasKey('item', $output['items'][0]['properties']);
969+
970+
# assert item type is h-event
971+
$this->assertCount(1, $output['items'][0]['properties']['item'][0]['type']);
972+
$this->assertEquals('h-event', $output['items'][0]['properties']['item'][0]['type'][0]);
973+
974+
# assert expected h-event properties
975+
$properties = $output['items'][0]['properties']['item'][0]['properties'];
976+
$this->assertArrayHasKey('name', $properties);
977+
$this->assertArrayHasKey('url', $properties);
978+
}
979+
980+
public function testHReviewItemHproduct()
981+
{
982+
$input = '<div class="hreview">
983+
<span><span class="rating">4</span> out of 5 stars</span>
984+
<span class="item hproduct">
985+
<span class="fn">Widget</span> -
986+
<a href="https://example.com/widget/" class="url">example.com/widget/</a>
987+
</span>
988+
</div>';
989+
$parser = new Parser($input);
990+
$output = $parser->parse();
991+
992+
$this->assertArrayHasKey('item', $output['items'][0]['properties']);
993+
994+
# assert item type is h-product
995+
$this->assertCount(1, $output['items'][0]['properties']['item'][0]['type']);
996+
$this->assertEquals('h-product', $output['items'][0]['properties']['item'][0]['type'][0]);
997+
998+
# assert expected h-product properties
999+
$properties = $output['items'][0]['properties']['item'][0]['properties'];
1000+
$this->assertArrayHasKey('name', $properties);
1001+
$this->assertArrayHasKey('url', $properties);
1002+
}
1003+
9491004
/**
9501005
* Should return the last non-empty URL segment
9511006
* @see https://github.com/indieweb/php-mf2/issues/157

tests/Mf2/ParserTest.php

+10-4
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,21 @@ public function testParseEWithWhitespace() {
139139
* @group parseH
140140
*/
141141
public function testInvalidClassnamesContainingHAreIgnored() {
142-
$input = '<div class="asdfgh-jkl"></div>';
142+
$classname = 'asdfgh-jkl';
143+
$input = sprintf('<div class="%s"></div>', $classname);
143144
$parser = new Parser($input);
144145
$output = $parser->parse();
145146

146-
// Look through $output for an item which indicate failure
147+
// Look through parsed items for `type` matching the classname.
148+
// There shouldn't be any
149+
$matches = 0;
147150
foreach ($output['items'] as $item) {
148-
if (in_array('asdfgh-jkl', $item['type']))
149-
$this->fail();
151+
if (in_array($classname, $item['type'])) {
152+
$matches++;
153+
}
150154
}
155+
156+
$this->assertEquals(0, $matches, sprintf('Class name "%s" should not have parsed as a root microformat', $classname));
151157
}
152158

153159
public function testHtmlSpecialCharactersWorks() {

tests/Mf2/URLTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Mf2;
1010
use Yoast\PHPUnitPolyfills\TestCases\TestCase;
1111

12-
class UrlTest extends TestCase {
12+
class URLTest extends TestCase {
1313
protected function set_up() {
1414
date_default_timezone_set('Europe/London');
1515
}
@@ -149,15 +149,15 @@ public function testResolvesProtocolRelativeUrlsCorrectly() {
149149
}
150150

151151
/**
152-
* @dataProvider testData
152+
* @dataProvider dataProvider
153153
*/
154154
public function testReturnsUrlIfAbsolute($assert, $base, $url, $expected) {
155155
$actual = mf2\resolveUrl($base, $url);
156156

157157
$this->assertEquals($expected, $actual, $assert);
158158
}
159159

160-
public function testData() {
160+
public function dataProvider() {
161161
// seriously, please update to PHP 5.4 so I can use nice array syntax ;)
162162
// fail message, base, url, expected
163163
$cases = array(

0 commit comments

Comments
 (0)