Files
fewo-stommer/tests/Feature/OfferWorkflowTest.php
Lars StommerandClaude Opus 5.5 4f69b426a4 Booking path 1 (offer + guest acceptance), 2FA for all, privacy hardening, brand graphics in theme
- Offer workflow: "Angebot senden" in the admin (dates, guests, promo code, manual price adjustment,
  validity from the property's hold time, message, guest data for the contract) with live price;
  contract draft PDF; signed, versioned offer page for the guest to accept (acceptance record without
  IP) or decline; expiry job, landlord notifications, editable offer texts, wording on the website.
- 2FA is mandatory for every admin account.
- Privacy: complete anonymisation (notes, attribution, review names, offer messages), shorter
  periods for unbinding requests, portal mail excerpts and attribution (configurable proposals),
  iCal summary not stored, known e-mail no longer overwrites guest data, security headers, bearer
  tokens for automations, metadata stripped from photo originals, review link throttled.
- Daily database backup (backup:database) and scripts/backup-pull.sh for the NAS.
- Theme: brand mark in header/footer, favicon/touch icon, wave divider, icons, illustration as
  image placeholder; navy follows the text colour for dark appearances.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 21:38:22 +02:00

192 lines
8.1 KiB
PHP

<?php
namespace Tests\Feature;
use App\Actions\ExpireReservations;
use App\Actions\SendOffer;
use App\Availability\AvailabilityChecker;
use App\Enums\BookingStatus;
use App\Enums\QuoteLineType;
use App\Filament\Resources\Bookings\Pages\ViewBooking;
use App\Mail\BookingConfirmed;
use App\Mail\OfferAnswered;
use App\Mail\OfferMade;
use App\Models\Booking;
use App\Models\Property;
use App\Models\SpecialOffer;
use App\Models\User;
use Carbon\CarbonImmutable;
use Database\Seeders\PropertySeeder;
use Database\Seeders\SiteSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\URL;
use Livewire\Livewire;
use Tests\TestCase;
class OfferWorkflowTest extends TestCase
{
use RefreshDatabase;
private Property $property;
private Booking $booking;
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();
$this->booking = Booking::factory()->for($this->property)->reserved(now()->addHours(48))->create([
'arrival' => '2026-11-07',
'departure' => '2026-11-10',
'guests' => 2,
]);
}
public function test_offer_recalculates_the_price_with_adjustment_and_promo_code_and_updates_the_guest(): void
{
SpecialOffer::factory()->for($this->property)->create(['name' => 'Stammgast', 'code' => 'OSTSEE', 'discount_value' => 10]);
$offer = app(SendOffer::class)->handle($this->booking, [
'departure' => '2026-11-11',
'promo_code' => 'ostsee',
'price_adjustment' => -2000,
'price_adjustment_label' => 'Nachlass für Wiederkehrer',
'valid_until' => '2026-10-25 18:00',
'offer_message' => 'Wir freuen uns auf Sie!',
'guest' => ['first_name' => 'Erika', 'last_name' => 'Musterfrau', 'street' => 'Seeweg 1', 'postal_code' => '18225', 'city' => 'Kühlungsborn'],
]);
$lines = collect($offer->quote['lines']);
$this->assertSame(BookingStatus::OfferSent, $offer->status);
$this->assertSame([1, '2026-11-11', 'ostsee', -2000], [$offer->offer_version, $offer->departure->toDateString(), $offer->promo_code, $offer->price_adjustment]);
$this->assertSame('2026-10-25 18:00', $offer->hold_expires_at->format('Y-m-d H:i'));
$this->assertTrue($lines->contains(fn (array $line): bool => $line['label'] === 'Stammgast' && $line['amount'] < 0));
$this->assertTrue($lines->contains(fn (array $line): bool => $line['type'] === QuoteLineType::Adjustment->value && $line['amount'] === -2000 && $line['label'] === 'Nachlass für Wiederkehrer'));
$this->assertSame($lines->sum('amount'), $offer->total);
$this->assertSame(['Erika', 'Kühlungsborn'], [$offer->guest->first_name, $offer->guest->city]);
Storage::disk('local')->assertExists($offer->offer_path);
Mail::assertSent(OfferMade::class, fn (OfferMade $mail): bool => $mail->hasTo($offer->guest->email) && str_contains($mail->offerUrl, 'signature='));
}
public function test_guest_accepts_the_offer_and_the_booking_becomes_binding(): void
{
$url = $this->sendOffer();
$this->get($url)->assertOk()->assertSee('Angebot zahlungspflichtig annehmen')->assertSee('Gesamtpreis')->assertSee('Gesamtbetrag');
$acceptUrl = $this->formAction($url, 'annehmen');
$this->post($acceptUrl, [])->assertSessionHasErrors('terms');
$this->followingRedirects()->post($acceptUrl, ['terms' => '1'])->assertOk()->assertSee('Ihre Buchung ist verbindlich');
$booking = $this->booking->fresh();
$this->assertSame(BookingStatus::Confirmed, $booking->status);
$this->assertNotNull($booking->accepted_at);
$this->assertSame(1, $booking->acceptance['offer_version']);
$this->assertSame($booking->total, $booking->acceptance['total']);
Storage::disk('local')->assertExists($booking->contract_path);
Mail::assertSent(BookingConfirmed::class);
Mail::assertSent(OfferAnswered::class, fn (OfferAnswered $mail): bool => $mail->accepted);
$html = $this->get($url)->getContent();
preg_match('~<main.*</main>~s', $html, $m);
dump(strip_tags($m[0] ?? substr($html, 0, 3000)));
}
public function test_link_of_an_older_offer_version_cannot_be_used(): void
{
$oldUrl = $this->sendOffer();
$this->sendOffer();
$this->get($oldUrl)->assertOk()->assertSee('aktualisiertes Angebot')->assertDontSee('Angebot zahlungspflichtig annehmen');
$this->post(URL::temporarySignedRoute('offers.accept', now()->addDay(), ['booking' => $this->booking, 'v' => 1]), ['terms' => '1']);
$this->assertSame(BookingStatus::OfferSent, $this->booking->fresh()->status);
}
public function test_offer_holds_the_dates_until_it_expires(): void
{
$url = $this->sendOffer(['valid_until' => '2026-10-24 10:00']);
$availability = new AvailabilityChecker;
$this->assertFalse($availability->isAvailable($this->property, $this->booking->arrival, $this->booking->departure));
$this->travelTo(CarbonImmutable::parse('2026-10-24 11:00'));
$this->assertTrue($availability->isAvailable($this->property, $this->booking->arrival, $this->booking->departure));
$this->assertSame(1, app(ExpireReservations::class)->handle());
$this->assertSame(BookingStatus::Expired, $this->booking->fresh()->status);
$this->get($url)->assertForbidden();
}
public function test_guest_declines_the_offer(): void
{
$url = $this->sendOffer();
$this->post($this->formAction($url, 'ablehnen'))->assertRedirect();
$booking = $this->booking->fresh();
$this->assertSame([BookingStatus::Declined, 'guest'], [$booking->status, $booking->declined_by]);
$this->assertTrue((new AvailabilityChecker)->isAvailable($this->property, $booking->arrival, $booking->departure));
Mail::assertSent(OfferAnswered::class, fn (OfferAnswered $mail): bool => ! $mail->accepted);
}
public function test_offer_page_needs_a_valid_signature(): void
{
$this->sendOffer();
$this->get('/angebot/'.$this->booking->id.'?v=1')->assertForbidden();
$this->post('/angebot/'.$this->booking->id.'/annehmen?v=1', ['terms' => '1'])->assertForbidden();
}
public function test_landlord_sends_the_offer_from_the_booking_page(): void
{
$this->actingAs(User::factory()->create());
Livewire::test(ViewBooking::class, ['record' => $this->booking->getRouteKey()])
->callAction('sendOffer', [
'arrival' => '2026-11-07',
'departure' => '2026-11-10',
'guests' => 2,
'price_adjustment' => '-25.50',
'price_adjustment_label' => 'Sonderpreis',
'valid_until' => '2026-10-26 12:00',
'guest' => ['first_name' => 'Max', 'last_name' => 'Muster'],
])
->assertHasNoFormErrors()
->assertNotified('Angebot gesendet');
$this->assertSame([BookingStatus::OfferSent, -2550], [$this->booking->fresh()->status, $this->booking->fresh()->price_adjustment]);
Mail::assertSent(OfferMade::class);
}
/**
* @param array<string, mixed> $changes
*/
private function sendOffer(array $changes = []): string
{
$send = app(SendOffer::class);
$booking = $send->handle($this->booking->fresh(), $changes);
return $send->offerUrl($booking);
}
/**
* The signed form action of the offer page ("annehmen" or "ablehnen").
*/
private function formAction(string $pageUrl, string $action): string
{
preg_match('~action="([^"]*/'.$action.'[^"]*)"~', $this->get($pageUrl)->getContent(), $match);
$this->assertNotEmpty($match, "form for {$action} not found");
return html_entity_decode($match[1]);
}
}