jonnybarnes.uk/tests/Feature/Admin/ContactsTest.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

208 lines
6.3 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature\Admin;
use App\Models\Contact;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Http;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
class ContactsTest extends TestCase
{
use RefreshDatabase;
protected function tearDown(): void
{
if (file_exists(public_path().'/assets/profile-images/tantek.com/image')) {
unlink(public_path().'/assets/profile-images/tantek.com/image');
rmdir(public_path().'/assets/profile-images/tantek.com');
}
parent::tearDown();
}
#[Test]
public function contact_index_page_loads(): void
{
$user = User::factory()->make();
$response = $this->actingAs($user)->get('/admin/contacts');
$response->assertViewIs('admin.contacts.index');
}
#[Test]
public function contact_create_page_loads(): void
{
$user = User::factory()->make();
$response = $this->actingAs($user)->get('/admin/contacts/create');
$response->assertViewIs('admin.contacts.create');
}
#[Test]
public function admin_can_create_new_contact(): void
{
$user = User::factory()->make();
$this->actingAs($user)->post('/admin/contacts', [
'name' => 'Fred Bloggs',
'nick' => 'fred',
'homepage' => 'https://fred.blog/gs',
]);
$this->assertDatabaseHas('contacts', [
'name' => 'Fred Bloggs',
'nick' => 'fred',
'homepage' => 'https://fred.blog/gs',
]);
}
#[Test]
public function admin_can_see_form_to_edit_contact(): void
{
$user = User::factory()->make();
$contact = Contact::factory()->create();
$response = $this->actingAs($user)->get('/admin/contacts/'.$contact->id.'/edit');
$response->assertViewIs('admin.contacts.edit');
}
#[Test]
public function admin_can_update_contact(): void
{
$user = User::factory()->make();
$contact = Contact::factory()->create();
$this->actingAs($user)->post('/admin/contacts/'.$contact->id, [
'_method' => 'PUT',
'name' => 'Tantek Celik',
'nick' => 'tantek',
'homepage' => 'https://tantek.com',
'twitter' => 't',
]);
$this->assertDatabaseHas('contacts', [
'name' => 'Tantek Celik',
'homepage' => 'https://tantek.com',
]);
}
#[Test]
public function admin_can_edit_contact_and_upload_avatar(): void
{
copy(__DIR__.'/../../aaron.png', sys_get_temp_dir().'/tantek.png');
$path = sys_get_temp_dir().'/tantek.png';
$file = new UploadedFile($path, 'tantek.png', 'image/png', null, true);
$user = User::factory()->make();
$contact = Contact::factory()->create();
$this->actingAs($user)->post('/admin/contacts/'.$contact->id, [
'_method' => 'PUT',
'name' => 'Tantek Celik',
'nick' => 'tantek',
'homepage' => 'https://tantek.com',
'twitter' => 't',
'avatar' => $file,
]);
$this->assertFileEquals(
__DIR__.'/../../aaron.png',
public_path().'/assets/profile-images/tantek.com/image'
);
}
#[Test]
public function admin_can_delete_contact(): void
{
$user = User::factory()->make();
$contact = Contact::factory()->create(['nick' => 'tantek']);
$this->assertDatabaseHas('contacts', [
'nick' => 'tantek',
]);
$this->actingAs($user)->post('/admin/contacts/'.$contact->id, [
'_method' => 'DELETE',
]);
$this->assertDatabaseMissing('contacts', [
'nick' => 'tantek',
]);
}
#[Test]
public function admin_can_trigger_retrieval_of_remote_avatar(): void
{
$html = <<<'HTML'
<div class="h-card">
<img class="u-photo" alt="" src="http://tantek.com/tantek.png">
</div>
HTML;
$file = file_get_contents(__DIR__.'/../../aaron.png');
Http::fake([
'*' => Http::sequence()
->push($html, 200, ['Content-Type' => 'text/html'])
->push($file, 200, ['Content-Type' => 'image/png']),
]);
$user = User::factory()->make();
$contact = Contact::factory()->create([
'homepage' => 'https://tantek.com',
]);
$this->actingAs($user)->get('/admin/contacts/'.$contact->id.'/getavatar');
$this->assertFileEquals(
__DIR__.'/../../aaron.png',
public_path().'/assets/profile-images/tantek.com/image'
);
}
#[Test]
public function getting_remote_avatar_fails_gracefully_with_remote_not_found(): void
{
Http::fake([
'*' => Http::response('', 404),
]);
$user = User::factory()->make();
$contact = Contact::factory()->create();
$response = $this->actingAs($user)->get('/admin/contacts/'.$contact->id.'/getavatar');
$response->assertRedirect('/admin/contacts/'.$contact->id.'/edit');
}
#[Test]
public function getting_remote_avatar_fails_gracefully_with_remote_error(): void
{
$html = <<<'HTML'
<div class="h-card">
<img class="u-photo" src="http://tantek.com/tantek.png">
</div>
HTML;
Http::fake([
'*' => Http::sequence()
->push($html, 200, ['Content-Type' => 'text/html'])
->push('', 404),
]);
$user = User::factory()->make();
$contact = Contact::factory()->create();
$response = $this->actingAs($user)->get('/admin/contacts/'.$contact->id.'/getavatar');
$response->assertRedirect('/admin/contacts/'.$contact->id.'/edit');
}
#[Test]
public function getting_remote_avatar_fails_gracefully_for_contact_with_no_hompage(): void
{
$contact = Contact::create([
'nick' => 'fred',
'name' => 'Fred Bloggs',
]);
$user = User::factory()->make();
$response = $this->actingAs($user)->get('/admin/contacts/'.$contact->id.'/getavatar');
$response->assertRedirect('/admin/contacts/'.$contact->id.'/edit');
}
}