null, 'mail.default' => 'array']); } public function test_the_gate_is_registered_for_every_outgoing_message(): void { $listeners = json_encode(app('events')->getRawListeners()[MessageSending::class] ?? []); $this->assertStringContainsString(addslashes(EnforceMailTestMode::class), (string) $listeners); } public function test_in_test_operation_to_cc_and_bcc_all_go_to_the_test_recipient_only(): void { $this->landlord(['operating_mode' => Settings::MODE_TEST, 'test_recipient' => 'test@example.com']); Mail::raw('Hallo', fn ($message) => $message->to('gast@example.com')->cc('vermieterin@example.com')->bcc('archiv@example.com')->subject('Angebot')); $sent = $this->sent(); $this->assertCount(1, $sent); $this->assertSame(['test@example.com'], $this->recipients($sent[0])); $this->assertStringContainsString('gast@example.com', $sent[0]->getOriginalMessage()->getHeaders()->get(EnforceMailTestMode::ORIGINAL_RECIPIENTS_HEADER)?->getBodyAsString()); } public function test_any_mailer_is_gated_not_only_the_default_one(): void { $this->landlord(['operating_mode' => Settings::MODE_TEST, 'test_recipient' => 'test@example.com']); config(['mail.default' => 'log']); app(MailManager::class)->mailer('array')->raw('Hallo', fn ($message) => $message->to('gast@example.com')->subject('Direkt')); $this->assertSame(['test@example.com'], $this->recipients($this->sent()[0])); } public function test_test_operation_without_any_address_sends_nothing_instead_of_the_real_recipient(): void { config(['fewo.notification_email' => null]); $this->landlord(['operating_mode' => Settings::MODE_TEST, 'test_recipient' => null, 'notification_emails' => null]); Mail::raw('Hallo', fn ($message) => $message->to('gast@example.com')->subject('Angebot')); $this->assertSame([], $this->sent()); } public function test_a_mode_switched_in_the_database_applies_to_the_next_mail_at_once(): void { $tenant = $this->landlord(['operating_mode' => Settings::MODE_LIVE, 'test_recipient' => 'test@example.com']); // Switched back to test operation elsewhere (another process); this process was not told. DB::table('tenants')->where('id', $tenant->id)->update(['operating_mode' => Settings::MODE_TEST]); Mail::raw('Hallo', fn ($message) => $message->to('gast@example.com')->subject('Angebot')); $this->assertSame(['test@example.com'], $this->recipients($this->sent()[0])); } public function test_only_a_landlord_switched_live_reaches_real_recipients_and_the_server_override_always_wins(): void { $this->landlord(['operating_mode' => Settings::MODE_LIVE]); Mail::raw('Hallo', fn ($message) => $message->to('gast@example.com')->subject('Angebot')); $this->assertSame(['gast@example.com'], $this->recipients($this->sent()[0])); config(['fewo.mail_redirect_to' => 'server@example.com']); Mail::raw('Hallo', fn ($message) => $message->to('gast@example.com')->cc('x@example.com')->subject('Angebot')); $this->assertSame(['server@example.com'], $this->recipients($this->sent()[1])); } public function test_real_guest_mails_of_the_booking_process_go_to_the_test_recipient(): void { $this->travelTo(CarbonImmutable::parse('2026-10-22 10:00')); $this->seed(PropertySeeder::class); Storage::fake('local'); $property = Property::sole(); $property->tenant->update(['operating_mode' => Settings::MODE_TEST, 'test_recipient' => 'test@example.com']); app(TenantContext::class)->set($property->tenant->fresh()); $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' => 'gesa@gast.example', 'street' => 'Strandweg 1', 'postal_code' => '18225', 'city' => 'Kühlungsborn', 'country' => 'DE']); app(SendOffer::class)->handle($booking); $sent = $this->sent(); $this->assertNotEmpty($sent); foreach ($sent as $message) { $this->assertSame(['test@example.com'], $this->recipients($message)); } } /** * Nothing in the application may send around the gate: no direct transports, no PHP mail(), no own SMTP. */ public function test_no_code_sends_mail_around_the_gate(): void { $forbidden = [ '/(?:])mail\s*\(/' => 'PHP mail()', '/Symfony\\\\Component\\\\Mailer\\\\(Transport|Mailer)\b/' => 'Symfony-Transport direkt', '/getSymfonyTransport\s*\(/' => 'Transport direkt', '/fsockopen|stream_socket_client/' => 'eigene Socket-Verbindung', '/PHPMailer|SwiftMailer|Swift_/' => 'fremde Mail-Bibliothek', '/sendmail/' => 'sendmail', ]; $violations = []; foreach ((new Finder)->files()->in([app_path(), base_path('routes'), base_path('config')])->name('*.php') as $file) { foreach ($forbidden as $pattern => $what) { if (preg_match($pattern, $file->getContents()) === 1 && ! str_ends_with($file->getRealPath(), 'config'.DIRECTORY_SEPARATOR.'mail.php')) { $violations[] = $file->getRelativePathname().': '.$what; } } } $this->assertSame([], $violations); } /** * @return list */ private function sent(): array { return array_values(iterator_to_array(app(MailManager::class)->mailer('array')->getSymfonyTransport()->messages())); } /** * @return list */ private function recipients(object $sent): array { return array_map(fn ($address) => $address->getAddress(), $sent->getEnvelope()->getRecipients()); } /** * @param array $attributes */ private function landlord(array $attributes = []): Tenant { $tenant = Tenant::factory()->create($attributes); app(TenantContext::class)->set($tenant); app(TenantConfig::class)->refresh(); return $tenant; } }