trypost/tests/Feature/NotificationControllerTest.php
Paulo Castellano 74c6442728 refactor: code review fixes — policies, enums, data_get, tests
- Refactor WorkspacePolicy to use pivot role instead of workspace.user_id
- Add manageBilling policy (owner only) to BillingController
- Fix ApiKeyController authorization (view → manageTeam for store/destroy)
- Fix WorkspaceInviteController using workspace.user_id for owner checks
- Fix WorkspaceController settings is_owner using workspace.user_id
- Create PostAction enum for UpdatePost/PostController action strings
- Create ApiToken\Status enum
- Add User::SUBSCRIPTION_NAME constant, replace all hardcoded 'default'
- Convert wantsEmailFor to accept NotificationType enum
- Convert all $data[] to data_get() across publishers, controllers, jobs
- Fix SocialLoginController callback missing try/catch
- Fix SocialController::toggleActive missing workspace null check
- Fix UpdatePost NPE on meta merge when postPlatform not found
- Remove HTML5 required attributes from form inputs
- Convert function declarations to arrow functions in Vue components
- Replace hardcoded URLs with Wayfinder route helpers
- Replace new Date() with dayjs
- Add 16 new test files covering policies, authorization, publishing
2026-03-31 00:40:18 -03:00

133 lines
4.1 KiB
PHP

<?php
declare(strict_types=1);
use App\Enums\User\Setup;
use App\Enums\UserWorkspace\Role;
use App\Models\Notification;
use App\Models\User;
use App\Models\Workspace;
beforeEach(function () {
$this->user = User::factory()->create(['setup' => Setup::Completed]);
$this->workspace = Workspace::factory()->create(['user_id' => $this->user->id]);
$this->workspace->members()->attach($this->user->id, ['role' => Role::Owner->value]);
$this->user->update(['current_workspace_id' => $this->workspace->id]);
});
test('notifications index requires authentication', function () {
$response = $this->getJson(route('app.notifications.index'));
$response->assertUnauthorized();
});
test('notifications index returns notifications and unread count', function () {
Notification::factory()->count(3)->create([
'user_id' => $this->user->id,
'workspace_id' => $this->workspace->id,
]);
Notification::factory()->read()->create([
'user_id' => $this->user->id,
'workspace_id' => $this->workspace->id,
]);
$response = $this->actingAs($this->user)->getJson(route('app.notifications.index'));
$response->assertOk();
$response->assertJsonCount(4, 'notifications');
$response->assertJsonPath('unread_count', 3);
});
test('notifications index excludes archived notifications', function () {
Notification::factory()->create([
'user_id' => $this->user->id,
'workspace_id' => $this->workspace->id,
]);
Notification::factory()->create([
'user_id' => $this->user->id,
'workspace_id' => $this->workspace->id,
'archived_at' => now(),
]);
$response = $this->actingAs($this->user)->getJson(route('app.notifications.index'));
$response->assertOk();
$response->assertJsonCount(1, 'notifications');
});
test('notifications index only shows current workspace notifications', function () {
Notification::factory()->create([
'user_id' => $this->user->id,
'workspace_id' => $this->workspace->id,
]);
$otherWorkspace = Workspace::factory()->create(['user_id' => $this->user->id]);
Notification::factory()->create([
'user_id' => $this->user->id,
'workspace_id' => $otherWorkspace->id,
]);
$response = $this->actingAs($this->user)->getJson(route('app.notifications.index'));
$response->assertOk();
$response->assertJsonCount(1, 'notifications');
});
test('mark notification as read', function () {
$notification = Notification::factory()->create([
'user_id' => $this->user->id,
'workspace_id' => $this->workspace->id,
]);
$response = $this->actingAs($this->user)->putJson(route('app.notifications.read', $notification));
$response->assertOk();
expect($notification->fresh()->read_at)->not->toBeNull();
});
test('cannot mark another users notification as read', function () {
$otherUser = User::factory()->create();
$notification = Notification::factory()->create([
'user_id' => $otherUser->id,
'workspace_id' => $this->workspace->id,
]);
$response = $this->actingAs($this->user)->putJson(route('app.notifications.read', $notification));
$response->assertForbidden();
});
test('mark all as read', function () {
Notification::factory()->count(3)->create([
'user_id' => $this->user->id,
'workspace_id' => $this->workspace->id,
]);
$response = $this->actingAs($this->user)->postJson(route('app.notifications.read-all'));
$response->assertOk();
$unread = Notification::where('user_id', $this->user->id)
->whereNull('read_at')
->count();
expect($unread)->toBe(0);
});
test('archive all notifications', function () {
Notification::factory()->count(3)->create([
'user_id' => $this->user->id,
'workspace_id' => $this->workspace->id,
]);
$response = $this->actingAs($this->user)->postJson(route('app.notifications.archive-all'));
$response->assertOk();
$active = Notification::where('user_id', $this->user->id)
->whereNull('archived_at')
->count();
expect($active)->toBe(0);
});