From 7621bca4a6758ef19803b073016b09a5d799eb49 Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Sun, 29 Jul 2018 16:56:40 -0700 Subject: [PATCH] adds new "source-format" property to indicate how XRay found the data * mf2+html * mf2+json * feed+json * xml * instagram/facebook/github/xkcd --- README.md | 15 ++++++-- controllers/Parse.php | 2 + lib/XRay.php | 2 + lib/XRay/Formats/Facebook.php | 3 +- lib/XRay/Formats/GitHub.php | 3 +- lib/XRay/Formats/HTML.php | 1 + lib/XRay/Formats/Hackernews.php | 3 +- lib/XRay/Formats/Instagram.php | 9 +++-- lib/XRay/Formats/JSONFeed.php | 1 + lib/XRay/Formats/Mf2.php | 2 +- lib/XRay/Formats/Mf2Feed.php | 3 +- lib/XRay/Formats/Twitter.php | 1 + lib/XRay/Formats/XKCD.php | 3 +- lib/XRay/Formats/XML.php | 1 + lib/XRay/Parser.php | 12 ++++-- tests/AuthorTest.php | 1 + tests/FacebookTest.php | 6 +++ tests/FeedTest.php | 66 +++++++++++++++++++++------------ tests/GitHubTest.php | 15 ++++++++ tests/HackernewsTest.php | 6 +++ tests/InstagramTest.php | 29 +++++++++++++++ tests/ParseTest.php | 51 +++++++++++++++++++++++-- tests/TwitterTest.php | 9 +++++ 23 files changed, 201 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index 2e81c33..326519d 100644 --- a/README.md +++ b/README.md @@ -97,7 +97,8 @@ $parsed = Array ) [url] => https://aaronparecki.com/ - [code] => 200 + [code] => 200, + [source-format] => mf2+html ) ``` @@ -131,6 +132,8 @@ Array ) [url] => http://example.com/entry + + [source-format] => mf2+json ) ``` @@ -363,7 +366,8 @@ Possible errors are listed below: } }, "url":"https://aaronparecki.com/2017/03/01/14/hwc", - "code":200 + "code":200, + "source-format":"mf2+html" } ``` @@ -420,7 +424,12 @@ Other properties are returned in the response at the same level as the `data` pr * `url` - The effective URL that the document was retrieved from. This will be the final URL after following any redirects. * `code` - The HTTP response code returned by the URL. Typically this will be 200, but if the URL returned an alternate HTTP code that also included an h-entry (such as a 410 deleted notice with a stub h-entry), you can use this to find out that the original URL was actually deleted. - +* `source-format` - Indicates the format of the source URL that was used to generate the parsed result. Possible values are: + * `mf2+html` + * `mf2+json` + * `feed+json` + * `xml` + * `instagram`/`facebook`/`github`/`xkcd` #### Feeds diff --git a/controllers/Parse.php b/controllers/Parse.php index 8a9ef2d..1918eb5 100644 --- a/controllers/Parse.php +++ b/controllers/Parse.php @@ -126,6 +126,8 @@ class Parse { $data['info'] = $parsed['info']; if($request->get('include_original') && isset($parsed['original'])) $data['original'] = $parsed['original']; + if(isset($parsed['source-format'])) + $data['source-format'] = $parsed['source-format']; return $this->respond($response, 200, $data); } diff --git a/lib/XRay.php b/lib/XRay.php index 9670b4c..5b9f972 100644 --- a/lib/XRay.php +++ b/lib/XRay.php @@ -40,6 +40,7 @@ class XRay { unset($result['original']); $result['url'] = $url; $result['code'] = isset($result['code']) ? $result['code'] : $code; + $result['source-format'] = isset($result['source-format']) ? $result['source-format'] : null; return $result; } @@ -49,6 +50,7 @@ class XRay { if(!isset($opts['include_original']) || !$opts['include_original']) unset($result['original']); $result['url'] = $url; + $result['source-format'] = isset($result['source-format']) ? $result['source-format'] : null; return $result; } diff --git a/lib/XRay/Formats/Facebook.php b/lib/XRay/Formats/Facebook.php index 40593fa..4955b0b 100644 --- a/lib/XRay/Formats/Facebook.php +++ b/lib/XRay/Formats/Facebook.php @@ -68,7 +68,8 @@ class Facebook extends Format { return [ 'data' => $event, - 'original' => $fbObject + 'original' => $fbObject, + 'source-format' => 'facebook', ]; } } diff --git a/lib/XRay/Formats/GitHub.php b/lib/XRay/Formats/GitHub.php index 9ad36b8..9ea94b9 100644 --- a/lib/XRay/Formats/GitHub.php +++ b/lib/XRay/Formats/GitHub.php @@ -166,7 +166,8 @@ class GitHub extends Format { return [ 'data' => $entry, - 'original' => $json + 'original' => $json, + 'source-format' => 'github', ]; } diff --git a/lib/XRay/Formats/HTML.php b/lib/XRay/Formats/HTML.php index 9bcebec..f2a7554 100644 --- a/lib/XRay/Formats/HTML.php +++ b/lib/XRay/Formats/HTML.php @@ -105,6 +105,7 @@ class HTML extends Format { } $result['original'] = $html; $result['url'] = $url; // this will be the effective URL after following redirects + $result['source-format'] = 'mf2+html'; } } return $result; diff --git a/lib/XRay/Formats/Hackernews.php b/lib/XRay/Formats/Hackernews.php index af5c227..5b1dd40 100644 --- a/lib/XRay/Formats/Hackernews.php +++ b/lib/XRay/Formats/Hackernews.php @@ -83,7 +83,8 @@ class Hackernews extends Format { return [ 'data' => $entry, - 'original' => $json + 'original' => $json, + 'source-format' => 'hackernews', ]; } diff --git a/lib/XRay/Formats/Instagram.php b/lib/XRay/Formats/Instagram.php index f9ff365..274490b 100644 --- a/lib/XRay/Formats/Instagram.php +++ b/lib/XRay/Formats/Instagram.php @@ -34,7 +34,8 @@ class Instagram extends Format { $card = self::_buildHCardFromInstagramProfile($profileData); return [ - 'data' => $card + 'data' => $card, + 'source-format' => 'instagram', ]; } @@ -61,7 +62,8 @@ class Instagram extends Format { 'data' => [ 'type' => 'feed', 'items' => $items, - ] + ], + 'source-format' => 'instagram', ]; } @@ -206,7 +208,8 @@ class Instagram extends Format { 'photo' => $photoData, 'profiles' => $profiles, 'locations' => $locations - ]) + ]), + 'source-format' => 'instagram', ]; } diff --git a/lib/XRay/Formats/JSONFeed.php b/lib/XRay/Formats/JSONFeed.php index 86e7f77..e2db1e6 100644 --- a/lib/XRay/Formats/JSONFeed.php +++ b/lib/XRay/Formats/JSONFeed.php @@ -16,6 +16,7 @@ class JSONFeed extends Format { 'type' => 'unknown', ], 'url' => $url, + 'source-format' => 'feed+json', ]; if($feed) { diff --git a/lib/XRay/Formats/Mf2.php b/lib/XRay/Formats/Mf2.php index 910360c..357ea56 100644 --- a/lib/XRay/Formats/Mf2.php +++ b/lib/XRay/Formats/Mf2.php @@ -448,7 +448,7 @@ class Mf2 extends Format { $data['post-type'] = \p3k\XRay\PostType::discover($data); $response = [ - 'data' => $data + 'data' => $data, ]; if(count($refs)) { diff --git a/lib/XRay/Formats/Mf2Feed.php b/lib/XRay/Formats/Mf2Feed.php index 04dfad3..acf012b 100644 --- a/lib/XRay/Formats/Mf2Feed.php +++ b/lib/XRay/Formats/Mf2Feed.php @@ -67,7 +67,8 @@ trait Mf2Feed { } return [ - 'data' => $data + 'data' => $data, + 'source-format' => 'mf2+html', ]; } diff --git a/lib/XRay/Formats/Twitter.php b/lib/XRay/Formats/Twitter.php index d34c32e..f27847c 100644 --- a/lib/XRay/Formats/Twitter.php +++ b/lib/XRay/Formats/Twitter.php @@ -175,6 +175,7 @@ class Twitter extends Format { return [ 'data' => $entry, 'original' => $tweet, + 'source-format' => 'twitter', ]; } diff --git a/lib/XRay/Formats/XKCD.php b/lib/XRay/Formats/XKCD.php index ae9c302..987ec08 100644 --- a/lib/XRay/Formats/XKCD.php +++ b/lib/XRay/Formats/XKCD.php @@ -60,7 +60,8 @@ class XKCD extends Format { $entry['post-type'] = \p3k\XRay\PostType::discover($entry); $response = [ - 'data' => $entry + 'data' => $entry, + 'source-format' => 'xkcd', ]; return $response; diff --git a/lib/XRay/Formats/XML.php b/lib/XRay/Formats/XML.php index 8b227df..49431d4 100644 --- a/lib/XRay/Formats/XML.php +++ b/lib/XRay/Formats/XML.php @@ -18,6 +18,7 @@ class XML extends Format { 'type' => 'unknown', ], 'url' => $url, + 'source-format' => 'xml', ]; try { diff --git a/lib/XRay/Parser.php b/lib/XRay/Parser.php index 949a3b8..dda17f8 100644 --- a/lib/XRay/Parser.php +++ b/lib/XRay/Parser.php @@ -44,7 +44,9 @@ class Parser { // Check if an mf2 JSON object was passed in if(is_array($body) && isset($body['items'][0]['type']) && isset($body['items'][0]['properties'])) { - return Formats\Mf2::parse($body, $url, $this->http, $opts); + $data = Formats\Mf2::parse($body, $url, $this->http, $opts); + $data['source-format'] = 'mf2+json'; + return $data; } if(substr($body, 0, 5) == 'http, $opts); + $data = Formats\Mf2::parse($feeddata, $url, $this->http, $opts); + $data['source-format'] = 'mf2+json'; + return $data; } } // No special parsers matched, parse for Microformats now - return Formats\HTML::parse($this->http, $body, $url, $opts); + $data = Formats\HTML::parse($this->http, $body, $url, $opts); + $data['source-format'] = 'mf2+html'; + return $data; } } diff --git a/tests/AuthorTest.php b/tests/AuthorTest.php index 975468b..b40eee6 100644 --- a/tests/AuthorTest.php +++ b/tests/AuthorTest.php @@ -26,6 +26,7 @@ class AuthorTest extends PHPUnit_Framework_TestCase { $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body); + $this->assertEquals('mf2+html', $data->{'source-format'}); $this->assertEmpty($data->data->author->url); $this->assertEquals('Author Name', $data->data->author->name); $this->assertEmpty($data->data->author->photo); diff --git a/tests/FacebookTest.php b/tests/FacebookTest.php index 0b4df22..37c0d49 100644 --- a/tests/FacebookTest.php +++ b/tests/FacebookTest.php @@ -31,6 +31,9 @@ class FacebookTest extends PHPUnit_Framework_TestCase { $data = $this->parse(['url' => $url, 'body' => $json]); + $this->assertArrayHasKey('code', $data); + $this->assertEquals('facebook', $data['source-format']); + $this->assertEquals('event', $data['data']['type']); $this->assertEquals('event', $data['data']['post-type']); $this->assertEquals('IndieWeb Summit', $data['data']['name']); @@ -58,6 +61,9 @@ class FacebookTest extends PHPUnit_Framework_TestCase { $data = $this->parse(['url' => $url, 'body' => $json]); + $this->assertArrayHasKey('code', $data); + $this->assertEquals('facebook', $data['source-format']); + $this->assertEquals('event', $data['data']['type']); $this->assertEquals('event', $data['data']['post-type']); $this->assertEquals('Homebrew Website Club', $data['data']['name']); diff --git a/tests/FeedTest.php b/tests/FeedTest.php index 1671ef5..6a115c5 100644 --- a/tests/FeedTest.php +++ b/tests/FeedTest.php @@ -24,8 +24,9 @@ class FeedTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); - $data = json_decode($body)->data; - + $result = json_decode($body); + $this->assertEquals('mf2+html', $result->{'source-format'}); + $data = $result->data; $this->assertEquals('feed', $data->type); $this->assertEquals(4, count($data->items)); $this->assertEquals('One', $data->items[0]->name); @@ -42,8 +43,9 @@ class FeedTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); - $data = json_decode($body)->data; - + $result = json_decode($body); + $this->assertEquals('mf2+html', $result->{'source-format'}); + $data = $result->data; $this->assertEquals('feed', $data->type); $this->assertEquals(4, count($data->items)); $this->assertEquals('One', $data->items[0]->name); @@ -65,8 +67,9 @@ class FeedTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); - $data = json_decode($body)->data; - + $result = json_decode($body); + $this->assertEquals('mf2+html', $result->{'source-format'}); + $data = $result->data; $this->assertEquals('feed', $data->type); // This test should find the h-entry rather than the h-card, because expect=feed $this->assertEquals('entry', $data->items[0]->type); @@ -80,8 +83,9 @@ class FeedTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); - $data = json_decode($body)->data; - + $result = json_decode($body); + $this->assertEquals('mf2+html', $result->{'source-format'}); + $data = $result->data; $this->assertEquals('feed', $data->type); $this->assertEquals(4, count($data->items)); $this->assertEquals('One', $data->items[0]->name); @@ -96,8 +100,9 @@ class FeedTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); - $data = json_decode($body)->data; - + $result = json_decode($body); + $this->assertEquals('mf2+html', $result->{'source-format'}); + $data = $result->data; $this->assertEquals('feed', $data->type); $this->assertEquals(4, count($data->items)); $this->assertEquals('One', $data->items[0]->name); @@ -116,8 +121,9 @@ class FeedTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); - $data = json_decode($body)->data; - + $result = json_decode($body); + $this->assertEquals('mf2+html', $result->{'source-format'}); + $data = $result->data; $this->assertEquals('feed', $data->type); $this->assertEquals(4, count($data->items)); $this->assertEquals('One', $data->items[0]->name); @@ -132,8 +138,9 @@ class FeedTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); - $data = json_decode($body)->data; - + $result = json_decode($body); + $this->assertEquals('mf2+html', $result->{'source-format'}); + $data = $result->data; $this->assertEquals('feed', $data->type); $this->assertEquals(4, count($data->items)); $this->assertEquals('One', $data->items[0]->name); @@ -153,8 +160,9 @@ class FeedTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); - $data = json_decode($body)->data; - + $result = json_decode($body); + $this->assertEquals('mf2+html', $result->{'source-format'}); + $data = $result->data; $this->assertEquals('feed', $data->type); $this->assertEquals(4, count($data->items)); $this->assertEquals('One', $data->items[0]->name); @@ -174,8 +182,9 @@ class FeedTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); - $data = json_decode($body)->data; - + $result = json_decode($body); + $this->assertEquals('mf2+html', $result->{'source-format'}); + $data = $result->data; $this->assertEquals('card', $data->type); $this->assertEquals('Author Name', $data->name); } @@ -186,7 +195,9 @@ class FeedTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); - $data = json_decode($body)->data; + $result = json_decode($body); + $this->assertEquals('feed+json', $result->{'source-format'}); + $data = $result->data; $this->assertEquals(10, count($data->items)); for($i=0; $i<8; $i++) { @@ -217,7 +228,9 @@ class FeedTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); - $data = json_decode($body)->data; + $result = json_decode($body); + $this->assertEquals('xml', $result->{'source-format'}); + $data = $result->data; $this->assertEquals(8, count($data->items)); for($i=0; $i<8; $i++) { @@ -246,7 +259,9 @@ class FeedTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); - $data = json_decode($body)->data; + $result = json_decode($body); + $this->assertEquals('xml', $result->{'source-format'}); + $data = $result->data; $this->assertEquals(10, count($data->items)); for($i=0; $i<10; $i++) { @@ -276,7 +291,9 @@ class FeedTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); - $data = json_decode($body)->data; + $result = json_decode($body); + $this->assertEquals('xml', $result->{'source-format'}); + $data = $result->data; $this->assertEquals(12, count($data->items)); for($i=0; $i<12; $i++) { @@ -309,8 +326,9 @@ class FeedTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); - $data = json_decode($body)->data; - + $result = json_decode($body); + $this->assertEquals('xml', $result->{'source-format'}); + $data = $result->data; $this->assertEquals(12, count($data->items)); $this->assertEquals('Marshall Kirkpatrick', $data->items[11]->author->name); diff --git a/tests/GitHubTest.php b/tests/GitHubTest.php index 506300f..d17a00e 100644 --- a/tests/GitHubTest.php +++ b/tests/GitHubTest.php @@ -27,6 +27,9 @@ class GitHubTest extends PHPUnit_Framework_TestCase { $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); + $this->assertEquals(200, $data['code']); + $this->assertEquals('github', $data['source-format']); + $this->assertEquals('entry', $data['data']['type']); $this->assertEquals('reply', $data['data']['post-type']); $this->assertEquals('https://github.com/idno/Known', $data['data']['in-reply-to'][0]); @@ -47,6 +50,9 @@ class GitHubTest extends PHPUnit_Framework_TestCase { $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); + $this->assertEquals(200, $data['code']); + $this->assertEquals('github', $data['source-format']); + $this->assertEquals('entry', $data['data']['type']); $this->assertEquals('reply', $data['data']['post-type']); $this->assertEquals('https://github.com/aaronpk/XRay/issues', $data['data']['in-reply-to'][0]); @@ -68,6 +74,9 @@ class GitHubTest extends PHPUnit_Framework_TestCase { $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); + $this->assertEquals(200, $data['code']); + $this->assertEquals('github', $data['source-format']); + $this->assertEquals('entry', $data['data']['type']); $this->assertContains('silo', $data['data']['category']); } @@ -80,6 +89,9 @@ class GitHubTest extends PHPUnit_Framework_TestCase { $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); + $this->assertEquals(200, $data['code']); + $this->assertEquals('github', $data['source-format']); + $this->assertEquals('repo', $data['data']['type']); $this->assertArrayNotHasKey('post-type', $data['data']); $this->assertEquals('2016-02-19T16:53:20Z', $data['data']['published']); @@ -98,6 +110,9 @@ class GitHubTest extends PHPUnit_Framework_TestCase { $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); + $this->assertEquals(200, $data['code']); + $this->assertEquals('github', $data['source-format']); + $this->assertEquals('entry', $data['data']['type']); $this->assertEquals('reply', $data['data']['post-type']); $this->assertEquals('https://github.com/aaronpk/XRay/issues/25', $data['data']['in-reply-to'][0]); diff --git a/tests/HackernewsTest.php b/tests/HackernewsTest.php index 730516c..cd171af 100644 --- a/tests/HackernewsTest.php +++ b/tests/HackernewsTest.php @@ -26,6 +26,9 @@ class HackernewsTest extends PHPUnit_Framework_TestCase { $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); + $this->assertEquals(200, $data['code']); + $this->assertEquals('hackernews', $data['source-format']); + $this->assertEquals('entry', $data['data']['type']); $this->assertEquals('article', $data['data']['post-type']); $this->assertEquals('2017-06-08T19:32:12+00:00', $data['data']['published']); @@ -50,6 +53,9 @@ What projects or companies are you working on to combat filter bubbles, walled g $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); + $this->assertEquals(200, $data['code']); + $this->assertEquals('hackernews', $data['source-format']); + $this->assertEquals('entry', $data['data']['type']); $this->assertEquals('reply', $data['data']['post-type']); $this->assertEquals('2017-06-08T20:23:20+00:00', $data['data']['published']); diff --git a/tests/InstagramTest.php b/tests/InstagramTest.php index 004f5bb..4d2af9c 100644 --- a/tests/InstagramTest.php +++ b/tests/InstagramTest.php @@ -27,6 +27,8 @@ class InstagramTest extends PHPUnit_Framework_TestCase { $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); + $this->assertEquals(200, $data['code']); + $this->assertEquals('instagram', $data['source-format']); $this->assertEquals('entry', $data['data']['type']); $this->assertEquals('photo', $data['data']['post-type']); $this->assertEquals('2017-01-05T23:31:32+00:00', $data['data']['published']); @@ -49,6 +51,9 @@ class InstagramTest extends PHPUnit_Framework_TestCase { $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); + $this->assertEquals(200, $data['code']); + $this->assertEquals('instagram', $data['source-format']); + $this->assertEquals('entry', $data['data']['type']); $this->assertEquals('photo', $data['data']['post-type']); $this->assertSame([ @@ -88,6 +93,9 @@ class InstagramTest extends PHPUnit_Framework_TestCase { $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); + $this->assertEquals(200, $data['code']); + $this->assertEquals('instagram', $data['source-format']); + $this->assertEquals('entry', $data['data']['type']); $this->assertEquals('video', $data['data']['post-type']); $this->assertContains('100daysofmusic', $data['data']['category']); @@ -110,6 +118,9 @@ class InstagramTest extends PHPUnit_Framework_TestCase { $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); + $this->assertEquals(200, $data['code']); + $this->assertEquals('instagram', $data['source-format']); + $this->assertEquals(2, count($data['data']['category'])); $this->assertContains('http://www.kmikeym.com/', $data['data']['category']); $this->assertArrayHasKey('http://www.kmikeym.com/', $data['data']['refs']); @@ -125,6 +136,9 @@ class InstagramTest extends PHPUnit_Framework_TestCase { $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); + $this->assertEquals(200, $data['code']); + $this->assertEquals('instagram', $data['source-format']); + $this->assertEquals(1, count($data['data']['location'])); $this->assertContains('https://www.instagram.com/explore/locations/109284789535230/', $data['data']['location']); $this->assertArrayHasKey('https://www.instagram.com/explore/locations/109284789535230/', $data['data']['refs']); @@ -145,6 +159,9 @@ class InstagramTest extends PHPUnit_Framework_TestCase { $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); + $this->assertEquals(200, $data['code']); + $this->assertEquals('instagram', $data['source-format']); + $this->assertEquals(2, count($data['data']['photo'])); $this->assertEquals('https://instagram.fsea1-1.fna.fbcdn.net/vp/406101ff9601ab78147e121b65ce3eea/5B5BC738/t51.2885-15/e35/21827424_134752690591737_8093088291252862976_n.jpg', $data['data']['photo'][0]); $this->assertEquals('https://instagram.fsea1-1.fna.fbcdn.net/vp/03ddc8c03c8708439dae29663b8c2305/5B5EDE4D/t51.2885-15/e35/21909774_347707439021016_5237540582556958720_n.jpg', $data['data']['photo'][1]); @@ -161,6 +178,9 @@ class InstagramTest extends PHPUnit_Framework_TestCase { $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); + $this->assertEquals(200, $data['code']); + $this->assertEquals('instagram', $data['source-format']); + $this->assertEquals('photo', $data['data']['post-type']); // we discard videos in this case right now $this->assertEquals(3, count($data['data']['photo'])); $this->assertEquals('https://instagram.fsea1-1.fna.fbcdn.net/vp/b0f6cd9dc4d5c3371efe9f412a0d7f0b/5B6BC5B8/t51.2885-15/e35/21878922_686481254874005_8468823712617988096_n.jpg', $data['data']['photo'][0]); @@ -178,6 +198,9 @@ class InstagramTest extends PHPUnit_Framework_TestCase { $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); + $this->assertEquals(200, $data['code']); + $this->assertEquals('instagram', $data['source-format']); + $this->assertSame([ 'type' => 'card', 'name' => 'Aaron Parecki', @@ -195,6 +218,9 @@ class InstagramTest extends PHPUnit_Framework_TestCase { $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); + $this->assertEquals(200, $data['code']); + $this->assertEquals('instagram', $data['source-format']); + $this->assertSame([ 'type' => 'card', 'name' => 'pk_spam', @@ -212,6 +238,9 @@ class InstagramTest extends PHPUnit_Framework_TestCase { $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); + $this->assertEquals(200, $data['code']); + $this->assertEquals('instagram', $data['source-format']); + $this->assertEquals('feed', $data['data']['type']); $this->assertEquals(12, count($data['data']['items'])); $this->assertEquals('https://www.instagram.com/p/Be0lBpGDncI/', $data['data']['items'][0]['url']); diff --git a/tests/ParseTest.php b/tests/ParseTest.php index caac278..6507f97 100644 --- a/tests/ParseTest.php +++ b/tests/ParseTest.php @@ -87,6 +87,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { ]); $xray = new \p3k\XRay(); $parsed = $xray->process(false, $mf2JSON); + $this->assertEquals('mf2+json', $parsed['source-format']); $item = $parsed['data']; $this->assertEquals('entry', $item['type']); $this->assertEquals('note', $item['post-type']); @@ -107,6 +108,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { ]); $xray = new \p3k\XRay(); $parsed = $xray->process(false, $mf2JSON); + $this->assertEquals('mf2+json', $parsed['source-format']); $item = $parsed['data']; $this->assertEquals('entry', $item['type']); $this->assertEquals('note', $item['post-type']); @@ -127,6 +129,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { ]); $xray = new \p3k\XRay(); $parsed = $xray->process(false, $mf2JSON); + $this->assertEquals('mf2+json', $parsed['source-format']); $item = $parsed['data']; $this->assertEquals('entry', $item['type']); $this->assertEquals('note', $item['post-type']); @@ -147,6 +150,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { ]); $xray = new \p3k\XRay(); $parsed = $xray->process(false, $mf2JSON); + $this->assertEquals('mf2+json', $parsed['source-format']); $item = $parsed['data']; $this->assertEquals('entry', $item['type']); $this->assertEquals('note', $item['post-type']); @@ -167,6 +171,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { ]); $xray = new \p3k\XRay(); $parsed = $xray->process(false, $mf2JSON); + $this->assertEquals('mf2+json', $parsed['source-format']); $item = $parsed['data']; $this->assertEquals('entry', $item['type']); $this->assertEquals('note', $item['post-type']); @@ -180,6 +185,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body); + $this->assertEquals('mf2+html', $data->{'source-format'}); $this->assertEquals('photo', $data->data->{'post-type'}); $this->assertObjectNotHasAttribute('name', $data->data); $this->assertEquals('This page has an img tag with the target URL.', $data->data->content->text); @@ -192,6 +198,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body); + $this->assertEquals('mf2+html', $data->{'source-format'}); $this->assertEquals('video', $data->data->{'post-type'}); $this->assertObjectNotHasAttribute('name', $data->data); $this->assertEquals('This page has a video tag with the target URL.', $data->data->content->text); @@ -204,6 +211,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body); + $this->assertEquals('mf2+html', $data->{'source-format'}); $this->assertEquals('audio', $data->data->{'post-type'}); $this->assertObjectNotHasAttribute('name', $data->data); $this->assertEquals('This page has an audio tag with the target URL.', $data->data->content->text); @@ -216,6 +224,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body); + $this->assertEquals('mf2+html', $data->{'source-format'}); $this->assertObjectNotHasAttribute('name', $data->data); $this->assertEquals('This page has a link to target.example.com and some formatted text but is in a p-content element so is plaintext.', $data->data->content->text); } @@ -227,6 +236,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body); + $this->assertEquals('mf2+html', $data->{'source-format'}); $this->assertEquals('article', $data->data->{'post-type'}); $this->assertEquals('Post Title', $data->data->name); $this->assertEquals('This is a blog post.', $data->data->content->text); @@ -240,6 +250,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body); + $this->assertEquals('mf2+html', $data->{'source-format'}); $this->assertObjectNotHasAttribute('name', $data->data); $this->assertEquals('note', $data->data->{'post-type'}); $this->assertEquals('This page has a link to target.example.com and some formatted text.', $data->data->content->text); @@ -253,6 +264,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body); + $this->assertEquals('mf2+html', $data->{'source-format'}); $this->assertEquals('Hello World', $data->data->name); $this->assertEquals('article', $data->data->{'post-type'}); $this->assertEquals('This page has a link to target.example.com and some formatted text.', $data->data->content->text); @@ -266,6 +278,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body); + $this->assertEquals('mf2+html', $data->{'source-format'}); $this->assertEquals('Hello World', $data->data->name); $this->assertEquals('article', $data->data->{'post-type'}); $this->assertObjectNotHasAttribute('content', $data->data); @@ -278,6 +291,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body); + $this->assertEquals('mf2+html', $data->{'source-format'}); $this->assertEquals(['indieweb'], $data->data->category); } @@ -288,6 +302,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body); + $this->assertEquals('mf2+html', $data->{'source-format'}); $this->assertContains('indieweb', $data->data->category); $this->assertContains('xray', $data->data->category); $this->assertEquals(2, count($data->data->category)); @@ -300,6 +315,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body); + $this->assertEquals('mf2+html', $data->{'source-format'}); $this->assertEquals('unknown', $data->data->type); } @@ -310,6 +326,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); + $this->assertEquals('mf2+html', $data['source-format']); $this->assertEquals('entry', $data['data']['type']); $this->assertEquals('http://example.com/100', $data['data']['in-reply-to'][0]); } @@ -321,6 +338,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); + $this->assertEquals('mf2+html', $data['source-format']); $this->assertEquals('entry', $data['data']['type']); $this->assertEquals('reply', $data['data']['post-type']); $this->assertEquals('http://example.com/100', $data['data']['in-reply-to'][0]); @@ -336,6 +354,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); + $this->assertEquals('mf2+html', $data['source-format']); $this->assertEquals('entry', $data['data']['type']); $this->assertEquals('http://alice.example.com/', $data['data']['category'][0]); } @@ -347,6 +366,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); + $this->assertEquals('mf2+html', $data['source-format']); $this->assertEquals('entry', $data['data']['type']); $this->assertEquals('http://alice.example.com/', $data['data']['category'][0]); $this->assertArrayHasKey('http://alice.example.com/', $data['data']['refs']); @@ -362,6 +382,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); + $this->assertEquals('mf2+html', $data['source-format']); $this->assertEquals('entry', $data['data']['type']); $this->assertEquals('http://syndicated.example/', $data['data']['syndication'][0]); } @@ -373,6 +394,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body); + $this->assertEquals('mf2+html', $data->{'source-format'}); $this->assertObjectNotHasAttribute('content', $data->data); $this->assertEquals('This is a Post', $data->data->name); } @@ -384,6 +406,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); + $this->assertEquals('mf2+html', $data['source-format']); $this->assertEquals('entry', $data['data']['type']); $this->assertEquals('Hello World', $data['data']['content']['text']); } @@ -395,6 +418,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); + $this->assertEquals('mf2+html', $data['source-format']); $this->assertEquals('entry', $data['data']['type']); $this->assertEquals('rsvp', $data['data']['post-type']); $this->assertEquals('I\'ll be there!', $data['data']['content']['text']); @@ -408,6 +432,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); + $this->assertEquals('mf2+html', $data['source-format']); $this->assertEquals('entry', $data['data']['type']); $this->assertEquals('Primary Post', $data['data']['name']); } @@ -419,6 +444,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); + $this->assertEquals('mf2+html', $data['source-format']); $this->assertEquals('entry', $data['data']['type']); $this->assertEquals('Hello World', $data['data']['content']['text']); } @@ -430,6 +456,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); + $this->assertEquals('mf2+html', $data['source-format']); $this->assertEquals('entry', $data['data']['type']); $this->assertEquals('Hello World', $data['data']['content']['text']); } @@ -441,6 +468,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); + $this->assertEquals('mf2+html', $data['source-format']); $this->assertEquals('entry', $data['data']['type']); $this->assertEquals('Hello World', $data['data']['content']['text']); } @@ -452,6 +480,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); + $this->assertEquals('mf2+html', $data['source-format']); $this->assertEquals('entry', $data['data']['type']); } @@ -463,6 +492,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); + $this->assertEquals('mf2+html', $data['source-format']); $this->assertEquals('event', $data['data']['type']); $this->assertEquals('event', $data['data']['post-type']); $this->assertEquals('Homebrew Website Club', $data['data']['name']); @@ -482,6 +512,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); + $this->assertEquals('mf2+html', $data['source-format']); $this->assertEquals('event', $data['data']['type']); $this->assertEquals('event', $data['data']['post-type']); $this->assertEquals('Homebrew Website Club', $data['data']['name']); @@ -500,7 +531,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); - + $this->assertEquals('mf2+html', $data['source-format']); $this->assertEquals('event', $data['data']['type']); $this->assertEquals('event', $data['data']['post-type']); $this->assertEquals('Homebrew Website Club', $data['data']['name']); @@ -520,7 +551,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); - + $this->assertEquals('mf2+html', $data['source-format']); $this->assertEquals('review', $data['data']['type']); $this->assertEquals('review', $data['data']['post-type']); $this->assertEquals('Review', $data['data']['name']); @@ -544,7 +575,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); - + $this->assertEquals('mf2+html', $data['source-format']); $this->assertEquals('review', $data['data']['type']); $this->assertEquals('review', $data['data']['post-type']); $this->assertEquals('Review', $data['data']['name']); @@ -566,7 +597,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); - + $this->assertEquals('mf2+html', $data['source-format']); $this->assertEquals('review', $data['data']['type']); $this->assertEquals('review', $data['data']['post-type']); $this->assertEquals('Not great', $data['data']['name']); @@ -588,6 +619,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); + $this->assertEquals('mf2+html', $data['source-format']); $this->assertEquals('recipe', $data['data']['type']); $this->assertEquals('recipe', $data['data']['post-type']); $this->assertEquals('Cookie Recipe', $data['data']['name']); @@ -606,6 +638,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); + $this->assertEquals('mf2+html', $data['source-format']); $this->assertEquals('entry', $data['data']['type']); $this->assertEquals('https://www.facebook.com/555707837940351#tantek', $data['data']['url']); $this->assertContains('https://www.facebook.com/tantek.celik', $data['data']['invitee']); @@ -620,6 +653,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); + $this->assertEquals('mf2+html', $data['source-format']); $this->assertEquals('entry', $data['data']['type']); $this->assertEquals('note', $data['data']['post-type']); $this->assertEquals('Comment text', $data['data']['content']['text']); @@ -634,6 +668,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); + $this->assertEquals('mf2+html', $data['source-format']); $this->assertEquals('entry', $data['data']['type']); $this->assertEquals('http://source.example.com/fragment-id', $data['data']['url']); $this->assertFalse($data['info']['found_fragment']); @@ -647,6 +682,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); + $this->assertEquals('mf2+html', $data['source-format']); $this->assertEquals('entry', $data['data']['type']); $venue = $data['data']['checkin']; $this->assertEquals('checkin', $data['data']['post-type']); @@ -667,6 +703,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); + $this->assertEquals('mf2+html', $data['source-format']); $this->assertEquals('entry', $data['data']['type']); $this->assertEquals('checkin', $data['data']['post-type']); $venue = $data['data']['checkin']; @@ -684,6 +721,8 @@ class ParseTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); + $this->assertEquals(200, $data['code']); + $this->assertEquals('xkcd', $data['source-format']); $this->assertEquals('entry', $data['data']['type']); $this->assertEquals('photo', $data['data']['post-type']); $this->assertEquals('http://xkcd.com/1810/', $data['data']['url']); @@ -728,6 +767,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); + $this->assertEquals('mf2+json', $data['source-format']); $this->assertEquals('Hello World', $data['data']['content']['text']); $this->assertEquals('http://example.com/photo.jpg', $data['data']['photo'][0]); @@ -747,6 +787,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); + $this->assertEquals('mf2+json', $data['source-format']); $this->assertEquals('Hello World', $data['data']['content']['text']); $this->assertEquals('Hello World', $data['data']['content']['html']); $this->assertEquals('http://example.com/photo.jpg', $data['data']['photo'][0]); @@ -759,6 +800,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body, true); + $this->assertEquals('mf2+html', $data['source-format']); $this->assertEquals('app', $data['data']['type']); $this->assertEquals('http://source.example.com/images/quill.png', $data['data']['logo']); $this->assertEquals('Quill', $data['data']['name']); @@ -774,6 +816,7 @@ class ParseTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body); + $this->assertEquals('mf2+html', $data->{'source-format'}); $this->assertEquals('app', $data->data->type); $this->assertEquals('http://source.example.com/images/quill.png', $data->data->logo); $this->assertEquals('Quill', $data->data->name); diff --git a/tests/TwitterTest.php b/tests/TwitterTest.php index cfe0e68..b498f18 100644 --- a/tests/TwitterTest.php +++ b/tests/TwitterTest.php @@ -31,6 +31,8 @@ class TwitterTest extends PHPUnit_Framework_TestCase { $data = $this->parse(['url' => $url, 'body' => $json]); + $this->assertEquals('twitter', $data['source-format']); + $this->assertEquals('entry', $data['data']['type']); $this->assertEquals('aaronpk dev', $data['data']['author']['name']); $this->assertEquals('pkdev', $data['data']['author']['nickname']); @@ -54,6 +56,8 @@ class TwitterTest extends PHPUnit_Framework_TestCase { $data = $this->parse(['url' => $url, 'body' => $json]); $this->assertEquals(null, $data['code']); // no code is expected if we pass in the body + $this->assertEquals('twitter', $data['source-format']); + $this->assertEquals('https://twitter.com/pkdev/status/818913630569664512', $data['url']); $this->assertEquals('entry', $data['data']['type']); $this->assertEquals('note', $data['data']['post-type']); @@ -77,6 +81,7 @@ class TwitterTest extends PHPUnit_Framework_TestCase { $data = $this->parse(['url' => $url, 'body' => $json]); + $this->assertEquals('twitter', $data['source-format']); $this->assertEquals('entry', $data['data']['type']); $this->assertEquals('Here 🎉 have an emoji', $data['data']['content']['text']); } @@ -158,6 +163,7 @@ class TwitterTest extends PHPUnit_Framework_TestCase { $data = $this->parse(['url' => $url, 'body' => $json]); + $this->assertEquals('twitter', $data['source-format']); $this->assertEquals('entry', $data['data']['type']); $this->assertEquals('repost', $data['data']['post-type']); $this->assertArrayNotHasKey('content', $data['data']); @@ -212,6 +218,7 @@ class TwitterTest extends PHPUnit_Framework_TestCase { list($url, $json) = $this->loadTweet('streaming-tweet-with-link'); $data = $this->parse(['url' => $url, 'body' => $json]); + $this->assertEquals('twitter', $data['source-format']); $this->assertEquals('what happens if i include a link like https://kmikeym.com', $data['data']['content']['text']); $this->assertEquals('what happens if i include a link like https://kmikeym.com', $data['data']['content']['html']); } @@ -269,6 +276,7 @@ Woke up this morning feeling compelled to run to Corona… http://tantek.com/201 list($url, $json) = $this->loadTweet('streaming-tweet-reply'); $data = $this->parse(['url' => $url, 'body' => $json]); + $this->assertEquals('twitter', $data['source-format']); $this->assertEquals('https://twitter.com/anomalily/status/967024586423386112', $data['data']['in-reply-to'][0]); } @@ -276,6 +284,7 @@ Woke up this morning feeling compelled to run to Corona… http://tantek.com/201 list($url, $json) = $this->loadTweet('967046438822674432'); $data = $this->parse(['url' => $url, 'body' => $json]); + $this->assertEquals('twitter', $data['source-format']); $this->assertEquals('https://twitter.com/anomalily/status/967024586423386112', $data['data']['in-reply-to'][0]); }