Teren-app/tests/Feature/ActivityNotificationReadTest.php
2025-11-06 21:54:07 +01:00

134 lines
4.4 KiB
PHP

<?php
use App\Models\Activity;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Inertia\Testing\AssertableInertia as Assert;
uses(RefreshDatabase::class);
it('marks activity as read with patch request', function () {
$user = User::factory()->create();
$this->actingAs($user);
// Create required related models (using the same approach as NotificationsUnreadFilterTest)
$action = \App\Models\Action::factory()->create();
$decision = \App\Models\Decision::factory()->create();
$clientCase = \App\Models\ClientCase::factory()->create();
// Create an activity
$activity = Activity::query()->create([
'due_date' => now()->toDateString(),
'amount' => 100,
'action_id' => $action->id,
'decision_id' => $decision->id,
'client_case_id' => $clientCase->id,
]);
// Ensure no read record exists initially
$this->assertDatabaseMissing('activity_notification_reads', [
'user_id' => $user->id,
'activity_id' => $activity->id,
]);
// Send PATCH request to mark as read
$response = $this->patch(route('notifications.activity.read'), [
'activity_id' => $activity->id,
]);
$response->assertRedirect();
// Verify the read record was created
$this->assertDatabaseHas('activity_notification_reads', [
'user_id' => $user->id,
'activity_id' => $activity->id,
]);
});
it('requires authentication', function () {
// Create required related models
$action = \App\Models\Action::factory()->create();
$decision = \App\Models\Decision::factory()->create();
$clientCase = \App\Models\ClientCase::factory()->create();
$activity = Activity::query()->create([
'due_date' => now()->toDateString(),
'amount' => 100,
'action_id' => $action->id,
'decision_id' => $decision->id,
'client_case_id' => $clientCase->id,
]);
$response = $this->patch(route('notifications.activity.read'), [
'activity_id' => $activity->id,
]);
$response->assertStatus(302); // Redirect to login
});
it('validates activity_id parameter', function () {
$user = User::factory()->create();
$this->actingAs($user);
// Test missing activity_id
$response = $this->patch(route('notifications.activity.read'), []);
$response->assertSessionHasErrors(['activity_id']);
// Test invalid activity_id
$response = $this->patch(route('notifications.activity.read'), [
'activity_id' => 99999, // Non-existent ID
]);
$response->assertSessionHasErrors(['activity_id']);
});
it('excludes read activities from unread notifications page', function () {
$user = User::factory()->create();
$this->actingAs($user);
// Create required related models
$action = \App\Models\Action::factory()->create();
$decision = \App\Models\Decision::factory()->create();
$clientCase = \App\Models\ClientCase::factory()->create();
// Create two activities due today
$activity1 = Activity::query()->create([
'due_date' => now()->toDateString(),
'amount' => 100,
'action_id' => $action->id,
'decision_id' => $decision->id,
'client_case_id' => $clientCase->id,
]);
$activity2 = Activity::query()->create([
'due_date' => now()->toDateString(),
'amount' => 200,
'action_id' => $action->id,
'decision_id' => $decision->id,
'client_case_id' => $clientCase->id,
]);
// Initially, both activities should appear in unread notifications
$response = $this->get(route('notifications.unread'));
$response->assertInertia(function (Assert $page) {
$page->where('activities.total', 2);
});
// Mark first activity as read
$this->patch(route('notifications.activity.read'), ['activity_id' => $activity1->id]);
// Now only one activity should appear in unread notifications
$response = $this->get(route('notifications.unread'));
$response->assertInertia(function (Assert $page) {
$page->where('activities.total', 1);
});
// Mark second activity as read
$this->patch(route('notifications.activity.read'), ['activity_id' => $activity2->id]);
// Now no activities should appear in unread notifications
$response = $this->get(route('notifications.unread'));
$response->assertInertia(function (Assert $page) {
$page->where('activities.total', 0);
});
});