Loheide-eu-mitarbeiterplatt.../tests/Feature/Profile/AvatarTest.php
Friederich Loheide 293d7f86a4
Some checks failed
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 3m4s
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 3m8s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 3m32s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 3m35s
CI / PHP 8.3 / mysql:8.0 (pull_request) Successful in 5m7s
CI / PHP 8.4 / mysql:8.0 (pull_request) Successful in 5m1s
CI / PHP 8.3 / mysql:8.4 (pull_request) Successful in 5m30s
CI / PHP 8.4 / mysql:8.4 (pull_request) Successful in 5m31s
CI / Build-Artefakte aktuell (pull_request) Failing after 1m26s
Plugins bei fehlender Klasse nicht dauerhaft deaktivieren, Speichern-Abstand im Admin, Profilbild-Fehlermeldung
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 18:28:03 +00:00

94 lines
3.5 KiB
PHP

<?php
namespace Tests\Feature\Profile;
use App\Models\User;
use App\Support\Avatars;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;
class AvatarTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
Storage::fake('uploads');
}
public function test_server_problems_show_a_message_instead_of_an_error_page(): void
{
$this->partialMock(Avatars::class)->shouldReceive('store')->andThrow(new \RuntimeException('Encoder WebP nicht verfügbar'));
$user = User::factory()->create();
$this->actingAs($user)
->from('/profil/bearbeiten')
->post('/profil/bild', ['avatar' => UploadedFile::fake()->image('foto.jpg', 800, 600)])
->assertRedirect('/profil/bearbeiten')
->assertSessionHasErrors(['avatar' => 'Das Bild konnte nicht gespeichert werden. Bitte ein anderes Foto versuchen oder die Administration informieren (Details unter „Systemzustand“ und im Fehlerprotokoll).']);
$this->assertNull($user->fresh()->avatar_path);
}
public function test_avatar_is_stored_as_square_webp_in_all_sizes(): void
{
$user = User::factory()->create();
$this->actingAs($user)
->post('/profil/bild', ['avatar' => UploadedFile::fake()->image('foto.jpg', 800, 600)])
->assertRedirect()
->assertSessionHasNoErrors();
$user->refresh();
$this->assertStringStartsWith("avatars/{$user->id}/", $user->avatar_path);
Storage::disk('uploads')->assertExists("{$user->avatar_path}-96.webp");
Storage::disk('uploads')->assertExists("{$user->avatar_path}-256.webp");
[$width, $height] = getimagesizefromstring(Storage::disk('uploads')->get("{$user->avatar_path}-256.webp"));
$this->assertSame([256, 256], [$width, $height]);
$this->assertStringEndsWith("{$user->avatar_path}-96.webp", $user->avatarUrl(96));
}
public function test_old_avatar_is_replaced_and_removed(): void
{
$user = User::factory()->create();
$this->actingAs($user)->post('/profil/bild', ['avatar' => UploadedFile::fake()->image('a.png', 300, 300)]);
$old = $user->fresh()->avatar_path;
$this->actingAs($user)->post('/profil/bild', ['avatar' => UploadedFile::fake()->image('b.png', 300, 300)]);
$new = $user->fresh()->avatar_path;
$this->assertNotSame($old, $new);
Storage::disk('uploads')->assertMissing("{$old}-256.webp");
Storage::disk('uploads')->assertExists("{$new}-256.webp");
}
public function test_avatar_can_be_removed(): void
{
$user = User::factory()->create();
$this->actingAs($user)->post('/profil/bild', ['avatar' => UploadedFile::fake()->image('a.png', 300, 300)]);
$path = $user->fresh()->avatar_path;
$this->actingAs($user)->delete('/profil/bild')->assertRedirect();
$this->assertNull($user->fresh()->avatar_path);
Storage::disk('uploads')->assertMissing("{$path}-96.webp");
}
public function test_only_images_are_accepted(): void
{
$user = User::factory()->create();
$this->actingAs($user)
->post('/profil/bild', ['avatar' => UploadedFile::fake()->create('lebenslauf.pdf', 100, 'application/pdf')])
->assertSessionHasErrors('avatar');
$this->assertNull($user->fresh()->avatar_path);
}
}