Test on notification controller
This commit is contained in:
parent
9cc8dc83f3
commit
08ed90a7dc
|
|
@ -0,0 +1,108 @@
|
|||
<?php
|
||||
|
||||
namespace App\Tests\Controller;
|
||||
|
||||
use App\Tests\Functional\AbstractFunctionalTest;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
|
||||
|
||||
class NotificationControllerTest extends AbstractFunctionalTest{
|
||||
|
||||
//region index tests
|
||||
#[Test]
|
||||
public function test_index_super_admin_success(): void
|
||||
{
|
||||
$admin = $this->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
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue