Files
fewo-stommer/tests/Feature/FloorPlanTest.php
T

158 lines
6.7 KiB
PHP

<?php
namespace Tests\Feature;
use App\Enums\PhotoCategory;
use App\Enums\UserRole;
use App\Filament\Resources\FloorPlans\FloorPlanResource;
use App\Filament\Resources\FloorPlans\Pages\CreateFloorPlan;
use App\Models\Amenity;
use App\Models\FloorPlan;
use App\Models\Page;
use App\Models\Photo;
use App\Models\Property;
use App\Models\Site;
use App\Models\User;
use Carbon\CarbonImmutable;
use Database\Seeders\PropertySeeder;
use Database\Seeders\SiteSeeder;
use Database\Seeders\WebsiteContentSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;
class FloorPlanTest extends TestCase
{
use RefreshDatabase;
private Property $property;
protected function setUp(): void
{
parent::setUp();
$this->travelTo(CarbonImmutable::parse('2026-10-22 10:00'));
$this->seed([PropertySeeder::class, WebsiteContentSeeder::class, SiteSeeder::class]);
$this->withoutVite();
$this->property = Property::sole();
}
public function test_floor_plan_belongs_to_the_landlord_of_its_apartment(): void
{
$plan = FloorPlan::factory()->for($this->property)->create();
$this->assertSame($this->property->tenant_id, $plan->tenant_id);
$this->assertTrue($this->property->floorPlan->is($plan));
}
public function test_area_points_are_parsed_in_percent_and_invalid_pairs_are_dropped(): void
{
$this->assertSame([[10.0, 20.5], [50.0, 20.5], [50.0, 65.0]], FloorPlan::points(' 10,20.5 50,20.5 50,65 abc 150,3 '));
$this->assertSame([], FloorPlan::points(null));
}
public function test_section_shows_visible_rooms_with_area_photo_equipment_and_hint(): void
{
$roomPhoto = Photo::factory()->create(['alt_text' => 'Glasdusche im Bad']);
$shower = Amenity::factory()->for($this->property)->create(['name' => 'Dusche mit niedrigem Einstieg']);
FloorPlan::factory()->for($this->property)->create([
'photo_id' => Photo::factory()->create(['alt_text' => 'Grundriss der Wohnung'])->id,
'rooms' => [
['id' => 'a', 'name' => 'Bad', 'description' => 'Bad mit Glasdusche.', 'hint' => 'Flache Dusche mit kleiner Kante.', 'points' => '50,40 66,40 66,65', 'photo_ids' => [$roomPhoto->id], 'amenity_ids' => [$shower->id], 'is_visible' => true],
['id' => 'b', 'name' => 'Abstellkammer', 'points' => '1,1 2,1 2,2', 'is_visible' => false],
['id' => 'c', 'name' => 'Ohne Fläche', 'points' => '1,1 2,2', 'is_visible' => true],
],
]);
$this->placeSectionOnApartmentPage();
$this->get('/wohnung')
->assertOk()
->assertSee('Der Grundriss')
->assertSee('points="50,40 66,40 66,65"', false)
->assertSee('id="raum-bad-1"', false)
->assertSee('Bad mit Glasdusche.')
->assertSee('Flache Dusche mit kleiner Kante.')
->assertSee('Dusche mit niedrigem Einstieg')
->assertSee('Glasdusche im Bad')
->assertDontSee('Abstellkammer')
->assertDontSee('Ohne Fläche');
}
public function test_plan_pictures_serve_the_floor_plan_even_when_hidden_but_never_appear_in_galleries(): void
{
$plan = Photo::factory()->create(['category' => PhotoCategory::Plan, 'is_published' => false]);
Photo::factory()->create(['alt_text' => 'Lageplan des Hauses', 'category' => PhotoCategory::Plan]);
Photo::factory()->create(['alt_text' => 'Wohnzimmer mit Erker', 'category' => PhotoCategory::Apartment]);
FloorPlan::factory()->for($this->property)->create([
'photo_id' => $plan->id,
'rooms' => [['id' => 'a', 'name' => 'Bad', 'points' => '50,40 66,40 66,65', 'is_visible' => true]],
]);
$this->placeSectionOnApartmentPage();
$page = Site::sole()->pages()->where('slug', 'wohnung')->sole();
$page->update(['sections' => [...$page->sections, ['type' => 'gallery', 'data' => ['heading' => 'Alle Bilder', 'category' => 'all']]]]);
$this->get('/wohnung')
->assertOk()
->assertSee('points="50,40 66,40 66,65"', false)
->assertSee('Wohnzimmer mit Erker')
->assertDontSee('Lageplan des Hauses');
}
public function test_floor_plan_pictures_move_to_the_plan_category(): void
{
$plan = Photo::factory()->create(['category' => PhotoCategory::Apartment, 'is_published' => false]);
$other = Photo::factory()->create(['category' => PhotoCategory::Apartment]);
FloorPlan::factory()->for($this->property)->create(['photo_id' => $plan->id]);
(require database_path('migrations/2026_10_29_184027_move_floor_plan_pictures_to_the_plan_category.php'))->up();
$this->assertSame(PhotoCategory::Plan, $plan->fresh()->category);
$this->assertFalse($plan->fresh()->is_published);
$this->assertSame(PhotoCategory::Apartment, $other->fresh()->category);
}
public function test_section_shows_nothing_without_a_floor_plan(): void
{
$this->placeSectionOnApartmentPage();
$this->get('/wohnung')->assertOk()->assertDontSee('Der Grundriss');
}
public function test_content_editor_creates_a_floor_plan_and_a_room_needs_an_area(): void
{
$this->actingAs(User::factory()->role(UserRole::Content)->create());
$planPhoto = Photo::factory()->create();
$this->get(FloorPlanResource::getUrl('index'))->assertOk();
Livewire::test(CreateFloorPlan::class)
->fillForm([
'property_id' => $this->property->id,
'photo_id' => $planPhoto->id,
'rooms' => ['diele' => ['id' => 'x', 'name' => 'Diele', 'points' => '10,10 20,10', 'is_visible' => true]],
])
->call('create')
->assertHasFormErrors(['rooms.diele.points']);
Livewire::test(CreateFloorPlan::class)
->fillForm([
'property_id' => $this->property->id,
'photo_id' => $planPhoto->id,
'rooms' => ['diele' => ['id' => 'x', 'name' => 'Diele', 'points' => '10,10 20,10 20,30 10,30', 'is_visible' => true]],
])
->call('create')
->assertHasNoFormErrors();
$plan = FloorPlan::sole();
$this->assertSame($this->property->tenant_id, $plan->tenant_id);
$this->assertSame(['Diele'], $plan->visibleRooms()->pluck('name')->all());
}
private function placeSectionOnApartmentPage(): void
{
$page = Site::sole()->pages()->where('slug', 'wohnung')->sole();
$page->update(['sections' => [...$page->sections, ['type' => 'floor_plan', 'data' => ['heading' => 'Der Grundriss']]]]);
$this->assertInstanceOf(Page::class, $page);
}
}