Move webmention parsing and authorship into the app
The jonnybarnes/webmentions-parser package is now archived, and its guzzlehttp/guzzle ^6|^7 constraint was the only thing holding Guzzle back from 8. Port the parts we use into App\Services\WebMention and drop the package. Parser keeps getMentionType and the checkInReplyTo/LikeOf/RepostOf checks unchanged. Authorship follows the same authorship algorithm, with some fixes: - fetch the author page with Laravel's Http client, so tests can fake it, and treat connection failures as AuthorshipParserException too - keep state in locals so one instance can be reused safely - make the url == uid == author-page check actually compare the h-card's uid, rather than an undefined variable Add direct tests for both, and mark the library as archived on the projects page. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
79c112bb86
commit
df9b0dc490
1 changed files with 526 additions and 64 deletions
89
tests/Unit/Services/WebMention/ParserTest.php
Normal file
89
tests/Unit/Services/WebMention/ParserTest.php
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\Services\WebMention;
|
||||
|
||||
use App\Exceptions\InvalidMentionException;
|
||||
use App\Services\WebMention\Parser;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ParserTest extends TestCase
|
||||
{
|
||||
private const string TARGET = 'https://jonnybarnes.uk/notes/A';
|
||||
|
||||
private function entry(array $properties): array
|
||||
{
|
||||
return ['items' => [['type' => ['h-entry'], 'properties' => $properties]]];
|
||||
}
|
||||
|
||||
public static function mentionTypes(): array
|
||||
{
|
||||
return [
|
||||
'reply' => ['in-reply-to'],
|
||||
'like' => ['like-of'],
|
||||
'repost' => ['repost-of'],
|
||||
];
|
||||
}
|
||||
|
||||
#[Test]
|
||||
#[DataProvider('mentionTypes')]
|
||||
public function it_determines_the_mention_type(string $type): void
|
||||
{
|
||||
$this->assertSame($type, (new Parser)->getMentionType($this->entry([$type => [self::TARGET]])));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_finds_the_mention_type_in_nested_microformats(): void
|
||||
{
|
||||
$mf = ['items' => [['type' => ['h-feed'], 'children' => [
|
||||
['type' => ['h-entry'], 'properties' => ['like-of' => [self::TARGET]]],
|
||||
]]]];
|
||||
|
||||
$this->assertSame('like-of', (new Parser)->getMentionType($mf));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_throws_when_the_mention_type_cannot_be_determined(): void
|
||||
{
|
||||
$this->expectException(InvalidMentionException::class);
|
||||
|
||||
(new Parser)->getMentionType($this->entry(['content' => ['Just a post']]));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_checks_a_reply_url_matches_the_target(): void
|
||||
{
|
||||
$parser = new Parser;
|
||||
|
||||
$this->assertTrue($parser->checkInReplyTo($this->entry(['in-reply-to' => [self::TARGET]]), self::TARGET));
|
||||
$this->assertFalse($parser->checkInReplyTo($this->entry(['in-reply-to' => ['https://example.org/other']]), self::TARGET));
|
||||
$this->assertFalse($parser->checkInReplyTo($this->entry(['content' => ['No reply']]), self::TARGET));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_checks_a_reply_context_h_cite_matches_the_target(): void
|
||||
{
|
||||
$hCite = fn (string $url): array => ['type' => ['h-cite'], 'properties' => ['url' => [$url]]];
|
||||
$parser = new Parser;
|
||||
|
||||
$this->assertTrue($parser->checkInReplyTo($this->entry(['in-reply-to' => [$hCite(self::TARGET)]]), self::TARGET));
|
||||
$this->assertFalse($parser->checkInReplyTo($this->entry(['in-reply-to' => [$hCite('https://example.org/other')]]), self::TARGET));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_checks_likes_and_reposts_match_the_target(): void
|
||||
{
|
||||
$parser = new Parser;
|
||||
|
||||
$this->assertTrue($parser->checkLikeOf($this->entry(['like-of' => [self::TARGET]]), self::TARGET));
|
||||
$this->assertFalse($parser->checkLikeOf($this->entry(['like-of' => ['https://example.org/other']]), self::TARGET));
|
||||
$this->assertFalse($parser->checkLikeOf($this->entry(['repost-of' => [self::TARGET]]), self::TARGET));
|
||||
|
||||
$this->assertTrue($parser->checkRepostOf($this->entry(['repost-of' => [self::TARGET]]), self::TARGET));
|
||||
$this->assertFalse($parser->checkRepostOf($this->entry(['repost-of' => ['https://example.org/other']]), self::TARGET));
|
||||
$this->assertFalse($parser->checkRepostOf($this->entry(['like-of' => [self::TARGET]]), self::TARGET));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue