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' => 'Ostsee']);
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, '<b>Ostsee</b>')
&& ! str_contains($html, 'Ostsee');
});
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 $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,
]);
}
}