Files
fewo-stommer/tests/Feature/GuidedCaseHandlingTest.php
LarsandClaude Opus 5.5 ccac78a6c9 v0.11 U2: Angebots-Gate, gemeinsamer Fakteneingang mit Herkunft
- CaseRequirements: Vor einem eigenen verbindlichen Angebot und vor der
  verbindlichen Bestätigung eines Buchungswunsches müssen Vor- und
  Nachname, E-Mail, vollständige Anschrift mit Land, Personenzahl und
  Zeitraum vorliegen – harter Blocker, auch von Hand (G2a, B3). Die
  Auskunft unter Vorbehalt bleibt ohne dieses Gate.
- ContributeCaseFacts (S5): jede Angabe mit Herkunft und Ergebnis in
  case_facts; fehlend → übernommen, gleich → bestätigt, abweichend vom
  Gast → Prüfaufgabe (Kartei bleibt), Vermieterin maßgeblich, Zeitraum und
  Personen → Änderungswunsch; idempotent; danach einmal CaseAutomation.
- ProvideGuestContact und Webformular nutzen den Fakteneingang.
- Land als ISO-Code mit „Deutschland“ als änderbarem Formularstandard;
  guests.country ohne stillen Standardwert (B4).
- Testdaten bestehender Tests um Anschriften ergänzt.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 13:07:18 +01:00

261 lines
11 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace Tests\Feature;
use App\Actions\CreateReservation;
use App\Actions\ProvideGuestContact;
use App\Enums\AdminMode;
use App\Enums\BookingStatus;
use App\Enums\ProcessingMode;
use App\Filament\Pages\HandlingSettings;
use App\Filament\Resources\Bookings\Pages\CreateBooking;
use App\Filament\Resources\Bookings\Pages\ViewBooking;
use App\Filament\Resources\Bookings\Schemas\BookingForm;
use App\Filament\Widgets\WorkListWidget;
use App\Mail\OfferMade;
use App\Models\BlockedPeriod;
use App\Models\Booking;
use App\Models\Channel;
use App\Models\ChannelTask;
use App\Models\InboxMessage;
use App\Models\Property;
use App\Models\User;
use App\Pricing\StayRequest;
use App\Process\CaseActivities;
use App\Process\WorkList;
use App\Support\Content;
use App\Support\Settings;
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\Http;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Storage;
use Livewire\Livewire;
use Tests\TestCase;
/**
* Package 2 of UMSETZUNGSPLAN_v0.10: the work list, the guided case page, automatic or manual handling, continuing
* after a stop and ending a case without bypassing obligations.
*/
class GuidedCaseHandlingTest 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');
Http::fake(['*' => Http::response('', 500)]);
$this->withoutVite();
$this->actingAs(User::factory()->create(['name' => 'Ingrid', 'admin_mode' => AdminMode::Simple]));
}
/** 2.1 */
public function test_an_automatic_request_gets_its_offer_without_a_click_and_leaves_nothing_to_do(): void
{
$this->releaseContractTexts();
Settings::tenant()->update(['process_defaults' => ['processing' => 'automatic']]);
$booking = $this->request(sendMails: true);
$this->assertSame(BookingStatus::OfferSent, $booking->status);
Mail::assertSent(OfferMade::class);
$this->assertNull(CaseActivities::next($booking));
$this->assertSame([], WorkList::items());
}
/** 2.2 */
public function test_a_manual_request_is_prepared_and_waits_for_release_in_the_work_list(): void
{
$booking = $this->request();
$this->assertSame(BookingStatus::Reserved, $booking->status);
$items = WorkList::items();
$this->assertCount(1, $items);
$this->assertSame('Angebot prüfen und senden', $items[0]['title']);
$this->assertStringContainsString('Gesa Gast', $items[0]['detail']);
Livewire::test(ViewBooking::class, ['record' => $booking->getRouteKey()])
->assertSee('Angebot prüfen und senden')
->assertSee('Sie geben jeden Schritt selbst frei')
->callAction(TestAction::make('sendOffer')->arguments([]))
->assertNotified('Angebot gesendet');
$this->assertSame(BookingStatus::OfferSent, $booking->fresh()->status);
$this->assertSame('Ingrid', $booking->events()->where('type', 'offer')->value('actor'));
}
/** 2.3 */
public function test_missing_data_stops_the_case_with_a_task_and_it_continues_automatically_afterwards(): void
{
$this->releaseContractTexts();
Settings::tenant()->update(['process_defaults' => ['processing' => 'automatic']]);
Livewire::test(CreateBooking::class)
->fillForm([
'kind' => BookingForm::INQUIRY, 'property_id' => $this->property->id, 'guests' => 2,
'arrival' => '2026-11-07', 'departure' => '2026-11-10', 'first_name' => 'Paul', 'last_name' => 'Telefon', 'street' => 'Strandweg 1', 'postal_code' => '18225', 'city' => 'Kühlungsborn', 'country' => 'DE', 'send_mails' => true,
])
->call('create')
->assertHasNoFormErrors();
$booking = Booking::query()->sole();
$this->assertSame(BookingStatus::Reserved, $booking->status);
$this->assertSame('provideContact', CaseActivities::next($booking)?->action);
$this->assertSame('E-Mail-Adresse des Gastes eintragen', WorkList::items()[0]['title']);
Mail::assertNotSent(OfferMade::class);
Livewire::test(ViewBooking::class, ['record' => $booking->getRouteKey()])
->callAction(TestAction::make('provideContact')->arguments([]), ['guest' => ['first_name' => 'Paul', 'last_name' => 'Telefon', 'email' => 'paul@example.org', 'street' => 'Strandweg 1', 'postal_code' => '18225', 'city' => 'Kühlungsborn', 'country' => 'DE'], 'process' => ProvideGuestContact::AUTOMATIC, 'guest_confirmed' => false])
->assertNotified('Kontaktdaten gespeichert, Angebot gesendet');
$this->assertSame(BookingStatus::OfferSent, $booking->fresh()->status);
Mail::assertSent(OfferMade::class, 1);
}
public function test_after_completing_a_task_the_landlord_may_keep_releasing_by_hand(): void
{
Settings::tenant()->update(['process_defaults' => ['processing' => 'automatic']]);
$booking = $this->request(email: '');
app(ProvideGuestContact::class)->handle($booking, ['email' => 'gesa@example.org'], ProvideGuestContact::MANUAL);
$this->assertSame(BookingStatus::Reserved, $booking->fresh()->status);
$this->assertSame('sendOffer', CaseActivities::next($booking->fresh())?->action);
Mail::assertNotSent(OfferMade::class);
}
public function test_the_handling_of_one_case_can_be_switched_and_is_recorded(): void
{
$booking = $this->request();
Livewire::test(ViewBooking::class, ['record' => $booking->getRouteKey()])
->callAction('automation', ['mode' => ProcessingMode::Automatic->value]);
$this->assertSame(ProcessingMode::Automatic, $booking->fresh()->processing_mode);
$this->assertStringContainsString('Automatisch', $booking->events()->where('type', 'mode')->value('summary'));
}
/** 2.5 / 2.6 */
public function test_the_work_list_brings_everything_to_do_into_one_list(): void
{
$this->request();
$portal = Channel::factory()->for($this->property)->create(['name' => 'Ostsee-Portal']);
ChannelTask::factory()->for($portal)->create(['starts_on' => '2026-12-01', 'ends_on' => '2026-12-05']);
BlockedPeriod::factory()->for($this->property)->create(['channel_id' => $portal->id, 'source' => 'channel', 'external_uid' => 'x1', 'starts_on' => '2026-12-20', 'ends_on' => '2026-12-22', 'needs_review' => true]);
InboxMessage::factory()->create(['subject' => 'Angebot nicht zugestellt']);
$titles = array_column(WorkList::items(), 'title');
$this->assertContains('Angebot prüfen und senden', $titles);
$this->assertContains('In Ostsee-Portal sperren', $titles);
$this->assertContains('Belegung von Ostsee-Portal klären', $titles);
$this->assertContains('Angebot nicht zugestellt', $titles);
Livewire::test(WorkListWidget::class)->assertSee('In Ostsee-Portal sperren')->assertSee('Vorgang öffnen');
}
/** 2.5 */
public function test_the_case_page_shows_the_state_the_next_step_and_only_fitting_buttons(): void
{
$booking = $this->request();
Livewire::test(ViewBooking::class, ['record' => $booking->getRouteKey()])
->assertSee('Anfrage über die Webseite')
->assertSee('Zeitraum reserviert bis')
->assertSee('Als Nächstes')
->assertActionVisible('sendOffer')
->assertActionVisible('end')
->assertActionHidden('resendConfirmation')
->assertActionHidden('portalPrice')
->assertDontSee('Technische Angaben')
->assertDontSee('Status von Hand ändern');
}
/** 2.7 */
public function test_the_expert_view_shows_technical_details_on_the_same_page(): void
{
$booking = $this->request();
$this->actingAs(User::factory()->create(['admin_mode' => AdminMode::Expert]));
Livewire::test(ViewBooking::class, ['record' => $booking->getRouteKey()])
->assertSee('Technische Angaben')
->assertSee('Abwicklungsprofil (eingefroren)')
->assertSee('Grundeinstellung');
}
/** 2.8 */
public function test_nationality_is_never_a_reason_to_stop(): void
{
$this->releaseContractTexts();
Settings::tenant()->update(['process_defaults' => ['processing' => 'automatic']]);
$foreign = app(CreateReservation::class)->handle(
$this->property->fresh(),
new StayRequest(CarbonImmutable::parse('2026-11-07'), CarbonImmutable::parse('2026-11-10'), 2),
['first_name' => 'Ayşe', 'last_name' => 'Yılmaz', 'email' => 'ayse@example.com.tr', 'country' => 'TR', 'city' => 'İzmir', 'street' => 'Strandweg 1', 'postal_code' => '18225', 'city' => 'Kühlungsborn', 'country' => 'DE'],
);
$this->assertSame(BookingStatus::OfferSent, $foreign->status);
}
public function test_ending_a_request_needs_a_reason_and_ends_it_without_booking(): void
{
$booking = $this->request();
Livewire::test(ViewBooking::class, ['record' => $booking->getRouteKey()])
->callAction('end', ['reason' => '', 'notify' => false])
->assertHasFormErrors(['reason' => 'required']);
Livewire::test(ViewBooking::class, ['record' => $booking->getRouteKey()])
->callAction('end', ['reason' => 'Hund nicht erlaubt', 'notify' => false])
->assertNotified('Vorgang beendet');
$this->assertSame(BookingStatus::Declined, $booking->fresh()->status);
$this->assertSame(0, Booking::query()->occupying()->count());
$this->assertSame('Hund nicht erlaubt', $booking->events()->where('type', 'status')->value('reason'));
}
public function test_the_landlord_sets_defaults_on_one_page_and_a_portal_deviates_per_intake(): void
{
$this->actingAs(User::factory()->create(['admin_mode' => AdminMode::Expert]));
Livewire::test(HandlingSettings::class)
->fillForm(['processing' => 'automatic', 'hold_hours' => 24, 'pre_reservation' => null])
->call('save')
->assertHasNoFormErrors()
->assertNotified('Gespeichert');
$this->assertSame(['processing' => 'automatic', 'hold_hours' => 24], Settings::tenant()->fresh()->process_defaults);
}
private function request(string $email = 'gesa@example.org', bool $sendMails = false): Booking
{
return app(CreateReservation::class)->handle(
$this->property->fresh(),
new StayRequest(CarbonImmutable::parse('2026-11-07'), CarbonImmutable::parse('2026-11-10'), 2),
['first_name' => 'Gesa', 'last_name' => 'Gast', 'email' => $email, 'street' => 'Strandweg 1', 'postal_code' => '18225', 'city' => 'Kühlungsborn', 'country' => 'DE'],
sendMails: $sendMails,
);
}
/**
* Automatic offers wait for released contract texts in live mode; tests run in test mode, but the texts are set as
* a real landlord would have them.
*/
private function releaseContractTexts(): void
{
Content::save(['contract.terms' => '<p>Echte Bedingungen</p>', 'stay.cancellation' => '<p>Echtes Storno</p>']);
Settings::tenant()->update(['bank_iban' => 'DE02120300000000202051']);
}
}