Files
servicedesk/src/tests/Feature/EmailNotificationsAdminTest.php
2026-07-21 23:39:19 +02:00

81 lines
3.6 KiB
PHP

<?php
use App\Livewire\Admin\Panel;
use App\Models\EmailTemplate;
use App\Models\NotificationSetting;
use App\Notifications\TicketNotification;
use App\Services\TicketService;
use Illuminate\Support\Facades\Notification;
use Livewire\Livewire;
test('admin can toggle a notification on/off but cannot add, delete, or reassign templates', function () {
$admin = adminUser();
$statusChanged = NotificationSetting::query()->where('trigger_key', 'status_changed')->firstOrFail();
Livewire::actingAs($admin)->test(Panel::class)
->call('setTab', 'templates')
->call('toggleNotificationEnabled', $statusChanged->id)
->assertOk();
expect($statusChanged->fresh()->enabled)->toBeFalse();
expect(method_exists(Panel::class, 'openEmailTemplateForm'))->toBeFalse()
->and(method_exists(Panel::class, 'submitEmailTemplateForm'))->toBeFalse()
->and(method_exists(Panel::class, 'removeEmailTemplate'))->toBeFalse()
->and(method_exists(Panel::class, 'setNotificationTemplate'))->toBeFalse();
});
test('admin can edit the subject and body of a trigger\'s fixed template', function () {
$admin = adminUser();
// A bare migrated (unseeded) database has no email_templates rows yet, so
// ticket_created's binding is still null at this point — give it one.
$template = EmailTemplate::query()->create(['key' => 'tpl-created-test', 'name' => 'Nowe', 'trigger_label' => 'x', 'subject' => 'S', 'body' => 'B']);
$ticketCreated = NotificationSetting::query()->where('trigger_key', 'ticket_created')->firstOrFail();
$ticketCreated->update(['email_template_id' => $template->id]);
Livewire::actingAs($admin)->test(Panel::class)
->call('setTab', 'templates')
->call('editEmailTemplate', $ticketCreated->email_template_id)
->call('setTemplateSubject', 'Nowy temat #{numer}')
->call('setTemplateBody', 'Nowa treść {imie}')
->call('closeEmailTemplateEditor')
->assertSet('editingTemplateId', null)
->assertOk();
$template = EmailTemplate::query()->find($ticketCreated->email_template_id);
expect($template->subject)->toBe('Nowy temat #{numer}')
->and($template->body)->toBe('Nowa treść {imie}');
});
test('a disabled trigger sends no notification, an enabled one sends the assigned template with a working ticket link', function () {
Notification::fake();
seedStatusesAndPriorities();
$template = EmailTemplate::query()->create([
'key' => 'tpl-new-test', 'name' => 'Nowe', 'trigger_label' => 'x',
'subject' => 'Zgloszenie #{numer}', 'body' => 'Podglad: {link}',
]);
NotificationSetting::query()->where('trigger_key', 'ticket_created')->update(['email_template_id' => $template->id]);
NotificationSetting::query()->where('trigger_key', 'status_changed')->update(['enabled' => false]);
$ticket = app(TicketService::class)->create([
'email' => 'client-notify@example.com',
'subject' => 'Problem testowy',
'body' => 'Opis problemu.',
], null);
Notification::assertSentOnDemand(
TicketNotification::class,
fn ($notification, $channels, $notifiable) => $notifiable->routes['mail'] === 'client-notify@example.com'
);
app(TicketService::class)->setStatus($ticket, 'in_progress');
Notification::assertSentOnDemandTimes(TicketNotification::class, 1);
$mail = (new TicketNotification($ticket->fresh(), $template->id))->toMail((object) ['routes' => ['mail' => $ticket->email]]);
expect($mail->subject)->toBe('Zgloszenie #'.$ticket->number)
->and($mail->viewData['html'])->toContain(route('client.ticket', $ticket));
});