@ -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; | |||
} | |||
} | |||
} |
@ -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'); | |||
} | |||
} |
@ -0,0 +1,3 @@ | |||
HTTP/1.1 404 Not Found | |||
The page was not found. |
@ -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> |
@ -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> |
@ -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> |
@ -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> |
@ -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> |
@ -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> |
@ -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> |
@ -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> |