Browse Source

adds tests for processing pingbacks

pull/3/head
Aaron Parecki 8 years ago
parent
commit
036e69b12b
12 changed files with 266 additions and 9 deletions
  1. +5
    -5
      composer.lock
  2. +39
    -4
      lib/Telegraph/Webmention.php
  3. +88
    -0
      tests/ProcessTest.php
  4. +3
    -0
      tests/data/404.response.txt
  5. +23
    -0
      tests/data/pingback.example.com/error
  6. +16
    -0
      tests/data/pingback.example.com/success
  7. +16
    -0
      tests/data/source.example.com/no-endpoint
  8. +16
    -0
      tests/data/source.example.com/pingback-failed
  9. +16
    -0
      tests/data/source.example.com/pingback-success
  10. +14
    -0
      tests/data/target.example.com/no-endpoint
  11. +15
    -0
      tests/data/target.example.com/pingback-failed
  12. +15
    -0
      tests/data/target.example.com/pingback-success

+ 5
- 5
composer.lock View File

@ -177,16 +177,16 @@
},
{
"name": "indieweb/mention-client",
"version": "1.0.0",
"version": "1.1.0",
"source": {
"type": "git",
"url": "https://github.com/indieweb/mention-client-php.git",
"reference": "4f07d535716f5b2eec05e33e9b3d6e33843a3b9a"
"reference": "0bc331432e0490cc739b8a99d808889555c8d587"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/indieweb/mention-client-php/zipball/4f07d535716f5b2eec05e33e9b3d6e33843a3b9a",
"reference": "4f07d535716f5b2eec05e33e9b3d6e33843a3b9a",
"url": "https://api.github.com/repos/indieweb/mention-client-php/zipball/0bc331432e0490cc739b8a99d808889555c8d587",
"reference": "0bc331432e0490cc739b8a99d808889555c8d587",
"shasum": ""
},
"require": {
@ -215,7 +215,7 @@
],
"description": "Client library for sending webmention and pingback notifications",
"homepage": "https://github.com/indieweb/mention-client-php",
"time": "2015-12-09 19:10:25"
"time": "2015-12-22 02:48:43"
},
{
"name": "j4mie/idiorm",

+ 39
- 4
lib/Telegraph/Webmention.php View File

@ -1,17 +1,52 @@
<?php
namespace Telegraph;
use ORM, Exception;
use IndieWeb\MentionClient;
class Webmention {
public static function send($id) {
$w = ORM::for_table('webmentions')->where('id', $id)->find_one();
if(!$w) {
private static function saveStatus($webmentionID, $code, $raw=null) {
$status = ORM::for_table('webmention_status')->create();
$status->webmention_id = $webmentionID;
$status->created_at = date('Y-m-d H:i:s');
$status->status = $code;
$status->save();
}
public static function send($id, $client=false) {
$webmention = ORM::for_table('webmentions')->where('id', $id)->find_one();
if(!$webmention) {
echo 'Webmention '.$id.' was not found'."\n";
return;
}
if(!$client)
$client = new MentionClient();
// Discover the webmention or pingback endpoint
$endpoint = $client->discoverWebmentionEndpoint($webmention->target);
if(!$endpoint) {
// If no webmention endpoint found, try to send a pingback
$pingbackEndpoint = $client->discoverPingbackEndpoint($webmention->target);
// If no pingback endpoint was found, we can't do anything else
if(!$pingbackEndpoint) {
self::saveStatus($id, 'not_supported');
return;
}
$webmention->pingback_endpoint = $pingbackEndpoint;
$webmention->save();
$success = $client->sendPingbackToEndpoint($pingbackEndpoint, $webmention->source, $webmention->target);
self::saveStatus($id, $success ? 'pingback_success' : 'pingback_error');
return;
}
}
}

+ 88
- 0
tests/ProcessTest.php View File

@ -0,0 +1,88 @@
<?php
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class ProcessTest extends PHPUnit_Framework_TestCase {
private $client;
public function setUp() {
$this->client = new API();
$this->client->http = new Telegraph\HTTPTest();
ORM::for_table('users')->raw_query('TRUNCATE users')->delete_many();
ORM::for_table('roles')->raw_query('TRUNCATE roles')->delete_many();
ORM::for_table('sites')->raw_query('TRUNCATE sites')->delete_many();
ORM::for_table('webmentions')->raw_query('TRUNCATE webmentions')->delete_many();
ORM::for_table('webmention_status')->raw_query('TRUNCATE webmention_status')->delete_many();
}
private function _createExampleAccount() {
$user = ORM::for_table('users')->create();
$user->url = 'http://example.com';
$user->save();
$site = ORM::for_table('sites')->create();
$site->name = 'Example';
$site->created_by = $user->id();
$site->save();
$role = ORM::for_table('roles')->create();
$role->site_id = $site->id();
$role->user_id = $user->id();
$role->role = 'owner';
$role->token = 'a';
$role->save();
}
private function webmention($params) {
$request = new Request($params);
$response = new Response();
$response = $this->client->webmention($request, $response);
$webmention = ORM::for_table('webmentions')->where(['source' => $params['source'], 'target' => $params['target']])->find_one();
$client = new IndieWeb\MentionClientTest();
$client::$dataDir = dirname(__FILE__) . '/data/';
if(!is_object($webmention)) {
throw new Exception("No webmention was queued for this test");
}
Telegraph\Webmention::send($webmention->id, $client);
return $webmention;
}
private static function webmentionStatus($id) {
return ORM::for_table('webmention_status')->where(['webmention_id'=>$id])->order_by_desc('created_at')->find_one();
}
public function testNoEndpoint() {
$this->_createExampleAccount();
$webmention = $this->webmention([
'token' => 'a',
'source' => 'http://source.example.com/no-endpoint',
'target' => 'http://target.example.com/no-endpoint'
]);
$status = $this->webmentionStatus($webmention->id);
$this->assertEquals($status->status, 'not_supported');
}
public function testPingbackSuccess() {
$this->_createExampleAccount();
$webmention = $this->webmention([
'token' => 'a',
'source' => 'http://source.example.com/pingback-success',
'target' => 'http://target.example.com/pingback-success'
]);
$status = $this->webmentionStatus($webmention->id);
$this->assertEquals($status->status, 'pingback_success');
}
public function testPingbackFailed() {
$this->_createExampleAccount();
$webmention = $this->webmention([
'token' => 'a',
'source' => 'http://source.example.com/pingback-failed',
'target' => 'http://target.example.com/pingback-failed'
]);
$status = $this->webmentionStatus($webmention->id);
$this->assertEquals($status->status, 'pingback_error');
}
}

+ 3
- 0
tests/data/404.response.txt View File

@ -0,0 +1,3 @@
HTTP/1.1 404 Not Found
The page was not found.

+ 23
- 0
tests/data/pingback.example.com/error View File

@ -0,0 +1,23 @@
HTTP/1.1 200 OK
Server: Apache
Date: Wed, 09 Dec 2015 03:29:14 GMT
Content-Type: text/xml; charset=utf-8
Connection: keep-alive
<?xml version="1.0" ?>
<methodResponse>
<fault>
<value>
<struct>
<member>
<name>faultCode</name>
<value><i4>17</i4></value>
</member>
<member>
<name>faultString</name>
<value><string>The source does not link to the target</string></value>
</member>
</struct>
</value>
</fault>
</methodResponse>

+ 16
- 0
tests/data/pingback.example.com/success View File

@ -0,0 +1,16 @@
HTTP/1.1 200 OK
Server: Apache
Date: Wed, 09 Dec 2015 03:29:14 GMT
Content-Type: text/xml; charset=utf-8
Connection: keep-alive
<?xml version="1.0" ?>
<methodResponse>
<params>
<param>
<value>
<string>pingback was successful</string>
</value>
</param>
</params>
</methodResponse>

+ 16
- 0
tests/data/source.example.com/no-endpoint View File

@ -0,0 +1,16 @@
HTTP/1.1 200 OK
Server: Apache
Date: Wed, 09 Dec 2015 03:29:14 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
<html>
<head>
<title>Test</title>
</head>
<body class="h-entry">
<p class="e-content">
<a href="http://target.example.com/no-endpoint">target</a>
</p>
</body>
</html>

+ 16
- 0
tests/data/source.example.com/pingback-failed View File

@ -0,0 +1,16 @@
HTTP/1.1 200 OK
Server: Apache
Date: Wed, 09 Dec 2015 03:29:14 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
<html>
<head>
<title>Test</title>
</head>
<body class="h-entry">
<p class="e-content">
<a href="http://target.example.com/pingback-failed">target</a>
</p>
</body>
</html>

+ 16
- 0
tests/data/source.example.com/pingback-success View File

@ -0,0 +1,16 @@
HTTP/1.1 200 OK
Server: Apache
Date: Wed, 09 Dec 2015 03:29:14 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
<html>
<head>
<title>Test</title>
</head>
<body class="h-entry">
<p class="e-content">
<a href="http://target.example.com/pingback-success">target</a>
</p>
</body>
</html>

+ 14
- 0
tests/data/target.example.com/no-endpoint View File

@ -0,0 +1,14 @@
HTTP/1.1 200 OK
Server: Apache
Date: Wed, 09 Dec 2015 03:29:14 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
<html>
<head>
<title>Test</title>
</head>
<body class="h-entry">
<p class="e-content">This page has no webmention or pingback endpoint configured</p>
</body>
</html>

+ 15
- 0
tests/data/target.example.com/pingback-failed View File

@ -0,0 +1,15 @@
HTTP/1.1 200 OK
Server: Apache
Date: Wed, 09 Dec 2015 03:29:14 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
<html>
<head>
<title>Test</title>
<link rel="pingback" href="http://pingback.example.com/error">
</head>
<body class="h-entry">
<p class="e-content">This has a pingback endpoint that will return an error</p>
</body>
</html>

+ 15
- 0
tests/data/target.example.com/pingback-success View File

@ -0,0 +1,15 @@
HTTP/1.1 200 OK
Server: Apache
Date: Wed, 09 Dec 2015 03:29:14 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
<html>
<head>
<title>Test</title>
<link rel="pingback" href="http://pingback.example.com/success">
</head>
<body class="h-entry">
<p class="e-content">This has a pingback endpoint that will return success</p>
</body>
</html>

Loading…
Cancel
Save