- 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>
175 lines
7.2 KiB
PHP
175 lines
7.2 KiB
PHP
<?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', '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 = [
|
||
'/(?<![\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;
|
||
}
|
||
}
|