221 lines
13 KiB
PHP
221 lines
13 KiB
PHP
<?php
|
||
|
||
namespace Tests\Feature\Process;
|
||
|
||
use App\Enums\IntakeKind;
|
||
use App\Filament\Resources\Channels\Pages\ChannelProcess;
|
||
use App\Filament\Resources\Channels\Pages\ChannelProcessTest;
|
||
use App\Models\Booking;
|
||
use App\Models\BookingTask;
|
||
use App\Models\Channel;
|
||
use App\Models\ProcessTestRun;
|
||
use App\Models\Property;
|
||
use App\Models\User;
|
||
use App\Process\Tester\ProcessSandbox;
|
||
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\Mail;
|
||
use Illuminate\Support\Facades\Process;
|
||
use Livewire\Livewire;
|
||
use PHPUnit\Framework\Attributes\Group;
|
||
use Tests\TestCase;
|
||
|
||
/**
|
||
* Package U-D: the process tester plays scenarios in a process of its own with the same business logic (T01, T02,
|
||
* T05) and leaves the real data untouched (T03).
|
||
*/
|
||
#[Group('sandbox')]
|
||
class ProcessTesterTest extends TestCase
|
||
{
|
||
use RefreshDatabase;
|
||
|
||
private Property $property;
|
||
|
||
private User $user;
|
||
|
||
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();
|
||
Content::save(['contract.terms' => '<p>Echte Bedingungen</p>', 'stay.cancellation' => '<p>Echtes Storno</p>']);
|
||
Settings::tenant()->update(['bank_iban' => 'DE02120300000000202051', 'test_recipient' => 'vermieter@example.org', 'process_defaults' => ['processing' => 'automatic', 'clarification_hold_auto_minutes' => 120, 'data_request_mode' => 'automatic']]);
|
||
$this->user = User::factory()->create();
|
||
self::template();
|
||
}
|
||
|
||
public static function template(): void
|
||
{
|
||
$template = storage_path('app/sandbox/template.sqlite');
|
||
$newest = max(array_map('filemtime', glob(database_path('migrations/*.php'))));
|
||
if (! is_file($template) || filemtime($template) < $newest) {
|
||
// A fresh process, as at deployment (the migrator binds the schema of the process that runs it).
|
||
Process::path(base_path())->timeout(120)->run([PHP_BINARY, 'artisan', 'process:sandbox-template'])->throw();
|
||
}
|
||
}
|
||
|
||
public function test_a_scenario_runs_isolated_with_the_real_business_logic_and_leaves_no_trace(): void
|
||
{
|
||
Mail::fake();
|
||
$website = Channel::websiteOf($this->property);
|
||
|
||
$run = app(ProcessSandbox::class)->run($website, IntakeKind::Inquiry, ['model' => 'a1.offer'], 'missing_address', ['arrival' => '2026-11-07', 'nights' => 3, 'guests' => 2], 3, $this->user);
|
||
|
||
$report = $run->report;
|
||
$this->assertNull($report['aborted'] ?? null, json_encode($report));
|
||
$this->assertSame(ProcessTestRun::COMPLETED, $run->outcome);
|
||
$this->assertCount(3, $report['timeline']);
|
||
$this->assertSame(['a1.offer', 1, 'A1'], [$report['plan']['model'], $report['plan']['version'], $report['plan']['case_type']]);
|
||
|
||
// Same logic as the real case: data request and clarification hold, later the offer.
|
||
[$request, $expired, $answer] = $report['timeline'];
|
||
$this->assertStringContainsString('reserviert', $request['situation']);
|
||
$this->assertContains('tester@sandbox.invalid', array_column($request['mails'], 'to'), 'mails are simulated and go to the test address');
|
||
$this->assertSame('Angebot gesendet', $answer['status']);
|
||
$this->assertStringContainsString('Angebot', implode(' ', array_column($answer['mails'], 'subject')));
|
||
$this->assertSame([], $report['violations']);
|
||
|
||
// Nothing reached the real data: no case, no mail.
|
||
$this->assertSame(0, Booking::query()->count());
|
||
Mail::assertNothingSent();
|
||
$this->assertDirectoryDoesNotExist(storage_path('app/sandbox/runs/'.$run->id));
|
||
}
|
||
|
||
public function test_a_run_with_the_same_snapshot_gives_the_same_report(): void
|
||
{
|
||
$website = Channel::websiteOf($this->property);
|
||
$inputs = ['arrival' => '2026-11-07', 'nights' => 3, 'guests' => 2];
|
||
|
||
$first = app(ProcessSandbox::class)->run($website, IntakeKind::Inquiry, ['model' => 'a1.offer'], 'complete_request', $inputs, 3, $this->user);
|
||
$second = app(ProcessSandbox::class)->run($website, IntakeKind::Inquiry, ['model' => 'a1.offer'], 'complete_request', $inputs, 3, $this->user);
|
||
|
||
$strip = fn (array $report): array => array_map(fn (array $event): array => array_diff_key($event, ['effects' => true]), $report['timeline']);
|
||
$this->assertSame($strip($first->report), $strip($second->report));
|
||
}
|
||
|
||
public function test_the_loyalty_variant_runs_in_the_tester_only(): void
|
||
{
|
||
$website = Channel::websiteOf($this->property);
|
||
|
||
$run = app(ProcessSandbox::class)->run($website, IntakeKind::Inquiry, ['model' => 'a1.offer', 'variant' => 'loyalty'], 'returning_guest', ['arrival' => '2026-11-07', 'nights' => 3, 'guests' => 2], 3, $this->user);
|
||
|
||
$this->assertNull($run->report['aborted'] ?? null, json_encode($run->report));
|
||
$this->assertSame('loyalty', $run->report['plan']['variant']);
|
||
$timeline = $run->report['timeline'];
|
||
$lines = array_column(end($timeline)['price']['lines'], 'label');
|
||
$this->assertNotEmpty(array_filter($lines, fn (string $label): bool => str_starts_with($label, 'Stammgastrabatt')), 'the discount enters the price before it is fixed');
|
||
$this->assertNull($website->fresh()->process_overrides['inquiry']['variant'] ?? null, 'nothing was saved');
|
||
}
|
||
|
||
/** S07/S08/S13: a portal booking by mail (twice), portal amounts and a cancellation report – A4 of FeWo-direkt */
|
||
public function test_a_portal_booking_scenario_with_cancellation_report(): void
|
||
{
|
||
$fewo = Channel::factory()->for($this->property)->create(['name' => 'FeWo-direkt', 'booking_model' => 'mixed', 'status' => 'setup', 'is_active' => false, 'import_enabled' => false, 'settings' => ['import_url' => 'https://www.fewo-direkt.de/x.ics']]);
|
||
|
||
$run = app(ProcessSandbox::class)->run($fewo, IntakeKind::Booking, ['model' => 'a4.portal_booking', 'settings' => ['guest_messages' => 'portal']], 'portal_booking', ['arrival' => '2026-11-14', 'nights' => 4, 'guests' => 2, 'portal_total' => 980, 'cancellation' => true], 6, $this->user);
|
||
|
||
$report = $run->report;
|
||
$this->assertNull($report['aborted'] ?? null, json_encode($report));
|
||
$this->assertSame(['a4.portal_booking', 'A4'], [$report['plan']['model'], $report['plan']['case_type']]);
|
||
[$mail, $repeated, , $amounts, $cancellation] = $report['timeline'];
|
||
$this->assertSame('Bestätigt', $mail['status'] ?? null, json_encode($mail));
|
||
$this->assertSame($mail['situation'], $repeated['situation'], 'the same mail twice: one case');
|
||
$this->assertStringContainsString('laut Portal', implode(' ', array_column($amounts['activities'], 'detail')));
|
||
$this->assertContains('Das Portal meldet eine Stornierung – bitte prüfen', array_column($cancellation['activities'], 'label'));
|
||
$this->assertSame('Bestätigt', $cancellation['status'], 'the report never cancels');
|
||
$this->assertSame(0, Booking::query()->count());
|
||
$this->assertFalse((bool) $fewo->fresh()->is_active, 'the real channel is unchanged');
|
||
}
|
||
|
||
/** V2.1: waiting for the portal's confirmation – shows the wait, the protected occupation and the escalation in the tester */
|
||
public function test_the_portal_confirmation_scenarios_show_waiting_arrival_and_escalation(): void
|
||
{
|
||
$portal = Channel::factory()->for($this->property)->create(['name' => 'Destination Solution', 'booking_model' => 'portal_booking', 'status' => 'setup', 'is_active' => false, 'import_enabled' => false, 'settings' => ['import_url' => 'https://ds.example/a.ics', 'email_senders' => 'ds.example']]);
|
||
$draft = ['model' => 'a3.portal_booking', 'settings' => ['await_portal_confirmation' => '1', 'booking_notice' => 'automatic']];
|
||
|
||
$late = app(ProcessSandbox::class)->run($portal, IntakeKind::Booking, $draft, 'portal_confirmation_wait', ['arrival' => '2026-11-14', 'nights' => 4, 'guests' => 2, 'confirmation' => 'late'], 6, $this->user)->report;
|
||
$missing = app(ProcessSandbox::class)->run($portal, IntakeKind::Booking, $draft, 'portal_confirmation_wait', ['arrival' => '2026-11-14', 'nights' => 4, 'guests' => 2, 'confirmation' => 'missing'], 6, $this->user)->report;
|
||
|
||
$this->assertNull($late['aborted'] ?? null, json_encode($late));
|
||
$known = $late['timeline'][0];
|
||
$this->assertStringContainsString('wartet', json_encode($known['activities'], JSON_UNESCAPED_UNICODE));
|
||
$this->assertSame([], $known['mails'], 'nothing goes out while the confirmation is missing');
|
||
$this->assertNotEmpty(end($late['timeline'])['mails'], 'after the confirmation the booking information goes out (sandbox only)');
|
||
$this->assertStringContainsString('ausgeblieben', json_encode(end($missing['timeline'])['activities'], JSON_UNESCAPED_UNICODE));
|
||
$this->assertSame(0, Booking::query()->count());
|
||
$this->assertSame(0, BookingTask::query()->count(), 'the escalation task exists only inside the sandbox');
|
||
}
|
||
|
||
/** V2.1 E/F: other media and questions without a case, through the isolated tester */
|
||
public function test_the_message_scenarios_show_assignment_and_never_create_a_case(): void
|
||
{
|
||
config(['communication.media.chat.active' => true, 'communication.media.whatsapp.active' => true]);
|
||
$website = Channel::websiteOf($this->property);
|
||
|
||
$question = app(ProcessSandbox::class)->run($website, IntakeKind::Inquiry, ['model' => 'a1.offer'], 'general_question_message', ['text' => 'question'], 1, $this->user)->report;
|
||
$reply = app(ProcessSandbox::class)->run($website, IntakeKind::Inquiry, ['model' => 'a1.offer'], 'message_other_medium', ['arrival' => '2026-11-07', 'nights' => 3, 'guests' => 2, 'medium' => 'whatsapp', 'reference' => 'none', 'consent' => true], 2, $this->user)->report;
|
||
|
||
$this->assertNull($question['aborted'] ?? null, json_encode($question));
|
||
$this->assertNull($reply['aborted'] ?? null, json_encode($reply));
|
||
$this->assertSame(0, Booking::query()->count());
|
||
}
|
||
|
||
/** S12/T06: a failing mail server leaves the case in a defined state */
|
||
public function test_a_mail_failure_keeps_the_case(): void
|
||
{
|
||
$run = app(ProcessSandbox::class)->run(Channel::websiteOf($this->property), IntakeKind::Inquiry, ['model' => 'a1.offer'], 'mail_failure', ['arrival' => '2026-11-07', 'nights' => 3, 'guests' => 2], 2, $this->user);
|
||
|
||
$request = $run->report['timeline'][1];
|
||
$this->assertNull($run->report['aborted'] ?? null);
|
||
// v0.11 behaviour: the offer is saved even if its mail fails (SendOffer reports it); nothing went out.
|
||
$this->assertSame('Angebot gesendet', $request['status'], 'the case is kept');
|
||
$this->assertSame([], $request['mails'], 'no mail went out');
|
||
}
|
||
|
||
/** T01/T02: from the configurator with the unsaved draft, step by step, with a readable report */
|
||
public function test_the_landlord_tests_the_unsaved_draft_step_by_step(): void
|
||
{
|
||
$this->actingAs($this->user);
|
||
$website = Channel::websiteOf($this->property);
|
||
|
||
$configurator = Livewire::test(ChannelProcess::class, ['record' => $website->getRouteKey()])
|
||
->call('toggleStep', 'review.request')
|
||
->call('test');
|
||
$configurator->assertRedirect();
|
||
$this->assertFalse(session('process-tester.draft.'.$website->id.'.inquiry')['steps']['review.request']['on']);
|
||
|
||
Livewire::test(ChannelProcessTest::class, ['record' => $website->getRouteKey()])
|
||
->assertSee('Vollständige Anfrage bis zur Buchung')
|
||
->set('inputs.arrival', '2026-11-07')
|
||
->call('start')
|
||
->assertSee('Bericht')
|
||
->assertSee('Gast fragt mit allen Angaben an')
|
||
->assertSee('simuliert')
|
||
->call('next')
|
||
->assertSee('Sie senden das Angebot');
|
||
$this->assertNull($website->fresh()->process_overrides['inquiry']['steps'] ?? null, 'the draft was never saved');
|
||
$this->assertSame(2, ProcessTestRun::query()->count());
|
||
}
|
||
|
||
public function test_without_the_isolation_the_tester_is_locked(): void
|
||
{
|
||
$this->actingAs($this->user);
|
||
$template = storage_path('app/sandbox/template.sqlite');
|
||
rename($template, $template.'.off');
|
||
|
||
try {
|
||
Livewire::test(ChannelProcessTest::class, ['record' => Channel::websiteOf($this->property)->getRouteKey()])
|
||
->assertSee('auf diesem Server gesperrt')
|
||
->assertDontSee('Test starten');
|
||
} finally {
|
||
rename($template.'.off', $template);
|
||
}
|
||
}
|
||
}
|