From 08ed90a7dcd9bdc2fdd030c019b41e732ca139a2 Mon Sep 17 00:00:00 2001 From: Charles Date: Fri, 19 Dec 2025 15:45:23 +0100 Subject: [PATCH] Test on notification controller --- .../Controller/NotificationControllerTest.php | 108 ++++++++++++++++++ tests/Functional/AbstractFunctionalTest.php | 13 +++ 2 files changed, 121 insertions(+) create mode 100644 tests/Controller/NotificationControllerTest.php diff --git a/tests/Controller/NotificationControllerTest.php b/tests/Controller/NotificationControllerTest.php new file mode 100644 index 0000000..1a91872 --- /dev/null +++ b/tests/Controller/NotificationControllerTest.php @@ -0,0 +1,108 @@ +createUser('admin@test.com', ['ROLE_SUPER_ADMIN']); + $this->client->loginUser($admin); + $this->createNotification($admin, 'Test Notification 1'); + $this->createNotification($admin, 'Test Notification 2', true); + $this->client->request('GET', '/notifications/'); + self::assertResponseIsSuccessful(); + $responseData = json_decode($this->client->getResponse()->getContent(), true); + $this->assertArrayHasKey('notifications', $responseData); + $this->assertArrayHasKey('unreadCount', $responseData); + $this->assertCount(2, $responseData['notifications']); + $this->assertEquals(1, $responseData['unreadCount']); + } + + #[Test] + public function test_index_non_super_admin_forbidden(): void + { + $admin = $this->createUser('admin@test.com', ['ROLE_ADMIN']); + $user = $this->createUser('user@test.com', ['ROLE_USER']); + $this->client->loginUser($admin); + $this->client->request('GET', '/notifications/'); + self::assertResponseStatusCodeSame(403); + $this->client->loginUser($user); + $this->client->request('GET', '/notifications/'); + self::assertResponseStatusCodeSame(403); + } + + //endregion + + //region unread tests + #[Test] + public function test_unread_authenticated_user_success(): void + { + $user = $this->createUser('s', ['ROLE_SUPER_ADMIN']); + $this->client->loginUser($user); + $this->createNotification($user, 'Unread Notification 1'); + $this->createNotification($user, 'Read Notification 1', true); + $this->client->request('GET', '/notifications/unread'); + self::assertResponseIsSuccessful(); + $responseData = json_decode($this->client->getResponse()->getContent(), true); + $this->assertArrayHasKey('notifications', $responseData); + $this->assertArrayHasKey('unreadCount', $responseData); + $this->assertCount(1, $responseData['notifications']); + $this->assertEquals(1, $responseData['unreadCount']); + } + + #[Test] + public function test_unread_unauthenticated_user_forbidden(): void + { + $this->client->request('GET', '/notifications/unread'); + self::assertResponseStatusCodeSame(401); + } + + //endregion + + //region markAsRead tests + + #[Test] + public function test_markAsRead_authenticated_user_success(): void + { + $user = $this->createUser('user'); + $this->client->loginUser($user); + $notification = $this->createNotification($user, 'Notification to Mark Read'); + $this->client->request('POST', '/notifications/' . $notification->getId() . '/read'); + self::assertResponseIsSuccessful(); + $responseData = json_decode($this->client->getResponse()->getContent(), true); + $this->assertArrayHasKey('success', $responseData); + $this->assertTrue($responseData['success']); + } + + #[Test] + public function test_markAsRead_notification_not_found(): void + { + $user = $this->createUser('user'); + $this->client->loginUser($user); + $notification = $this->createNotification($user, 'Notification to Mark Read'); + $this->client->request('POST', '/notifications/9999/read'); // Non-existent ID + self::assertResponseStatusCodeSame(404); + $responseData = json_decode($this->client->getResponse()->getContent(), true); + $this->assertArrayHasKey('error', $responseData); + $this->assertEquals('Notification not found', $responseData['error']); + } + + #[Test] + public function test_markAsRead_unauthenticated_user_forbidden(): void + { + $this->client->request('POST', '/notifications/1/read'); + self::assertResponseRedirects('/login'); + $this->client->followRedirect(); + self::assertResponseStatusCodeSame(200); // Login page + } + + + //endregion +} \ No newline at end of file diff --git a/tests/Functional/AbstractFunctionalTest.php b/tests/Functional/AbstractFunctionalTest.php index fd0e13a..a0d3542 100644 --- a/tests/Functional/AbstractFunctionalTest.php +++ b/tests/Functional/AbstractFunctionalTest.php @@ -3,6 +3,7 @@ namespace App\Tests\Functional; use App\Entity\Apps; +use App\Entity\Notification; use App\Entity\Organizations; use App\Entity\Roles; use App\Entity\User; @@ -102,4 +103,16 @@ abstract class AbstractFunctionalTest extends WebTestCase return $role; } + + protected function createNotification($user, string $title, bool $isRead = false): Notification{ + $notification = new Notification(); + $notification->setUser($user); + $notification->setTitle($title); + $notification->setMessage('This is a test notification message.'); + $notification->setType('info'); + $notification->setIsRead($isRead); + $this->entityManager->persist($notification); + $this->entityManager->flush(); + return $notification; + } } \ No newline at end of file