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); // Die Administration sieht zusätzlich den technischen Grund $this->actingAs(User::factory()->admin()->create()) ->post('/profil/bild', ['avatar' => UploadedFile::fake()->image('foto.jpg', 800, 600)]) ->assertSessionHasErrors('avatar'); $this->assertStringContainsString('Technischer Grund: RuntimeException: Encoder WebP nicht verfügbar', session('errors')->first('avatar')); } 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); } }