mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 15:13:50 +00:00
notification: update comment and api endpoint for marking as read/unread
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Controller;
|
||||
|
||||
use Chill\MainBundle\Entity\Notification;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Chill\MainBundle\Repository\UserRepository;
|
||||
use Chill\MainBundle\Test\PrepareClientTrait;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
final class NotificationApiControllerTest extends WebTestCase
|
||||
{
|
||||
use PrepareClientTrait;
|
||||
|
||||
private array $toDelete = [];
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
$em = self::$container->get(EntityManagerInterface::class);
|
||||
|
||||
foreach ($this->toDelete as [$className, $id]) {
|
||||
$object = $em->find($className, $id);
|
||||
$em->remove($object);
|
||||
}
|
||||
|
||||
$em->flush();
|
||||
}
|
||||
|
||||
public function generateDataMarkAsRead()
|
||||
{
|
||||
self::bootKernel();
|
||||
$em = self::$container->get(EntityManagerInterface::class);
|
||||
$userRepository = self::$container->get(UserRepository::class);
|
||||
$userA = $userRepository->findOneBy(['username' => 'center a_social']);
|
||||
$userB = $userRepository->findOneBy(['username' => 'center b_social']);
|
||||
|
||||
$notification = new Notification();
|
||||
$notification
|
||||
->setMessage('Test generated')
|
||||
->setRelatedEntityClass(AccompanyingPeriod::class)
|
||||
->setRelatedEntityId(0)
|
||||
->setSender($userB)
|
||||
->addAddressee($userA);
|
||||
$em->persist($notification);
|
||||
$em->refresh($notification);
|
||||
$em->flush();
|
||||
|
||||
$this->toDelete[] = [Notification::class, $notification->getId()];
|
||||
|
||||
yield [$notification->getId()];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider generateDataMarkAsRead
|
||||
*/
|
||||
public function testMarkAsReadOrUnRead(int $notificationId)
|
||||
{
|
||||
$client = $this->getClientAuthenticated();
|
||||
$client->request('POST', "/api/1.0/main/notification/{$notificationId}/mark/read");
|
||||
|
||||
$this->assertResponseIsSuccessful('test marking as read');
|
||||
|
||||
$em = self::$container->get(EntityManagerInterface::class);
|
||||
/** @var Notification $notification */
|
||||
$notification = $em->find(Notification::class, $notificationId);
|
||||
$user = self::$container->get(UserRepository::class)->findOneBy(['username' => 'center a_social']);
|
||||
|
||||
$this->assertTrue($notification->isReadBy($user));
|
||||
|
||||
$client->request('POST', "/api/1.0/main/notification/{$notificationId}/mark/unread");
|
||||
|
||||
$this->assertResponseIsSuccessful('test marking as unread');
|
||||
|
||||
$notification = $em->find(Notification::class, $notificationId);
|
||||
$user = $em->find(User::class, $user->getId());
|
||||
$em->refresh($notification);
|
||||
$em->refresh($user);
|
||||
|
||||
$this->assertFalse($notification->isReadBy($user));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user