Add ticket status changed notification dispatch
This commit is contained in:
@@ -0,0 +1,209 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Notifications;
|
||||
|
||||
use App\Actions\Notifications\DeliverNotificationAction;
|
||||
use App\Actions\ProjectTicketMetadata\InitializeProjectTicketMetadata;
|
||||
use App\Enums\NotificationChannel;
|
||||
use App\Enums\NotificationEventType;
|
||||
use App\Models\BoardColumn;
|
||||
use App\Models\NotificationAction;
|
||||
use App\Models\Project;
|
||||
use App\Models\Ticket;
|
||||
use App\Models\TicketPriority;
|
||||
use App\Models\TicketStatus;
|
||||
use App\Models\User;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Queue;
|
||||
use Tests\TestCase;
|
||||
|
||||
class TicketStatusChangedNotificationDispatchTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* Ensure status changes queue deliveries only for enabled matching actions in the ticket project.
|
||||
*/
|
||||
public function test_status_changes_queue_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);
|
||||
|
||||
$doingColumn = BoardColumn::factory()->for($project)->create([
|
||||
'name' => 'Doing',
|
||||
'position' => 1,
|
||||
]);
|
||||
$doingStatus = $this->createColumnStatus($doingColumn);
|
||||
$backlogStatus = $this->backlogStatus($project);
|
||||
$this->allowMove($backlogStatus, $doingStatus);
|
||||
|
||||
$ticket = $this->createTicket($project, $backlogStatus, 'Fix webhook dispatch');
|
||||
|
||||
$webhookAction = NotificationAction::factory()
|
||||
->webhook()
|
||||
->for($project)
|
||||
->create([
|
||||
'name' => 'Primary webhook',
|
||||
]);
|
||||
$discordAction = NotificationAction::factory()
|
||||
->discord()
|
||||
->for($project)
|
||||
->create([
|
||||
'name' => 'Discord alerts',
|
||||
]);
|
||||
$disabledAction = NotificationAction::factory()
|
||||
->disabled()
|
||||
->for($project)
|
||||
->create([
|
||||
'name' => 'Disabled webhook',
|
||||
]);
|
||||
$foreignProjectAction = NotificationAction::factory()
|
||||
->for($otherProject)
|
||||
->create([
|
||||
'name' => 'Foreign webhook',
|
||||
]);
|
||||
|
||||
DB::table('notification_actions')->insert([
|
||||
'project_id' => $project->id,
|
||||
'name' => 'Wrong event',
|
||||
'enabled' => true,
|
||||
'event_type' => 'ticket_created',
|
||||
'channel' => NotificationChannel::Webhook->value,
|
||||
'channel_configuration' => json_encode([]),
|
||||
'message_template' => null,
|
||||
'delivery_url' => 'https://hooks.example.test/kanbangit/wrong-event',
|
||||
'created_at' => CarbonImmutable::now(),
|
||||
'updated_at' => CarbonImmutable::now(),
|
||||
]);
|
||||
|
||||
$this->syncProjectPermissions($project, $user, ['edit', 'view']);
|
||||
|
||||
$this->actingAs($user)
|
||||
->patch(route('projects.tickets.status.update', [$project, $ticket]), [
|
||||
'redirect_to' => 'backlog',
|
||||
'ticket_status_id' => $doingStatus->id,
|
||||
])
|
||||
->assertRedirect(route('projects.backlog.index', $project))
|
||||
->assertSessionHas('status', 'CORE-1 moved to Doing.');
|
||||
|
||||
Queue::assertPushed(DeliverNotificationAction::class, 2);
|
||||
Queue::assertPushed(DeliverNotificationAction::class, function (DeliverNotificationAction $job) use ($webhookAction, $project, $ticket, $user, $backlogStatus, $doingStatus): 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 === 'CORE-1'
|
||||
&& $job->payload->ticketTitle === 'Fix webhook dispatch'
|
||||
&& $job->payload->ticketUrl === route('projects.tickets.show', [$project, $ticket])
|
||||
&& $job->payload->actorId === $user->id
|
||||
&& $job->payload->actorName === 'Erica Example'
|
||||
&& $job->payload->fromStatusId === $backlogStatus->id
|
||||
&& $job->payload->fromStatusLabel === 'Backlog'
|
||||
&& $job->payload->toStatusId === $doingStatus->id
|
||||
&& $job->payload->toStatusLabel === 'Doing'
|
||||
&& $job->payload->eventType === NotificationEventType::TicketStatusChanged;
|
||||
});
|
||||
Queue::assertPushed(DeliverNotificationAction::class, fn (DeliverNotificationAction $job): bool => $job->notificationActionId === $discordAction->id);
|
||||
Queue::assertNotPushed(DeliverNotificationAction::class, fn (DeliverNotificationAction $job): bool => $job->notificationActionId === $disabledAction->id);
|
||||
Queue::assertNotPushed(DeliverNotificationAction::class, fn (DeliverNotificationAction $job): bool => $job->notificationActionId === $foreignProjectAction->id);
|
||||
|
||||
$this->assertDatabaseHas('ticket_worklog_entries', [
|
||||
'ticket_id' => $ticket->id,
|
||||
'actor_id' => $user->id,
|
||||
'entry_type' => 'status_changed',
|
||||
'summary' => 'Moved from Backlog to Doing.',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure no-op moves keep their response behavior and do not queue deliveries.
|
||||
*/
|
||||
public function test_no_op_status_moves_do_not_queue_notification_deliveries(): void
|
||||
{
|
||||
Queue::fake();
|
||||
|
||||
$user = User::factory()->create();
|
||||
$project = Project::factory()->create([
|
||||
'key' => 'CORE',
|
||||
]);
|
||||
|
||||
app(InitializeProjectTicketMetadata::class)->handle($project);
|
||||
|
||||
$backlogStatus = $this->backlogStatus($project);
|
||||
$ticket = $this->createTicket($project, $backlogStatus, 'Stay in backlog');
|
||||
|
||||
NotificationAction::factory()
|
||||
->for($project)
|
||||
->create([
|
||||
'name' => 'Backlog webhook',
|
||||
]);
|
||||
|
||||
$this->syncProjectPermissions($project, $user, ['edit', 'view']);
|
||||
|
||||
$this->actingAs($user)
|
||||
->patch(route('projects.tickets.status.update', [$project, $ticket]), [
|
||||
'redirect_to' => 'backlog',
|
||||
'ticket_status_id' => $backlogStatus->id,
|
||||
])
|
||||
->assertRedirect(route('projects.backlog.index', $project))
|
||||
->assertSessionHas('status', 'CORE-1 already belongs to Backlog.');
|
||||
|
||||
Queue::assertNothingPushed();
|
||||
$this->assertDatabaseCount('ticket_worklog_entries', 0);
|
||||
}
|
||||
|
||||
private function backlogStatus(Project $project): TicketStatus
|
||||
{
|
||||
return $project->ticketStatuses()
|
||||
->whereNull('board_column_id')
|
||||
->firstOrFail();
|
||||
}
|
||||
|
||||
private function createColumnStatus(BoardColumn $boardColumn): TicketStatus
|
||||
{
|
||||
return TicketStatus::factory()
|
||||
->for($boardColumn->project)
|
||||
->for($boardColumn)
|
||||
->create([
|
||||
'name' => $boardColumn->name,
|
||||
'position' => 1,
|
||||
]);
|
||||
}
|
||||
|
||||
private function createTicket(Project $project, TicketStatus $ticketStatus, string $title): Ticket
|
||||
{
|
||||
$priority = TicketPriority::factory()->for($project)->create([
|
||||
'name' => 'High',
|
||||
'position' => 2,
|
||||
]);
|
||||
|
||||
return Ticket::factory()->create([
|
||||
'project_id' => $project->id,
|
||||
'sequence_number' => 1,
|
||||
'ticket_key' => 'CORE-1',
|
||||
'ticket_priority_id' => $priority->id,
|
||||
'ticket_status_id' => $ticketStatus->id,
|
||||
'ticket_type_id' => $project->ticketTypes()->firstOrFail()->id,
|
||||
'title' => $title,
|
||||
]);
|
||||
}
|
||||
|
||||
private function allowMove(TicketStatus $sourceStatus, TicketStatus $targetStatus): void
|
||||
{
|
||||
$sourceStatus->allowedTargetStatuses()->syncWithoutDetaching([$targetStatus->id]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user