Files
fewo-stommer/tests/Feature/ContractAnnexTest.php
T
LarsandClaude Opus 5.5 cf11c8a474
Deploy FeWo mit PipelineManager / deploy (push) Successful in 3m59s
Dokumentenzusammenstellung und Vorlagensteuerung (Paket 3, Prozessarchitektur v0.10)
Eine Zusammenstellung vor jedem Dokument (Angebot, Bestaetigung, Buchungsunterlagen
extern), Dokumentart je Vorgangsart, Freigaben fuer Vorlagen und Dokumentarten,
Schutz vor unvereinbarten Bedingungen bei Portalbuchungen (E-004), kein doppelter
Versand, Absicherung Hold 0 bis Paket 7, A6-Aufenthaltsbestaetigung vorbereitet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 19:44:52 +01:00

147 lines
7.3 KiB
PHP

<?php
namespace Tests\Feature;
use App\Actions\SendOffer;
use App\Documents\OfferTerms;
use App\Filament\Resources\Bookings\Pages\ViewBooking;
use App\Filament\Resources\ContractAnnexes\ContractAnnexResource;
use App\Filament\Resources\ContractAnnexes\Pages\ListContractAnnexes;
use App\Models\Booking;
use App\Models\Channel;
use App\Models\ContractAnnex;
use App\Models\ContractTemplate;
use App\Models\Extra;
use App\Models\Property;
use App\Models\User;
use Carbon\CarbonImmutable;
use Database\Seeders\PropertySeeder;
use Database\Seeders\SiteSeeder;
use Filament\Actions\Testing\TestAction;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Storage;
use Livewire\Livewire;
use Tests\TestCase;
/**
* Contract annexes: own documents with conditions, attached per booking, frozen with the offer.
*/
class ContractAnnexTest 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, SiteSeeder::class]);
$this->property = Property::sole();
Mail::fake();
Storage::fake('local');
$this->withoutVite();
}
public function test_conditions_decide_which_annexes_fit_a_booking(): void
{
$linen = Extra::query()->create(['property_id' => $this->property->id, 'name' => 'Wäschepaket', 'price' => 2300, 'pricing' => 'per_item', 'is_active' => true]);
$portal = Channel::factory()->for($this->property)->create(['name' => 'FeWo-direkt']);
$always = $this->annex('WLAN', ContractAnnex::ALWAYS);
$withLinen = $this->annex('Wäsche', ContractAnnex::EXTRA, $linen->id);
$viaPortal = $this->annex('Portal', ContractAnnex::CHANNEL, $portal->id);
$manual = $this->annex('Key', ContractAnnex::MANUAL);
$template = ContractTemplate::query()->create(['released_at' => now(), 'name' => 'T', 'version' => 1, 'annex_ids' => [$always->id, $withLinen->id, $viaPortal->id, $manual->id]]);
$plain = $this->booking();
$this->assertSame(['WLAN'], $template->annexesFor($plain)->pluck('name')->all());
$withExtra = $this->booking(['extras' => [$linen->id => 2], 'arrival' => '2026-12-01', 'departure' => '2026-12-04']);
$this->assertEqualsCanonicalizing(['WLAN', 'Wäsche'], $template->annexesFor($withExtra)->pluck('name')->all());
$fromPortal = $this->booking(['import_channel_id' => $portal->id, 'source' => 'portal', 'arrival' => '2026-11-20', 'departure' => '2026-11-23']);
$this->assertEqualsCanonicalizing(['WLAN', 'Portal'], $template->annexesFor($fromPortal)->pluck('name')->all());
}
public function test_the_offer_freezes_the_matching_annexes_and_shows_them_everywhere(): void
{
$annex = $this->annex('WLAN', ContractAnnex::ALWAYS, null, 'Anlage WLAN', '<p>WLAN für {{ mieter }}</p>');
$template = ContractTemplate::query()->create(['released_at' => now(), 'name' => 'T', 'version' => 1, 'body' => '<p>Vertrag</p>', 'annex_ids' => [$annex->id]]);
$offer = app(SendOffer::class)->handle($this->booking(), ['contract_template_id' => $template->id]);
$this->assertSame([$annex->id], array_column($offer->offer_terms['annexes'], 'id'));
$annex->update(['body' => '<p>GEÄNDERT</p>']);
$rendered = OfferTerms::annexes($offer->fresh());
$this->assertStringContainsString($offer->guest->full_name, $rendered[0]['html']);
$this->assertStringNotContainsString('GEÄNDERT', $rendered[0]['html']);
$this->get(app(SendOffer::class)->offerUrl($offer))->assertOk()->assertSee('Anlage: Anlage WLAN');
Storage::disk('local')->assertExists($offer->offer_path);
}
public function test_an_explicit_selection_in_the_offer_replaces_the_automatic_one(): void
{
$auto = $this->annex('WLAN', ContractAnnex::ALWAYS);
$manual = $this->annex('Key', ContractAnnex::MANUAL);
$template = ContractTemplate::query()->create(['released_at' => now(), 'name' => 'T', 'version' => 1, 'annex_ids' => [$auto->id]]);
$offer = app(SendOffer::class)->handle($this->booking(), ['contract_template_id' => $template->id, 'annex_ids' => [$manual->id]]);
$this->assertSame(['Key'], array_column($offer->offer_terms['annexes'], 'name'));
$none = app(SendOffer::class)->handle($this->booking(['arrival' => '2026-12-01', 'departure' => '2026-12-04']), ['contract_template_id' => $template->id, 'annex_ids' => []]);
$this->assertSame([], $none->offer_terms['annexes']);
}
public function test_used_annex_versions_are_locked_and_new_versions_can_be_made(): void
{
$annex = $this->annex('WLAN', ContractAnnex::ALWAYS);
$template = ContractTemplate::query()->create(['released_at' => now(), 'name' => 'T', 'version' => 1, 'annex_ids' => [$annex->id]]);
$this->assertTrue(ContractAnnexResource::canEdit($annex));
app(SendOffer::class)->handle($this->booking(), ['contract_template_id' => $template->id]);
$this->assertFalse(ContractAnnexResource::canEdit($annex->fresh()));
$this->actingAs(User::factory()->create());
Livewire::test(ListContractAnnexes::class)->callAction(TestAction::make('newVersion')->table($annex), []);
$this->assertSame([1, 2], ContractAnnex::query()->where('name', 'WLAN')->orderBy('version')->pluck('version')->all());
}
public function test_recording_extra_services_in_the_booking_changes_the_preselected_annexes(): void
{
$linen = Extra::query()->create(['property_id' => $this->property->id, 'name' => 'Wäschepaket', 'price' => 0, 'pricing' => 'per_item', 'is_active' => true]);
$annex = $this->annex('Wäsche', ContractAnnex::EXTRA, $linen->id);
$template = ContractTemplate::query()->create(['released_at' => now(), 'name' => 'T', 'version' => 1, 'annex_ids' => [$annex->id]]);
$this->property->update(['contract_template_id' => $template->id]);
$booking = $this->booking();
$this->actingAs(User::factory()->create());
$this->assertSame([], OfferTerms::templateFor($booking)->annexesFor($booking)->all());
Livewire::test(ViewBooking::class, ['record' => $booking->getRouteKey()])
->callAction('services', ['items' => [['extra_id' => $linen->id, 'quantity' => 2]]])
->assertNotified('Zusatzleistungen gespeichert');
$booking = $booking->fresh();
$this->assertSame([$linen->id => 2], $booking->extras);
$this->assertSame(['Wäsche'], OfferTerms::templateFor($booking)->annexesFor($booking)->pluck('name')->all());
}
private function annex(string $name, string $type, ?int $conditionId = null, string $title = 'Anlage', string $body = '<p>Text</p>'): ContractAnnex
{
return ContractAnnex::query()->create(['name' => $name, 'version' => 1, 'title' => $title, 'body' => $body, 'condition_type' => $type, 'condition_id' => $conditionId]);
}
/**
* @param array<string, mixed> $attributes
*/
private function booking(array $attributes = []): Booking
{
return Booking::factory()->for($this->property)->reserved(now()->addHours(48))->create([
'arrival' => '2026-11-07', 'departure' => '2026-11-10', 'guests' => 2, ...$attributes,
]);
}
}