From 036e69b12bc2ce7ca1c3dc54fcd0327bb999dc42 Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Mon, 21 Dec 2015 18:52:18 -0800 Subject: [PATCH] adds tests for processing pingbacks --- composer.lock | 10 +-- lib/Telegraph/Webmention.php | 43 ++++++++- tests/ProcessTest.php | 88 +++++++++++++++++++ tests/data/404.response.txt | 3 + tests/data/pingback.example.com/error | 23 +++++ tests/data/pingback.example.com/success | 16 ++++ tests/data/source.example.com/no-endpoint | 16 ++++ tests/data/source.example.com/pingback-failed | 16 ++++ .../data/source.example.com/pingback-success | 16 ++++ tests/data/target.example.com/no-endpoint | 14 +++ tests/data/target.example.com/pingback-failed | 15 ++++ .../data/target.example.com/pingback-success | 15 ++++ 12 files changed, 266 insertions(+), 9 deletions(-) create mode 100644 tests/ProcessTest.php create mode 100644 tests/data/404.response.txt create mode 100644 tests/data/pingback.example.com/error create mode 100644 tests/data/pingback.example.com/success create mode 100644 tests/data/source.example.com/no-endpoint create mode 100644 tests/data/source.example.com/pingback-failed create mode 100644 tests/data/source.example.com/pingback-success create mode 100644 tests/data/target.example.com/no-endpoint create mode 100644 tests/data/target.example.com/pingback-failed create mode 100644 tests/data/target.example.com/pingback-success diff --git a/composer.lock b/composer.lock index 0651435..9c1e000 100644 --- a/composer.lock +++ b/composer.lock @@ -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", diff --git a/lib/Telegraph/Webmention.php b/lib/Telegraph/Webmention.php index d96d157..a49666c 100644 --- a/lib/Telegraph/Webmention.php +++ b/lib/Telegraph/Webmention.php @@ -1,17 +1,52 @@ 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; + } + + + + } } diff --git a/tests/ProcessTest.php b/tests/ProcessTest.php new file mode 100644 index 0000000..3c4246c --- /dev/null +++ b/tests/ProcessTest.php @@ -0,0 +1,88 @@ +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'); + } + +} diff --git a/tests/data/404.response.txt b/tests/data/404.response.txt new file mode 100644 index 0000000..f0bd083 --- /dev/null +++ b/tests/data/404.response.txt @@ -0,0 +1,3 @@ +HTTP/1.1 404 Not Found + +The page was not found. diff --git a/tests/data/pingback.example.com/error b/tests/data/pingback.example.com/error new file mode 100644 index 0000000..280d278 --- /dev/null +++ b/tests/data/pingback.example.com/error @@ -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 + + + + + + + + faultCode + 17 + + + faultString + The source does not link to the target + + + + + diff --git a/tests/data/pingback.example.com/success b/tests/data/pingback.example.com/success new file mode 100644 index 0000000..a776dcf --- /dev/null +++ b/tests/data/pingback.example.com/success @@ -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 + + + + + + + pingback was successful + + + + diff --git a/tests/data/source.example.com/no-endpoint b/tests/data/source.example.com/no-endpoint new file mode 100644 index 0000000..bb5fff3 --- /dev/null +++ b/tests/data/source.example.com/no-endpoint @@ -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 + + + + Test + + +

+ target +

+ + diff --git a/tests/data/source.example.com/pingback-failed b/tests/data/source.example.com/pingback-failed new file mode 100644 index 0000000..0506fab --- /dev/null +++ b/tests/data/source.example.com/pingback-failed @@ -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 + + + + Test + + +

+ target +

+ + diff --git a/tests/data/source.example.com/pingback-success b/tests/data/source.example.com/pingback-success new file mode 100644 index 0000000..0104ad1 --- /dev/null +++ b/tests/data/source.example.com/pingback-success @@ -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 + + + + Test + + +

+ target +

+ + diff --git a/tests/data/target.example.com/no-endpoint b/tests/data/target.example.com/no-endpoint new file mode 100644 index 0000000..564fc75 --- /dev/null +++ b/tests/data/target.example.com/no-endpoint @@ -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 + + + + Test + + +

This page has no webmention or pingback endpoint configured

+ + diff --git a/tests/data/target.example.com/pingback-failed b/tests/data/target.example.com/pingback-failed new file mode 100644 index 0000000..9048cd1 --- /dev/null +++ b/tests/data/target.example.com/pingback-failed @@ -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 + + + + Test + + + +

This has a pingback endpoint that will return an error

+ + diff --git a/tests/data/target.example.com/pingback-success b/tests/data/target.example.com/pingback-success new file mode 100644 index 0000000..6392a72 --- /dev/null +++ b/tests/data/target.example.com/pingback-success @@ -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 + + + + Test + + + +

This has a pingback endpoint that will return success

+ +