Skip to content

Commit a5d6184

Browse files
committed
add tests for sending mentions from various source URLs
1 parent be14115 commit a5d6184

16 files changed

+247
-15
lines changed

src/IndieWeb/MentionClient.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,9 @@ public function sendMentions($sourceURL, $sourceBody=false) {
270270
foreach($this->_links as $target) {
271271
self::_debug("Checking $target for webmention and pingback endpoints");
272272

273-
$totalAccepted += $this->sendFirstSupportedMention($sourceURL, $target);
273+
if($this->sendFirstSupportedMention($sourceURL, $target)) {
274+
$totalAccepted++;
275+
}
274276
}
275277

276278
return $totalAccepted;
@@ -282,17 +284,22 @@ public function sendFirstSupportedMention($source, $target) {
282284

283285
// Look for a webmention endpoint first
284286
if($this->discoverWebmentionEndpoint($target)) {
285-
$accepted = $this->sendWebmention($source, $target);
287+
$result = $this->sendWebmention($source, $target);
288+
if($result &&
289+
($result['code'] == 200
290+
|| $result['code'] == 201
291+
|| $result['code'] == 202)) {
292+
$accepted = 'webmention';
293+
}
286294
// Only look for a pingback server if we didn't find a webmention server
287-
} else
288-
if($this->discoverPingbackEndpoint($target)) {
289-
$accepted = $this->sendPingback($source, $target);
295+
} else if($this->discoverPingbackEndpoint($target)) {
296+
$result = $this->sendPingback($source, $target);
297+
if($result) {
298+
$accepted = 'pingback';
299+
}
290300
}
291301

292-
if($accepted)
293-
return 1;
294-
else
295-
return 0;
302+
return $accepted;
296303
}
297304

298305
/**

tests/DiscoverTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ public function testDiscoverPingbackEndpoint() {
3434
public function testDiscoverWebmentionEndpointInHeader() {
3535
$target = 'http://target.example.com/header.html';
3636
$endpoint = $this->client->discoverWebmentionEndpoint($target);
37-
$this->assertEquals('http://webmention.example/webmention', $endpoint);
37+
$this->assertEquals('http://webmention-endpoint.example/queued-response', $endpoint);
3838
}
3939

4040
public function testDiscoverPingbackEndpointInHeader() {
4141
$target = 'http://target.example.com/header.html';
4242
$endpoint = $this->client->discoverPingbackEndpoint($target);
43-
$this->assertEquals('http://webmention.example/pingback', $endpoint);
43+
$this->assertEquals('http://pingback-endpoint.example/valid-response', $endpoint);
4444
}
4545

4646
public function testDiscoverWebmentionEndpointInBodyLink() {

tests/SendMentionsTest.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
class SendMentionsTest extends PHPUnit_Framework_TestCase {
3+
4+
public $client;
5+
6+
public function setUp() {
7+
$this->client = new IndieWeb\MentionClientTest(false, 'empty');
8+
}
9+
10+
public function testFromBodyWithNoLinks() {
11+
$total = $this->client->sendMentions('http://source.example.com/no-links.html', '<p>No links here</p>');
12+
$this->assertEquals(0, $total);
13+
}
14+
15+
public function testFromURLWithNoLinks() {
16+
$total = $this->client->sendMentions('http://source.example.com/no-links.html');
17+
$this->assertEquals(0, $total);
18+
}
19+
20+
public function testFromURLWithTwoValidLinks() {
21+
$total = $this->client->sendMentions('http://source.example.com/two-valid-links.html');
22+
$this->assertEquals(2, $total);
23+
}
24+
25+
public function testFromURLWithOneValidAndOneInvalidLink() {
26+
$total = $this->client->sendMentions('http://source.example.com/mixed-success-links.html');
27+
$this->assertEquals(1, $total);
28+
}
29+
30+
public function testDoesNotSendToLinksOutsideHEntry() {
31+
$total = $this->client->sendMentions('http://source.example.com/send-to-h-entry-links.html');
32+
$this->assertEquals(1, $total);
33+
}
34+
35+
public function testPrioritizesWebmentionEndpointOverPingback() {
36+
$result = $this->client->sendFirstSupportedMention('http://source.example.com/example.html', 'http://target.example.com/header.html');
37+
$this->assertEquals('webmention', $result);
38+
}
39+
40+
public function testFindsPingbackEndpointBecauseNoWebmentionEndpoint() {
41+
$result = $this->client->sendFirstSupportedMention('http://source.example.com/example.html', 'http://target.example.com/only-pingback.html');
42+
$this->assertEquals('pingback', $result);
43+
}
44+
45+
public function testDoesNotSendPingbackDespiteWebmentionFail() {
46+
$result = $this->client->sendFirstSupportedMention('http://source.example.com/example.html', 'http://target.example.com/webmention-failed.html');
47+
$this->assertEquals(false, $result);
48+
}
49+
50+
public function testSendsFailingPingback() {
51+
$result = $this->client->sendFirstSupportedMention('http://source.example.com/example.html', 'http://target.example.com/pingback-failed.html');
52+
$this->assertEquals(false, $result);
53+
}
54+
55+
public function testSendsFailingWebmention() {
56+
$result = $this->client->sendFirstSupportedMention('http://source.example.com/example.html', 'http://target.example.com/webmention-only-failed.html');
57+
$this->assertEquals(false, $result);
58+
}
59+
60+
public function testSendsWebmentionAndWasCreated() {
61+
$result = $this->client->sendFirstSupportedMention('http://source.example.com/example.html', 'http://target.example.com/webmention-created.html');
62+
$this->assertEquals('webmention', $result);
63+
}
64+
65+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
HTTP/1.1 200 OK
2+
Server: Apache
3+
Date: Wed, 09 Dec 2015 03:29:14 GMT
4+
Content-Type: text/html; charset=utf-8
5+
Connection: keep-alive
6+
7+
<html>
8+
<head>
9+
<title>Test</title>
10+
</head>
11+
<body class="h-entry">
12+
<p class="e-content">
13+
<a href="http://target.example.com/only-pingback.html">one</a>
14+
<a href="http://target.example.com/webmention-failed.html">two</a>
15+
</p>
16+
</body>
17+
</html>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
HTTP/1.1 200 OK
2+
Server: Apache
3+
Date: Wed, 09 Dec 2015 03:29:14 GMT
4+
Content-Type: text/html; charset=utf-8
5+
Connection: keep-alive
6+
7+
<html>
8+
<head>
9+
<title>Test</title>
10+
</head>
11+
<body>
12+
<p>There are no links in this page</p>
13+
</body>
14+
</html>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
HTTP/1.1 200 OK
2+
Server: Apache
3+
Date: Wed, 09 Dec 2015 03:29:14 GMT
4+
Content-Type: text/html; charset=utf-8
5+
Connection: keep-alive
6+
7+
<html>
8+
<head>
9+
<title>Test</title>
10+
</head>
11+
<body>
12+
<div class="h-entry">
13+
<p class="e-content">
14+
<a href="http://target.example.com/webmention-created.html">two</a>
15+
</p>
16+
</div>
17+
<a href="http://target.example.com/only-pingback.html">one</a>
18+
</body>
19+
</html>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
HTTP/1.1 200 OK
2+
Server: Apache
3+
Date: Wed, 09 Dec 2015 03:29:14 GMT
4+
Content-Type: text/html; charset=utf-8
5+
Connection: keep-alive
6+
7+
<html>
8+
<head>
9+
<title>Test</title>
10+
</head>
11+
<body>
12+
<a href="http://target.example.com/header.html">Great post</a>
13+
</body>
14+
</html>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
HTTP/1.1 200 OK
2+
Server: Apache
3+
Date: Wed, 09 Dec 2015 03:29:14 GMT
4+
Content-Type: text/html; charset=utf-8
5+
Connection: keep-alive
6+
7+
<html>
8+
<head>
9+
<title>Test</title>
10+
</head>
11+
<body class="h-entry">
12+
<p class="e-content">
13+
<a href="http://target.example.com/only-pingback.html">one</a>
14+
<a href="http://target.example.com/webmention-created.html">two</a>
15+
</p>
16+
</body>
17+
</html>

tests/data/target.example.com/body-link.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
<link rel="pingback" href="http://webmention.example/pingback">
1111
</head>
1212
<body>
13-
<p>You can send me a webmention</p>
13+
<p>You can send me a webmention or pingback</p>
1414
</body>
1515
</html>

tests/data/target.example.com/header.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Date: Wed, 09 Dec 2015 03:29:14 GMT
44
Content-Type: text/html; charset=utf-8
55
Connection: keep-alive
6-
Link: <http://webmention.example/webmention>; rel="webmention"
7-
X-Pingback: http://webmention.example/pingback
6+
Link: <http://webmention-endpoint.example/queued-response>; rel="webmention"
7+
X-Pingback: http://pingback-endpoint.example/valid-response
88

9-
<p>You can send me a webmention</p>
9+
<p>You can send a webmention or a pingback here</p>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
HTTP/1.1 200 OK
2+
Server: Apache
3+
Date: Wed, 09 Dec 2015 03:29:14 GMT
4+
Content-Type: text/html; charset=utf-8
5+
Connection: keep-alive
6+
7+
<html>
8+
<head>
9+
<link rel="pingback" href="http://pingback-endpoint.example/valid-response">
10+
</head>
11+
<body>
12+
<p>You can send me a pingback</p>
13+
</body>
14+
</html>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
HTTP/1.1 200 OK
2+
Server: Apache
3+
Date: Wed, 09 Dec 2015 03:29:14 GMT
4+
Content-Type: text/html; charset=utf-8
5+
Connection: keep-alive
6+
7+
<html>
8+
<head>
9+
<link rel="pingback" href="http://pingback-endpoint.example/invalid-request">
10+
</head>
11+
<body>
12+
<p>You can send me a pingback but it will fail</p>
13+
</body>
14+
</html>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
HTTP/1.1 200 OK
2+
Server: Apache
3+
Date: Wed, 09 Dec 2015 03:29:14 GMT
4+
Content-Type: text/html; charset=utf-8
5+
Connection: keep-alive
6+
7+
<html>
8+
<head>
9+
<link rel="webmention" href="http://webmention-endpoint.example/created-response">
10+
</head>
11+
<body>
12+
<p>You can send me a webmention</p>
13+
</body>
14+
</html>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
HTTP/1.1 200 OK
2+
Server: Apache
3+
Date: Wed, 09 Dec 2015 03:29:14 GMT
4+
Content-Type: text/html; charset=utf-8
5+
Connection: keep-alive
6+
7+
<html>
8+
<head>
9+
<link rel="webmention" href="http://webmention-endpoint.example/invalid-request">
10+
<link rel="pingback" href="http://pingback-endpoint.example/valid-response">
11+
</head>
12+
<body>
13+
<p>You can send me a webmention or pingback but the webmention will fail</p>
14+
</body>
15+
</html>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
HTTP/1.1 200 OK
2+
Server: Apache
3+
Date: Wed, 09 Dec 2015 03:29:14 GMT
4+
Content-Type: text/html; charset=utf-8
5+
Connection: keep-alive
6+
7+
<html>
8+
<head>
9+
<link rel="webmention" href="http://webmention-endpoint.example/invalid-request">
10+
</head>
11+
<body>
12+
<p>You can send me a webmention but it will fail</p>
13+
</body>
14+
</html>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
HTTP/1.1 201 Created
2+
Date: Wed, 09 Dec 2015 04:40:18 GMT
3+
Server: Apache
4+
Connection: close
5+
Vary: Accept-Encoding
6+
Location: http://webmention-endpoint.example/status/skldjfosuet
7+
8+
Webmention processing queued

0 commit comments

Comments
 (0)