129 lines
5.8 KiB
PHP
129 lines
5.8 KiB
PHP
<?php
|
||
|
||
namespace Tests\Feature;
|
||
|
||
use App\Actions\AcceptOffer;
|
||
use App\Actions\CreateReservation;
|
||
use App\Actions\SendOffer;
|
||
use App\Enums\PortalEmailStatus;
|
||
use App\Enums\PortalEmailType;
|
||
use App\Mail\ArrivalInformation;
|
||
use App\Mail\BookingConfirmed;
|
||
use App\Mail\BookingDeclined;
|
||
use App\Mail\ChannelConflict;
|
||
use App\Mail\ChannelSyncAlert;
|
||
use App\Mail\ChannelTasksNotification;
|
||
use App\Mail\MailboxAlert;
|
||
use App\Mail\MarketingConsentRequest;
|
||
use App\Mail\NewReservation;
|
||
use App\Mail\NewReview;
|
||
use App\Mail\OfferAnswered;
|
||
use App\Mail\OfferMade;
|
||
use App\Mail\PaymentReminder;
|
||
use App\Mail\PortalMailNotification;
|
||
use App\Mail\ReservationExpired;
|
||
use App\Mail\ReservationReceived;
|
||
use App\Mail\ReviewRequest;
|
||
use App\Models\Booking;
|
||
use App\Models\Channel;
|
||
use App\Models\ChannelTask;
|
||
use App\Models\PortalEmail;
|
||
use App\Models\Property;
|
||
use App\Models\Review;
|
||
use App\Pricing\StayRequest;
|
||
use Carbon\CarbonImmutable;
|
||
use Database\Seeders\PropertySeeder;
|
||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||
use Illuminate\Mail\Mailable;
|
||
use Illuminate\Support\Facades\Mail;
|
||
use Illuminate\Support\Facades\Storage;
|
||
use Tests\TestCase;
|
||
|
||
/**
|
||
* Every e-mail of the system is built completely (subject, body, attachments) in the state in which it is
|
||
* really sent – so a broken template is found here and not by a guest. Delivery itself is a server matter.
|
||
*/
|
||
class MailRenderingTest extends TestCase
|
||
{
|
||
use RefreshDatabase;
|
||
|
||
protected function setUp(): void
|
||
{
|
||
parent::setUp();
|
||
|
||
$this->travelTo(CarbonImmutable::parse('2026-10-22 10:00'));
|
||
$this->seed(PropertySeeder::class);
|
||
Mail::fake();
|
||
Storage::fake('local');
|
||
}
|
||
|
||
public function test_every_mail_along_the_booking_path_renders_in_its_state(): void
|
||
{
|
||
$booking = app(CreateReservation::class)->handle(
|
||
Property::sole(),
|
||
new StayRequest(CarbonImmutable::parse('2026-12-01'), CarbonImmutable::parse('2026-12-05'), 2),
|
||
['first_name' => 'Erika', 'last_name' => 'Muster', 'email' => 'erika@example.com', 'street' => 'Seeweg 1', 'postal_code' => '18225', 'city' => 'Kühlungsborn'],
|
||
'Wir kommen mit Hund – ist das in Ordnung?',
|
||
sendMails: false,
|
||
);
|
||
$this->assertRenders(new ReservationReceived($booking), 'vorgemerkt');
|
||
$this->assertRenders(new NewReservation($booking), 'Erika Muster');
|
||
|
||
$send = app(SendOffer::class);
|
||
$booking = $send->handle($booking, ['price_adjustment' => -1500, 'offer_message' => 'Hund ist willkommen.'], sendMail: false);
|
||
$this->assertRenders(new OfferMade($booking, $send->offerUrl($booking)), 'Angebot ansehen und annehmen', attachments: 1);
|
||
$this->assertRenders(new ReservationExpired($booking), 'Angebot abgelaufen');
|
||
$this->assertRenders(new BookingDeclined($booking), 'Erika');
|
||
|
||
app(AcceptOffer::class)->handle($booking, $booking->offer_version);
|
||
$booking->refresh();
|
||
$this->assertRenders(new OfferAnswered($booking, accepted: true), 'Angebot angenommen');
|
||
$this->assertRenders(new OfferAnswered($booking, accepted: false), 'abgelehnt');
|
||
$this->assertRenders(new BookingConfirmed($booking), 'Sie haben unser Angebot', attachments: 1);
|
||
|
||
foreach ([PaymentReminder::DEPOSIT_OVERDUE, PaymentReminder::BALANCE_REQUEST, PaymentReminder::BALANCE_OVERDUE] as $kind) {
|
||
$this->assertRenders(new PaymentReminder($booking, $kind), 'Erika Muster');
|
||
}
|
||
$this->assertRenders(new ArrivalInformation($booking), 'Erika');
|
||
$this->assertRenders(new ReviewRequest($booking), 'Erika');
|
||
}
|
||
|
||
public function test_directly_confirmed_booking_and_other_notifications_render(): void
|
||
{
|
||
$booking = Booking::factory()->for(Property::sole())->create(['confirmed_at' => now(), 'deposit_due_on' => now()->addWeek()->toDateString()]);
|
||
$this->assertRenders(new BookingConfirmed($booking), 'verbindlich zu bestätigen');
|
||
|
||
$channel = Channel::factory()->for(Property::sole())->create(['name' => 'FeWo-direkt']);
|
||
$this->assertRenders(new ChannelSyncAlert($channel), 'FeWo-direkt');
|
||
$this->assertRenders(new ChannelConflict($channel, ['14.11.–17.11.2026 überschneidet Buchung 2026-0001']), 'überschneidet');
|
||
$this->assertRenders(new ChannelTasksNotification([ChannelTask::factory()->for($channel)->create()]), 'FeWo-direkt');
|
||
$this->assertRenders(new MailboxAlert('Login fehlgeschlagen'), 'Login fehlgeschlagen');
|
||
$this->assertRenders(new MarketingConsentRequest($booking->guest), $booking->guest->first_name);
|
||
$this->assertRenders(new NewReview(Review::factory()->create(['booking_id' => $booking->id, 'title' => 'Wunderbar'])), 'Wunderbar');
|
||
$this->assertRenders(new PortalMailNotification(PortalEmail::query()->create([
|
||
'channel_id' => $channel->id, 'message_id' => 'm1@portal', 'from' => 'noreply@portal.example', 'subject' => 'Neue Buchung',
|
||
'received_at' => now(), 'type' => PortalEmailType::Booking, 'status' => PortalEmailStatus::Blocked, 'starts_on' => '2026-11-14', 'ends_on' => '2026-11-17',
|
||
])), 'Neue Buchung');
|
||
}
|
||
|
||
public function test_test_mail_command_sends_and_respects_the_redirect(): void
|
||
{
|
||
config(['fewo.mail_redirect_to' => 'test@example.com']);
|
||
|
||
$this->artisan('mail:test', ['to' => 'gast@example.com'])
|
||
->expectsOutputToContain('Umleitung aktiv')
|
||
->assertSuccessful();
|
||
}
|
||
|
||
private function assertRenders(Mailable $mail, string $expected, int $attachments = 0): void
|
||
{
|
||
$html = $mail->render();
|
||
|
||
$this->assertNotEmpty($mail->envelope()->subject, $mail::class.' needs a subject');
|
||
$this->assertStringContainsString(e($expected), $html, $mail::class);
|
||
if (method_exists($mail, 'attachments')) {
|
||
$this->assertCount($attachments, $mail->attachments(), $mail::class.' attachments');
|
||
}
|
||
}
|
||
}
|