Add ticket create notification event support
This commit is contained in:
@@ -0,0 +1,152 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Notifications;
|
||||
|
||||
use App\Actions\Notifications\DeliverNotificationAction;
|
||||
use App\Actions\ProjectTicketMetadata\InitializeProjectTicketMetadata;
|
||||
use App\Enums\NotificationDeliveryStatus;
|
||||
use App\Enums\NotificationEventType;
|
||||
use App\Models\NotificationAction;
|
||||
use App\Models\Project;
|
||||
use App\Models\TicketStatus;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Queue;
|
||||
use Tests\TestCase;
|
||||
|
||||
class TicketCreatedNotificationDispatchTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* Ensure ticket creation queues deliveries only for enabled matching actions in the ticket project.
|
||||
*/
|
||||
public function test_ticket_creation_queues_delivery_jobs_for_enabled_matching_actions(): void
|
||||
{
|
||||
Queue::fake();
|
||||
|
||||
$user = User::factory()->create([
|
||||
'name' => 'Erica Example',
|
||||
]);
|
||||
$project = Project::factory()->create([
|
||||
'key' => 'CORE',
|
||||
'name' => 'Core Platform',
|
||||
]);
|
||||
$otherProject = Project::factory()->create();
|
||||
|
||||
app(InitializeProjectTicketMetadata::class)->handle($project);
|
||||
app(InitializeProjectTicketMetadata::class)->handle($otherProject);
|
||||
|
||||
$backlogStatus = $this->backlogStatus($project);
|
||||
$ticketType = $project->ticketTypes()
|
||||
->where('is_parent', false)
|
||||
->orderBy('position')
|
||||
->firstOrFail();
|
||||
$priority = $project->ticketPriorities()->firstOrFail();
|
||||
|
||||
$webhookAction = NotificationAction::factory()
|
||||
->webhook()
|
||||
->for($project)
|
||||
->create([
|
||||
'name' => 'Primary webhook',
|
||||
'event_type' => NotificationEventType::TicketCreate,
|
||||
]);
|
||||
$discordAction = NotificationAction::factory()
|
||||
->discord()
|
||||
->for($project)
|
||||
->create([
|
||||
'name' => 'Discord alerts',
|
||||
'event_type' => NotificationEventType::TicketCreate,
|
||||
]);
|
||||
$disabledAction = NotificationAction::factory()
|
||||
->disabled()
|
||||
->for($project)
|
||||
->create([
|
||||
'name' => 'Disabled webhook',
|
||||
'event_type' => NotificationEventType::TicketCreate,
|
||||
]);
|
||||
$statusChangedAction = NotificationAction::factory()
|
||||
->for($project)
|
||||
->create([
|
||||
'name' => 'Status changed webhook',
|
||||
'event_type' => NotificationEventType::TicketStatusChanged,
|
||||
]);
|
||||
$foreignProjectAction = NotificationAction::factory()
|
||||
->for($otherProject)
|
||||
->create([
|
||||
'name' => 'Foreign webhook',
|
||||
'event_type' => NotificationEventType::TicketCreate,
|
||||
]);
|
||||
|
||||
$this->syncProjectPermissions($project, $user, ['edit', 'view']);
|
||||
|
||||
$response = $this->actingAs($user)
|
||||
->post(route('projects.tickets.store', $project), [
|
||||
'acceptance_criteria' => 'Ship it.',
|
||||
'assignee_user_id' => null,
|
||||
'description' => 'Created from notification dispatch test.',
|
||||
'story_points' => 3,
|
||||
'ticket_priority_id' => $priority->id,
|
||||
'ticket_status_id' => $backlogStatus->id,
|
||||
'ticket_type_id' => $ticketType->id,
|
||||
'title' => 'Trigger ticket created notification',
|
||||
]);
|
||||
|
||||
$ticket = $project->tickets()
|
||||
->where('ticket_key', 'CORE-1')
|
||||
->firstOrFail();
|
||||
|
||||
$response
|
||||
->assertRedirect(route('projects.tickets.show', [$project, $ticket]))
|
||||
->assertSessionHas('status', 'CORE-1 was created.');
|
||||
|
||||
Queue::assertPushed(DeliverNotificationAction::class, 2);
|
||||
Queue::assertPushed(DeliverNotificationAction::class, function (DeliverNotificationAction $job) use ($webhookAction, $project, $ticket, $user, $backlogStatus): bool {
|
||||
return $job->notificationActionId === $webhookAction->id
|
||||
&& $job->payload->projectId === $project->id
|
||||
&& $job->payload->projectKey === 'CORE'
|
||||
&& $job->payload->projectName === 'Core Platform'
|
||||
&& $job->payload->ticketId === $ticket->id
|
||||
&& $job->payload->ticketKey === $ticket->ticket_key
|
||||
&& $job->payload->ticketTitle === 'Trigger ticket created notification'
|
||||
&& $job->payload->ticketUrl === route('projects.tickets.show', [$project, $ticket])
|
||||
&& $job->payload->actorId === $user->id
|
||||
&& $job->payload->actorName === 'Erica Example'
|
||||
&& $job->payload->fromStatusId === null
|
||||
&& $job->payload->fromStatusLabel === null
|
||||
&& $job->payload->toStatusId === $backlogStatus->id
|
||||
&& $job->payload->toStatusLabel === 'Backlog'
|
||||
&& $job->payload->eventType === NotificationEventType::TicketCreate
|
||||
&& $job->notificationDeliveryLogId !== null;
|
||||
});
|
||||
Queue::assertPushed(DeliverNotificationAction::class, fn (DeliverNotificationAction $job): bool => $job->notificationActionId === $discordAction->id
|
||||
&& $job->payload->eventType === NotificationEventType::TicketCreate
|
||||
&& $job->notificationDeliveryLogId !== null);
|
||||
Queue::assertNotPushed(DeliverNotificationAction::class, fn (DeliverNotificationAction $job): bool => $job->notificationActionId === $disabledAction->id);
|
||||
Queue::assertNotPushed(DeliverNotificationAction::class, fn (DeliverNotificationAction $job): bool => $job->notificationActionId === $statusChangedAction->id);
|
||||
Queue::assertNotPushed(DeliverNotificationAction::class, fn (DeliverNotificationAction $job): bool => $job->notificationActionId === $foreignProjectAction->id);
|
||||
|
||||
$this->assertDatabaseCount('notification_delivery_logs', 2);
|
||||
$this->assertDatabaseHas('notification_delivery_logs', [
|
||||
'notification_action_id' => $webhookAction->id,
|
||||
'delivery_status' => NotificationDeliveryStatus::Pending->value,
|
||||
'event_type' => NotificationEventType::TicketCreate->value,
|
||||
'attempt_count' => 0,
|
||||
'http_status' => null,
|
||||
]);
|
||||
$this->assertDatabaseHas('notification_delivery_logs', [
|
||||
'notification_action_id' => $discordAction->id,
|
||||
'delivery_status' => NotificationDeliveryStatus::Pending->value,
|
||||
'event_type' => NotificationEventType::TicketCreate->value,
|
||||
'attempt_count' => 0,
|
||||
'http_status' => null,
|
||||
]);
|
||||
}
|
||||
|
||||
private function backlogStatus(Project $project): TicketStatus
|
||||
{
|
||||
return $project->ticketStatuses()
|
||||
->whereNull('board_column_id')
|
||||
->firstOrFail();
|
||||
}
|
||||
}
|
||||
@@ -47,6 +47,7 @@ class ProjectNotificationSettingsControllerTest extends TestCase
|
||||
->where('notificationActions.0.updated_at', $notificationAction->updated_at?->toISOString())
|
||||
->where('channelOptions.0.value', NotificationChannel::Discord->value)
|
||||
->where('eventTypeOptions.0.value', NotificationEventType::TicketStatusChanged->value)
|
||||
->where('eventTypeOptions.1.value', NotificationEventType::TicketCreate->value)
|
||||
->where('templateVariables.0.value', '$url')
|
||||
->where('templateVariables.6.value', '$actor_name')
|
||||
->missing('notificationActions.0.delivery_url'),
|
||||
|
||||
Reference in New Issue
Block a user