Files
fewo-stommer/tests/Feature/WebsiteContentTest.php
LarsandClaude Opus 5.5 1871c04507
Deploy / test (push) Failing after 2s
Deploy / deploy (push) Skipped
Fix: photos uploaded in the admin were stored privately and did not show on the website
The admin upload field used Filament's default disk (FILESYSTEM_DISK=local, private), while imports
and the website use the public disk – a photo uploaded in the admin appeared in the photo list but
its web sizes returned 403 (e.g. as floor plan picture). The photo collection and the upload field
now always use the public disk; a data migration moves photos already stored privately (original and
web sizes, folder = media id) to the public disk. Tests for the upload and the migration.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 17:12:32 +02:00

144 lines
6.1 KiB
PHP

<?php
namespace Tests\Feature;
use App\Enums\PhotoCategory;
use App\Enums\UserRole;
use App\Filament\Pages\EditContent;
use App\Filament\Resources\Amenities\AmenityResource;
use App\Filament\Resources\Faqs\FaqResource;
use App\Filament\Resources\Notices\NoticeResource;
use App\Filament\Resources\Photos\Pages\CreatePhoto;
use App\Filament\Resources\Photos\Pages\ListPhotos;
use App\Filament\Resources\Photos\PhotoResource;
use App\Filament\Resources\Properties\PropertyResource;
use App\Models\Amenity;
use App\Models\Notice;
use App\Models\Photo;
use App\Models\User;
use App\Support\Content;
use Carbon\CarbonImmutable;
use Database\Seeders\PropertySeeder;
use Database\Seeders\WebsiteContentSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Livewire\Livewire;
use Tests\TestCase;
class WebsiteContentTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
Storage::fake('public');
$this->seed([PropertySeeder::class, WebsiteContentSeeder::class]);
$this->actingAs(User::factory()->role(UserRole::Content)->create());
}
public function test_content_editor_sees_website_areas_but_not_bookings(): void
{
foreach ([PhotoResource::class, AmenityResource::class, FaqResource::class, NoticeResource::class] as $resource) {
$this->get($resource::getUrl('index'))->assertOk();
}
$this->get(EditContent::getUrl())->assertOk()->assertSee('Webseiten-Texte');
$this->get(PropertyResource::getUrl('index'))->assertForbidden();
}
public function test_texts_fall_back_to_defaults_and_can_be_saved(): void
{
$this->assertStringContainsString('seit über 20 Jahren', Content::get('host.quote'));
Livewire::test(EditContent::class)
->set('data.host__quote', 'Willkommen an der Ostsee')
->set('data.legal__imprint', '<p>Angaben gemäß § 5 DDG</p><script>alert(1)</script>')
->call('save')
->assertNotified('Texte gespeichert');
$this->assertSame('Willkommen an der Ostsee', Content::get('host.quote'));
$this->assertStringContainsString('§ 5 DDG', (string) Content::html('legal.imprint'));
$this->assertStringNotContainsString('<script>', (string) Content::html('legal.imprint'));
}
public function test_uploaded_photos_get_webp_web_sizes(): void
{
$photo = Photo::factory()->create();
$photo->addMedia(UploadedFile::fake()->image('terrasse.jpg', 3000, 2000))->toMediaCollection(Photo::COLLECTION);
$media = $photo->fresh()->getFirstMedia(Photo::COLLECTION);
foreach (array_keys(Photo::WIDTHS) as $conversion) {
$this->assertTrue($media->hasGeneratedConversion($conversion), "conversion {$conversion}");
$this->assertStringEndsWith('.webp', $photo->url($conversion));
}
$this->assertStringContainsString('2048w', $photo->srcset());
}
public function test_several_photos_are_uploaded_at_once(): void
{
Livewire::test(ListPhotos::class)
->callAction('bulkUpload', [
'files' => [UploadedFile::fake()->image('wohn-zimmer.jpg', 1600, 1000), UploadedFile::fake()->image('kueche.jpg', 1600, 1000)],
'category' => PhotoCategory::Apartment->value,
])
->assertNotified('2 Bilder hochgeladen');
$this->assertSame(['Wohn zimmer', 'Kueche'], Photo::orderBy('sort_order')->pluck('alt_text')->all());
}
public function test_photo_uploaded_in_the_admin_is_stored_on_the_public_disk(): void
{
Storage::fake('local');
Livewire::test(CreatePhoto::class)
->fillForm([
'image' => [UploadedFile::fake()->image('grundriss.png', 1600, 900)],
'alt_text' => 'Grundriss mit Teppichen',
'category' => PhotoCategory::Apartment,
])
->call('create')
->assertHasNoFormErrors();
$media = Photo::query()->where('alt_text', 'Grundriss mit Teppichen')->sole()->getFirstMedia(Photo::COLLECTION);
$this->assertSame(['public', 'public'], [$media->disk, $media->conversions_disk]);
Storage::disk('public')->assertExists($media->id.'/'.$media->file_name);
$this->assertSame([], Storage::disk('local')->allFiles());
}
public function test_migration_moves_photos_from_the_private_disk_to_the_public_disk(): void
{
Storage::fake('local');
$photo = Photo::factory()->create();
$media = $photo->addMedia(UploadedFile::fake()->image('alt.jpg', 1200, 800))->toMediaCollection(Photo::COLLECTION, 'local');
Storage::disk('local')->assertExists($media->id.'/alt.jpg');
(require database_path('migrations/2026_10_23_171001_move_photo_media_to_the_public_disk.php'))->up();
$media->refresh();
$this->assertSame(['public', 'public'], [$media->disk, $media->conversions_disk]);
Storage::disk('public')->assertExists($media->id.'/alt.jpg');
Storage::disk('public')->assertExists($media->id.'/conversions/alt-lg.webp');
Storage::disk('local')->assertMissing($media->id.'/alt.jpg');
$this->assertStringEndsWith('/conversions/alt-lg.webp', $photo->fresh()->url('lg'));
}
public function test_notices_are_visible_only_within_their_dates(): void
{
$this->travelTo(CarbonImmutable::parse('2026-11-10'));
Notice::factory()->create(['text' => 'aktuell', 'starts_on' => '2026-11-01', 'ends_on' => '2026-11-15']);
Notice::factory()->create(['text' => 'vorbei', 'ends_on' => '2026-11-09']);
Notice::factory()->create(['text' => 'inaktiv', 'is_active' => false]);
Notice::factory()->create(['text' => 'dauerhaft']);
$this->assertEqualsCanonicalizing(['aktuell', 'dauerhaft'], Notice::visible()->pluck('text')->all());
}
public function test_amenities_from_the_old_website_are_seeded(): void
{
$this->assertGreaterThan(20, Amenity::count());
$this->assertSame(6, Amenity::where('is_highlight', true)->count());
}
}