2023-03-09 16:56:54 +00:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
use Lewisdale\Webmentions\Endpoint;
|
2023-03-14 08:27:46 +00:00
|
|
|
use Lewisdale\Webmentions\Exceptions\InvalidTargetException;
|
|
|
|
use Lewisdale\Webmentions\Exceptions\InvalidUrlException;
|
|
|
|
use Lewisdale\Webmentions\Exceptions\SourceNotFoundException;
|
|
|
|
use Lewisdale\Webmentions\Exceptions\TargetNotMentionedException;
|
2023-03-14 09:12:53 +00:00
|
|
|
use Lewisdale\Webmentions\Gateways\WebmentionGatewayInterface;
|
|
|
|
use Lewisdale\Webmentions\Models\MentionType;
|
2023-03-09 16:56:54 +00:00
|
|
|
use PHPUnit\Framework\Attributes\TestWith;
|
2023-03-15 09:15:55 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2023-03-14 08:27:46 +00:00
|
|
|
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
|
|
|
use Symfony\Contracts\HttpClient\ResponseInterface;
|
2023-03-09 16:56:54 +00:00
|
|
|
|
2023-03-15 09:15:55 +00:00
|
|
|
class EndpointTest extends TestCase
|
|
|
|
{
|
2023-03-15 13:49:53 +00:00
|
|
|
private HttpClientInterface $mockClient;
|
|
|
|
private ResponseInterface $mockResponse;
|
|
|
|
private WebmentionGatewayInterface $mockGateway;
|
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
|
|
|
$this->mockClient = $this->createMock(HttpClientInterface::class);
|
|
|
|
$this->mockResponse = $this->createMock(ResponseInterface::class);
|
|
|
|
$this->mockGateway = $this->createMock(WebmentionGatewayInterface::class);
|
|
|
|
}
|
|
|
|
|
2023-03-15 09:15:55 +00:00
|
|
|
private function objectContains(string $key, mixed $expected)
|
|
|
|
{
|
|
|
|
return $this->callback(function (object $obj) use ($expected, $key) {
|
|
|
|
$val = $obj->$key;
|
|
|
|
$type = gettype($val);
|
|
|
|
|
|
|
|
return match ($type) {
|
|
|
|
"object" => $val == $expected,
|
|
|
|
"array" => count(array_diff($val, $expected)) === 0,
|
|
|
|
default => $val === $expected
|
|
|
|
};
|
|
|
|
});
|
2023-03-14 13:54:05 +00:00
|
|
|
}
|
|
|
|
|
2023-03-15 13:49:53 +00:00
|
|
|
private function callEndpoint(string $source, string $target)
|
|
|
|
{
|
|
|
|
$this->mockClient->method('request')
|
|
|
|
->with($this->identicalTo('GET'), $this->identicalTo($source))
|
|
|
|
->will($this->returnValue($this->mockResponse));
|
|
|
|
|
|
|
|
$endpoint = new Endpoint($this->mockClient, $this->mockGateway);
|
|
|
|
$endpoint->receiveWebmention($source, $target);
|
|
|
|
}
|
2023-03-17 10:19:50 +00:00
|
|
|
|
2023-03-14 08:27:46 +00:00
|
|
|
#[TestWith(["https://my-valid-source.url", "htt://my-invalid-target.com"])]
|
|
|
|
#[TestWith(["my-invalid-source", "https://my-valid-target.com"])]
|
|
|
|
#[TestWith(["http:///an-invalid-source", "an-invalid-target"])]
|
|
|
|
public function testThrowsInvalidUrlExceptionIfTheUrlIsInvalid(string $source, string $target)
|
|
|
|
{
|
|
|
|
$this->expectException(InvalidUrlException::class);
|
2023-03-15 13:49:53 +00:00
|
|
|
$this->callEndpoint($source, $target);
|
2023-03-14 08:27:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testThrowsInvalidTargetExceptionIfTheTargetUrlIsNotOnCurrentSite()
|
|
|
|
{
|
|
|
|
$this->expectException(InvalidTargetException::class);
|
2023-03-15 13:49:53 +00:00
|
|
|
$this->callEndpoint("https://a-valid-source.url", "https://not-my-site.com");
|
2023-03-14 08:27:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[TestWith([404])]
|
|
|
|
#[TestWith([401])]
|
|
|
|
#[TestWith([500])]
|
|
|
|
#[TestWith([501])]
|
|
|
|
#[TestWith([502])]
|
|
|
|
#[TestWith([503])]
|
|
|
|
#[TestWith([500])]
|
|
|
|
public function testItShouldThrowASourceNotFoundExceptionIfTheSourceUrlIsUnreachable(int $statusCode)
|
|
|
|
{
|
|
|
|
$source = "https://my-valid-source-url.com";
|
|
|
|
$target = "https://lewisdale.dev/post/a-post-page";
|
|
|
|
|
2023-03-15 13:49:53 +00:00
|
|
|
$this->mockClient->expects($this->once())
|
2023-03-14 08:27:46 +00:00
|
|
|
->method('request')
|
|
|
|
->with($this->identicalTo('GET'), $this->identicalTo($source))
|
2023-03-15 13:49:53 +00:00
|
|
|
->will($this->returnValue($this->mockResponse));
|
2023-03-14 08:27:46 +00:00
|
|
|
|
2023-03-15 13:49:53 +00:00
|
|
|
$this->mockResponse->method('getStatusCode')
|
2023-03-14 08:27:46 +00:00
|
|
|
->will($this->returnValue($statusCode));
|
|
|
|
|
|
|
|
$this->expectException(SourceNotFoundException::class);
|
|
|
|
|
2023-03-15 13:49:53 +00:00
|
|
|
$this->callEndpoint($source, $target);
|
2023-03-14 08:27:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testItShouldThrowATargetNotMentionedErrorIfTheSourceDoesNotMentionTheTarget()
|
|
|
|
{
|
|
|
|
$source = "https://my-valid-source-url.com";
|
|
|
|
$target = "https://lewisdale.dev/post/a-post-page";
|
|
|
|
|
|
|
|
$content = <<<XML
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<h1>Some content</h1>
|
|
|
|
<p>Here's some body content. It <a href="/another/page">contains a url</a>.</p>
|
|
|
|
<p>But it does not mention the target.</p>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
XML;
|
|
|
|
|
|
|
|
|
2023-03-15 13:49:53 +00:00
|
|
|
$this->mockResponse->method('getStatusCode')
|
2023-03-14 08:27:46 +00:00
|
|
|
->will($this->returnValue(200));
|
|
|
|
|
2023-03-15 13:49:53 +00:00
|
|
|
$this->mockResponse->method('getContent')
|
2023-03-14 08:27:46 +00:00
|
|
|
->willReturn($content);
|
|
|
|
|
|
|
|
$this->expectException(TargetNotMentionedException::class);
|
|
|
|
|
2023-03-15 13:49:53 +00:00
|
|
|
$this->callEndpoint($source, $target);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function verifyContent(string $source, string $target, string $content, \PHPUnit\Framework\Constraint\Callback $verify)
|
|
|
|
{
|
|
|
|
$this->mockResponse->method('getStatusCode')
|
|
|
|
->will($this->returnValue(200));
|
|
|
|
|
|
|
|
$this->mockResponse->method('getContent')
|
|
|
|
->willReturn($content);
|
|
|
|
|
|
|
|
$this->mockGateway->expects($this->once())
|
|
|
|
->method('save')
|
|
|
|
->with($verify);
|
|
|
|
|
|
|
|
$this->callEndpoint($source, $target);
|
2023-03-14 09:12:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testItShouldParseAWebmentionAsALikeIfItHasTheCorrectMicroFormat()
|
|
|
|
{
|
|
|
|
$source = "https://my-valid-source-url.com";
|
|
|
|
$target = "https://lewisdale.dev/post/a-post-page";
|
|
|
|
|
|
|
|
$content = <<<XML
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
</head>
|
|
|
|
<body>
|
2023-03-14 21:08:33 +00:00
|
|
|
<article>
|
|
|
|
<h1>Some content</h1>
|
|
|
|
<p>Here's some body content. It <a href="/another/page">contains a url</a>.</p>
|
|
|
|
<p>I'm liking <a href="$target" class="u-like-of">this post</a>.</p>
|
|
|
|
</article>
|
2023-03-14 09:12:53 +00:00
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
XML;
|
|
|
|
|
2023-03-15 13:49:53 +00:00
|
|
|
$this->verifyContent($source, $target, $content, $this->objectContains('type', MentionType::Like));
|
2023-03-14 09:12:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testItShouldParseAWebmentionAsAMentionIfItHasNoMicroformat()
|
|
|
|
{
|
|
|
|
$source = "https://my-valid-source-url.com";
|
|
|
|
$target = "https://lewisdale.dev/post/a-post-page";
|
|
|
|
|
|
|
|
$content = <<<XML
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
</head>
|
|
|
|
<body>
|
2023-03-14 21:08:33 +00:00
|
|
|
<article class="h-entry">
|
|
|
|
<h1>Some content</h1>
|
|
|
|
<p>Here's some body content. It <a href="/another/page">contains a url</a>.</p>
|
|
|
|
<p>I'm writing about <a href="$target" class="my-link">this post</a>.</p>
|
|
|
|
</article>
|
2023-03-14 09:12:53 +00:00
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
XML;
|
|
|
|
|
2023-03-15 13:49:53 +00:00
|
|
|
$this->verifyContent($source, $target, $content, $this->objectContains('type', MentionType::Mention));
|
2023-03-14 09:12:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testItShouldParseAWebmentionAsAReplyIfItHasTheCorrectMicroFormat()
|
|
|
|
{
|
|
|
|
$source = "https://my-valid-source-url.com";
|
|
|
|
$target = "https://lewisdale.dev/post/a-post-page";
|
|
|
|
|
|
|
|
$content = <<<XML
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
</head>
|
|
|
|
<body>
|
2023-03-14 21:08:33 +00:00
|
|
|
<article class="h-entry">
|
|
|
|
<h1>Some content</h1>
|
|
|
|
<p>Here's some body content. It <a href="/another/page">contains a url</a>.</p>
|
|
|
|
<p>I'm writing about <a href="$target" class="u-in-reply-to">this post</a>.</p>
|
|
|
|
</article>
|
2023-03-14 09:12:53 +00:00
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
XML;
|
|
|
|
|
2023-03-15 13:49:53 +00:00
|
|
|
$this->verifyContent($source, $target, $content, $this->objectContains('type', MentionType::Reply));
|
2023-03-14 13:54:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testItShouldParseAWebmentionAsARepost()
|
|
|
|
{
|
|
|
|
$source = "https://my-valid-source-url.com";
|
|
|
|
$target = "https://lewisdale.dev/post/a-post-page";
|
|
|
|
|
|
|
|
$content = <<<XML
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
</head>
|
|
|
|
<body>
|
2023-03-14 21:08:33 +00:00
|
|
|
<article="h-entry">
|
|
|
|
<h1>Some content</h1>
|
|
|
|
<p>Here's some body content. It <a href="/another/page">contains a url</a>.</p>
|
|
|
|
<p>I'm writing about <a href="$target" class="u-repost-of">this post</a>.</p>
|
|
|
|
</article>
|
2023-03-14 13:54:05 +00:00
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
XML;
|
|
|
|
|
2023-03-15 13:49:53 +00:00
|
|
|
$this->verifyContent($source, $target, $content, $this->objectContains('type', MentionType::Repost));
|
2023-03-14 08:27:46 +00:00
|
|
|
}
|
2023-03-15 09:15:55 +00:00
|
|
|
|
|
|
|
public function testItShouldParseARepostsContent()
|
|
|
|
{
|
|
|
|
$source = "https://my-valid-source-url.com";
|
|
|
|
$target = "https://lewisdale.dev/post/a-post-page";
|
|
|
|
|
|
|
|
$content = <<<XML
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<article="h-entry">
|
|
|
|
<h1>Some content</h1>
|
|
|
|
<p>Here's some body content. It <a href="/another/page">contains a url</a>.</p>
|
|
|
|
<p>I'm writing about <a href="$target" class="u-repost-of">this post</a>.</p>
|
|
|
|
</article>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
XML;
|
|
|
|
|
|
|
|
|
2023-03-15 13:49:53 +00:00
|
|
|
$this->verifyContent($source, $target, $content, $this->objectContains('content', "Reposted this post"));
|
2023-03-15 09:15:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testItShouldParseALikeContent()
|
|
|
|
{
|
|
|
|
$source = "https://my-valid-source-url.com";
|
|
|
|
$target = "https://lewisdale.dev/post/a-post-page";
|
|
|
|
|
|
|
|
$content = <<<HTML
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<span class="h-entry">
|
|
|
|
<a class="u-like-of" href="$target">A Cool Post</a><a class="u-url" href="/"></a>
|
|
|
|
</span>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
HTML;
|
|
|
|
|
2023-03-15 13:49:53 +00:00
|
|
|
$this->verifyContent($source, $target, $content, $this->objectContains('content', "Liked this post"));
|
2023-03-15 09:15:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testItShouldParseAReplyContent()
|
|
|
|
{
|
|
|
|
$source = "https://my-valid-source-url.com";
|
|
|
|
$target = "https://lewisdale.dev/post/a-post-page";
|
|
|
|
|
|
|
|
$content = <<<HTML
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<article class="h-entry">
|
|
|
|
<a class="u-in-reply-to" rel="in-reply-to" href="$target">@post</a>: That's a great idea!<a class="u-url" href="/"></a>
|
|
|
|
</article>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
HTML;
|
|
|
|
|
2023-03-15 13:49:53 +00:00
|
|
|
$this->verifyContent($source, $target, $content, $this->objectContains('content', "@post: That's a great idea!"));
|
2023-03-15 09:15:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testItShouldParseAnAuthorCardWithANameUrlAndPhoto()
|
|
|
|
{
|
|
|
|
$source = "https://my-valid-source-url.com";
|
|
|
|
$target = "https://lewisdale.dev/post/a-post-page";
|
|
|
|
|
|
|
|
$content = <<<HTML
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<article class="h-entry">
|
|
|
|
<a class="u-in-reply-to" rel="in-reply-to" href="$target">@post</a>: That's a great idea!<a class="u-url" href="/"></a>
|
|
|
|
<div class="h-card">
|
|
|
|
<p class="p-name">Anne Author</p> who can be found at <a class="u-url" href="https://my-blog.com">my-blog.com</a>.
|
|
|
|
<img src="https://dummyimage.com/100x100/fff/aaa" class="u-photo" alt="My profile picture" />
|
|
|
|
</div>
|
|
|
|
</article>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
HTML;
|
|
|
|
|
2023-03-15 13:49:53 +00:00
|
|
|
$expected = new \Lewisdale\Webmentions\Models\Author(null, "Anne Author", "https://my-blog.com", "https://dummyimage.com/100x100/fff/aaa");
|
2023-03-15 13:16:30 +00:00
|
|
|
|
2023-03-15 13:49:53 +00:00
|
|
|
$this->verifyContent($source, $target, $content, $this->objectContains('author', $expected));
|
2023-03-15 13:16:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testItShouldParseAnAuthorCardWithAMinimalHCard()
|
|
|
|
{
|
|
|
|
$source = "https://my-valid-source-url.com";
|
|
|
|
$target = "https://lewisdale.dev/post/a-post-page";
|
|
|
|
|
|
|
|
$content = <<<HTML
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<article class="h-entry">
|
|
|
|
<a class="u-in-reply-to" rel="in-reply-to" href="$target">@post</a>: That's a great idea!<a class="u-url" href="/"></a>
|
|
|
|
<span class="h-card">
|
|
|
|
<a class="p-name p-org u-url" href="https://microformats.org/">microformats.org</a>
|
|
|
|
</span>
|
|
|
|
</article>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
HTML;
|
|
|
|
|
|
|
|
$expected = new \Lewisdale\Webmentions\Models\Author(
|
|
|
|
null,
|
|
|
|
"microformats.org",
|
|
|
|
"https://microformats.org/",
|
|
|
|
""
|
|
|
|
);
|
2023-03-15 13:49:53 +00:00
|
|
|
$this->verifyContent($source, $target, $content, $this->objectContains('author', $expected));
|
2023-03-15 13:16:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testItShouldParseAnAuthorCardWithAVeryMinimalHCard()
|
|
|
|
{
|
|
|
|
$source = "https://my-valid-source-url.com";
|
|
|
|
$target = "https://lewisdale.dev/post/a-post-page";
|
|
|
|
|
|
|
|
$content = <<<HTML
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<article class="h-entry">
|
|
|
|
<a class="u-in-reply-to" rel="in-reply-to" href="$target">@post</a>: That's a great idea!<a class="u-url" href="/"></a>
|
|
|
|
<a class="h-card" href="https://microformats.org/">microformats.org</a>
|
|
|
|
</article>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
HTML;
|
|
|
|
|
|
|
|
$expected = new \Lewisdale\Webmentions\Models\Author(
|
|
|
|
null,
|
|
|
|
"microformats.org",
|
|
|
|
"https://microformats.org/",
|
|
|
|
""
|
|
|
|
);
|
2023-03-15 13:49:53 +00:00
|
|
|
$this->verifyContent($source, $target, $content, $this->objectContains('author', $expected));
|
2023-03-15 13:16:30 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testItShouldParseAnAuthorCardOutsideTheEntrry()
|
|
|
|
{
|
|
|
|
$source = "https://my-valid-source-url.com";
|
|
|
|
$target = "https://lewisdale.dev/post/a-post-page";
|
|
|
|
|
|
|
|
$content = <<<HTML
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<article class="h-entry">
|
|
|
|
<a class="u-in-reply-to" rel="in-reply-to" href="$target">@post</a>: That's a great idea!<a class="u-url" href="/"></a>
|
|
|
|
</article>
|
|
|
|
<div class="h-card">
|
|
|
|
<p class="p-name">Anne Author</p> who can be found at <a class="u-url" href="https://my-blog.com">my-blog.com</a>.
|
|
|
|
<img src="https://dummyimage.com/100x100/fff/aaa" class="u-photo" alt="My profile picture" />
|
|
|
|
</div>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
HTML;
|
2023-03-15 09:15:55 +00:00
|
|
|
$expected = new \Lewisdale\Webmentions\Models\Author(
|
|
|
|
null,
|
|
|
|
"Anne Author",
|
|
|
|
"https://my-blog.com",
|
|
|
|
"https://dummyimage.com/100x100/fff/aaa"
|
|
|
|
);
|
2023-03-15 13:49:53 +00:00
|
|
|
$this->verifyContent($source, $target, $content, $this->objectContains('author', $expected));
|
2023-03-15 09:15:55 +00:00
|
|
|
}
|
|
|
|
}
|