feat: add notifications module with bell UI, event listeners, and cleanup job

In-app notification system with topbar bell icon, dropdown panel,
mark-as-read/dismiss actions, and scheduled cleanup of old read
notifications. Includes event listeners for user.created and
user.deleted events, authorization policy, and full test coverage.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-19 22:09:44 +01:00
parent 1f0b1caf54
commit fb6e30a062
27 changed files with 1632 additions and 1 deletions

View File

@@ -0,0 +1,145 @@
<?php
namespace MintyPHP\Tests\Module\Notifications\Listeners;
use MintyPHP\Module\Notifications\Listeners\UserCreatedNotificationListener;
use MintyPHP\Module\Notifications\Service\NotificationService;
use MintyPHP\Repository\Tenant\UserTenantRepositoryInterface;
use MintyPHP\Repository\User\UserReadRepositoryInterface;
use MintyPHP\Service\Access\PermissionService;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
class UserCreatedNotificationListenerTest extends TestCase
{
private NotificationService&MockObject $notifService;
private UserReadRepositoryInterface&MockObject $userRepo;
private UserTenantRepositoryInterface&MockObject $utRepo;
private UserCreatedNotificationListener $listener;
protected function setUp(): void
{
$this->notifService = $this->createMock(NotificationService::class);
$this->userRepo = $this->createMock(UserReadRepositoryInterface::class);
$this->utRepo = $this->createMock(UserTenantRepositoryInterface::class);
$this->listener = new UserCreatedNotificationListener($this->notifService, $this->userRepo, $this->utRepo);
}
public function testHandleCreatesNotificationsForTenantAdminUsers(): void
{
$this->userRepo->method('find')->with(10)->willReturn([
'id' => 10,
'display_name' => 'Alice Smith',
'first_name' => 'Alice',
'last_name' => 'Smith',
]);
$this->utRepo->method('listTenantIdsByUserId')->with(10)->willReturn([1, 2]);
$this->userRepo->method('listPrivilegedUserIdsByPermissionKeys')
->with([PermissionService::USERS_CREATE])
->willReturn([20, 30]);
$this->notifService->expects($this->exactly(2))
->method('createForTenantAdminUsers')
->willReturnCallback(function (int $tenantId, string $type, string $title, ?string $body, ?string $link, array $data, ?int $excludeUserId, array $adminSet): int {
$this->assertContains($tenantId, [1, 2]);
$this->assertSame('user.created', $type);
$this->assertStringContainsString('Alice Smith', $title);
$this->assertSame('admin/users/edit/abc-uuid', $link);
$this->assertSame(5, $excludeUserId);
$this->assertArrayHasKey(20, $adminSet);
$this->assertArrayHasKey(30, $adminSet);
return 2;
});
$this->listener->handle('user.created', [
'user_id' => 10,
'uuid' => 'abc-uuid',
'actor_user_id' => 5,
]);
}
public function testHandleExcludesActorFromRecipients(): void
{
$this->userRepo->method('find')->with(10)->willReturn([
'id' => 10,
'display_name' => 'Bob',
]);
$this->utRepo->method('listTenantIdsByUserId')->with(10)->willReturn([1]);
$this->userRepo->method('listPrivilegedUserIdsByPermissionKeys')->willReturn([20]);
$this->notifService->expects($this->once())
->method('createForTenantAdminUsers')
->with(
1,
'user.created',
$this->anything(),
null,
'admin/users/edit/xyz',
$this->anything(),
7, // actor excluded
$this->anything()
)
->willReturn(1);
$this->listener->handle('user.created', [
'user_id' => 10,
'uuid' => 'xyz',
'actor_user_id' => 7,
]);
}
public function testHandleSkipsInvalidPayload(): void
{
$this->notifService->expects($this->never())->method('createForTenantAdminUsers');
$this->listener->handle('user.created', ['uuid' => 'abc']);
$this->listener->handle('user.created', ['user_id' => 0, 'uuid' => 'abc']);
$this->listener->handle('user.created', ['user_id' => 10, 'uuid' => '']);
}
public function testHandleSkipsWhenUserNotFound(): void
{
$this->userRepo->method('find')->with(99)->willReturn(null);
$this->notifService->expects($this->never())->method('createForTenantAdminUsers');
$this->listener->handle('user.created', [
'user_id' => 99,
'uuid' => 'abc',
'actor_user_id' => 1,
]);
}
public function testHandleSkipsWhenNoTenants(): void
{
$this->userRepo->method('find')->with(10)->willReturn([
'id' => 10,
'display_name' => 'Alice',
]);
$this->utRepo->method('listTenantIdsByUserId')->with(10)->willReturn([]);
$this->notifService->expects($this->never())->method('createForTenantAdminUsers');
$this->listener->handle('user.created', [
'user_id' => 10,
'uuid' => 'abc',
'actor_user_id' => 1,
]);
}
public function testHandleSkipsWhenNoAdminUsers(): void
{
$this->userRepo->method('find')->with(10)->willReturn([
'id' => 10,
'display_name' => 'Alice',
]);
$this->utRepo->method('listTenantIdsByUserId')->with(10)->willReturn([1]);
$this->userRepo->method('listPrivilegedUserIdsByPermissionKeys')->willReturn([]);
$this->notifService->expects($this->never())->method('createForTenantAdminUsers');
$this->listener->handle('user.created', [
'user_id' => 10,
'uuid' => 'abc',
'actor_user_id' => 1,
]);
}
}

View File

@@ -0,0 +1,129 @@
<?php
namespace MintyPHP\Tests\Module\Notifications\Service;
use MintyPHP\Module\Notifications\Repository\NotificationRepositoryInterface;
use MintyPHP\Module\Notifications\Service\NotificationService;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
class NotificationServiceTest extends TestCase
{
private NotificationRepositoryInterface&MockObject $notifRepo;
private NotificationService $service;
protected function setUp(): void
{
$this->notifRepo = $this->createMock(NotificationRepositoryInterface::class);
$this->service = new NotificationService($this->notifRepo);
}
public function testCreateForUserInsertsNotification(): void
{
$this->notifRepo->expects($this->once())
->method('create')
->with($this->callback(function (array $data): bool {
return $data['recipient_user_id'] === 5
&& $data['tenant_id'] === 1
&& $data['type'] === 'user.created'
&& $data['title'] === 'New user: Alice';
}))
->willReturn(42);
$result = $this->service->createForUser(5, 1, 'user.created', 'New user: Alice', null, 'admin/users/edit/abc', []);
$this->assertSame(42, $result);
}
public function testCreateForUserRejectsMissingFields(): void
{
$this->notifRepo->expects($this->never())->method('create');
$this->assertFalse($this->service->createForUser(0, 1, 'user.created', 'Test', null, null, []));
$this->assertFalse($this->service->createForUser(1, 1, '', 'Test', null, null, []));
$this->assertFalse($this->service->createForUser(1, 1, 'user.created', '', null, null, []));
}
public function testUnreadCountReturnsCorrectCount(): void
{
$this->notifRepo->method('countUnreadByUser')->with(3)->willReturn(7);
$this->assertSame(7, $this->service->unreadCount(3));
}
public function testUnreadCountReturnsZeroForInvalidUser(): void
{
$this->notifRepo->expects($this->never())->method('countUnreadByUser');
$this->assertSame(0, $this->service->unreadCount(0));
}
public function testMarkAsReadUpdatesState(): void
{
$this->notifRepo->expects($this->once())
->method('markRead')
->with(10, 3)
->willReturn(true);
$this->assertTrue($this->service->markAsRead(3, 10));
}
public function testMarkAsReadRejectsInvalidIds(): void
{
$this->notifRepo->expects($this->never())->method('markRead');
$this->assertFalse($this->service->markAsRead(0, 10));
$this->assertFalse($this->service->markAsRead(3, 0));
}
public function testMarkAllAsReadAffectsAllUserNotifications(): void
{
$this->notifRepo->expects($this->once())
->method('markAllReadByUser')
->with(3)
->willReturn(5);
$this->assertSame(5, $this->service->markAllAsRead(3));
}
public function testDismissDeletesNotification(): void
{
$this->notifRepo->expects($this->once())
->method('delete')
->with(10, 3)
->willReturn(true);
$this->assertTrue($this->service->dismiss(3, 10));
}
public function testPurgeOldReadRemovesExpiredOnly(): void
{
$this->notifRepo->expects($this->once())
->method('purgeReadOlderThanDays')
->with(90)
->willReturn(12);
$this->assertSame(12, $this->service->purgeOldRead(90));
}
public function testDeleteAllForUserDelegates(): void
{
$this->notifRepo->expects($this->once())
->method('deleteAllByUser')
->with(5)
->willReturn(3);
$this->assertSame(3, $this->service->deleteAllForUser(5));
}
public function testListForUserReturnsNotifications(): void
{
$expected = [
['id' => 1, 'title' => 'Test', 'is_read' => 0],
['id' => 2, 'title' => 'Test 2', 'is_read' => 1],
];
$this->notifRepo->method('listByUser')->with(3, 50, 0)->willReturn($expected);
$this->assertSame($expected, $this->service->listForUser(3));
}
}