- 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>
190 lines
9.4 KiB
PHP
190 lines
9.4 KiB
PHP
<?php
|
||
|
||
namespace Tests\Feature;
|
||
|
||
use App\Actions\CreateReservation;
|
||
use App\Actions\SendBookingReminders;
|
||
use App\Documents\DocumentReleases;
|
||
use App\Enums\AdminMode;
|
||
use App\Enums\DocumentKind;
|
||
use App\Filament\Resources\Bookings\Pages\ViewBooking;
|
||
use App\Listeners\EnforceMailTestMode;
|
||
use App\Mail\BookingWishNotice;
|
||
use App\Mail\InformationSent;
|
||
use App\Models\Booking;
|
||
use App\Models\Property;
|
||
use App\Models\User;
|
||
use App\Pricing\StayRequest;
|
||
use App\Support\Content;
|
||
use App\Support\Settings;
|
||
use App\Tenancy\TenantContext;
|
||
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\DB;
|
||
use Illuminate\Support\Facades\Http;
|
||
use Illuminate\Support\Facades\Mail;
|
||
use Illuminate\Support\Facades\Storage;
|
||
use Illuminate\Support\Facades\URL;
|
||
use Livewire\Livewire;
|
||
use Tests\TestCase;
|
||
|
||
/**
|
||
* T23 – test operation against a real, isolated SMTP receiver (tests/Support/smtp-sink.php): every message of packages
|
||
* 7 and 9 that really goes over the wire – automatic, manual, scheduled, queued, with Cc and Bcc – reaches only the test
|
||
* address; the original recipients never appear in the SMTP envelope, and the case history does not call a redirected
|
||
* message a delivery to the guest.
|
||
*/
|
||
class TestOperationSmtpAcceptanceTest extends TestCase
|
||
{
|
||
use RefreshDatabase;
|
||
|
||
private const TEST_ADDRESS = 'testpostfach@example.com';
|
||
|
||
private const GUEST = 'gesa@gast.example';
|
||
|
||
/** @var resource|null */
|
||
private $sink = null;
|
||
|
||
private string $capture;
|
||
|
||
protected function setUp(): void
|
||
{
|
||
parent::setUp();
|
||
|
||
$port = $this->freePort();
|
||
$this->capture = storage_path('framework/testing/smtp-sink-'.getmypid().'.jsonl');
|
||
@mkdir(dirname($this->capture), 0777, true);
|
||
@unlink($this->capture);
|
||
@unlink($this->capture.'.stop');
|
||
$this->sink = proc_open([PHP_BINARY, base_path('tests/Support/smtp-sink.php'), (string) $port, $this->capture, '120'], [], $pipes);
|
||
$this->waitFor(fn (): bool => file_exists($this->capture.'.ready'));
|
||
|
||
config([
|
||
'mail.default' => 'smtp',
|
||
'mail.mailers.smtp' => ['transport' => 'smtp', 'scheme' => 'smtp', 'host' => '127.0.0.1', 'port' => $port, 'username' => null, 'password' => null, 'timeout' => 5, 'local_domain' => 'localhost', 'auto_tls' => false],
|
||
'fewo.mail_redirect_to' => null,
|
||
'queue.default' => 'database',
|
||
]);
|
||
// The real mail manager behind the test-operation gate, not the fake.
|
||
$manager = new MailManager(app());
|
||
app()->instance('mail.manager', $manager);
|
||
Mail::swap($manager);
|
||
|
||
$this->travelTo(CarbonImmutable::parse('2026-10-22 10:00'));
|
||
$this->seed([PropertySeeder::class, SiteSeeder::class]);
|
||
app(TenantContext::class)->set(Property::sole()->tenant);
|
||
Storage::fake('local');
|
||
Http::fake(fn () => Http::response('', 500));
|
||
$this->withoutVite();
|
||
$this->actingAs(User::factory()->create(['name' => 'Ingrid', 'admin_mode' => AdminMode::Simple]));
|
||
}
|
||
|
||
protected function tearDown(): void
|
||
{
|
||
if ($this->sink !== null) {
|
||
touch($this->capture.'.stop');
|
||
$this->waitFor(fn (): bool => ! file_exists($this->capture.'.ready'));
|
||
proc_close($this->sink);
|
||
}
|
||
@unlink($this->capture);
|
||
@unlink($this->capture.'.stop');
|
||
|
||
parent::tearDown();
|
||
}
|
||
|
||
public function test_t23_every_message_goes_only_to_the_test_address_over_real_smtp(): void
|
||
{
|
||
Content::save(['contract.terms' => '<p>Echte Bedingungen</p>', 'stay.cancellation' => '<p>Echtes Storno</p>']);
|
||
DocumentReleases::set(DocumentKind::Information, true, 'Test');
|
||
Settings::tenant()->update(['operating_mode' => Settings::MODE_TEST, 'test_recipient' => self::TEST_ADDRESS, 'notification_emails' => 'vermieterin@example.org',
|
||
'bank_iban' => 'DE02120300000000202051', 'process_defaults' => ['processing' => 'automatic', 'pre_reservation' => false]]);
|
||
app(TenantContext::class)->set(Settings::tenant()->fresh());
|
||
$property = Property::sole();
|
||
|
||
// Automatic: request → information; guest: booking wish → confirmation; scheduled: arrival information.
|
||
$booking = app(CreateReservation::class)->handle($property, new StayRequest(CarbonImmutable::parse('2026-11-07'), CarbonImmutable::parse('2026-11-10'), 2), ['first_name' => 'Gesa', 'last_name' => 'Gast', 'email' => self::GUEST, 'street' => 'Strandweg 1', 'postal_code' => '18225', 'city' => 'Kühlungsborn', 'country' => 'DE']);
|
||
$booking->refresh();
|
||
$this->post(URL::temporarySignedRoute('information.wish', $booking->information_valid_until, ['booking' => $booking, 'v' => $booking->information_version]), ['terms' => '1'])->assertRedirect();
|
||
$this->assertTrue($booking->fresh()->isBinding());
|
||
$this->travelTo(CarbonImmutable::parse('2026-11-04 08:00'));
|
||
app(SendBookingReminders::class)->handle();
|
||
|
||
// Manual: the landlord resends the information of a second request; with Cc and Bcc to outside addresses.
|
||
$second = app(CreateReservation::class)->handle($property, new StayRequest(CarbonImmutable::parse('2026-11-21'), CarbonImmutable::parse('2026-11-24'), 2), ['first_name' => 'Ole', 'last_name' => 'Zweit', 'email' => 'ole@gast.example', 'street' => 'Strandweg 1', 'postal_code' => '18225', 'city' => 'Kühlungsborn', 'country' => 'DE']);
|
||
Livewire::test(ViewBooking::class, ['record' => $second->getRouteKey()])->callAction(TestAction::make('sendInformation')->arguments([]), ['acknowledged' => true])->assertNotified('Auskunft gesendet');
|
||
Mail::to('ole@gast.example')->cc('cc@aussen.example')->bcc('bcc@aussen.example')->send(new BookingWishNotice($second->fresh(), 'pending'));
|
||
|
||
// Queued while the landlord was live – sent after switching back to test operation: the gate decides at sending time.
|
||
Settings::tenant()->update(['operating_mode' => Settings::MODE_LIVE]);
|
||
Mail::to('ole@gast.example')->queue(new InformationSent($second->fresh(), 'https://example.test'));
|
||
$this->assertSame(1, DB::table('jobs')->count(), 'the message waits in the queue');
|
||
Settings::tenant()->update(['operating_mode' => Settings::MODE_TEST]);
|
||
$this->artisan('queue:work', ['--once' => true, '--stop-when-empty' => true]);
|
||
|
||
$messages = $this->captured();
|
||
$this->assertGreaterThanOrEqual(8, count($messages), 'information, notices, wish, confirmation, arrival information, manual and queued mails');
|
||
foreach ($messages as $message) {
|
||
$this->assertSame([self::TEST_ADDRESS], $message['to'], 'SMTP envelope of: '.$message['subject']);
|
||
$this->assertStringNotContainsString('gast.example>', $message['headers_to']);
|
||
}
|
||
$all = implode("\n", array_column($messages, 'envelope'));
|
||
foreach ([self::GUEST, 'ole@gast.example', 'cc@aussen.example', 'bcc@aussen.example', 'vermieterin@example.org'] as $original) {
|
||
$this->assertStringNotContainsString($original, $all, $original.' never in an SMTP envelope');
|
||
}
|
||
$this->assertTrue(collect($messages)->contains(fn (array $message): bool => str_contains($message['data'], EnforceMailTestMode::ORIGINAL_RECIPIENTS_HEADER.': '.self::GUEST)), 'the original recipient is documented in a header');
|
||
$this->assertTrue(collect($messages)->contains(fn (array $message): bool => str_contains($message['subject'], 'Unverbindliche Auskunft')));
|
||
|
||
// The history says where the mails really went.
|
||
$history = Booking::query()->find($booking->id)->events()->pluck('summary')->implode(' | ');
|
||
$this->assertStringContainsString('im Testbetrieb an die Testadresse (nicht an den Gast)', $history);
|
||
$this->assertStringContainsString('Testbetrieb: nur an die Testadresse, nicht an den Gast', $history);
|
||
$this->assertStringNotContainsString('E-Mail an den Gast:', $history);
|
||
}
|
||
|
||
/**
|
||
* @return list<array{to: list<string>, subject: string, headers_to: string, envelope: string, data: string}>
|
||
*/
|
||
private function captured(): array
|
||
{
|
||
$this->waitFor(fn (): bool => file_exists($this->capture));
|
||
$messages = [];
|
||
foreach (file($this->capture, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) ?: [] as $line) {
|
||
$entry = json_decode($line, true);
|
||
$data = (string) $entry['data'];
|
||
$headers = substr($data, 0, (int) strpos($data, "\r\n\r\n"));
|
||
preg_match('/^Subject: (.*)$/mi', $headers, $subject);
|
||
preg_match('/^To: (.*)$/mi', $headers, $to);
|
||
$messages[] = [
|
||
'to' => $entry['to'],
|
||
'subject' => mb_decode_mimeheader(trim($subject[1] ?? '')),
|
||
'headers_to' => $to[1] ?? '',
|
||
'envelope' => $entry['from'].' -> '.implode(',', $entry['to']),
|
||
'data' => $data,
|
||
];
|
||
}
|
||
|
||
return $messages;
|
||
}
|
||
|
||
private function freePort(): int
|
||
{
|
||
$socket = stream_socket_server('tcp://127.0.0.1:0');
|
||
$name = stream_socket_get_name($socket, false);
|
||
fclose($socket);
|
||
|
||
return (int) substr((string) $name, strrpos((string) $name, ':') + 1);
|
||
}
|
||
|
||
private function waitFor(callable $condition): void
|
||
{
|
||
$until = microtime(true) + 10;
|
||
while (! $condition() && microtime(true) < $until) {
|
||
usleep(50000);
|
||
}
|
||
}
|
||
}
|