Browse Source

add tests for feeds, catch case when a permalink has other h-entrys

pull/39/head
Aaron Parecki 8 years ago
parent
commit
162d2f5ef8
9 changed files with 177 additions and 31 deletions
  1. +54
    -29
      lib/Formats/Mf2.php
  2. +86
    -0
      tests/FeedTest.php
  3. +11
    -0
      tests/ParseTest.php
  4. +1
    -1
      tests/data/feed.example.com/h-card-with-child-h-entrys
  5. +1
    -1
      tests/data/feed.example.com/h-card-with-child-h-feed
  6. +1
    -0
      tests/data/feed.example.com/short-list-of-hentrys-with-h-card
  7. +21
    -0
      tests/data/source.example.com/multiple-h-entry-on-permalink
  8. +1
    -0
      tests/data/source.example.com/person-tag-is-url
  9. +1
    -0
      tests/data/source.example.com/reply-is-url

+ 54
- 29
lib/Formats/Mf2.php View File

@ -6,56 +6,81 @@ use HTMLPurifier, HTMLPurifier_Config;
class Mf2 {
public static function parse($mf2, $url, $http) {
if(count($mf2['items']) == 0)
return false;
// TODO: Check if the list of items is a bunch of h-entrys and return as a feed
// Check if the list of items is a bunch of h-entrys and return as a feed
// Unless this page's URL matches one of the entries, then treat it as a permalink
$hentrys = 0;
$lastSeenEntry = false;
foreach($mf2['items'] as $item) {
if(in_array('h-entry', $item['type']) || in_array('h-cite', $item['type'])) {
if(array_key_exists('url', $item['properties'])) {
$urls = $item['properties']['url'];
$urls = array_map('\normalize_url', $urls);
if(in_array($url, $urls)) {
return self::parseAsHEntry($mf2, $item, $http, $url);
}
$lastSeenEntry = $item;
}
$hentrys++;
}
}
// If there was more than one h-entry on the page, treat the whole page as a feed
if($hentrys > 1) {
return self::parseAsHFeed($mf2, $http);
}
// If the first item is an h-feed, parse as a feed
$first = $mf2['items'][0];
if(in_array('h-feed', $first['type'])) {
return self::parseAsHFeed($mf2, $http);
}
if($item = $mf2['items'][0]) {
// If the first item is a feed, the page is a feed
if(in_array('h-feed', $item['type'])) {
return self::parseAsHFeed($mf2, $http);
// Check each top-level h-card, and if there is one that matches this URL, the page is an h-card
foreach($mf2['items'] as $item) {
if(in_array('h-card', $item['type'])
and array_key_exists('url', $item['properties'])
) {
$urls = $item['properties']['url'];
$urls = array_map('\normalize_url', $urls);
if(in_array($url, $urls)) {
// TODO: check for children h-entrys (like tantek.com), or sibling h-entries (like aaronparecki.com)
// and return the result as a feed instead
return self::parseAsHCard($item, $http, $url);
}
}
}
// Check each top-level h-card, and if there is one that matches this URL, the page is an h-card
foreach($mf2['items'] as $i) {
if(in_array('h-card', $i['type'])
and array_key_exists('url', $i['properties'])
) {
$urls = $i['properties']['url'];
$urls = array_map('\normalize_url', $urls);
if(in_array($url, $urls)) {
// TODO: check for children h-entrys (like tantek.com), or sibling h-entries (like aaronparecki.com)
// and return the result as a feed instead
return self::parseAsHCard($i, $http, $url);
}
// If there was only one h-entry, but the URL for it is not the same as this page, then treat as a feed
if($hentrys == 1) {
if($lastSeenEntry) {
$urls = $lastSeenEntry['properties']['url'];
$urls = array_map('\normalize_url', $urls);
if(count($urls) && !in_array($url, $urls)) {
return self::parseAsHFeed($mf2, $http);
}
}
}
// Fallback case, but hopefully we have found something before this point
foreach($mf2['items'] as $item) {
// Otherwise check for an h-entry
if(in_array('h-entry', $item['type']) || in_array('h-cite', $item['type'])) {
return self::parseAsHEntry($mf2, $http);
return self::parseAsHEntry($mf2, $item, $http);
}
}
return false;
}
private static function parseAsHEntry($mf2, $http) {
private static function parseAsHEntry($mf2, $item, $http) {
$data = [
'type' => 'entry'
];
$refs = [];
// Find the first h-entry
foreach($mf2['items'] as $i) {
if(in_array('h-entry', $i['type']) || in_array('h-cite', $i['type'])) {
$item = $i;
continue;
}
}
// Single plaintext values
$properties = ['url','published','summary','rsvp'];
foreach($properties as $p) {
@ -166,12 +191,12 @@ class Mf2 {
'url' => null,
'photo' => null
],
'items' => [],
'todo' => 'Not yet implemented. Please see https://github.com/aaronpk/XRay/issues/1'
];
return [
'data' => $data
'data' => $data,
'entries' => []
];
}

+ 86
- 0
tests/FeedTest.php View File

@ -0,0 +1,86 @@
<?php
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class FeedTest extends PHPUnit_Framework_TestCase {
private $http;
public function setUp() {
$this->client = new Parse();
$this->client->http = new p3k\HTTPTest(dirname(__FILE__).'/data/');
}
private function parse($params) {
$request = new Request($params);
$response = new Response();
return $this->client->parse($request, $response);
}
public function testListOfHEntrys() {
$url = 'http://feed.example.com/list-of-hentrys';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertEquals('feed', $data->data->type);
}
public function testListOfHEntrysWithHCard() {
$url = 'http://feed.example.com/list-of-hentrys-with-h-card';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertEquals('feed', $data->data->type);
}
public function testShortListOfHEntrysWithHCard() {
$url = 'http://feed.example.com/short-list-of-hentrys-with-h-card';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertEquals('feed', $data->data->type);
}
public function testTopLevelHFeed() {
$url = 'http://feed.example.com/top-level-h-feed';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertEquals('feed', $data->data->type);
}
public function testHCardWithChildHEntrys() {
$url = 'http://feed.example.com/h-card-with-child-h-entrys';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertEquals('card', $data->data->type);
}
public function testHCardWithChildHFeed() {
$url = 'http://feed.example.com/h-card-with-child-h-feed';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertEquals('card', $data->data->type);
}
}

+ 11
- 0
tests/ParseTest.php View File

@ -202,4 +202,15 @@ class ParseTest extends PHPUnit_Framework_TestCase {
$this->assertEquals('yes', $data['data']['rsvp']);
}
public function testMultipleHEntryOnPermalink() {
$url = 'http://source.example.com/multiple-h-entry-on-permalink';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body, true);
$this->assertEquals('entry', $data['data']['type']);
$this->assertEquals('Primary Post', $data['data']['name']);
}
}

+ 1
- 1
tests/data/feed.example.com/h-card-with-child-h-entrys View File

@ -11,7 +11,7 @@ Connection: keep-alive
<body>
<div class="h-card">
<a href="/author" class="u-url p-name">Author Name</a>
<a href="/h-card-with-child-h-entrys" class="u-url p-name">Author Name</a>
<ul>
<li class="h-entry">

+ 1
- 1
tests/data/feed.example.com/h-card-with-child-h-feed View File

@ -11,7 +11,7 @@ Connection: keep-alive
<body>
<div class="h-card">
<a href="/author" class="u-url p-name">Author Name</a>
<a href="/h-card-with-child-h-feed" class="u-url p-name">Author Name</a>
<ul class="h-feed">
<li class="h-entry">

+ 1
- 0
tests/data/feed.example.com/short-list-of-hentrys-with-h-card View File

@ -13,6 +13,7 @@ Connection: keep-alive
<ul>
<li class="h-entry">
<a href="/1" class="u-url p-name">One</a>
<a href="/author" class="p-author h-card">Author</a>
</li>
</ul>

+ 21
- 0
tests/data/source.example.com/multiple-h-entry-on-permalink View File

@ -0,0 +1,21 @@
HTTP/1.1 200 OK
Server: Apache
Date: Wed, 09 Dec 2015 03:29:14 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
<html>
<head>
<title>Test</title>
</head>
<body>
<div class="h-entry">
<h2 class="p-name">Primary Post</h2>
<a href="/multiple-h-entry-on-permalink">permalink</a>
</div>
<div class="h-entry">
<h3 class="p-name">Next Post</h3>
<a href="/another-post">read more</a>
</div>
</body>
</html>

+ 1
- 0
tests/data/source.example.com/person-tag-is-url View File

@ -11,5 +11,6 @@ Connection: keep-alive
<body class="h-entry">
<h2 class="p-name">Hello World</h2>
<a href="http://alice.example.com/" class="u-category">Alice</a>
<a href="/person-tag-is-url" class="u-url">permalink</a>
</body>
</html>

+ 1
- 0
tests/data/source.example.com/reply-is-url View File

@ -11,5 +11,6 @@ Connection: keep-alive
<body class="h-entry">
<a href="http://example.com/100" class="u-in-reply-to">in reply to</a>
<p class="e-content">This page has a link to <a href="http://target.example.com">target.example.com</a> and some <b>formatted text</b>.</p>
<a href="/reply-is-url" class="u-url">permalink</a>
</body>
</html>

Loading…
Cancel
Save