Browse Source

Merge pull request #107 from Lohn/main

Updates to support php 8 anc CI
pull/108/head
Aaron Parecki 3 years ago
committed by GitHub
parent
commit
b136e424df
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
26 changed files with 5909 additions and 4683 deletions
  1. +42
    -0
      .github/workflows/php.yml
  2. +2
    -0
      .gitignore
  3. +5
    -15
      README.md
  4. +13
    -11
      composer.json
  5. +1540
    -607
      composer.lock
  6. +1
    -2
      composer.production.json
  7. +3
    -37
      lib/XRay/Formats/Facebook.php
  8. +3
    -0
      lib/XRay/Formats/HTML.php
  9. +240
    -225
      tests/ActivityStreamsTest.php
  10. +197
    -182
      tests/AuthorTest.php
  11. +0
    -75
      tests/FacebookTest.php
  12. +523
    -493
      tests/FeedTest.php
  13. +227
    -188
      tests/FetchTest.php
  14. +27
    -22
      tests/FetchTestDisabled.php
  15. +234
    -217
      tests/FindFeedsTest.php
  16. +132
    -124
      tests/GitHubTest.php
  17. +61
    -50
      tests/HackernewsTest.php
  18. +37
    -31
      tests/HelpersTest.php
  19. +318
    -292
      tests/InstagramTest.php
  20. +57
    -50
      tests/LibraryTest.php
  21. +42
    -36
      tests/MediaTypeTest.php
  22. +1298
    -1196
      tests/ParseTest.php
  23. +415
    -389
      tests/SanitizeTest.php
  24. +165
    -150
      tests/TokenTest.php
  25. +319
    -287
      tests/TwitterTest.php
  26. +8
    -4
      tests/bootstrap.php

+ 42
- 0
.github/workflows/php.yml View File

@ -0,0 +1,42 @@
name: PHP Composer
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
build:
strategy:
matrix:
php-versions: ["7.2", "7.3", "7.4", "8.0"]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup PHP, with composer and extensions
uses: shivammathur/setup-php@v2 #https://github.com/shivammathur/setup-php
with:
php-version: ${{ matrix.php-versions }}
extensions: mbstring, simplexml, dom, curl
coverage: pcov
- name: Validate composer.json and composer.lock
run: composer validate --strict
- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v2
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-
- name: Install dependencies
run: composer update --prefer-dist --no-progress
- name: Run test suite
run: composer run-script test

+ 2
- 0
.gitignore View File

@ -4,3 +4,5 @@ vendor/
php_errors.log
XRay-*.json
.gcloudignore
.phpunit.result.cache
.vscode

+ 5
- 15
README.md View File

@ -13,7 +13,7 @@ XRay will parse content in the following formats. First the URL is checked again
* GitHub
* XKCD
* Hackernews
* Facebook (public events)
* ~~Facebook (public events)~~ <REMOVED>
If the contents of the URL is XML or JSON, then XRay will parse the Atom, RSS or JSONFeed formats.
@ -245,7 +245,7 @@ url=https://aaronparecki.com/2016/01/16/11/
&body=<html>....</html>
```
or for Twitter/GitHub/Facebook where you might have JSON,
or for Twitter/GitHub where you might have JSON,
```
POST /parse
@ -282,9 +282,9 @@ url=https://aaronparecki.com/2016/01/16/11/
### API Authentication
XRay uses the Twitter, Github and Facebook APIs to fetch posts, and those API require authentication. In order to keep XRay stateless, it is required that you pass in the credentials to the parse call.
XRay uses the Twitter and Github APIs to fetch posts, and those API require authentication. In order to keep XRay stateless, it is required that you pass in the credentials to the parse call.
You should only send the credentials when the URL you are trying to parse is a Twitter URL, a GitHub URL or a Facebook URL, so you'll want to check for whether the hostname is `twitter.com`, `github.com`, etc. before you include credentials in this call.
You should only send the credentials when the URL you are trying to parse is a Twitter URL or a GitHub URL, so you'll want to check for whether the hostname is `twitter.com`, `github.com`, etc. before you include credentials in this call.
#### Twitter Authentication
@ -304,16 +304,6 @@ XRay uses the GitHub API to fetch GitHub URLs, which provides higher rate limits
* `github_access_token` - A GitHub access token
#### Facebook Authentication
XRay uses the Facebook API to fetch Facebook URLs. You can create a Facebook App on Facebooks developer website.
* facebook_app_id - Your application's App ID
* facebook_app_secret - Your application's App Secret
At this moment, XRay is able to get it's own access token from those credentials.
### Error Response
```json
@ -431,7 +421,7 @@ Other properties are returned in the response at the same level as the `data` pr
* `mf2+json`
* `feed+json`
* `xml`
* `instagram`/`facebook`/`github`/`xkcd`
* `instagram`/`github`/`xkcd`
#### Feeds

+ 13
- 11
composer.json View File

@ -7,13 +7,12 @@
"require": {
"mf2/mf2": "^0.4",
"ezyang/htmlpurifier": "^4.10",
"indieweb/link-rel-parser": "0.1.*",
"dg/twitter-php": "3.6.*",
"p3k/timezone": "*",
"p3k/http": ">=0.1.11",
"cebe/markdown": "1.1.*",
"p3k/picofeed": ">=0.1.39",
"facebook/graph-sdk": "^5.5",
"indieweb/link-rel-parser": "^0.1.0",
"dg/twitter-php": "^3.0.0",
"p3k/timezone": "^0.1.0",
"p3k/http": "^0.1.0",
"cebe/markdown": "^1.1.0",
"p3k/picofeed": "^0.1.0",
"masterminds/html5": "^2.3"
},
"autoload": {
@ -26,9 +25,9 @@
]
},
"require-dev": {
"league/plates": "3.*",
"league/route": "1.*",
"phpunit/phpunit": "4.8.*"
"league/plates": "^3.0",
"league/route": "^1.0",
"phpunit/phpunit": "^8.0.0|^9.0.0"
},
"autoload-dev": {
"files": [
@ -39,5 +38,8 @@
"controllers/Feeds.php",
"controllers/Certbot.php"
]
},
"scripts": {
"test": "vendor/bin/phpunit"
}
}
}

+ 1540
- 607
composer.lock
File diff suppressed because it is too large
View File


+ 1
- 2
composer.production.json View File

@ -15,7 +15,6 @@
"p3k/http": ">=0.1.8",
"cebe/markdown": "1.1.*",
"p3k/picofeed": ">=0.1.35",
"facebook/graph-sdk": "^5.5",
"masterminds/html5": "^2.3"
},
"autoload": {
@ -36,4 +35,4 @@
"require-dev": {
"phpunit/phpunit": "4.8.*"
}
}
}

+ 3
- 37
lib/XRay/Formats/Facebook.php View File

@ -79,46 +79,12 @@ class Facebook extends Format {
public static function fetch($url, $creds) {
$parts = self::extract_url_parts($url);
if(!$parts or $parts['api_uri'] == false) {
return [
'error' => 'unsupported_url',
'error_description' => 'This Facebook URL is not supported',
'error_code' => 400,
];
}
$fb = new \Facebook\Facebook(array(
'app_id' => $creds['facebook_app_id'],
'app_secret' => $creds['facebook_app_secret'],
'default_graph_version' => 'v2.9',
));
$fbApp = new \Facebook\FacebookApp($creds['facebook_app_id'], $creds['facebook_app_secret']);
$token = $fbApp->getAccessToken();
$request = new \Facebook\FacebookRequest($fbApp, $token, 'GET', $parts['api_uri']);
try {
$response = $fb->getClient()->sendRequest($request);
} catch(\Facebook\Exceptions\FacebookResponseException $e) {
return [
'error' => 'facebook_graph_error',
'error_description' => 'Graph returned an error: ' . $e->getMessage(),
'error_code' => 400,
];
} catch(\Facebook\Exceptions\FacebookSDKException $e) {
return [
'error' => 'facebook_sdk_error',
'error_description' => 'Facebook SDK returned an error: ' . $e->getMessage(),
'error_code' => 400,
];
}
//Disabled Function for now
//TODO: Search all references to this class and remove it.
return [
'code' => 200,
'body' => $response->getDecodedBody(),
'body' => '',
'url' => $url
];
}

+ 3
- 0
lib/XRay/Formats/HTML.php View File

@ -25,6 +25,9 @@ class HTML extends Format {
// attempt to parse the page as HTML
$doc = new DOMDocument();
if (empty($html)) {
$html=' '; // ugly hack to make DOMDocument happy
};
@$doc->loadHTML(self::toHtmlEntities($html));
if(!$doc) {

+ 240
- 225
tests/ActivityStreamsTest.php View File

@ -2,230 +2,245 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class ActivityStreamsTest extends PHPUnit_Framework_TestCase {
private $http;
public function setUp() {
$this->client = new Parse();
$this->client->http = new p3k\HTTP\Test(dirname(__FILE__).'/data/');
$this->client->mc = null;
}
private function parse($params) {
$request = new Request($params);
$response = new Response();
return $this->client->parse($request, $response);
}
public function testAuthorProfile() {
$url = 'http://activitystreams.example/aaronpk';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body, true);
$this->assertEquals('activity+json', $data['source-format']);
$this->assertEquals('card', $data['data']['type']);
$this->assertEquals('aaronpk', $data['data']['name']);
$this->assertEquals('https://aaronparecki.com/images/profile.jpg', $data['data']['photo']);
$this->assertEquals('https://aaronparecki.com/', $data['data']['url']);
}
public function testNoteWithTags() {
$url = 'http://activitystreams.example/note.json';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body, true);
$this->assertEquals('activity+json', $data['source-format']);
$this->assertEquals('note', $data['data']['post-type']);
$this->assertEquals($url, $data['data']['url']);
$this->assertEquals('2018-07-12T13:02:04-07:00', $data['data']['published']);
$this->assertEquals('This is the text content of an ActivityStreams note', $data['data']['content']['text']);
$this->assertArrayNotHasKey('html', $data['data']['content']);
$this->assertSame(['activitystreams'], $data['data']['category']);
$this->assertEquals('aaronpk', $data['data']['author']['name']);
$this->assertEquals('https://aaronparecki.com/images/profile.jpg', $data['data']['author']['photo']);
$this->assertEquals('https://aaronparecki.com/', $data['data']['author']['url']);
}
public function testArticle() {
$url = 'http://activitystreams.example/article.json';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body, true);
$this->assertEquals('activity+json', $data['source-format']);
$this->assertEquals('article', $data['data']['post-type']);
$this->assertEquals($url, $data['data']['url']);
$this->assertEquals('An Article', $data['data']['name']);
$this->assertEquals('This is the content of an ActivityStreams article', $data['data']['content']['text']);
$this->assertEquals('<p>This is the content of an <b>ActivityStreams</b> article</p>', $data['data']['content']['html']);
$this->assertEquals('aaronpk', $data['data']['author']['name']);
$this->assertEquals('https://aaronparecki.com/images/profile.jpg', $data['data']['author']['photo']);
$this->assertEquals('https://aaronparecki.com/', $data['data']['author']['url']);
}
public function testPhoto() {
$url = 'http://activitystreams.example/photo.json';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body, true);
$this->assertEquals('activity+json', $data['source-format']);
$this->assertEquals($url, $data['data']['url']);
$this->assertEquals('photo', $data['data']['post-type']);
$this->assertEquals('2018-07-12T13:02:04-07:00', $data['data']['published']);
$this->assertEquals('This is the text content of an ActivityStreams photo', $data['data']['content']['text']);
$this->assertArrayNotHasKey('html', $data['data']['content']);
$this->assertSame(['activitystreams'], $data['data']['category']);
$this->assertSame(['https://aaronparecki.com/2018/06/28/26/photo.jpg'], $data['data']['photo']);
}
public function testVideo() {
$url = 'http://activitystreams.example/video.json';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body, true);
$this->assertEquals('activity+json', $data['source-format']);
$this->assertEquals('video', $data['data']['post-type']);
$this->assertEquals('2018-07-12T13:02:04-07:00', $data['data']['published']);
$this->assertSame(['https://aaronparecki.com/2018/07/21/19/video.mp4'], $data['data']['video']);
}
public function testReply() {
$url = 'http://activitystreams.example/reply.json';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body, true);
$this->assertEquals('activity+json', $data['source-format']);
$this->assertEquals('reply', $data['data']['post-type']);
$this->assertEquals('2018-07-12T13:02:04-07:00', $data['data']['published']);
$this->assertArrayNotHasKey('category', $data['data']); // should not include the person-tag
// For now, don't fetch the reply context
$this->assertEquals(['http://activitystreams.example/note.json'], $data['data']['in-reply-to']);
}
public function testCustomEmoji() {
$url = 'http://activitystreams.example/custom-emoji.json';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body, true);
$this->assertEquals('activity+json', $data['source-format']);
$this->assertEquals('note', $data['data']['post-type']);
$this->assertEquals("https://mastodon.social/@Gargron/100465999501820229", $data['data']['url']);
$this->assertEquals('2018-07-30T22:24:54+00:00', $data['data']['published']);
$this->assertEquals(':yikes:', $data['data']['content']['text']);
$this->assertEquals('<p><img src="https://files.mastodon.social/custom_emojis/images/000/031/275/original/yikes.png" alt=":yikes:" title=":yikes:" height="24" class="xray-emoji"></p>', $data['data']['content']['html']);
$this->assertEquals('Eugen', $data['data']['author']['name']);
$this->assertEquals('Gargron', $data['data']['author']['nickname']);
$this->assertEquals('https://files.mastodon.social/accounts/avatars/000/000/001/original/eb9e00274b135808.png', $data['data']['author']['photo']);
$this->assertEquals('https://mastodon.social/@Gargron', $data['data']['author']['url']);
}
public function testRelAlternatePriority() {
$url = 'http://source.example.com/rel-alternate-as2';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body, true);
$this->assertEquals('activity+json', $data['source-format']);
$this->assertEquals('http://activitystreams.example/note.json', $data['parsed-url']);
$this->assertEquals('http://source.example.com/rel-alternate-as2', $data['url']);
$this->assertEquals('note', $data['data']['post-type']);
$this->assertEquals('2018-07-12T13:02:04-07:00', $data['data']['published']);
$this->assertEquals('This is the text content of an ActivityStreams note', $data['data']['content']['text']);
$this->assertArrayNotHasKey('html', $data['data']['content']);
$this->assertSame(['activitystreams'], $data['data']['category']);
}
public function testSensitiveContent() {
$url = 'http://activitystreams.example/sensitive.json';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body, true);
$this->assertEquals('activity+json', $data['source-format']);
$this->assertEquals('note', $data['data']['post-type']);
$this->assertEquals('sensitive topic', $data['data']['summary']);
$this->assertEquals('This is the text content of a sensitive ActivityStreams note', $data['data']['content']['text']);
$this->assertArrayNotHasKey('name', $data['data']);
}
public function testRepost() {
$url = 'http://activitystreams.example/repost.json';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body, true);
$this->assertEquals('activity+json', $data['source-format']);
$this->assertEquals('repost', $data['data']['post-type']);
$this->assertArrayNotHasKey('content', $data['data']);
$this->assertArrayNotHasKey('name', $data['data']);
$this->assertEquals('Gargron', $data['data']['author']['nickname']);
$this->assertEquals(['http://activitystreams.example/note.json'], $data['data']['repost-of']);
$this->assertArrayHasKey('http://activitystreams.example/note.json', $data['data']['refs']);
$this->assertEquals('This is the text content of an ActivityStreams note', $data['data']['refs']['http://activitystreams.example/note.json']['content']['text']);
}
public function testLike() {
$url = 'http://activitystreams.example/like.json';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body, true);
$this->assertEquals('activity+json', $data['source-format']);
$this->assertEquals('like', $data['data']['post-type']);
$this->assertArrayNotHasKey('content', $data['data']);
$this->assertArrayNotHasKey('name', $data['data']);
$this->assertEquals('Gargron', $data['data']['author']['nickname']);
$this->assertEquals(['http://activitystreams.example/note.json'], $data['data']['like-of']);
$this->assertArrayHasKey('http://activitystreams.example/note.json', $data['data']['refs']);
$this->assertEquals('This is the text content of an ActivityStreams note', $data['data']['refs']['http://activitystreams.example/note.json']['content']['text']);
}
public function testNoteWrappedInCreate() {
$url = 'http://activitystreams.example/create.json';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body, true);
$this->assertEquals('activity+json', $data['source-format']);
$this->assertEquals('reply', $data['data']['post-type']);
$this->assertEquals('https://toot.cat/@jamey/100471682482196371', $data['data']['url']);
$this->assertEquals('2018-07-31T22:30:09+00:00', $data['data']['published']);
$this->assertEquals('@darius Huh, I just have never encountered anyone using the phrase generically like that.But you might consider writing IndieWeb.org-style bots (Atom+WebSub, and optionally WebMention if you want them to be interactive), and then using https://fed.brid.gy/ as an alternative to implementing ActivityPub yourself...', $data['data']['content']['text']);
$this->assertEquals('https://social.tinysubversions.com/users/darius/statuses/100471614681787834', $data['data']['in-reply-to'][0]);
$this->assertEquals('Jamey Sharp', $data['data']['author']['name']);
$this->assertEquals('https://s3-us-west-2.amazonaws.com/tootcatapril2017/accounts/avatars/000/013/259/original/c904452a8411e4f5.jpg', $data['data']['author']['photo']);
$this->assertEquals('https://toot.cat/@jamey', $data['data']['author']['url']);
}
class ActivityStreamsTest extends PHPUnit\Framework\TestCase
{
private $http;
public function setUp(): void
{
$this->client = new Parse();
$this->client->http = new p3k\HTTP\Test(dirname(__FILE__).'/data/');
$this->client->mc = null;
}
private function parse($params)
{
$request = new Request($params);
$response = new Response();
return $this->client->parse($request, $response);
}
public function testAuthorProfile()
{
$url = 'http://activitystreams.example/aaronpk';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body, true);
$this->assertEquals('activity+json', $data['source-format']);
$this->assertEquals('card', $data['data']['type']);
$this->assertEquals('aaronpk', $data['data']['name']);
$this->assertEquals('https://aaronparecki.com/images/profile.jpg', $data['data']['photo']);
$this->assertEquals('https://aaronparecki.com/', $data['data']['url']);
}
public function testNoteWithTags()
{
$url = 'http://activitystreams.example/note.json';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body, true);
$this->assertEquals('activity+json', $data['source-format']);
$this->assertEquals('note', $data['data']['post-type']);
$this->assertEquals($url, $data['data']['url']);
$this->assertEquals('2018-07-12T13:02:04-07:00', $data['data']['published']);
$this->assertEquals('This is the text content of an ActivityStreams note', $data['data']['content']['text']);
$this->assertArrayNotHasKey('html', $data['data']['content']);
$this->assertSame(['activitystreams'], $data['data']['category']);
$this->assertEquals('aaronpk', $data['data']['author']['name']);
$this->assertEquals('https://aaronparecki.com/images/profile.jpg', $data['data']['author']['photo']);
$this->assertEquals('https://aaronparecki.com/', $data['data']['author']['url']);
}
public function testArticle()
{
$url = 'http://activitystreams.example/article.json';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body, true);
$this->assertEquals('activity+json', $data['source-format']);
$this->assertEquals('article', $data['data']['post-type']);
$this->assertEquals($url, $data['data']['url']);
$this->assertEquals('An Article', $data['data']['name']);
$this->assertEquals('This is the content of an ActivityStreams article', $data['data']['content']['text']);
$this->assertEquals('<p>This is the content of an <b>ActivityStreams</b> article</p>', $data['data']['content']['html']);
$this->assertEquals('aaronpk', $data['data']['author']['name']);
$this->assertEquals('https://aaronparecki.com/images/profile.jpg', $data['data']['author']['photo']);
$this->assertEquals('https://aaronparecki.com/', $data['data']['author']['url']);
}
public function testPhoto()
{
$url = 'http://activitystreams.example/photo.json';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body, true);
$this->assertEquals('activity+json', $data['source-format']);
$this->assertEquals($url, $data['data']['url']);
$this->assertEquals('photo', $data['data']['post-type']);
$this->assertEquals('2018-07-12T13:02:04-07:00', $data['data']['published']);
$this->assertEquals('This is the text content of an ActivityStreams photo', $data['data']['content']['text']);
$this->assertArrayNotHasKey('html', $data['data']['content']);
$this->assertSame(['activitystreams'], $data['data']['category']);
$this->assertSame(['https://aaronparecki.com/2018/06/28/26/photo.jpg'], $data['data']['photo']);
}
public function testVideo()
{
$url = 'http://activitystreams.example/video.json';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body, true);
$this->assertEquals('activity+json', $data['source-format']);
$this->assertEquals('video', $data['data']['post-type']);
$this->assertEquals('2018-07-12T13:02:04-07:00', $data['data']['published']);
$this->assertSame(['https://aaronparecki.com/2018/07/21/19/video.mp4'], $data['data']['video']);
}
public function testReply()
{
$url = 'http://activitystreams.example/reply.json';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body, true);
$this->assertEquals('activity+json', $data['source-format']);
$this->assertEquals('reply', $data['data']['post-type']);
$this->assertEquals('2018-07-12T13:02:04-07:00', $data['data']['published']);
$this->assertArrayNotHasKey('category', $data['data']); // should not include the person-tag
// For now, don't fetch the reply context
$this->assertEquals(['http://activitystreams.example/note.json'], $data['data']['in-reply-to']);
}
public function testCustomEmoji()
{
$url = 'http://activitystreams.example/custom-emoji.json';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body, true);
$this->assertEquals('activity+json', $data['source-format']);
$this->assertEquals('note', $data['data']['post-type']);
$this->assertEquals("https://mastodon.social/@Gargron/100465999501820229", $data['data']['url']);
$this->assertEquals('2018-07-30T22:24:54+00:00', $data['data']['published']);
$this->assertEquals(':yikes:', $data['data']['content']['text']);
$this->assertEquals('<p><img src="https://files.mastodon.social/custom_emojis/images/000/031/275/original/yikes.png" alt=":yikes:" title=":yikes:" height="24" class="xray-emoji"></p>', $data['data']['content']['html']);
$this->assertEquals('Eugen', $data['data']['author']['name']);
$this->assertEquals('Gargron', $data['data']['author']['nickname']);
$this->assertEquals('https://files.mastodon.social/accounts/avatars/000/000/001/original/eb9e00274b135808.png', $data['data']['author']['photo']);
$this->assertEquals('https://mastodon.social/@Gargron', $data['data']['author']['url']);
}
public function testRelAlternatePriority()
{
$url = 'http://source.example.com/rel-alternate-as2';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body, true);
$this->assertEquals('activity+json', $data['source-format']);
$this->assertEquals('http://activitystreams.example/note.json', $data['parsed-url']);
$this->assertEquals('http://source.example.com/rel-alternate-as2', $data['url']);
$this->assertEquals('note', $data['data']['post-type']);
$this->assertEquals('2018-07-12T13:02:04-07:00', $data['data']['published']);
$this->assertEquals('This is the text content of an ActivityStreams note', $data['data']['content']['text']);
$this->assertArrayNotHasKey('html', $data['data']['content']);
$this->assertSame(['activitystreams'], $data['data']['category']);
}
public function testSensitiveContent()
{
$url = 'http://activitystreams.example/sensitive.json';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body, true);
$this->assertEquals('activity+json', $data['source-format']);
$this->assertEquals('note', $data['data']['post-type']);
$this->assertEquals('sensitive topic', $data['data']['summary']);
$this->assertEquals('This is the text content of a sensitive ActivityStreams note', $data['data']['content']['text']);
$this->assertArrayNotHasKey('name', $data['data']);
}
public function testRepost()
{
$url = 'http://activitystreams.example/repost.json';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body, true);
$this->assertEquals('activity+json', $data['source-format']);
$this->assertEquals('repost', $data['data']['post-type']);
$this->assertArrayNotHasKey('content', $data['data']);
$this->assertArrayNotHasKey('name', $data['data']);
$this->assertEquals('Gargron', $data['data']['author']['nickname']);
$this->assertEquals(['http://activitystreams.example/note.json'], $data['data']['repost-of']);
$this->assertArrayHasKey('http://activitystreams.example/note.json', $data['data']['refs']);
$this->assertEquals('This is the text content of an ActivityStreams note', $data['data']['refs']['http://activitystreams.example/note.json']['content']['text']);
}
public function testLike()
{
$url = 'http://activitystreams.example/like.json';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body, true);
$this->assertEquals('activity+json', $data['source-format']);
$this->assertEquals('like', $data['data']['post-type']);
$this->assertArrayNotHasKey('content', $data['data']);
$this->assertArrayNotHasKey('name', $data['data']);
$this->assertEquals('Gargron', $data['data']['author']['nickname']);
$this->assertEquals(['http://activitystreams.example/note.json'], $data['data']['like-of']);
$this->assertArrayHasKey('http://activitystreams.example/note.json', $data['data']['refs']);
$this->assertEquals('This is the text content of an ActivityStreams note', $data['data']['refs']['http://activitystreams.example/note.json']['content']['text']);
}
public function testNoteWrappedInCreate()
{
$url = 'http://activitystreams.example/create.json';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body, true);
$this->assertEquals('activity+json', $data['source-format']);
$this->assertEquals('reply', $data['data']['post-type']);
$this->assertEquals('https://toot.cat/@jamey/100471682482196371', $data['data']['url']);
$this->assertEquals('2018-07-31T22:30:09+00:00', $data['data']['published']);
$this->assertEquals('@darius Huh, I just have never encountered anyone using the phrase generically like that.But you might consider writing IndieWeb.org-style bots (Atom+WebSub, and optionally WebMention if you want them to be interactive), and then using https://fed.brid.gy/ as an alternative to implementing ActivityPub yourself...', $data['data']['content']['text']);
$this->assertEquals('https://social.tinysubversions.com/users/darius/statuses/100471614681787834', $data['data']['in-reply-to'][0]);
$this->assertEquals('Jamey Sharp', $data['data']['author']['name']);
$this->assertEquals('https://s3-us-west-2.amazonaws.com/tootcatapril2017/accounts/avatars/000/013/259/original/c904452a8411e4f5.jpg', $data['data']['author']['photo']);
$this->assertEquals('https://toot.cat/@jamey', $data['data']['author']['url']);
}
}

+ 197
- 182
tests/AuthorTest.php View File

@ -2,186 +2,201 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class AuthorTest extends PHPUnit_Framework_TestCase {
private $http;
public function setUp() {
$this->client = new Parse();
$this->client->http = new p3k\HTTP\Test(dirname(__FILE__).'/data/');
$this->client->mc = null;
}
private function parse($params) {
$request = new Request($params);
$response = new Response();
return $this->client->parse($request, $response);
}
public function testHEntryAuthorIsName() {
$url = 'http://author.example.com/h-entry-author-is-name';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$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);
}
public function testHEntryAuthorIsRelLinkToHCardOnPage() {
$url = 'http://author.example.com/h-entry-author-is-rel-link-to-h-card-on-page';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertEquals('http://author.example.com/about', $data->data->author->url);
$this->assertEquals('Author', $data->data->author->name);
$this->assertEquals('http://author.example.com/photo.jpg', $data->data->author->photo);
}
public function testHEntryAuthorIsRelLinkToHCardWithRelMe() {
$url = 'http://author.example.com/h-entry-author-is-rel-link-to-h-card-with-rel-me';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertEquals('http://author.example.com/about-rel-me', $data->data->author->url);
$this->assertEquals('Author Full Name', $data->data->author->name);
$this->assertEquals('http://author.example.com/photo.jpg', $data->data->author->photo);
}
public function testHEntryAuthorIsRelLinkToHCardWithUrlUid() {
$url = 'http://author.example.com/h-entry-author-is-rel-link-to-h-card-with-url-uid';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertEquals('http://author.example.com/about-url-uid', $data->data->author->url);
$this->assertEquals('Author Full Name', $data->data->author->name);
$this->assertEquals('http://author.example.com/photo.jpg', $data->data->author->photo);
}
public function testHEntryAuthorIsUrlToHCardOnPage() {
$url = 'http://author.example.com/h-entry-author-is-url-to-h-card-on-page';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertEquals('http://author.example.com/about', $data->data->author->url);
$this->assertEquals('Author', $data->data->author->name);
$this->assertEquals('http://author.example.com/photo.jpg', $data->data->author->photo);
}
public function testHEntryAuthorIsUrlToHCardWithMultipleLinks() {
$url = 'http://author.example.com/h-entry-author-is-url-to-h-card-with-multiple-links';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertEquals('http://author.example.com/about-with-multiple-urls', $data->data->author->url);
$this->assertEquals('Author Full Name', $data->data->author->name);
$this->assertEquals('http://author.example.com/photo.jpg', $data->data->author->photo);
}
public function testHEntryAuthorIsUrlToHCardWithNoURL() {
$url = 'http://author.example.com/h-entry-author-is-url-to-h-card-with-no-url';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertEquals('http://author.example.com/about-no-url', $data->data->author->url);
$this->assertEquals('Author Full Name', $data->data->author->name);
$this->assertEquals('http://author.example.com/photo.jpg', $data->data->author->photo);
}
public function testHEntryHasHCardAndUrlAuthor() {
$url = 'http://author.example.com/h-entry-has-h-card-and-url-author';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertEquals('http://author.example.com/about', $data->data->author->url);
$this->assertEquals('Author', $data->data->author->name);
$this->assertEquals('http://author.example.com/photo.jpg', $data->data->author->photo);
}
public function testHEntryHasHCardAuthor() {
$url = 'http://author.example.com/h-entry-has-h-card-author';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertEquals('http://author.example.com/about', $data->data->author->url);
$this->assertEquals('Author', $data->data->author->name);
$this->assertEquals('http://author.example.com/photo.jpg', $data->data->author->photo);
}
public function testPageIsHCard() {
$url = 'http://author.example.com/about';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertEquals('card', $data->data->type);
$this->assertEquals('http://author.example.com/about', $data->data->url);
$this->assertEquals('Author Full Name', $data->data->name);
$this->assertEquals('http://author.example.com/photo.jpg', $data->data->photo);
}
public function testPageIsHCardWithNoURL() {
$url = 'http://author.example.com/about-no-url';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertEquals('card', $data->data->type);
$this->assertEquals('Author Full Name', $data->data->name);
$this->assertEquals($url, $data->data->url);
$this->assertEquals('http://author.example.com/photo.jpg', $data->data->photo);
}
public function testHEntryAuthorIs0() {
$url = 'http://author.example.com/author-name-is-0';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertEquals('card', $data->data->type);
$this->assertEquals('http://author.example.com/photo.jpg', $data->data->photo);
$this->assertEquals('0', $data->data->name);
}
/*
public function testHFeedHasHCardAuthor() {
class AuthorTest extends PHPUnit\Framework\TestCase
{
private $http;
public function setUp(): void
{
$this->client = new Parse();
$this->client->http = new p3k\HTTP\Test(dirname(__FILE__).'/data/');
$this->client->mc = null;
}
private function parse($params)
{
$request = new Request($params);
$response = new Response();
return $this->client->parse($request, $response);
}
public function testHEntryAuthorIsName()
{
$url = 'http://author.example.com/h-entry-author-is-name';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$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);
}
public function testHEntryAuthorIsRelLinkToHCardOnPage()
{
$url = 'http://author.example.com/h-entry-author-is-rel-link-to-h-card-on-page';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertEquals('http://author.example.com/about', $data->data->author->url);
$this->assertEquals('Author', $data->data->author->name);
$this->assertEquals('http://author.example.com/photo.jpg', $data->data->author->photo);
}
public function testHEntryAuthorIsRelLinkToHCardWithRelMe()
{
$url = 'http://author.example.com/h-entry-author-is-rel-link-to-h-card-with-rel-me';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertEquals('http://author.example.com/about-rel-me', $data->data->author->url);
$this->assertEquals('Author Full Name', $data->data->author->name);
$this->assertEquals('http://author.example.com/photo.jpg', $data->data->author->photo);
}
public function testHEntryAuthorIsRelLinkToHCardWithUrlUid()
{
$url = 'http://author.example.com/h-entry-author-is-rel-link-to-h-card-with-url-uid';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertEquals('http://author.example.com/about-url-uid', $data->data->author->url);
$this->assertEquals('Author Full Name', $data->data->author->name);
$this->assertEquals('http://author.example.com/photo.jpg', $data->data->author->photo);
}
public function testHEntryAuthorIsUrlToHCardOnPage()
{
$url = 'http://author.example.com/h-entry-author-is-url-to-h-card-on-page';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertEquals('http://author.example.com/about', $data->data->author->url);
$this->assertEquals('Author', $data->data->author->name);
$this->assertEquals('http://author.example.com/photo.jpg', $data->data->author->photo);
}
public function testHEntryAuthorIsUrlToHCardWithMultipleLinks()
{
$url = 'http://author.example.com/h-entry-author-is-url-to-h-card-with-multiple-links';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertEquals('http://author.example.com/about-with-multiple-urls', $data->data->author->url);
$this->assertEquals('Author Full Name', $data->data->author->name);
$this->assertEquals('http://author.example.com/photo.jpg', $data->data->author->photo);
}
public function testHEntryAuthorIsUrlToHCardWithNoURL()
{
$url = 'http://author.example.com/h-entry-author-is-url-to-h-card-with-no-url';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertEquals('http://author.example.com/about-no-url', $data->data->author->url);
$this->assertEquals('Author Full Name', $data->data->author->name);
$this->assertEquals('http://author.example.com/photo.jpg', $data->data->author->photo);
}
public function testHEntryHasHCardAndUrlAuthor()
{
$url = 'http://author.example.com/h-entry-has-h-card-and-url-author';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertEquals('http://author.example.com/about', $data->data->author->url);
$this->assertEquals('Author', $data->data->author->name);
$this->assertEquals('http://author.example.com/photo.jpg', $data->data->author->photo);
}
public function testHEntryHasHCardAuthor()
{
$url = 'http://author.example.com/h-entry-has-h-card-author';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertEquals('http://author.example.com/about', $data->data->author->url);
$this->assertEquals('Author', $data->data->author->name);
$this->assertEquals('http://author.example.com/photo.jpg', $data->data->author->photo);
}
public function testPageIsHCard()
{
$url = 'http://author.example.com/about';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertEquals('card', $data->data->type);
$this->assertEquals('http://author.example.com/about', $data->data->url);
$this->assertEquals('Author Full Name', $data->data->name);
$this->assertEquals('http://author.example.com/photo.jpg', $data->data->photo);
}
public function testPageIsHCardWithNoURL()
{
$url = 'http://author.example.com/about-no-url';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertEquals('card', $data->data->type);
$this->assertEquals('Author Full Name', $data->data->name);
$this->assertEquals($url, $data->data->url);
$this->assertEquals('http://author.example.com/photo.jpg', $data->data->photo);
}
public function testHEntryAuthorIs0()
{
$url = 'http://author.example.com/author-name-is-0';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertEquals('card', $data->data->type);
$this->assertEquals('http://author.example.com/photo.jpg', $data->data->photo);
$this->assertEquals('0', $data->data->name);
}
/*
public function testHFeedHasHCardAuthor() {
$url = 'http://author.example.com/h-feed-has-h-card-author';
$response = $this->parse(['url' => $url]);
@ -192,7 +207,7 @@ class AuthorTest extends PHPUnit_Framework_TestCase {
$this->assertEquals('http://author.example.com/about', $data->data->author->url);
$this->assertEquals('Author', $data->data->author->name);
$this->assertEquals('http://author.example.com/photo.jpg', $data->data->author->photo);
}
*/
}
*/
}

+ 0
- 75
tests/FacebookTest.php View File

@ -1,75 +0,0 @@
<?php
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class FacebookTest extends PHPUnit_Framework_TestCase {
private $http;
public function setUp() {
$this->client = new Parse();
$this->client->mc = null;
}
private function parse($params) {
$request = new Request($params);
$response = new Response();
$result = $this->client->parse($request, $response);
$body = $result->getContent();
$this->assertEquals(200, $result->getStatusCode());
return json_decode($body, true);
}
private function loadFbObject($id) {
return file_get_contents(dirname(__FILE__).'/data/graph.facebook.com/'.$id.'.json');
}
public function testFacebookEventWithHCard() {
$url = 'https://www.facebook.com/events/446197069049722/';
$json = $this->loadFbObject('446197069049722');
$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']);
$this->assertEquals('2017-06-24T09:00:00-0700', $data['data']['start']);
$this->assertEquals('2017-06-25T18:00:00-0700', $data['data']['end']);
$this->assertContains('The seventh annual gathering for independent web creators of all kinds,', $data['data']['summary']);
$this->assertEquals('https://facebook.com/332204056925945', $data['data']['location']);
$card = $data['data']['refs'][0];
$this->assertEquals('card', $card['type']);
$this->assertEquals('https://facebook.com/332204056925945', $card['url']);
$this->assertEquals('Mozilla PDX', $card['name']);
$this->assertEquals('97209', $card['postal-code']);
$this->assertEquals('Portland', $card['locality']);
$this->assertEquals('OR', $card['region']);
$this->assertEquals('1120 NW Couch St, Ste 320', $card['street-address']);
$this->assertEquals('United States', $card['country']);
$this->assertEquals('45.5233192', $card['latitude']);
$this->assertEquals('-122.6824722', $card['longitude']);
}
public function testFacebookEvent() {
$url = 'https://www.facebook.com/events/1596554663924436/';
$json = $this->loadFbObject('1596554663924436');
$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']);
$this->assertEquals('2015-04-22T19:00:00-0400', $data['data']['start']);
$this->assertContains('Are you building your own website? Indie reader?', $data['data']['summary']);
$this->assertEquals('Charging Bull - WeeWork - 25 Broadway, New York, NY 10004', $data['data']['location']);
}
}

+ 523
- 493
tests/FeedTest.php
File diff suppressed because it is too large
View File


+ 227
- 188
tests/FetchTest.php View File

@ -2,192 +2,231 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class FetchTest extends PHPUnit_Framework_TestCase {
private $http;
public function setUp() {
$this->client = new Parse();
$this->client->http = new p3k\HTTP\Test(dirname(__FILE__).'/data/');
$this->client->mc = null;
}
private function parse($params) {
$request = new Request($params);
$response = new Response();
return $this->client->parse($request, $response);
}
public function testRedirectLimit() {
$url = 'http://redirect.example.com/3';
$response = $this->parse([
'url' => $url,
'max_redirects' => 1
]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertObjectHasAttribute('error', $data);
$this->assertEquals('too_many_redirects', $data->error);
$url = 'http://redirect.example.com/2';
$response = $this->parse([
'url' => $url,
'max_redirects' => 1
]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertObjectHasAttribute('error', $data);
$this->assertEquals('too_many_redirects', $data->error);
}
public function testRedirectUnderLimit() {
$url = 'http://redirect.example.com/2';
$response = $this->parse([
'url' => $url,
'max_redirects' => 2
]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertObjectNotHasAttribute('error', $data);
$this->assertEquals(200, $data->code);
$this->assertEquals('The Final Page', $data->data->name);
$this->assertEquals('http://redirect.example.com/0', $data->url);
}
public function testReturnsHTTPStatusCode() {
$url = 'http://redirect.example.com/code-418';
$response = $this->parse([
'url' => $url
]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertObjectNotHasAttribute('error', $data);
$this->assertEquals($url, $data->url);
$this->assertEquals(418, $data->code);
}
public function testReturnsForbidden() {
$url = 'http://redirect.example.com/code-403';
$response = $this->parse([
'url' => $url
]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertObjectHasAttribute('error', $data);
$this->assertEquals('forbidden', $data->error);
$this->assertEquals($url, $data->url);
$this->assertEquals(403, $data->code);
}
public function testReturnsUnauthorized() {
$url = 'http://redirect.example.com/code-401';
$response = $this->parse([
'url' => $url
]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertObjectHasAttribute('error', $data);
$this->assertEquals('unauthorized', $data->error);
$this->assertEquals($url, $data->url);
$this->assertEquals(401, $data->code);
}
public function testDeleted() {
$url = 'http://source.example.com/deleted-gone';
$response = $this->parse([
'url' => $url
]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertObjectNotHasAttribute('error', $data);
$this->assertEquals(410, $data->code);
$this->assertEquals('This post has been deleted.', $data->data->content->text);
}
public function testDeletedEmptyBody() {
$url = 'http://source.example.com/deleted-empty';
$response = $this->parse([
'url' => $url
]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertObjectNotHasAttribute('error', $data);
$this->assertEquals(410, $data->code);
$this->assertEquals('unknown', $data->data->type);
}
public function testDeletedTargetProvided() {
$url = 'http://source.example.com/deleted-gone';
$response = $this->parse([
'url' => $url,
'target' => 'http://example.com/'
]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertObjectHasAttribute('error', $data);
$this->assertEquals('no_link_found', $data->error);
$this->assertEquals(410, $data->code);
}
public function testMetaEquivDeleted() {
$url = 'http://source.example.com/deleted';
$response = $this->parse([
'url' => $url
]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertObjectNotHasAttribute('error', $data);
$this->assertEquals(410, $data->code);
$this->assertEquals('This post has been deleted.', $data->data->content->text);
}
public function testMetaEquivDeletedCaps() {
$url = 'http://source.example.com/deleted-2';
$response = $this->parse([
'url' => $url
]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertObjectNotHasAttribute('error', $data);
$this->assertEquals(410, $data->code);
$this->assertEquals('This post has been deleted.', $data->data->content->text);
}
public function testMetaEquivDeletedTargetProvided() {
// for example when verifying a webmention but the source was replaced with an html deleted page
$url = 'http://source.example.com/deleted';
$response = $this->parse([
'url' => $url,
'target' => 'http://example.com/'
]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertObjectHasAttribute('error', $data);
$this->assertEquals('no_link_found', $data->error);
$this->assertEquals(410, $data->code);
}
class FetchTest extends PHPUnit\Framework\TestCase
{
private $http;
public function setUp(): void
{
$this->client = new Parse();
$this->client->http = new p3k\HTTP\Test(dirname(__FILE__).'/data/');
$this->client->mc = null;
}
private function parse($params)
{
$request = new Request($params);
$response = new Response();
return $this->client->parse($request, $response);
}
public function testRedirectLimit()
{
$url = 'http://redirect.example.com/3';
$response = $this->parse(
[
'url' => $url,
'max_redirects' => 1
]
);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertObjectHasAttribute('error', $data);
$this->assertEquals('too_many_redirects', $data->error);
$url = 'http://redirect.example.com/2';
$response = $this->parse(
[
'url' => $url,
'max_redirects' => 1
]
);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertObjectHasAttribute('error', $data);
$this->assertEquals('too_many_redirects', $data->error);
}
public function testRedirectUnderLimit()
{
$url = 'http://redirect.example.com/2';
$response = $this->parse(
[
'url' => $url,
'max_redirects' => 2
]
);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertObjectNotHasAttribute('error', $data);
$this->assertEquals(200, $data->code);
$this->assertEquals('The Final Page', $data->data->name);
$this->assertEquals('http://redirect.example.com/0', $data->url);
}
public function testReturnsHTTPStatusCode()
{
$url = 'http://redirect.example.com/code-418';
$response = $this->parse(
[
'url' => $url
]
);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertObjectNotHasAttribute('error', $data);
$this->assertEquals($url, $data->url);
$this->assertEquals(418, $data->code);
}
public function testReturnsForbidden()
{
$url = 'http://redirect.example.com/code-403';
$response = $this->parse(
[
'url' => $url
]
);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertObjectHasAttribute('error', $data);
$this->assertEquals('forbidden', $data->error);
$this->assertEquals($url, $data->url);
$this->assertEquals(403, $data->code);
}
public function testReturnsUnauthorized()
{
$url = 'http://redirect.example.com/code-401';
$response = $this->parse(
[
'url' => $url
]
);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertObjectHasAttribute('error', $data);
$this->assertEquals('unauthorized', $data->error);
$this->assertEquals($url, $data->url);
$this->assertEquals(401, $data->code);
}
public function testDeleted()
{
$url = 'http://source.example.com/deleted-gone';
$response = $this->parse(
[
'url' => $url
]
);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertObjectNotHasAttribute('error', $data);
$this->assertEquals(410, $data->code);
$this->assertEquals('This post has been deleted.', $data->data->content->text);
}
public function testDeletedEmptyBody()
{
$url = 'http://source.example.com/deleted-empty';
$response = $this->parse(
[
'url' => $url
]
);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body);
$this->assertObjectNotHasAttribute('error', $data);
$this->assertEquals(410, $data->code);
$this->assertEquals('unknown', $data->data->type);
}
public function testDeletedTargetProvided()
{
$url = 'http://source.example.com/deleted-gone';
$response = $this->parse(
[
'url' => $url,
'target' => 'http://example.com/'
]