Files
fewo-stommer/tests/Feature/DataRequestTest.php
T
LarsandClaude Opus 5.5 e4a3cb7f7b v0.11 U4: Rückfrage, Folgefrage, Erinnerung und Ergänzungslink
- data_requests: ein Beschaffungsvorgang je Vorgang mit Modus
  (automatisch / Freigabe / manuell), getrennten Zählern für Folgefragen
  und Erinnerungen ohne Rücksetzen, Antwortfrist ≠ Reservierungsfrist.
- RequestCaseData: fragt nach, was für das nächste Angebot, die
  Wunschbestätigung oder (bei verbindlichen Buchungen) administrativ fehlt;
  ohne E-Mail keine Rückfrage. Teilantwort → gezielte Folgefrage, Schweigen
  → Erinnerungen, an der Grenze Aufgabe für die Vermieterin. Nie ein Hold.
- Mail DataRequested mit Texten nach tatsächlichem Sperrstatus (M11);
  Dokumentart „Rückfrage“ – im Live-Betrieb ohne Freigabe nur nach Klick.
- Signierter, versionierter Ergänzungslink (/angebot/angaben): Lesen
  schreibt nicht, Absenden über den Fakteneingang, idempotent.
- Vorgang: „Rückfrage an den Gast senden“ / „Rückfrage beenden“;
  Eingangsbestätigung nennt die Klärungsreservierung korrekt.
- Zeitplan: cases:data-requests alle fünf Minuten.

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

272 lines
13 KiB
PHP
Raw 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\ContributeCaseFacts;
use App\Actions\CreateReservation;
use App\Actions\RequestCaseData;
use App\Actions\TakeOverExternalBooking;
use App\Enums\BookingStatus;
use App\Enums\BookingTaskKind;
use App\Enums\FactSource;
use App\Mail\DataRequested;
use App\Mail\ReservationReceived;
use App\Models\Booking;
use App\Models\BookingTask;
use App\Models\CaseFact;
use App\Models\Channel;
use App\Models\DataRequest;
use App\Models\Property;
use App\Pricing\StayRequest;
use App\Process\CaseActivities;
use App\Support\Content;
use App\Support\Settings;
use Carbon\CarbonImmutable;
use Database\Seeders\PropertySeeder;
use Database\Seeders\SiteSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\URL;
use Tests\TestCase;
/**
* v0.11 package U4: the data request with first question, follow-up question, reminders and the guest's link
* (S1, S4, CH-04, CH-15–CH-18; M02 form part, M03, M08, M09, M11).
*/
class DataRequestTest 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)]);
Content::save(['contract.terms' => '<p>Echte Bedingungen</p>', 'stay.cancellation' => '<p>Echtes Storno</p>']);
Settings::tenant()->update(['bank_iban' => 'DE02120300000000202051']);
}
public function test_automatic_mode_asks_the_guest_at_once_instead_of_only_confirming_the_receipt(): void
{
Settings::tenant()->update(['process_defaults' => ['processing' => 'automatic', 'data_request_mode' => 'automatic']]);
$booking = $this->request();
$request = $booking->openDataRequest();
$this->assertSame(DataRequest::OPEN, $request->status);
$this->assertSame(['guest.street', 'guest.postal_code', 'guest.city', 'guest.country'], $request->fields);
Mail::assertSent(DataRequested::class, fn (DataRequested $mail): bool => $mail->hasTo('gesa@example.org') && $mail->kind === DataRequest::INITIAL);
Mail::assertNotSent(ReservationReceived::class);
$this->assertTrue($booking->hold_expires_at->equalTo(now()->addMinutes(15)), 'the clarification hold is unchanged by the request');
}
public function test_approval_mode_prepares_the_question_and_the_landlord_releases_it(): void
{
$booking = $this->request();
$request = $booking->openDataRequest();
$this->assertSame(DataRequest::DRAFT, $request->status);
Mail::assertNotSent(DataRequested::class);
Mail::assertSent(ReservationReceived::class);
$this->assertSame('approveDataRequest', CaseActivities::next($booking)?->action);
$this->assertTrue(app(RequestCaseData::class)->approve($request));
Mail::assertSent(DataRequested::class, 1);
$this->assertSame(DataRequest::OPEN, $request->fresh()->status);
}
public function test_manual_mode_leaves_the_question_to_the_landlord(): void
{
Settings::tenant()->update(['process_defaults' => ['data_request_mode' => 'manual']]);
$booking = $this->request();
$this->assertSame(DataRequest::MANUAL, $booking->openDataRequest()->status);
Mail::assertNotSent(DataRequested::class);
}
public function test_without_an_email_address_nothing_is_sent_and_the_case_asks_for_the_contact(): void
{
$booking = app(CreateReservation::class)->handle($this->property->fresh(), new StayRequest(CarbonImmutable::parse('2026-11-07'), CarbonImmutable::parse('2026-11-10'), 2), ['first_name' => 'Paul', 'last_name' => 'Telefon', 'email' => ''], source: 'phone');
$this->assertNull($booking->openDataRequest());
$this->assertSame('provideContact', CaseActivities::next($booking)?->action);
}
/** M02 (form), M03 */
public function test_the_guest_completes_the_details_through_the_link_partly_and_then_fully(): void
{
Settings::tenant()->update(['process_defaults' => ['processing' => 'automatic', 'data_request_mode' => 'automatic']]);
$booking = $this->request();
$request = $booking->openDataRequest();
$url = app(RequestCaseData::class)->url($request);
$this->get($url)->assertOk()->assertSee('Straße und Hausnummer')->assertSee('zur Klärung bis');
$this->assertSame(0, CaseFact::query()->where('source', FactSource::GuestLink)->count(), 'opening the link writes nothing');
$this->post($this->submitUrl($booking, $request), ['guest_street' => 'Strandweg 1', 'guest_postal_code' => '18225'])->assertRedirect();
$request->refresh();
$this->assertSame('Strandweg 1', $booking->guest->fresh()->street);
$this->assertSame(['guest.city', 'guest.country'], $request->fields);
$this->assertSame(1, $request->followups_sent, 'a partial answer gets a targeted follow-up question');
$this->assertSame(2, $request->link_version);
Mail::assertSent(DataRequested::class, fn (DataRequested $mail): bool => $mail->kind === DataRequest::FOLLOWUP);
$this->get($url)->assertSee('nicht mehr aktuell');
$fresh = app(RequestCaseData::class)->url($request);
$this->post($this->submitUrl($booking, $request), ['guest_city' => 'Kühlungsborn', 'guest_country' => 'DE'])->assertRedirect();
$this->post($this->submitUrl($booking, $request), ['guest_city' => 'Kühlungsborn', 'guest_country' => 'DE']);
$booking->refresh();
$this->assertSame(DataRequest::COMPLETED, $request->fresh()->status);
$this->assertSame(BookingStatus::OfferSent, $booking->status, 'complete: the existing process sends the offer');
$this->assertSame(1, $booking->offer_version, 'no second offer from the repeated submit');
$this->get($fresh)->assertSee('alle Angaben vor');
}
public function test_a_differing_value_from_the_link_never_overwrites_the_guest_record(): void
{
$booking = $this->request(['street' => 'Alter Weg 2']);
$request = $booking->openDataRequest();
app(RequestCaseData::class)->approve($request);
$this->post($this->submitUrl($booking, $request->fresh()), ['guest_postal_code' => '18225', 'guest_city' => 'Kühlungsborn', 'guest_country' => 'DE', 'guest_street' => 'Neuer Weg 3']);
$this->assertSame('Alter Weg 2', $booking->guest->fresh()->street);
}
/** M08 */
public function test_reminders_follow_their_limit_and_then_the_landlord_takes_over_without_extending_any_deadline(): void
{
Settings::tenant()->update(['process_defaults' => ['data_request_mode' => 'automatic']]);
$booking = $this->request();
$holdBefore = $booking->hold_expires_at;
$requests = app(RequestCaseData::class);
$this->travel(25)->hours();
$this->assertSame(1, $requests->remindDue());
$this->travel(25)->hours();
$this->assertSame(1, $requests->remindDue());
$this->travel(25)->hours();
$this->assertSame(0, $requests->remindDue());
$request = $booking->openDataRequest();
$this->assertSame([2, DataRequest::MANUAL], [$request->reminders_sent, $request->status]);
Mail::assertSent(DataRequested::class, fn (DataRequested $mail): bool => $mail->kind === DataRequest::REMINDER);
$this->assertSame(1, BookingTask::query()->where('booking_id', $booking->id)->where('kind', BookingTaskKind::GuestUnresponsive)->count());
$this->assertTrue($booking->fresh()->hold_expires_at->equalTo($holdBefore), 'no reminder extends the clarification hold');
$this->assertSame(BookingStatus::Reserved, $booking->fresh()->status);
}
public function test_an_answer_stops_the_reminder_and_the_follow_up_limit_hands_over(): void
{
Settings::tenant()->update(['process_defaults' => ['data_request_mode' => 'automatic', 'data_request_followups_max' => 0]]);
$booking = $this->request();
app(ContributeCaseFacts::class)->handle($booking, ['guest.street' => 'Strandweg 1'], FactSource::Email, ['reference' => 'mail-1']);
$request = $booking->openDataRequest();
$this->assertSame(DataRequest::MANUAL, $request->status);
$this->assertNull($request->next_reminder_at);
Mail::assertNotSent(DataRequested::class, fn (DataRequested $mail): bool => $mail->kind === DataRequest::FOLLOWUP);
}
/** M09 */
public function test_foreign_outdated_and_unsigned_links_change_nothing(): void
{
$booking = $this->request();
$other = $this->request(email: 'andere@example.org', arrival: '2026-11-20', departure: '2026-11-23');
$request = $booking->openDataRequest();
$this->get(route('guest-data.show', ['booking' => $booking, 'r' => $request->id, 'v' => 1]))->assertForbidden();
$this->get(app(RequestCaseData::class)->url($other->openDataRequest(), $booking))->assertSee('gehört nicht zu dieser Anfrage');
$this->post($this->submitUrl($booking, $other->openDataRequest()), ['guest_street' => 'Falsch 1'])->assertRedirect();
$this->assertNull($booking->guest->fresh()->street);
$this->travel(15)->days();
$this->get(app(RequestCaseData::class)->url($request))->assertOk();
$this->travelBack();
$expired = app(RequestCaseData::class)->url($request);
$this->travel(15)->days();
$this->get($expired)->assertForbidden();
}
public function test_a_mail_failure_is_visible_and_hands_over(): void
{
Settings::tenant()->update(['process_defaults' => ['data_request_mode' => 'automatic']]);
Mail::shouldReceive('to')->andThrow(new \RuntimeException('SMTP down'));
$booking = 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']);
$this->assertSame(DataRequest::MANUAL, $booking->openDataRequest()->status);
$this->assertSame(1, BookingTask::query()->where('booking_id', $booking->id)->where('kind', BookingTaskKind::GuestUnresponsive)->count());
}
/** M11: what the guest is told follows the real hold. */
public function test_the_texts_follow_the_real_state_of_the_hold(): void
{
$active = $this->request();
$this->assertStringContainsString('zur Klärung bis 22.10.2026, 13:00 Uhr vorläufig reserviert', $this->mailText($active));
$this->travel(4)->hours();
$this->assertStringContainsString('vorläufige Reservierung zur Klärung ist abgelaufen', $this->mailText($active->fresh()));
$this->travelBack();
Settings::tenant()->update(['process_defaults' => ['clarification_hold_manual_minutes' => 0]]);
$none = $this->request(email: 'null@example.org', arrival: '2026-11-20', departure: '2026-11-23');
$text = $this->mailText($none);
$this->assertStringContainsString('noch nicht für Sie reserviert', $text);
$this->assertStringNotContainsString('vorläufig reserviert', $text);
}
/** M12 part: a binding portal booking is only asked for administrative details – no hold, no offer texts. */
public function test_a_binding_portal_booking_is_asked_only_for_administrative_details(): void
{
$portal = Channel::factory()->for($this->property)->create(['name' => 'FeWo-direkt', 'booking_model' => 'portal_booking']);
$booking = app(TakeOverExternalBooking::class)->handle($this->property, $portal, CarbonImmutable::parse('2026-12-05'), CarbonImmutable::parse('2026-12-08'), ['portal_reference' => 'HA-7', 'guest' => ['first_name' => 'Erika', 'last_name' => 'Muster', 'email' => 'erika@example.org']]);
$request = $booking->openDataRequest();
$this->assertSame(RequestCaseData::FOR_BOOKING, $request->purpose);
$this->assertSame(BookingStatus::Confirmed, $booking->fresh()->status);
$text = $this->mailText($booking, $request);
$this->assertStringContainsString('verbindlich bestätigt', $text);
$this->assertStringNotContainsString('reserviert', $text);
$this->assertNotSame('approveDataRequest', CaseActivities::next($booking->fresh())?->action, 'the booking\'s own steps come first');
}
/**
* @param array<string, string> $guest
*/
private function request(array $guest = [], string $email = 'gesa@example.org', string $arrival = '2026-11-07', string $departure = '2026-11-10'): Booking
{
return app(CreateReservation::class)->handle(
$this->property->fresh(),
new StayRequest(CarbonImmutable::parse($arrival), CarbonImmutable::parse($departure), 2),
['first_name' => 'Gesa', 'last_name' => 'Gast', 'email' => $email, ...$guest],
)->fresh();
}
private function submitUrl(Booking $booking, DataRequest $request): string
{
return URL::temporarySignedRoute('guest-data.submit', now()->addHour(), ['booking' => $booking, 'r' => $request->id, 'v' => $request->fresh()->link_version]);
}
private function mailText(Booking $booking, ?DataRequest $request = null): string
{
$request ??= $booking->openDataRequest();
return strip_tags((new DataRequested($booking->fresh(), $request, DataRequest::INITIAL, 'https://example.test/link'))->render());
}
}