Testbetrieb: eine Schranke fuer jede ausgehende E-Mail
Deploy FeWo mit PipelineManager / deploy (push) Failing after 3m10s

EnforceMailTestMode prueft jede Mail unmittelbar vor dem Versand (MessageSending,
jeder Mailer): im Testbetrieb gehen An/Cc/Bcc nur an die Testadresse, ohne
Testadresse wird nichts versendet; der Modus wird je Mail frisch gelesen.
Ersetzt alwaysTo am Standard-Mailer. Tests inkl. Pruefung, dass kein Code am
Mailer vorbei versendet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
LarsandClaude Opus 5.5 committed 2026-11-01 21:03:24 +01:00
1 parent ac9d20b812
commit 0c10880e4f
6 files changed
+248 -9

No files matched your search

+174
View File
@@ -0,0 +1,174 @@
<?php
namespace Tests\Feature;
use App\Actions\CreateReservation;
use App\Actions\SendOffer;
use App\Listeners\EnforceMailTestMode;
use App\Models\Property;
use App\Models\Tenant;
use App\Pricing\StayRequest;
use App\Support\Settings;
use App\Tenancy\TenantConfig;
use App\Tenancy\TenantContext;
use Carbon\CarbonImmutable;
use Database\Seeders\PropertySeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Mail\Events\MessageSending;
use Illuminate\Mail\MailManager;
use Illuminate\Mail\SentMessage;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\Finder\Finder;
use Tests\TestCase;
/**
* The test operation is one gate for every outgoing e-mail ({@see EnforceMailTestMode}): whichever mailer or mailable
* sends, in test operation nothing reaches a real recipient – and without a test address nothing is sent at all.
*/
class MailTestModeGateTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
config(['fewo.mail_redirect_to' => 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']);
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 = [
'/(?<![\w>:])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<SentMessage|\Symfony\Component\Mailer\SentMessage>
*/
private function sent(): array
{
return array_values(iterator_to_array(app(MailManager::class)->mailer('array')->getSymfonyTransport()->messages()));
}
/**
* @return list<string>
*/
private function recipients(object $sent): array
{
return array_map(fn ($address) => $address->getAddress(), $sent->getEnvelope()->getRecipients());
}
/**
* @param array<string, mixed> $attributes
*/
private function landlord(array $attributes = []): Tenant
{
$tenant = Tenant::factory()->create($attributes);
app(TenantContext::class)->set($tenant);
app(TenantConfig::class)->refresh();
return $tenant;
}
}