diff --git a/lib/Telegraph/Webmention.php b/lib/Telegraph/Webmention.php index 303a3b5..60fee6e 100644 --- a/lib/Telegraph/Webmention.php +++ b/lib/Telegraph/Webmention.php @@ -57,6 +57,13 @@ class Webmention { if(in_array($response['code'], [200,201,202])) { $status = 'webmention_accepted'; + + // Check if the endpoint returned a status URL + if(array_key_exists('Location', $response['headers'])) { + $webmention->webmention_status_url = \Mf2\resolveUrl($endpoint, $response['headers']['Location']); + $webmention->save(); + } + } else { $status = 'webmention_error'; } diff --git a/tests/ProcessTest.php b/tests/ProcessTest.php index 95466a9..aba31d8 100644 --- a/tests/ProcessTest.php +++ b/tests/ProcessTest.php @@ -72,6 +72,8 @@ class ProcessTest extends PHPUnit_Framework_TestCase { ]); $status = $this->webmentionStatus($webmention->id); $this->assertEquals($status->status, 'pingback_accepted'); + $webmention = ORM::for_table('webmentions')->where('id',$webmention->id)->find_one(); + $this->assertEquals('http://pingback.example.com/success', $webmention->pingback_endpoint); } public function testPingbackFailed() { @@ -96,8 +98,45 @@ class ProcessTest extends PHPUnit_Framework_TestCase { $this->assertEquals($status->status, 'webmention_accepted'); } - public function testWebmentionFailed() { + public function testWebmentionSucceeds() { + $this->_createExampleAccount(); + $webmention = $this->webmention([ + 'token' => 'a', + 'source' => 'http://source.example.com/webmention-success', + 'target' => 'http://target.example.com/webmention-success' + ]); + $status = $this->webmentionStatus($webmention->id); + $this->assertEquals($status->status, 'webmention_accepted'); + $webmention = ORM::for_table('webmentions')->where('id',$webmention->id)->find_one(); + $this->assertEquals('http://webmention.example.com/success', $webmention->webmention_endpoint); + } + public function testSavesWebmentionStatusURL() { + $this->_createExampleAccount(); + $webmention = $this->webmention([ + 'token' => 'a', + 'source' => 'http://source.example.com/webmention-status-url', + 'target' => 'http://target.example.com/webmention-status-url' + ]); + $status = $this->webmentionStatus($webmention->id); + $this->assertEquals($status->status, 'webmention_accepted'); + $webmention = ORM::for_table('webmentions')->where('id',$webmention->id)->find_one(); + $this->assertEquals('http://webmention.example.com/success-with-status', $webmention->webmention_endpoint); + // Make sure the status URL returned is an absolute URL + $this->assertEquals('http://webmention.example.com/webmention/1000', $webmention->webmention_status_url); + } + + public function testWebmentionFailed() { + $this->_createExampleAccount(); + $webmention = $this->webmention([ + 'token' => 'a', + 'source' => 'http://source.example.com/webmention-failed', + 'target' => 'http://target.example.com/webmention-failed' + ]); + $status = $this->webmentionStatus($webmention->id); + $this->assertEquals($status->status, 'webmention_error'); + $webmention = ORM::for_table('webmentions')->where('id',$webmention->id)->find_one(); + $this->assertEquals('http://webmention.example.com/error', $webmention->webmention_endpoint); } } diff --git a/tests/data/source.example.com/webmention-failed b/tests/data/source.example.com/webmention-failed new file mode 100644 index 0000000..22f5102 --- /dev/null +++ b/tests/data/source.example.com/webmention-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/webmention-status-url b/tests/data/source.example.com/webmention-status-url new file mode 100644 index 0000000..2371a30 --- /dev/null +++ b/tests/data/source.example.com/webmention-status-url @@ -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/webmention-success b/tests/data/source.example.com/webmention-success new file mode 100644 index 0000000..c97b298 --- /dev/null +++ b/tests/data/source.example.com/webmention-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/webmention-failed b/tests/data/target.example.com/webmention-failed new file mode 100644 index 0000000..8d46c3b --- /dev/null +++ b/tests/data/target.example.com/webmention-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 webmention endpoint that will return an error

+ + diff --git a/tests/data/target.example.com/webmention-status-url b/tests/data/target.example.com/webmention-status-url new file mode 100644 index 0000000..58e3bb4 --- /dev/null +++ b/tests/data/target.example.com/webmention-status-url @@ -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 webmention endpoint that will return a status URL

+ + diff --git a/tests/data/webmention.example.com/success-with-status b/tests/data/webmention.example.com/success-with-status new file mode 100644 index 0000000..1a69e3e --- /dev/null +++ b/tests/data/webmention.example.com/success-with-status @@ -0,0 +1,8 @@ +HTTP/1.1 201 Created +Server: Apache +Date: Wed, 09 Dec 2015 03:29:14 GMT +Content-Type: text/plain; charset=utf-8 +Connection: keep-alive +Location: /webmention/1000 + +Webmention was accepted