Files
fewo-stommer/tests/Feature/DigestTest.php
T
LarsandClaude Sonnet 5.5 a73b164ea1
Deploy FeWo mit PipelineManager / deploy (push) Successful in 3m36s
Zahlungsaufgaben, Freigabe-/Storno-Link, Vertragsvorlagen, Anreise-Mail
- Zahlungen werden nicht mehr automatisch angemahnt: faellige Anzahlung/Restzahlung werden Aufgaben
  (Zahlungen & Wuensche, Tages-/Wochenuebersicht); Mail nur per Klick; je Portal und Buchungstyp abschaltbar
- Freigabe-Link in der Eingangsbestaetigung, Storno-Link in der Buchungsbestaetigung; je Wohnung
  Wunsch / sofort / sofort bis Frist; Stornowuensche und Kosten bleiben Aufgabe des Gastgebers
- Vertragsvorlagen mit Versionen (je Angebot, Portal oder Wohnung waehlbar), benutzte Versionen gesperrt;
  Angebot und finaler Vertrag als unveraenderliche PDF mit SHA-256, Vertrag wird nie neu geschrieben
- Anreise-Mail je Wohnung einstellbar (Standard 3 Tage), neue Textfelder Schluessel/Parken/Ankunftstag

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-30 12:46:29 +01:00

161 lines
6.8 KiB
PHP

<?php
namespace Tests\Feature;
use App\Actions\SendDigests;
use App\Enums\BookingStatus;
use App\Enums\BookingTaskKind;
use App\Enums\BookingTaskStatus;
use App\Enums\DigestSection;
use App\Filament\Pages\Operations;
use App\Mail\Digest;
use App\Models\Booking;
use App\Models\BookingTask;
use App\Models\Guest;
use App\Models\InboxMessage;
use App\Models\Property;
use App\Models\Tenant;
use App\Models\User;
use App\Tenancy\TenantConfig;
use App\Tenancy\TenantContext;
use Carbon\CarbonImmutable;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Mail;
use Livewire\Livewire;
use Tests\TestCase;
class DigestTest extends TestCase
{
use RefreshDatabase;
private Tenant $tenant;
private Property $property;
protected function setUp(): void
{
parent::setUp();
config(['fewo.mail_redirect_to' => null]);
$this->travelTo(CarbonImmutable::parse('2026-11-02 06:30'));
$this->tenant = Tenant::factory()->create(['notification_emails' => 'vermieterin@example.com']);
app(TenantContext::class)->set($this->tenant);
app(TenantConfig::class)->refresh();
$this->property = Property::factory()->create();
Mail::fake();
}
public function test_daily_summary_goes_out_once_from_the_chosen_hour(): void
{
$this->openRequest();
$digests = app(SendDigests::class);
$this->assertNull($digests->handle($this->tenant));
$this->travelTo(CarbonImmutable::parse('2026-11-02 07:00'));
$this->assertSame('daily', $digests->handle($this->tenant->fresh()));
$this->assertNull($digests->handle($this->tenant->fresh()));
Mail::assertSent(Digest::class, 1);
Mail::assertSent(Digest::class, fn (Digest $mail): bool => $mail->hasTo('vermieterin@example.com') && ! $mail->weekly
&& str_starts_with($mail->envelope()->subject, 'Tagesübersicht 02.11.2026: 2 Punkte'));
}
public function test_on_the_weekly_day_only_the_weekly_summary_goes_out(): void
{
$this->openRequest();
$this->tenant->update(['digest_weekly' => true, 'digest_weekday' => 1, 'digest_hour' => 6]);
$this->assertSame('weekly', app(SendDigests::class)->handle($this->tenant));
$this->assertNull(app(SendDigests::class)->handle($this->tenant->fresh()));
Mail::assertSent(Digest::class, fn (Digest $mail): bool => $mail->weekly && str_starts_with($mail->envelope()->subject, 'Wochenübersicht KW 45'));
Mail::assertSent(Digest::class, 1);
$this->travel(1)->days();
$this->assertSame('daily', app(SendDigests::class)->handle($this->tenant->fresh()));
}
public function test_nothing_to_report_sends_nothing_unless_wanted(): void
{
$this->tenant->update(['digest_hour' => 6]);
$this->assertSame('daily', app(SendDigests::class)->handle($this->tenant));
Mail::assertNothingSent();
$this->tenant->update(['digest_when_empty' => true, 'digest_daily_sent_on' => null]);
app(SendDigests::class)->handle($this->tenant->fresh());
Mail::assertSent(Digest::class, fn (Digest $mail): bool => $mail->sections === [] && str_ends_with($mail->envelope()->subject, 'nichts Neues'));
}
public function test_summary_lists_requests_stays_payments_and_inbox_and_only_the_chosen_sections(): void
{
$this->openRequest();
$this->booking(['arrival' => '2026-11-03', 'departure' => '2026-11-07'], 'Erika Mustermann');
$late = $this->booking(['arrival' => '2026-12-01', 'departure' => '2026-12-05', 'balance_due_on' => '2026-10-20', 'created_at' => now()->subWeek()], 'Hans Säumig');
BookingTask::query()->create(['booking_id' => $late->id, 'kind' => BookingTaskKind::BalanceReminder, 'status' => BookingTaskStatus::Open, 'due_on' => '2026-10-20']);
InboxMessage::factory()->create(['subject' => 'Mögliche Doppelbuchung über FeWo-direkt', 'body' => '<b>Ostsee</b>']);
app(SendDigests::class)->send($this->tenant, weekly: false);
Mail::assertSent(Digest::class, function (Digest $mail): bool {
$html = $mail->render();
$titles = array_column($mail->sections, 'title');
return in_array('Offene Anfragen und Angebote', $titles, true)
&& in_array('An- und Abreisen heute und morgen', $titles, true)
&& in_array('Zahlungsaufgaben und Gästewünsche', $titles, true)
&& str_contains($html, 'Morgen: Anreise')
&& str_contains($html, 'Erika Mustermann')
&& str_contains($html, 'Hans Säumig')
&& str_contains($html, 'Frist bis')
&& str_contains($html, '&lt;b&gt;Ostsee&lt;/b&gt;')
&& ! str_contains($html, '<b>Ostsee</b>');
});
Mail::fake();
$this->tenant->update(['digest_sections' => [DigestSection::Inbox->value]]);
app(SendDigests::class)->send($this->tenant->fresh(), weekly: false);
Mail::assertSent(Digest::class, fn (Digest $mail): bool => array_column($mail->sections, 'section') === [DigestSection::Inbox]);
}
public function test_settings_are_saved_on_the_operations_page_and_a_summary_can_be_sent_right_away(): void
{
$this->actingAs(User::factory()->create());
Livewire::test(Operations::class)
->assertSet('data.reminder_interval_days', 0)
->assertSet('data.digest_daily', true)
->set('data.reminder_interval_days', 3)
->set('data.digest_weekly', true)
->set('data.digest_weekday', 5)
->set('data.digest_hour', 18)
->set('data.digest_sections', [DigestSection::Inbox->value, DigestSection::Portals->value])
->call('save')
->assertHasNoErrors()
->callAction('sendDigest', ['weekly' => 1]);
$tenant = $this->tenant->fresh();
$this->assertSame([3, true, 5, 18], [$tenant->reminder_interval_days, $tenant->digest_weekly, $tenant->digest_weekday, $tenant->digest_hour]);
$this->assertSame(['inbox', 'portals'], $tenant->digest_sections);
Mail::assertSent(Digest::class, fn (Digest $mail): bool => $mail->weekly);
}
private function openRequest(): Booking
{
return Booking::factory()->for($this->property)->reserved()->create(['guest_id' => Guest::factory()->create(['first_name' => 'Anna', 'last_name' => 'Anfrage'])->id]);
}
/**
* @param array<string, mixed> $attributes
*/
private function booking(array $attributes, string $name): Booking
{
[$first, $last] = explode(' ', $name);
return Booking::factory()->for($this->property)->create([
'status' => BookingStatus::Confirmed,
'guest_id' => Guest::factory()->create(['first_name' => $first, 'last_name' => $last])->id,
...$attributes,
]);
}
}