Files
fewo-stommer/tests/Feature/Process/InquiryTemplateTest.php
T

307 lines
20 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\Process;
use App\Actions\ConfigureChannelProcess;
use App\Actions\CreateReservation;
use App\Actions\TakeOverExternalBooking;
use App\Actions\TakeOverPortalInquiry;
use App\Enums\ActivityState;
use App\Enums\InquiryAnswer;
use App\Enums\IntakeKind;
use App\Enums\ProcessSetting;
use App\Enums\Responsibility;
use App\Models\Booking;
use App\Models\Channel;
use App\Models\Property;
use App\Pricing\StayRequest;
use App\Process\CaseActivities;
use App\Process\CaseContext;
use App\Process\ChannelProcessConfiguration;
use App\Process\ProcessCatalogue;
use App\Process\ProcessLanes;
use App\Process\ProcessPlan;
use App\Process\ProcessProfile;
use App\Process\ProcessRunner;
use App\Process\Templates\TemplateComposer;
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\Mail\MailManager;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Storage;
use Illuminate\Validation\ValidationException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use Tests\TestCase;
/**
* The request base process (`inquiry.request`): the same principle as the externally binding booking – base process,
* combinable function templates, individually configurable modules – for the own website and portal requests. Covers
* the equivalence with A1/A2, the decoupling of the answer from the reservation, guest communication per occasion and
* that the displayed dependencies are the executed ones.
*/
#[Group('configurator')]
class InquiryTemplateTest extends TestCase
{
use RefreshDatabase;
private Property $property;
protected function setUp(): void
{
parent::setUp();
$this->travelTo(CarbonImmutable::parse('2026-10-23 10:00'));
$this->seed([PropertySeeder::class, SiteSeeder::class]);
$this->property = Property::sole();
Storage::fake('local');
Content::save(['contract.terms' => '<p>Echte Bedingungen</p>', 'stay.cancellation' => '<p>Echtes Storno</p>']);
config(['mail.default' => 'array']);
Mail::swap(new MailManager($this->app));
Settings::tenant()->update(['bank_iban' => 'DE02120300000000202051', 'test_recipient' => 'test@example.org']);
Http::preventStrayRequests();
}
public function test_the_catalogue_with_the_request_base_process_is_consistent(): void
{
$this->assertSame([], app(ProcessCatalogue::class)->problems());
$model = app(ProcessCatalogue::class)->model('inquiry.request');
$this->assertEqualsCanonicalizing(['inquiry_answer', 'contract_documents', 'collection', 'guest_communication'], array_map(fn ($area): string => $area->value, $model->slots()));
foreach (['a1.offer', 'a1.information', 'a2.offer', 'a2.information'] as $legacy) {
$archetype = app(ProcessCatalogue::class)->model($legacy);
$this->assertTrue($archetype::isLegacy());
$this->assertSame([], app(TemplateComposer::class)->problems($model, $archetype::presetTemplates()), $legacy.' is a valid combination of the base process');
}
}
/**
* @return array<string, array{0: string, 1: string, 2: bool}> archetype, answer template, whether the portal sends the request
*/
public static function archetypes(): array
{
return [
'A1 offer' => ['a1.offer', 'inquiry.binding_offer', false],
'A1 information' => ['a1.information', 'inquiry.information', false],
'A2 offer' => ['a2.offer', 'inquiry.binding_offer', true],
'A2 information' => ['a2.information', 'inquiry.information', true],
];
}
#[DataProvider('archetypes')]
public function test_the_base_process_behaves_like_the_archetype_it_replaces(string $legacy, string $answer, bool $viaPortal): void
{
$channel = $viaPortal ? $this->portal() : Channel::websiteOf($this->property);
$this->configure($channel, ['model' => $legacy]);
$old = $this->request($channel->fresh(), $viaPortal, '2026-11-07');
$this->configure($channel->fresh(), ['model' => 'inquiry.request', 'templates' => ['inquiry_answer' => $answer]]);
$new = $this->request($channel->fresh(), $viaPortal, '2026-12-07');
$oldValues = $old->process()->values;
$newValues = $new->process()->values;
unset($oldValues['contract_template_id'], $newValues['contract_template_id']);
$this->assertEquals($oldValues, $newValues, 'identical effective configuration');
$this->assertSame($old->case_type, $new->case_type);
$this->assertSame($old->hold_purpose, $new->hold_purpose);
$this->assertSame($old->hold_expires_at === null, $new->hold_expires_at === null, 'the same reservation behaviour');
$summarise = fn (Booking $booking): array => collect(CaseActivities::for($booking->fresh()))->map(fn ($activity): array => [$activity->key, $activity->label, $activity->state->value])->all();
$this->assertSame($summarise($old), $summarise($new), 'identical activities and states');
$this->assertSame('inquiry.request', ProcessPlan::of($new)->model);
$this->assertArrayHasKey('guest_communication', ProcessPlan::of($new)->templates);
}
public function test_the_answer_template_does_not_force_a_reservation_strategy(): void
{
$website = Channel::websiteOf($this->property);
$configuration = app(ChannelProcessConfiguration::class);
// Binding offer: reservation at the request is a suggestion – both strategies are admissible.
foreach ([null, '1', '0'] as $reservation) {
$draft = ['model' => 'inquiry.request', 'templates' => ['inquiry_answer' => 'inquiry.binding_offer'], 'settings' => $reservation === null ? [] : ['pre_reservation' => $reservation]];
$this->assertSame([], $configuration->problems($website, IntakeKind::Inquiry, $draft), 'binding offer with reservation setting '.var_export($reservation, true));
}
$this->assertTrue($configuration->profileWith($website, IntakeKind::Inquiry, ['model' => 'inquiry.request', 'templates' => ['inquiry_answer' => 'inquiry.binding_offer']])->preReservation());
$this->assertSame(ProcessProfile::TEMPLATE_DEFAULT, $configuration->profileWith($website, IntakeKind::Inquiry, ['model' => 'inquiry.request', 'templates' => ['inquiry_answer' => 'inquiry.binding_offer']])->source(ProcessSetting::PreReservation));
// Information: by definition nothing is reserved – the documented rule, with its reason.
$problems = $configuration->problems($website, IntakeKind::Inquiry, ['model' => 'inquiry.request', 'templates' => ['inquiry_answer' => 'inquiry.information'], 'settings' => ['pre_reservation' => '1']]);
$this->assertStringContainsString('widerspricht der Schablone', implode(' ', $problems));
// Offer without early reservation: no hold at an incomplete request, the offer itself holds later.
$this->configure($website, ['model' => 'inquiry.request', 'templates' => ['inquiry_answer' => 'inquiry.binding_offer'], 'settings' => ['pre_reservation' => '0']]);
$incomplete = $this->request($website->fresh(), false, '2026-11-07', street: '');
$this->assertNull($incomplete->hold_expires_at, 'no reservation at the request');
$this->assertSame(InquiryAnswer::Offer, $incomplete->process()->inquiryAnswer());
$this->assertFalse(Booking::query()->occupying()->whereKey($incomplete->id)->exists());
$this->configure($website->fresh(), ['model' => 'inquiry.request', 'templates' => ['inquiry_answer' => 'inquiry.binding_offer'], 'settings' => ['pre_reservation' => null]]);
$held = $this->request($website->fresh(), false, '2026-12-07', street: '');
$this->assertNotNull($held->hold_expires_at, 'the suggested strategy: clarification hold at the request');
}
public function test_switching_the_answer_keeps_explicit_module_parameters_and_asks_before_dropping_a_contradicting_one(): void
{
$website = Channel::websiteOf($this->property);
$this->configure($website, ['model' => 'inquiry.request', 'templates' => ['inquiry_answer' => 'inquiry.binding_offer'], 'settings' => ['hold_hours' => '24', 'processing' => 'automatic', 'guest_link_valid_days' => '7']]);
$this->configure($website->fresh(), ['model' => 'inquiry.request', 'templates' => ['inquiry_answer' => 'inquiry.information'], 'settings' => ['hold_hours' => '24', 'processing' => 'automatic', 'guest_link_valid_days' => '7']]);
$saved = $website->fresh()->process_overrides['inquiry'];
$this->assertEquals(['24', 'automatic'], [(string) $saved['hold_hours'], $saved['processing']], 'explicit parameters survive the switch');
$this->assertSame('inquiry.information', $saved['templates']['inquiry_answer']);
// An explicitly stored reservation contradicts the information: it must be confirmed before it goes.
$website->fresh()->update(['process_overrides' => ['inquiry' => [...$saved, 'pre_reservation' => true]]]);
$conflicts = app(ChannelProcessConfiguration::class)->conflicts($website->fresh(), IntakeKind::Inquiry, 'inquiry.request', ['inquiry_answer' => 'inquiry.information']);
$this->assertSame([ProcessSetting::PreReservation], array_column($conflicts, 'setting'));
$this->expectException(ValidationException::class);
app(ConfigureChannelProcess::class)->handle($website->fresh(), IntakeKind::Inquiry, ['model' => 'inquiry.request', 'templates' => ['inquiry_answer' => 'inquiry.information']]);
}
public function test_requests_take_no_portal_collection_and_no_external_documents(): void
{
$website = Channel::websiteOf($this->property);
$configuration = app(ChannelProcessConfiguration::class);
$options = $configuration->templateOptions($website, app(ProcessCatalogue::class)->model('inquiry.request'));
$this->assertFalse($options['collection']['collection.portal']['admissible']);
$this->assertStringContainsString('Annahme des Angebots', $options['collection']['collection.portal']['restrictions'][0]->reason);
$this->assertFalse($options['contract_documents']['contract_docs.external']['admissible']);
$this->assertFalse($options['guest_communication']['guest_comm.portal']['admissible'], 'the own website has no portal');
$this->assertNotSame([], $configuration->problems($website, IntakeKind::Inquiry, ['model' => 'inquiry.request', 'templates' => ['collection' => 'collection.portal']]));
}
public function test_guest_communication_is_assigned_per_occasion_without_touching_other_tasks(): void
{
$channel = $this->bookingPortal();
$this->configure($channel, ['model' => 'external.booking', 'templates' => ['collection' => 'collection.own', 'contract_documents' => 'contract_docs.own']], IntakeKind::Booking);
$baseline = $this->takeOver($channel->fresh(), 'KOM-1', '2027-07-10');
$this->configure($channel->fresh(), ['model' => 'external.booking', 'templates' => ['collection' => 'collection.own', 'contract_documents' => 'contract_docs.own'], 'settings' => ['arrival_info_sender' => 'portal']], IntakeKind::Booking);
$changed = $this->takeOver($channel->fresh(), 'KOM-2', '2027-08-10');
$activities = fn (Booking $booking): array => collect(CaseActivities::for($booking->fresh()))->keyBy('key')->all();
[$before, $after] = [$activities($baseline), $activities($changed)];
$this->assertSame(ActivityState::External, $after['arrival_info']->state, 'the arrival information is now the portal’s');
$this->assertNotSame(ActivityState::External, $before['arrival_info']->state);
$this->assertArrayHasKey('review', $after, 'the review request stays with us');
$this->assertSame(array_keys($before), array_keys($after), 'no other task appears or disappears');
foreach (array_diff(array_keys($before), ['arrival_info']) as $key) {
$this->assertSame($before[$key]->state, $after[$key]->state, $key.' is unchanged');
}
$profile = $changed->process();
$this->assertSame([Responsibility::Portal, Responsibility::Landlord, Responsibility::Landlord], [$profile->arrivalInfoSender(), $profile->reviewRequestSender(), $profile->payments()]);
$this->assertSame(Responsibility::Landlord, $profile->guestMessages(), 'the umbrella setting is untouched');
// And the other way round: the review request goes to the portal, the arrival information stays ours.
$this->configure($channel->fresh(), ['model' => 'external.booking', 'templates' => ['collection' => 'collection.own', 'contract_documents' => 'contract_docs.own'], 'settings' => ['arrival_info_sender' => null, 'review_request_sender' => 'portal']], IntakeKind::Booking);
$other = $this->takeOver($channel->fresh(), 'KOM-3', '2027-09-10');
$this->assertArrayNotHasKey('review', $activities($other));
$this->assertNotSame(ActivityState::External, $activities($other)['arrival_info']->state);
}
public function test_a_portal_inquiry_gets_the_portal_as_standard_sender_that_each_occasion_can_take_back(): void
{
$portal = $this->portal();
$this->configure($portal, ['model' => 'inquiry.request', 'templates' => ['inquiry_answer' => 'inquiry.binding_offer', 'guest_communication' => 'guest_comm.portal']]);
$case = $this->request($portal->fresh(), true, '2027-01-12');
$this->assertEquals(['guest_communication' => 'guest_comm.portal@1'], array_intersect_key(ProcessPlan::of($case)->templates, ['guest_communication' => 1]));
$this->assertSame([Responsibility::Portal, Responsibility::Portal], [$case->process()->arrivalInfoSender(), $case->process()->reviewRequestSender()]);
$this->assertSame(ProcessProfile::TEMPLATE_DEFAULT, $case->process()->source(ProcessSetting::ArrivalInfoSender));
$this->configure($portal->fresh(), ['model' => 'inquiry.request', 'templates' => ['inquiry_answer' => 'inquiry.binding_offer', 'guest_communication' => 'guest_comm.portal'], 'settings' => ['review_request_sender' => 'landlord']]);
$mixed = $this->request($portal->fresh(), true, '2027-02-12');
$this->assertSame([Responsibility::Portal, Responsibility::Landlord], [$mixed->process()->arrivalInfoSender(), $mixed->process()->reviewRequestSender()], 'the explicit value wins over the template suggestion');
}
public function test_cases_frozen_before_the_new_settings_read_exactly_as_before(): void
{
$website = Channel::websiteOf($this->property);
$this->configure($website, ['model' => 'a1.information']);
$case = $this->request($website->fresh(), false, '2026-11-07');
$profile = $case->profile;
foreach (['inquiry_answer', 'arrival_info_sender', 'review_request_sender'] as $key) {
unset($profile['values'][$key], $profile['sources'][$key]);
}
$case->forceFill(['profile' => $profile])->saveQuietly();
$old = $case->fresh()->process();
$this->assertSame(InquiryAnswer::Information, $old->inquiryAnswer(), 'derived from the reservation, as before');
$this->assertSame([$old->guestMessages(), $old->guestMessages()], [$old->arrivalInfoSender(), $old->reviewRequestSender()]);
$this->assertSame('a1.information', ProcessPlan::of($case->fresh())->model);
}
public function test_the_displayed_dependencies_are_the_executed_ones(): void
{
$website = Channel::websiteOf($this->property);
$this->configure($website, ['model' => 'inquiry.request', 'templates' => ['inquiry_answer' => 'inquiry.binding_offer']]);
$draft = ['model' => 'inquiry.request', 'templates' => ['inquiry_answer' => 'inquiry.binding_offer']];
$lanes = app(ProcessLanes::class)->of(app(ChannelProcessConfiguration::class)->preview($website->fresh(), IntakeKind::Inquiry, $draft));
$labels = fn (string $lane): array => array_column($lanes[$lane], 'module');
$this->assertContains('hold', $labels('parallel'), 'the reservation runs at once');
$this->assertContains('guest.data_request', $labels('parallel'), 'so does the data collection – independent of each other');
$this->assertContains('offer.binding', $labels('waiting'));
$this->assertContains('documents.send', $labels('waiting'));
$this->assertContains('case.tasks', $labels('event'));
$this->assertContains('arrival.info', $labels('later'));
$this->assertNotContains('offer.information', array_merge(...array_values(array_map(fn (array $lane): array => array_column($lane, 'module'), $lanes))), 'an offer needs no information step: it is not in the plan at all');
// The runner agrees: with missing details the offer is held back, the hold and the data request are not.
$case = $this->request($website->fresh(), false, '2026-11-07', street: '');
$context = CaseContext::of($case);
$runner = app(ProcessRunner::class);
$catalogue = app(ProcessCatalogue::class);
foreach (array_column($lanes['waiting'], 'module') as $module) {
if (in_array($module, ['offer.binding', 'offer.acceptance'], true) && $context->plan->enabled($module)) {
$this->assertNotSame([], $runner->unmetHardDependencies($catalogue->module($module), $context), $module.' waits in the plan as shown');
}
}
foreach (['hold', 'guest.data_request'] as $module) {
$this->assertSame([], $runner->unmetHardDependencies($catalogue->module($module), $context), $module.' does not wait');
}
$waitingFor = collect($lanes['waiting'])->firstWhere('module', 'offer.binding')['after'];
$this->assertContains('Angaben des Gastes', $waitingFor);
}
/**
* @param array<string, mixed> $draft
*/
private function configure(Channel $channel, array $draft, IntakeKind $intake = IntakeKind::Inquiry): void
{
app(ConfigureChannelProcess::class)->handle($channel, $intake, $draft, acknowledgeConflicts: true);
}
private function request(Channel $channel, bool $viaPortal, string $arrival, string $street = 'Weg 1'): Booking
{
$arrival = CarbonImmutable::parse($arrival);
if ($viaPortal) {
return app(TakeOverPortalInquiry::class)->handle($channel, $arrival, $arrival->addDays(3), 'ANF-'.$arrival->format('md'))->fresh();
}
return app(CreateReservation::class)->handle($this->property->fresh(), new StayRequest($arrival, $arrival->addDays(3), 2), ['first_name' => 'Gesa', 'last_name' => 'Gast', 'email' => 'gesa@example.org', 'street' => $street, 'postal_code' => '10115', 'city' => 'Berlin', 'country' => 'DE'])->fresh();
}
private function takeOver(Channel $channel, string $reference, string $arrival): Booking
{
$arrival = CarbonImmutable::parse($arrival);
return app(TakeOverExternalBooking::class)->handle($this->property, $channel, $arrival, $arrival->addDays(7), ['portal_reference' => $reference]);
}
private function portal(): Channel
{
return Channel::factory()->for($this->property)->create(['name' => 'Anfrageportal', 'booking_model' => 'inquiry', 'import_enabled' => false, 'settings' => ['import_url' => 'https://anfrage.example/a.ics', 'portal_profile' => 'generic']]);
}
private function bookingPortal(): Channel
{
return Channel::factory()->for($this->property)->create(['name' => 'Buchungsportal', 'booking_model' => 'portal_booking', 'import_enabled' => false, 'settings' => ['import_url' => 'https://buchung.example/a.ics', 'portal_profile' => 'generic']]);
}
}