v0.11 U5: Posteingang, deterministische Zuordnung, Klärungsansicht

- mailboxes (Modell für mehrere, eines aktiv): Übernahme des bisherigen
  Postfachs als „nur Portal-Mails“; Modus „alle Nachrichten“ für ein
  eigenes Postfach; Adresse als Antwortadresse der Rückfragen.
- inbound_messages: jede Nachricht zuerst gespeichert, einmal je
  Message-ID, mit Testbetriebs-Kennzeichen (M10). Portal-Mails laufen
  unverändert über den Portalpfad (CH-08).
- MessageMatcher (CH-13, ohne Scoring): Antwortbezug; Vorgangsnummer nur
  mit dem Gast als Absender; sonst Absender + Wohnung + beide Termine eines
  einzigen offenen Vorgangs. Ein offener Vorgang des Absenders allein
  genügt nicht (nur Hinweis). Alles andere → Klärung (M04).
- Ausgehende Mails speichern ihre Message-ID; Antwort stoppt Erinnerungen.
- Filament „Posteingang“: lesen, Vorgang zuordnen, als neue Anfrage
  anlegen, ignorieren, erledigt.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
LarsandClaude Opus 5.5 committed 2026-11-03 13:39:20 +01:00
1 parent e4a3cb7f7b
commit dff9e2344e
19 files changed
+990 -59

No files matched your search

+155 -50
View File
@@ -2,88 +2,193 @@
namespace Tests\Feature;
use App\Enums\InboxMessageKind;
use App\Filament\Resources\InboxMessages\InboxMessageResource;
use App\Filament\Resources\InboxMessages\Pages\ListInboxMessages;
use App\Actions\CreateReservation;
use App\Actions\ReceiveInboundMessage;
use App\Channels\Mail\MailboxReader;
use App\Channels\Mail\PortalMail;
use App\Enums\BookingTaskKind;
use App\Filament\Pages\Operations;
use App\Filament\Resources\InboundMessages\Pages\ListInboundMessages;
use App\Mail\DataRequested;
use App\Models\Booking;
use App\Models\BookingMessage;
use App\Models\BookingTask;
use App\Models\Channel;
use App\Models\InboxMessage;
use App\Models\DataRequest;
use App\Models\InboundMessage;
use App\Models\Mailbox;
use App\Models\PortalEmail;
use App\Models\Property;
use App\Models\User;
use App\Support\CurrentProperty;
use App\Support\Inbox;
use App\Pricing\StayRequest;
use App\Support\Settings;
use Carbon\CarbonImmutable;
use Database\Seeders\PropertySeeder;
use Database\Seeders\SiteSeeder;
use Filament\Actions\Testing\TestAction;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Mail\MailManager;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Mail;
use Livewire\Livewire;
use Mockery;
use Tests\TestCase;
/**
* v0.11 package U5: the inbox of the guest communication mailbox – stored once, portal notifications through the
* unchanged portal path, deterministic assignment (CH-13) or clarification (M04), reply references (M07 part),
* test-mode mark (M10) and the landlord's clarification view.
*/
class InboxTest extends TestCase
{
use RefreshDatabase;
private Channel $channel;
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();
Http::fake(['*' => Http::response('', 500)]);
}
public function test_a_reply_to_our_mail_is_assigned_and_stops_the_reminders(): void
{
Settings::tenant()->update(['process_defaults' => ['data_request_mode' => 'automatic']]);
$booking = $this->request();
BookingMessage::query()->create(['booking_id' => $booking->id, 'audience' => 'test', 'subject' => 'Rückfrage', 'message_id' => '<abc@fewo.test>', 'sent_at' => now()]);
$message = $this->receive(from: 'irgendwer@example.org', subject: 'Re: Ihre Anfrage', text: 'Strandweg 1, 18225 Kühlungsborn', inReplyTo: '<abc@fewo.test>');
$this->assertSame([InboundMessage::ASSIGNED, $booking->id, 'reply'], [$message->status, $message->booking_id, $message->match_reason]);
$this->assertNull($booking->openDataRequest()->next_reminder_at, 'the answer stops the reminder');
$this->assertSame(1, BookingTask::query()->where('booking_id', $booking->id)->where('kind', BookingTaskKind::InboundReview)->count());
$this->assertTrue($message->test_mode, 'arrived in test operation');
}
public function test_the_case_number_counts_only_with_the_guest_as_sender(): void
{
$booking = $this->request();
$own = $this->receive(from: 'gesa@example.org', subject: 'Frage zu '.$booking->reference, text: 'Hallo');
$foreign = $this->receive(from: 'fremd@example.org', subject: 'Frage zu '.$booking->reference, text: 'Hallo', id: '<2@x>');
$this->assertSame([InboundMessage::ASSIGNED, 'reference_and_sender'], [$own->status, $own->match_reason]);
$this->assertSame(InboundMessage::NEEDS_REVIEW, $foreign->status);
$this->assertSame($booking->id, $foreign->hints[0]['booking_id']);
}
/** CH-13 as specified: one open case of the same sender alone is not enough. */
public function test_a_direct_guest_mail_needs_sender_property_and_dates(): void
{
$booking = $this->request();
$vague = $this->receive(from: 'gesa@example.org', subject: 'Unsere Reise', text: 'Wir freuen uns schon!');
$precise = $this->receive(from: 'gesa@example.org', subject: 'Unsere Reise', text: 'Für den Aufenthalt vom 07.11.2026 bis 10.11.2026 noch eine Frage.', id: '<3@x>');
$this->assertSame(InboundMessage::NEEDS_REVIEW, $vague->status);
$this->assertSame('offener Vorgang dieses Absenders (07.11.–10.11.2026)', $vague->hints[0]['why']);
$this->assertSame([InboundMessage::ASSIGNED, $booking->id, 'sender_property_dates'], [$precise->status, $precise->booking_id, $precise->match_reason]);
}
public function test_dates_of_two_cases_of_the_same_guest_are_resolved_only_when_unambiguous(): void
{
$first = $this->request();
$second = $this->request(arrival: '2026-11-20', departure: '2026-11-23');
$one = $this->receive(from: 'gesa@example.org', subject: 'Frage', text: 'Zum 20.11.2026 – 23.11.2026: Bringen wir den Hund mit?');
$both = $this->receive(from: 'gesa@example.org', subject: 'Frage', text: '07.11.2026 bis 10.11.2026 und 20.11.2026 bis 23.11.2026', id: '<4@x>');
$this->assertSame($second->id, $one->booking_id);
$this->assertSame(InboundMessage::NEEDS_REVIEW, $both->status);
$this->assertNotSame($first->id, $one->booking_id);
}
public function test_a_message_is_stored_once_and_our_own_mails_are_ignored(): void
{
$this->receive(from: 'gast@example.org', subject: 'Hallo', text: 'Test');
$this->receive(from: 'gast@example.org', subject: 'Hallo', text: 'Test');
$own = $this->receive(from: (string) config('mail.from.address'), subject: 'Kopie', text: 'x', id: '<own@x>');
$this->assertSame(2, InboundMessage::query()->count());
$this->assertSame(InboundMessage::IGNORED, $own->status);
}
/** CH-08: the portal path stays as it is. */
public function test_a_portal_notification_goes_through_the_unchanged_portal_path(): void
{
Channel::factory()->for($this->property)->create(['name' => 'FeWo-direkt', 'settings' => ['email_senders' => 'fewo-direkt.de']]);
$message = $this->receive(from: 'noreply@fewo-direkt.de', subject: 'Neue Buchung', text: "Anreise: 07.12.2026\nAbreise: 14.12.2026\nBuchungsnummer: HA-1");
$portal = PortalEmail::query()->sole();
$this->assertSame([InboundMessage::PORTAL, $portal->id], [$message->status, $message->portal_email_id]);
$this->assertSame($message->id, $portal->inbound_message_id);
$this->assertNotNull($portal->blocked_period_id, 'the booking was blocked as before');
}
/** M04: a general message creates nothing; the landlord decides in the inbox. */
public function test_the_landlord_assigns_a_message_or_makes_a_new_request_of_it(): void
{
$this->actingAs(User::factory()->create());
$this->channel = Channel::factory()->for(Property::factory())->create(['name' => 'FeWo-direkt']);
$booking = $this->request();
$general = $this->receive(from: 'neu@example.org', subject: 'Haben Sie im Dezember frei?', text: 'Wir sind zwei Personen.');
$other = $this->receive(from: 'unbekannt@example.org', subject: 'Nachtrag', text: 'Bitte zuordnen', id: '<5@x>');
$this->assertSame(1, Booking::query()->count(), 'no case from a message by itself');
Livewire::test(ListInboundMessages::class)
->callAction(TestAction::make('assign')->table($other), ['booking_id' => $booking->id])
->callAction(TestAction::make('createInquiry')->table($general), ['property_id' => $this->property->id, 'arrival' => '2026-12-01', 'departure' => '2026-12-05', 'guests' => 2, 'first_name' => 'Nina', 'last_name' => 'Neu', 'email' => 'neu@example.org']);
$this->assertSame([InboundMessage::ASSIGNED, $booking->id], [$other->fresh()->status, $other->fresh()->booking_id]);
$created = Booking::query()->where('source', 'email')->sole();
$this->assertSame([InboundMessage::PROCESSED, $created->id], [$general->fresh()->status, $general->fresh()->booking_id]);
$this->assertNotNull($created->openDataRequest(), 'the new request asks for its missing address');
}
public function test_a_finding_is_posted_only_once(): void
public function test_our_mails_keep_their_message_id_for_replies(): void
{
$inbox = app(Inbox::class);
config(['mail.default' => 'array']);
Settings::tenant()->update(['test_recipient' => 'test@example.org']);
Mail::swap(new MailManager($this->app));
$booking = $this->request();
$request = $booking->dataRequests()->create(['tenant_id' => $booking->tenant_id, 'status' => DataRequest::OPEN, 'mode' => 'automatic', 'purpose' => 'offer', 'fields' => ['guest.street']]);
$first = $inbox->post(InboxMessageKind::ChannelConflict, 'x', 'Mögliche Doppelbuchung', 'Text', $this->channel);
$again = $inbox->post(InboxMessageKind::ChannelConflict, 'x', 'Mögliche Doppelbuchung', 'Text', $this->channel);
Mail::to('gesa@example.org')->send(new DataRequested($booking, $request, DataRequest::INITIAL, 'https://example.test'));
$this->assertNotNull($first);
$this->assertNull($again);
$this->assertSame($this->channel->property_id, $first->property_id);
$this->assertSame(1, InboxMessage::count());
$this->assertMatchesRegularExpression('/^<.+@.+>$/', (string) BookingMessage::query()->where('booking_id', $booking->id)->latest('id')->value('message_id'));
}
public function test_menu_shows_the_number_of_unread_messages(): void
public function test_the_mailbox_is_kept_as_a_record_and_full_mode_reads_every_message(): void
{
InboxMessage::factory()->for($this->channel)->count(2)->create();
InboxMessage::factory()->for($this->channel)->read()->create();
$this->actingAs(User::factory()->create());
Livewire::test(Operations::class)
->fillForm(['mailbox_host' => 'imap.example.org', 'mailbox_username' => 'anfragen@example.org', 'mailbox_password' => 'geheim', 'mailbox_mode' => Mailbox::FULL, 'mailbox_address' => 'anfragen@example.org'])
->call('save')->assertHasNoFormErrors();
$mailbox = Mailbox::query()->sole();
$this->assertSame([Mailbox::FULL, 'anfragen@example.org', 'geheim'], [$mailbox->mode, $mailbox->address, $mailbox->password]);
$this->assertSame('anfragen@example.org', DataRequested::replyAddress(), 'questions ask for answers to this mailbox');
$this->assertSame('2', InboxMessageResource::getNavigationBadge());
$this->get(InboxMessageResource::getUrl('index'))->assertOk()->assertSee('Postfach');
$reader = Mockery::mock(MailboxReader::class);
$reader->shouldReceive('fetchAll')->once()->andReturn([new PortalMail('<9@x>', 'gast@example.org', 'Hallo', 'Text', CarbonImmutable::now())]);
$this->app->instance(MailboxReader::class, $reader);
$this->artisan('channels:fetch-mail')->assertSuccessful();
$this->assertSame(InboundMessage::NEEDS_REVIEW, InboundMessage::query()->sole()->status);
$this->assertNotNull($mailbox->fresh()->last_fetched_at);
}
public function test_messages_can_be_marked_as_read_and_opened(): void
private function request(string $arrival = '2026-11-07', string $departure = '2026-11-10'): Booking
{
$message = InboxMessage::factory()->for($this->channel)->create(['subject' => 'Mögliche Doppelbuchung über FeWo-direkt', 'action_url' => 'http://localhost/admin/belegung']);
$other = InboxMessage::factory()->for($this->channel)->create();
Livewire::test(ListInboxMessages::class)
->assertCanSeeTableRecords([$message, $other])
->assertSee('Mögliche Doppelbuchung über FeWo-direkt')
->callAction(TestAction::make('toggleRead')->table($other))
->callAction(TestAction::make('open')->table($message))
->assertRedirect('http://localhost/admin/belegung');
$this->assertTrue($message->fresh()->isRead());
$this->assertTrue($other->fresh()->isRead());
$this->assertNull(InboxMessageResource::getNavigationBadge());
return app(CreateReservation::class)->handle($this->property->fresh(), new StayRequest(CarbonImmutable::parse($arrival), CarbonImmutable::parse($departure), 2), ['first_name' => 'Gesa', 'last_name' => 'Gast', 'email' => 'gesa@example.org'])->fresh();
}
public function test_read_all_marks_only_the_chosen_apartments_messages(): void
private function receive(string $from, string $subject, string $text, ?string $inReplyTo = null, string $id = '<1@x>'): InboundMessage
{
$otherChannel = Channel::factory()->for(Property::factory())->create();
$own = InboxMessage::factory()->for($this->channel)->create(['property_id' => $this->channel->property_id]);
$general = InboxMessage::factory()->create(['tenant_id' => $this->channel->tenant_id]);
$foreign = InboxMessage::factory()->for($otherChannel)->create(['property_id' => $otherChannel->property_id]);
app(CurrentProperty::class)->select($this->channel->property);
Livewire::test(ListInboxMessages::class)
->assertCanSeeTableRecords([$own, $general])
->assertCanNotSeeTableRecords([$foreign])
->callAction('readAll');
$this->assertTrue($own->fresh()->isRead());
$this->assertTrue($general->fresh()->isRead());
$this->assertFalse($foreign->fresh()->isRead());
return app(ReceiveInboundMessage::class)->handle(new PortalMail($id, $from, $subject, $text, CarbonImmutable::now(), inReplyTo: $inReplyTo))->fresh();
}
}