Files
fewo-stommer/tests/Feature/CaseFactIntakeTest.php
LarsandClaude Opus 5.5 7e4e208d18
Deploy FeWo mit PipelineManager / deploy (push) Successful in 5m13s
Vorgang: Schritte im Ablauf anklickbar – was geschah, Herkunft der Angaben, korrigieren
- Ein Klick auf einen Schritt im „Ablauf“ öffnet seine Einzelheiten: die
  Verlaufseinträge dieses Schritts und bei den Gastangaben jede Angabe mit
  Wert, Herkunft (Webseite, Link, E-Mail, KI, Sie), Zeitpunkt, Belegstelle,
  Sicherheit und offenen Abweichungen.
- „Angaben korrigieren“ öffnet den Eintragsdialog: Ihre Eingabe gilt, der
  vorherige Wert bleibt im Nachweis; „Diesen Schritt ausführen“ führt
  direkt zur Aktion des Schritts.

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

269 lines
14 KiB
PHP

<?php
namespace Tests\Feature;
use App\Actions\ContributeCaseFacts;
use App\Actions\CreateReservation;
use App\Actions\DecideBookingWish;
use App\Actions\ProvideGuestContact;
use App\Actions\SendInformation;
use App\Actions\SendOffer;
use App\Actions\SubmitBookingWish;
use App\Actions\TakeOverExternalBooking;
use App\Documents\DocumentReleases;
use App\Enums\BookingStatus;
use App\Enums\BookingTaskKind;
use App\Enums\DocumentKind;
use App\Enums\FactSource;
use App\Exceptions\ReservationException;
use App\Filament\Resources\Bookings\Pages\ViewBooking;
use App\Models\Booking;
use App\Models\BookingTask;
use App\Models\CaseFact;
use App\Models\Channel;
use App\Models\Guest;
use App\Models\Property;
use App\Models\User;
use App\Pricing\StayRequest;
use App\Process\ActivityDetails;
use App\Process\CaseRequirements;
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;
/**
* v0.11 package U2: required details before an own binding offer and before the binding confirmation of a booking wish
* (G2a, E-032, B3, B4, M01) and the common fact intake with source, conflict and change request (S5, CH-03, E-038,
* M03, M06).
*/
class CaseFactIntakeTest extends TestCase
{
use RefreshDatabase;
private const ADDRESS = ['street' => 'Strandweg 1', 'postal_code' => '18225', 'city' => 'Kühlungsborn', 'country' => 'DE'];
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)]);
Content::save(['contract.terms' => '<p>Echte Bedingungen</p>', 'stay.cancellation' => '<p>Echtes Storno</p>']);
Settings::tenant()->update(['bank_iban' => 'DE02120300000000202051']);
}
/** M01 */
public function test_a_request_without_address_gets_no_offer_neither_automatically_nor_by_hand(): void
{
Settings::tenant()->update(['process_defaults' => ['processing' => 'automatic']]);
$booking = $this->request(sendMails: true);
$this->assertSame(0, $booking->fresh()->offer_version);
$this->assertSame(['guest.street', 'guest.postal_code', 'guest.city', 'guest.country'], CaseRequirements::missing($booking));
try {
app(SendOffer::class)->handle($booking->fresh(), ['acknowledged' => true]);
$this->fail('An offer without address must not go out.');
} catch (ReservationException $exception) {
$this->assertStringContainsString('Straße und Hausnummer, PLZ, Ort, Land', $exception->getMessage());
}
$this->assertSame(BookingStatus::Reserved, $booking->fresh()->status);
}
public function test_with_the_full_address_the_offer_goes_out_automatically(): void
{
Settings::tenant()->update(['process_defaults' => ['processing' => 'automatic']]);
$booking = $this->request(address: self::ADDRESS, sendMails: true);
$this->assertSame(BookingStatus::OfferSent, $booking->fresh()->status);
}
/** B3: the information "unter Vorbehalt" stays possible, the binding confirmation of the wish does not. */
public function test_an_information_needs_no_address_but_the_binding_confirmation_of_the_wish_does(): void
{
Settings::tenant()->update(['process_defaults' => ['processing' => 'automatic', 'pre_reservation' => false]]);
DocumentReleases::set(DocumentKind::Information, true, 'Test');
$booking = $this->request(sendMails: true)->fresh();
$this->assertSame(1, $booking->information_version, 'the information went out without address');
$outcome = app(SubmitBookingWish::class)->handle($booking, 1);
$this->assertSame(SubmitBookingWish::PENDING, $outcome);
$this->assertStringContainsString('fehlen noch Angaben', implode(' ', $booking->fresh()->wish['findings']));
$this->expectException(ReservationException::class);
app(DecideBookingWish::class)->confirm($booking->fresh());
}
public function test_the_wish_is_confirmed_once_the_address_is_there(): void
{
Settings::tenant()->update(['process_defaults' => ['pre_reservation' => false]]);
$booking = $this->request();
app(SendInformation::class)->handle($booking->fresh(), ['acknowledged' => true]);
app(SubmitBookingWish::class)->handle($booking->fresh(), 1);
app(ContributeCaseFacts::class)->handle($booking->fresh(), $this->prefixed(self::ADDRESS), FactSource::Landlord);
app(DecideBookingWish::class)->confirm($booking->fresh());
$this->assertSame(BookingStatus::Confirmed, $booking->fresh()->status);
}
/** B4 */
public function test_an_empty_country_is_missing_and_never_assumed(): void
{
$booking = $this->request(address: [...self::ADDRESS, 'country' => null]);
$this->assertSame(['guest.country'], CaseRequirements::missing($booking));
$this->assertNull($booking->guest->fresh()->country);
}
public function test_missing_values_are_taken_over_equal_ones_confirmed_and_different_ones_put_to_the_landlord(): void
{
$booking = $this->request(address: ['street' => 'Strandweg 1']);
$result = app(ContributeCaseFacts::class)->handle($booking, ['guest.street' => 'Am Deich 7', 'guest.postal_code' => '18225', 'guest.city' => 'Kühlungsborn', 'guest.first_name' => 'gesa', 'guest.phone' => 'kein Telefon?'], FactSource::Email, ['reference' => 'mail-1']);
$guest = $booking->guest->fresh();
$this->assertSame('Strandweg 1', $guest->street, 'the guest record is not written over');
$this->assertSame('18225', $guest->postal_code);
$this->assertSame(['guest.postal_code', 'guest.city', 'guest.phone'], $result->fields(CaseFact::APPLIED));
$this->assertSame(['guest.first_name'], $result->fields(CaseFact::CONFIRMED));
$this->assertSame(['guest.street'], $result->fields(CaseFact::CONFLICT));
$task = BookingTask::query()->where('booking_id', $booking->id)->where('kind', BookingTaskKind::FactConflict)->sole();
$this->assertStringContainsString('Am Deich 7', $task->note);
$this->assertStringContainsString('Strandweg 1', $task->note);
$landlord = app(ContributeCaseFacts::class)->handle($booking->fresh(), ['guest.street' => 'Am Deich 7'], FactSource::Landlord);
$this->assertSame(['guest.street'], $landlord->fields(CaseFact::APPLIED));
$this->assertSame('Am Deich 7', $booking->guest->fresh()->street);
$this->assertSame('Strandweg 1', CaseFact::query()->where('source', FactSource::Landlord)->sole()->previous_value);
}
/** M03 */
public function test_the_same_contribution_twice_is_recorded_once(): void
{
$booking = $this->request();
app(ContributeCaseFacts::class)->handle($booking, ['guest.phone' => '038293 1234'], FactSource::GuestLink, ['reference' => 'link-v1']);
$again = app(ContributeCaseFacts::class)->handle($booking->fresh(), ['guest.phone' => '038293 1234'], FactSource::GuestLink, ['reference' => 'link-v1']);
$this->assertTrue($again->isEmpty());
$this->assertSame(1, CaseFact::query()->where('field', 'guest.phone')->count());
$this->assertSame(2, $booking->events()->where('type', 'facts')->count(), 'the form and the first link contribution, not the repeated one');
}
/** M06 core: a binding booking gets administrative details, a change of the stay is only a request. */
public function test_a_binding_booking_takes_administrative_details_but_a_new_date_is_only_a_change_request(): void
{
$booking = $this->portalBooking(guest: ['first_name' => 'Erika', 'last_name' => 'Muster', 'email' => 'erika@example.org']);
$result = app(ContributeCaseFacts::class)->handle($booking, ['guest.phone' => '0171 555', 'booking.departure' => '2026-12-12', 'booking.guests' => '3'], FactSource::Email, ['reference' => 'mail-2']);
$booking->refresh();
$this->assertSame(BookingStatus::Confirmed, $booking->status);
$this->assertSame('2026-12-08', $booking->departure->toDateString(), 'nothing about the booking changed');
$this->assertNull($booking->guests);
$this->assertSame('0171 555', $booking->guest->phone);
$this->assertSame(['booking.departure', 'booking.guests'], $result->fields(CaseFact::CHANGE_REQUEST));
$this->assertSame(2, BookingTask::query()->where('booking_id', $booking->id)->where('kind', BookingTaskKind::ChangeRequest)->count());
$portal = app(ContributeCaseFacts::class)->handle($booking, ['booking.guests' => '3'], FactSource::PortalMail, ['reference' => 'portal-1']);
$this->assertSame(['booking.guests'], $portal->fields(CaseFact::APPLIED), 'the portal itself may complete an unknown number of guests');
}
public function test_a_case_without_guest_gets_one_from_a_name_but_never_joins_a_known_address_silently(): void
{
Guest::factory()->create(['email' => 'bekannt@example.org']);
$booking = $this->portalBooking();
$known = app(ContributeCaseFacts::class)->handle($booking, ['guest.first_name' => 'Kai', 'guest.last_name' => 'Kurz', 'guest.email' => 'bekannt@example.org'], FactSource::Email, ['reference' => 'mail-3']);
$this->assertNull($booking->fresh()->guest_id);
$this->assertSame(['guest.first_name', 'guest.last_name', 'guest.email'], $known->fields(CaseFact::CONFLICT));
app(ContributeCaseFacts::class)->handle($booking->fresh(), ['guest.first_name' => 'Kai', 'guest.last_name' => 'Kurz', 'guest.email' => 'kai@example.org'], FactSource::PortalMail, ['reference' => 'mail-4']);
$this->assertSame('kai@example.org', $booking->fresh()->guest->email);
}
/** S6: once the details are complete the existing process carries on by itself. */
public function test_completing_the_address_continues_an_automatic_case(): void
{
Settings::tenant()->update(['process_defaults' => ['processing' => 'automatic']]);
$booking = $this->request(sendMails: true);
$this->assertSame(0, $booking->fresh()->offer_version);
app(ContributeCaseFacts::class)->handle($booking->fresh(), $this->prefixed(self::ADDRESS), FactSource::GuestLink, ['reference' => 'link-v1']);
$this->assertSame(BookingStatus::OfferSent, $booking->fresh()->status);
}
/** A step of the progress list shows where each detail came from and lets the landlord correct it. */
public function test_the_progress_list_shows_the_source_of_each_detail_and_the_landlord_corrects_it(): void
{
$this->actingAs(User::factory()->create());
$booking = $this->request();
app(ContributeCaseFacts::class)->handle($booking, ['guest.street' => 'Lindenallee 12'], FactSource::Extraction, ['reference' => 'message:1', 'evidence' => ['guest.street' => 'unsere Anschrift: Lindenallee 12'], 'confidence' => ['guest.street' => 0.9]]);
Livewire::test(ViewBooking::class, ['record' => $booking->getRouteKey()])
->assertSeeHtml('mountAction(&#039;activityDetails&#039;, { key: &#039;contact&#039; })')
->mountAction(TestAction::make('activityDetails')->arguments(['key' => 'contact']))
->assertActionMounted('activityDetails')
->assertMountedActionModalSee(['Angaben und ihre Herkunft', 'Lindenallee 12', 'aus einer Nachricht erkannt (KI)', 'Sicherheit 90 %', 'unsere Anschrift: Lindenallee 12', 'Anfrageformular der Webseite']);
Livewire::test(ViewBooking::class, ['record' => $booking->getRouteKey()])
->callAction(TestAction::make('provideContact')->arguments([]), ['guest' => ['first_name' => 'Gesa', 'last_name' => 'Gast', 'email' => 'gesa@example.org', 'street' => 'Lindenallee 21'], 'process' => ProvideGuestContact::MANUAL, 'guest_confirmed' => false]);
$this->assertSame('Lindenallee 21', $booking->guest->fresh()->street);
$row = collect(ActivityDetails::facts($booking->fresh()))->firstWhere('field', 'guest.street');
$this->assertSame(['Lindenallee 21', 'von Ihnen eingetragen'], [$row['value'], $row['source']]);
$this->assertSame('Lindenallee 12', CaseFact::query()->where('field', 'guest.street')->where('source', FactSource::Landlord)->sole()->previous_value, 'the corrected value stays in the evidence');
}
/**
* @param array<string, ?string> $address
*/
private function request(array $address = [], 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' => 'gesa@example.org', ...$address],
sendMails: $sendMails,
);
}
/**
* @param array<string, ?string>|null $guest
*/
private function portalBooking(?array $guest = null): Booking
{
$portal = Channel::factory()->for($this->property)->create(['name' => 'FeWo-direkt', 'booking_model' => 'portal_booking']);
return app(TakeOverExternalBooking::class)->handle($this->property, $portal, CarbonImmutable::parse('2026-12-05'), CarbonImmutable::parse('2026-12-08'), ['portal_reference' => 'HA-7', 'guest' => $guest]);
}
/**
* @param array<string, ?string> $address
* @return array<string, ?string>
*/
private function prefixed(array $address): array
{
return collect($address)->mapWithKeys(fn (?string $value, string $key): array => ['guest.'.$key => $value])->all();
}
}