jonnybarnes.uk/tests/Unit/Jobs/SendWebMentionJobTest.php
Jonny Barnes e08763c526
Migrate outbound HTTP calls from Guzzle to the Http facade
Replaces every raw GuzzleHttp\Client usage (Nominatim, web.archive.org,
webmention fetch/discover/send, Bridgy syndication, IndieAuth client_id
lookup, contact avatar/h-card fetch, profile image download, and the
CloudConvert screenshot pipeline) with Illuminate\Support\Facades\Http,
and enables Http::preventStrayRequests() globally in tests so any
un-faked outbound call now fails loudly instead of silently hitting the
network.

The CloudConvert retry-until-finished middleware in AppServiceProvider
is replaced by a plain polling loop in SaveScreenshot, which also fixes
a latent bug where the old middleware decoded a response object instead
of its body. Bridgy syndication jobs keep their tries=1/no-retry
semantics unchanged to avoid duplicate publishes. Guzzle's PSR-7 helpers
(Header, UriResolver, Utils) stay in SendWebMentions since Http has no
equivalent for them.

Every affected test file's Guzzle MockHandler/HandlerStack boilerplate
is replaced with Http::fake()/Http::sequence().

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01P8j7cJhCiYDsGUgB7obKNQ
2026-07-26 09:12:01 +01:00

110 lines
3.3 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
declare(strict_types=1);
namespace Tests\Unit\Jobs;
use App\Jobs\SendWebMentions;
use App\Models\Note;
use Illuminate\Support\Facades\Http;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
class SendWebMentionJobTest extends TestCase
{
#[Test]
public function discover_webmention_endpoint_on_own_domain(): void
{
$note = new Note;
$job = new SendWebMentions($note);
$this->assertNull($job->discoverWebmentionEndpoint(config('app.url')));
$this->assertNull($job->discoverWebmentionEndpoint('/notes/tagged/test'));
}
#[Test]
public function discover_webmention_endpoint_from_header_links(): void
{
$url = 'https://example.org/webmention';
Http::fake([
'*' => Http::response('', 200, ['Link' => '<'.$url.'>; rel="webmention"']),
]);
$job = new SendWebMentions(new Note);
$this->assertEquals($url, $job->discoverWebmentionEndpoint('https://example.org'));
}
#[Test]
public function discover_webmention_endpoint_from_html_link_tags(): void
{
$html = '<link rel="webmention" href="https://example.org/webmention">';
Http::fake([
'*' => Http::response($html, 200),
]);
$job = new SendWebMentions(new Note);
$this->assertEquals(
'https://example.org/webmention',
$job->discoverWebmentionEndpoint('https://example.org')
);
}
#[Test]
public function discover_webmention_endpoint_from_legacy_html_markup(): void
{
$html = '<link rel="http://webmention.org/" href="https://example.org/webmention">';
Http::fake([
'*' => Http::response($html, 200),
]);
$job = new SendWebMentions(new Note);
$this->assertEquals(
'https://example.org/webmention',
$job->discoverWebmentionEndpoint('https://example.org')
);
}
#[Test]
public function ensure_empty_note_does_not_trigger_any_actions(): void
{
$job = new SendWebMentions(new Note);
$this->assertNull($job->handle());
}
#[Test]
public function we_resolve_relative_uris(): void
{
$uri = '/blog/post';
$base = 'https://example.org/';
$job = new SendWebMentions(new Note);
$this->assertEquals('https://example.org/blog/post', $job->resolveUri($uri, $base));
}
#[Test]
public function we_send_a_webmention_for_a_note(): void
{
$html = '<link rel="http://webmention.org/" href="https://example.org/webmention">';
Http::fake([
'example.org/webmention' => Http::response('', 202),
'*' => Http::response($html, 200),
]);
$note = new Note;
$note->note = 'Hi [Aaron](https://aaronparecki.com)';
$note->save();
$job = new SendWebMentions($note);
$job->handle();
$this->assertTrue(true);
}
#[Test]
public function links_in_notes_can_not_support_webmentions(): void
{
Http::fake([
// URLs with commas currently break the parse function Im using
'*' => Http::response('', 200, ['Link' => '<https://example.org/foo,bar>; rel="preconnect"']),
]);
$job = new SendWebMentions(new Note);
$this->assertNull($job->discoverWebmentionEndpoint('https://example.org'));
}
}