Files
fewo-stommer/tests/Feature/CaseCoreTest.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

224 lines
11 KiB
PHP

<?php
namespace Tests\Feature;
use App\Actions\CreateReservation;
use App\Actions\SendOffer;
use App\Actions\TakeOverExternalBooking;
use App\Documents\OfferTerms;
use App\Enums\BookingStatus;
use App\Enums\CaseType;
use App\Enums\IntakeKind;
use App\Enums\ProcessingMode;
use App\Enums\ProcessSetting;
use App\Enums\Responsibility;
use App\Models\Booking;
use App\Models\BookingEvent;
use App\Models\Channel;
use App\Models\ContractTemplate;
use App\Models\Guest;
use App\Models\Property;
use App\Models\User;
use App\Pricing\StayRequest;
use App\Process\ProcessSettings;
use App\Support\Settings;
use Carbon\CarbonImmutable;
use Database\Seeders\PropertySeeder;
use Database\Seeders\SiteSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;
/**
* Package 1 of UMSETZUNGSPLAN_v0.10: one case for every occasion, unknown stays unknown, settings in levels
* (landlord → property → channel per intake kind → case), the profile frozen per case and a stored history.
*/
class CaseCoreTest 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');
}
/** 1.2 */
public function test_a_portal_booking_without_guest_data_stays_unknown_instead_of_getting_a_placeholder(): void
{
$portal = Channel::factory()->for($this->property)->create(['name' => 'FeWo-direkt', 'booking_model' => 'portal_booking']);
$guestsBefore = Guest::query()->count();
$booking = app(TakeOverExternalBooking::class)->handle($this->property, $portal, CarbonImmutable::parse('2026-12-01'), CarbonImmutable::parse('2026-12-05'), ['portal_reference' => 'HA-1']);
$this->assertSame(BookingStatus::Confirmed, $booking->status);
$this->assertNull($booking->guest_id);
$this->assertNull($booking->guests);
$this->assertNull($booking->total);
$this->assertNull($booking->quote);
$this->assertSame('Gast noch nicht bekannt', $booking->guestName());
$this->assertSame($guestsBefore, Guest::query()->count());
$this->assertSame(IntakeKind::Booking, $booking->intake);
$this->assertSame(CaseType::A3, $booking->case_type);
$this->assertSame(0, $booking->openAmount());
$this->assertFalse($booking->wantsPaymentTasks());
}
public function test_a_known_portal_price_is_taken_over_as_one_line_and_never_split(): 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-01'), CarbonImmutable::parse('2026-12-05'), ['portal_total' => 97951, 'guests' => 2]);
$this->assertSame(97951, $booking->total);
$this->assertCount(1, $booking->quote['lines']);
$this->assertSame(97951, $booking->quote['lines'][0]['amount']);
$this->assertStringContainsString('laut Portal', $booking->quote['lines'][0]['label']);
}
/** 1.3 */
public function test_landlord_defaults_apply_to_a_new_request(): void
{
Settings::tenant()->update(['process_defaults' => ['hold_hours' => 24, 'processing' => 'manual']]);
$booking = $this->request();
$this->assertSame(BookingStatus::Reserved, $booking->status);
$this->assertTrue($booking->hold_expires_at->equalTo(now()->addHours(24)));
$this->assertSame(ProcessingMode::Manual, $booking->processingMode());
$this->assertSame(CaseType::A1, $booking->case_type);
}
/** 1.4 */
public function test_a_channel_value_that_is_not_set_inherits_and_a_set_value_wins_even_when_it_is_no(): void
{
Settings::tenant()->update(['process_defaults' => ['processing' => 'automatic', 'pre_reservation' => true]]);
$inherits = Channel::factory()->for($this->property)->create(['booking_model' => 'inquiry', 'process_overrides' => ['inquiry' => ['processing' => null]]]);
$deviates = Channel::factory()->for($this->property)->create(['booking_model' => 'inquiry', 'process_overrides' => ['inquiry' => ['processing' => 'manual', 'pre_reservation' => '0']]]);
$settings = app(ProcessSettings::class);
$inherited = $settings->resolve($this->property, $inherits, IntakeKind::Inquiry);
$deviated = $settings->resolve($this->property, $deviates, IntakeKind::Inquiry);
$this->assertSame(ProcessingMode::Automatic, $inherited->processing());
$this->assertTrue($inherited->preReservation());
$this->assertSame('tenant', $inherited->source(ProcessSetting::Processing));
$this->assertSame(ProcessingMode::Manual, $deviated->processing());
$this->assertFalse($deviated->preReservation());
$this->assertSame('channel', $deviated->source(ProcessSetting::PreReservation));
}
public function test_a_property_deviation_sits_between_landlord_default_and_channel(): void
{
Settings::tenant()->update(['process_defaults' => ['hold_hours' => 48]]);
$this->property->update(['process_overrides' => ['hold_hours' => 12]]);
$this->assertSame(12, app(ProcessSettings::class)->resolve($this->property->fresh(), null, IntakeKind::Inquiry)->holdHours());
}
/** 1.5 */
public function test_one_mixed_portal_handles_requests_and_bindings_differently(): void
{
$inquiryTemplate = ContractTemplate::query()->create(['released_at' => now(), 'name' => 'Anfrage', 'terms' => '<p>A</p>']);
$bookingTemplate = ContractTemplate::query()->create(['released_at' => now(), 'name' => 'Portalbuchung', 'terms' => '<p>B</p>']);
$portal = Channel::factory()->for($this->property)->create(['booking_model' => 'mixed', 'process_overrides' => [
'inquiry' => ['contract_template_id' => $inquiryTemplate->id],
'booking' => ['contract_template_id' => $bookingTemplate->id, 'payments' => 'portal'],
]]);
$request = $this->request(channel: $portal);
$binding = app(TakeOverExternalBooking::class)->handle($this->property, $portal, CarbonImmutable::parse('2026-12-10'), CarbonImmutable::parse('2026-12-14'));
$this->assertSame(CaseType::A2, $request->case_type);
$this->assertSame(CaseType::A4, $binding->case_type);
$this->assertSame('Anfrage', OfferTerms::templateFor($request)?->name);
$this->assertSame('Portalbuchung', OfferTerms::templateFor($binding)?->name);
$this->assertSame(Responsibility::Landlord, $request->process()->payments());
$this->assertSame(Responsibility::Portal, $binding->process()->payments());
}
/** 1.6 */
public function test_the_profile_is_frozen_with_the_case(): void
{
$before = $this->request();
Settings::tenant()->update(['process_defaults' => ['processing' => 'automatic', 'hold_hours' => 72]]);
$after = $this->request('zweite@example.org', '2026-12-01', '2026-12-04');
$this->assertSame(ProcessingMode::Manual, $before->fresh()->processingMode());
$this->assertSame(48, $before->fresh()->process()->holdHours());
$this->assertSame(72, $after->process()->holdHours());
$before->fresh()->update(['processing_mode' => ProcessingMode::Automatic]);
$this->assertSame(ProcessingMode::Automatic, $before->fresh()->processingMode());
$this->assertTrue(BookingEvent::query()->where('booking_id', $before->id)->where('type', 'mode')->exists());
}
/** 1.7 */
public function test_the_history_is_stored_and_does_not_change_with_the_case(): void
{
$booking = $this->request();
$this->actingAs(User::factory()->create(['name' => 'Ingrid']));
app(SendOffer::class)->handle($booking);
$summaries = $booking->events()->pluck('summary')->all();
$this->assertStringStartsWith('Vorgang angelegt', $summaries[0]);
$this->assertContains('Angebot Version 1 gesendet, gültig bis 24.10.2026, 10:00 Uhr', $summaries);
$this->assertSame('Ingrid', $booking->events()->where('type', 'offer')->value('actor'));
$booking->fresh()->update(['offer_version' => 0, 'offer_sent_at' => null]);
$this->assertContains('Angebot Version 1 gesendet, gültig bis 24.10.2026, 10:00 Uhr', $booking->events()->pluck('summary')->all());
}
/** 1.1 */
public function test_the_migration_keeps_behaviour_of_existing_cases_and_differing_properties(): void
{
$second = Property::factory()->create(['tenant_id' => $this->property->tenant_id, 'hold_hours' => 24, 'offer_mode' => 'manual']);
$this->property->update(['hold_hours' => 48, 'offer_mode' => 'manual']);
$portal = Channel::factory()->for($this->property)->create(['booking_model' => 'portal_booking', 'payment_tasks_reservation' => false]);
$placeholder = Guest::query()->create(['first_name' => 'Gast', 'last_name' => '(FeWo-direkt)', 'email' => '']);
$legacy = Booking::factory()->for($this->property)->create(['import_channel_id' => $portal->id, 'source' => 'portal', 'guest_id' => $placeholder->id, 'offer_hash' => 'abc', 'offer_version' => 2]);
DB::table('bookings')->update(['intake' => null, 'case_type' => null, 'profile' => null]);
DB::table('booking_events')->delete();
DB::table('tenants')->update(['process_defaults' => null]);
(require database_path('migrations/2026_11_01_090100_move_process_settings_and_backfill_cases.php'))->up();
$tenant = Settings::tenant()->fresh();
$this->assertSame('manual', $tenant->process_defaults['processing']);
$this->assertArrayNotHasKey('hold_hours', $tenant->process_defaults);
$this->assertSame(48, $this->property->fresh()->process_overrides['hold_hours']);
$this->assertSame(24, $second->fresh()->process_overrides['hold_hours']);
$this->assertSame(24, app(ProcessSettings::class)->resolve($second->fresh(), null, IntakeKind::Inquiry)->holdHours());
$legacy = $legacy->fresh();
$this->assertSame(CaseType::A4, $legacy->case_type);
$this->assertSame('migration', $legacy->process()->origin);
$this->assertFalse($legacy->wantsPaymentTasks());
$this->assertSame('abc', $legacy->offer_hash);
$this->assertSame(2, $legacy->offer_version);
$this->assertNull($legacy->guest_id);
$this->assertNull(Guest::query()->find($placeholder->id));
$this->assertTrue($legacy->events()->exists());
}
private function request(string $email = 'gast@example.org', string $arrival = '2026-11-07', string $departure = '2026-11-10', ?Channel $channel = null): 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, 'street' => 'Strandweg 1', 'postal_code' => '18225', 'city' => 'Kühlungsborn', 'country' => 'DE'],
sendMails: false,
originChannel: $channel,
);
}
}