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' => '

Echte Bedingungen

', 'stay.cancellation' => '

Echtes Storno

']); 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]); $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']); 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, 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); } } }