Add notification action foundation

This commit is contained in:
Eric Peters
2026-04-20 21:59:57 +02:00
parent 8df9622f1b
commit e4762f8e32
22 changed files with 1047 additions and 0 deletions
@@ -0,0 +1,65 @@
<?php
namespace Tests\Feature\Models;
use App\Enums\NotificationChannel;
use App\Enums\NotificationEventType;
use App\Http\Resources\NotificationActionResource;
use App\Models\NotificationAction;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class NotificationActionTest extends TestCase
{
use RefreshDatabase;
/**
* Ensure webhook notification actions encrypt their delivery URL and cast enum fields.
*/
public function test_factory_creates_an_encrypted_webhook_notification_action(): void
{
$notificationAction = NotificationAction::factory()->webhook()->create([
'delivery_url' => 'https://hooks.example.test/kanbangit/core-status',
]);
$storedDeliveryUrl = DB::table('notification_actions')
->where('id', $notificationAction->id)
->value('delivery_url');
$this->assertNotSame('https://hooks.example.test/kanbangit/core-status', $storedDeliveryUrl);
$this->assertSame('https://hooks.example.test/kanbangit/core-status', $notificationAction->fresh()->delivery_url);
$this->assertSame(NotificationEventType::TicketStatusChanged, $notificationAction->fresh()->event_type);
$this->assertSame(NotificationChannel::Webhook, $notificationAction->fresh()->channel);
$this->assertSame([], $notificationAction->fresh()->channel_configuration);
}
/**
* Ensure the Discord factory state creates message-driven Discord actions.
*/
public function test_factory_can_create_discord_notification_actions(): void
{
$notificationAction = NotificationAction::factory()->discord()->create();
$this->assertSame(NotificationChannel::Discord, $notificationAction->fresh()->channel);
$this->assertNotNull($notificationAction->fresh()->message_template);
$this->assertStringStartsWith('https://discord.com/api/webhooks/', $notificationAction->fresh()->delivery_url);
}
/**
* Ensure notification actions belong to a project and keep secret URLs out of serialized output.
*/
public function test_notification_actions_belong_to_projects_and_hide_delivery_urls(): void
{
$notificationAction = NotificationAction::factory()->create();
$resource = (new NotificationActionResource($notificationAction->fresh()))
->toArray(Request::create('/'));
$this->assertTrue($notificationAction->project()->exists());
$this->assertTrue($notificationAction->project->is($notificationAction->fresh()->project));
$this->assertArrayNotHasKey('delivery_url', $notificationAction->fresh()->toArray());
$this->assertArrayNotHasKey('delivery_url', $resource);
$this->assertTrue($resource['has_delivery_url']);
}
}
@@ -0,0 +1,66 @@
<?php
namespace Tests\Feature\Models;
use App\Enums\NotificationChannel;
use App\Enums\NotificationDeliveryStatus;
use App\Models\NotificationDeliveryLog;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class NotificationDeliveryLogTest extends TestCase
{
use RefreshDatabase;
/**
* Ensure delivery log factories create related notification actions with consistent casts.
*/
public function test_delivery_log_factory_creates_a_related_notification_action(): void
{
$deliveryLog = NotificationDeliveryLog::factory()->create();
$this->assertTrue($deliveryLog->notificationAction()->exists());
$this->assertTrue($deliveryLog->notificationAction->is($deliveryLog->fresh()->notificationAction));
$this->assertSame(NotificationChannel::Webhook, $deliveryLog->fresh()->channel);
$this->assertSame(NotificationDeliveryStatus::Pending, $deliveryLog->fresh()->delivery_status);
}
/**
* Ensure delivery logs can represent every supported delivery state.
*/
public function test_delivery_logs_support_pending_succeeded_failed_and_retryable_statuses(): void
{
NotificationDeliveryLog::factory()->pending()->create();
NotificationDeliveryLog::factory()->succeeded()->create();
NotificationDeliveryLog::factory()->failed()->create();
NotificationDeliveryLog::factory()->retryable()->create();
$this->assertSame(
[
NotificationDeliveryStatus::Pending,
NotificationDeliveryStatus::Succeeded,
NotificationDeliveryStatus::Failed,
NotificationDeliveryStatus::Retryable,
],
NotificationDeliveryLog::query()->orderBy('id')->get()->pluck('delivery_status')->all(),
);
}
/**
* Ensure delivery logs cast metadata and attempt information for later delivery tracking.
*/
public function test_delivery_logs_cast_metadata_and_attempt_information(): void
{
$deliveryLog = NotificationDeliveryLog::factory()->succeeded()->create([
'payload_metadata' => [
'project_key' => 'CORE',
'ticket_key' => 'CORE-27',
],
])->fresh();
$this->assertSame('CORE-27', $deliveryLog->payload_metadata['ticket_key']);
$this->assertSame(204, $deliveryLog->http_status);
$this->assertNotNull($deliveryLog->last_attempted_at);
$this->assertNotNull($deliveryLog->delivered_at);
}
}