Files
WMS/Modules/Helpdesk/Tests/Feature/WorkflowNotificationTest.php
T
2026-06-12 15:11:51 +01:00

101 lines
3.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Modules\Helpdesk\Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Mail;
use Modules\Helpdesk\Models\WorkflowTemplate;
use Modules\Helpdesk\Services\WorkflowNotificationService;
use Modules\Helpdesk\Tests\Concerns\BuildsHelpdeskFixtures;
use Tests\TestCase;
final class WorkflowNotificationTest extends TestCase
{
use BuildsHelpdeskFixtures;
use RefreshDatabase;
public function test_workflow_dispatch_sends_active_template_and_logs_it(): void
{
Mail::fake();
$fixtures = $this->seedLookupData();
$owner = $this->user(['name' => 'Requester Name', 'email' => 'requester@example.com']);
$agent = $this->agent(['name' => 'Agent Name', 'email' => 'agent@example.com']);
$ticket = $this->ticket([
'owner' => $owner,
'agent' => $agent,
'status_id' => $fixtures['open']->id,
'priority_id' => $fixtures['high']->id,
'category_id' => $fixtures['category']->id,
'project_id' => $fixtures['project']->id,
'team_id' => $fixtures['team']->id,
'subject' => 'Workflow ticket subject',
]);
WorkflowTemplate::query()->create([
'name' => 'Ticket created requester email',
'event' => 'ticket.created',
'subject' => 'Ticket {{ ticket.reference }} raised for {{ requester.name }}',
'html_body' => '<p>{{ ticket.subject }} / {{ ticket.priority }} / {{ ticket.status }}</p>',
'text_body' => '{{ ticket.subject }}',
'to_role' => 'requester',
'is_active' => true,
]);
app(WorkflowNotificationService::class)->dispatch($ticket, 'ticket.created', $agent);
$this->assertDatabaseHas('helpdesk_workflow_logs', [
'ticket_id' => $ticket->id,
'event' => 'ticket.created',
'recipient' => 'requester@example.com',
'status' => 'sent',
'subject' => 'Ticket '.$ticket->reference.' raised for Requester Name',
]);
}
public function test_inactive_workflow_template_is_not_dispatched(): void
{
Mail::fake();
$ticket = $this->ticket();
WorkflowTemplate::query()->create([
'name' => 'Inactive template',
'event' => 'ticket.updated',
'subject' => 'Inactive',
'html_body' => '<p>Inactive</p>',
'text_body' => 'Inactive',
'to_role' => 'requester',
'is_active' => false,
]);
app(WorkflowNotificationService::class)->dispatch($ticket, 'ticket.updated', $ticket->owner);
$this->assertDatabaseCount('helpdesk_workflow_logs', 0);
}
public function test_workflow_logs_skipped_when_recipient_is_missing(): void
{
$ticket = $this->ticket(['agent_id' => null]);
WorkflowTemplate::query()->create([
'name' => 'Agent template',
'event' => 'ticket.updated',
'subject' => 'Agent update',
'html_body' => '<p>Agent update</p>',
'text_body' => 'Agent update',
'to_role' => 'agent',
'is_active' => true,
]);
app(WorkflowNotificationService::class)->dispatch($ticket, 'ticket.updated', $ticket->owner);
$this->assertDatabaseHas('helpdesk_workflow_logs', [
'ticket_id' => $ticket->id,
'event' => 'ticket.updated',
'recipient' => null,
'status' => 'skipped',
]);
}
}