mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 15:13:50 +00:00
fix loading of unread addressees in notification
This commit is contained in:
@@ -1,23 +1,117 @@
|
||||
<?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 Entity;
|
||||
|
||||
use Chill\MainBundle\Entity\Notification;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Chill\MainBundle\Repository\UserRepository;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use function count;
|
||||
|
||||
class NotificationTest extends TestCase
|
||||
/**
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
final class NotificationTest extends KernelTestCase
|
||||
{
|
||||
private array $toDelete = [];
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
self::bootKernel();
|
||||
}
|
||||
|
||||
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 generateNotificationData()
|
||||
{
|
||||
self::bootKernel();
|
||||
$userRepository = self::$container->get(UserRepository::class);
|
||||
|
||||
$senderId = $userRepository
|
||||
->findOneBy(['username' => 'center b_social'])
|
||||
->getId();
|
||||
|
||||
$addressesIds = [];
|
||||
$addressesIds[] = $userRepository
|
||||
->findOneBy(['username' => 'center b_direction'])
|
||||
->getId();
|
||||
|
||||
yield [
|
||||
$senderId,
|
||||
$addressesIds,
|
||||
];
|
||||
}
|
||||
|
||||
public function testAddAddresseeStoreAnUread()
|
||||
{
|
||||
$notification = new Notification();
|
||||
$notification->addAddressee($user1 = new User());
|
||||
$notification->addAddressee($user2 = new User());
|
||||
$notification->getAddressees()->add($user3 = new User());
|
||||
|
||||
$this->assertCount(2, $notification->getAddressees());
|
||||
$this->assertCount(2, $notification->getUnreadBy());
|
||||
$this->assertCount(3, $notification->getAddressees());
|
||||
|
||||
// launch listener
|
||||
$notification->registerUnread();
|
||||
$this->assertCount(3, $notification->getUnreadBy());
|
||||
$this->assertContains($user1, $notification->getUnreadBy()->toArray());
|
||||
$this->assertContains($user2, $notification->getUnreadBy()->toArray());
|
||||
$this->assertContains($user3, $notification->getUnreadBy()->toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider generateNotificationData
|
||||
*/
|
||||
public function testPrePersistComputeUnread(int $senderId, array $addressesIds)
|
||||
{
|
||||
$em = self::$container->get(EntityManagerInterface::class);
|
||||
$notification = new Notification();
|
||||
$notification
|
||||
->setSender($em->find(User::class, $senderId))
|
||||
->setRelatedEntityId(0)
|
||||
->setRelatedEntityClass(AccompanyingPeriod::class)
|
||||
->setMessage('Fake message');
|
||||
|
||||
foreach ($addressesIds as $addresseeId) {
|
||||
$notification
|
||||
->getAddressees()->add($em->find(User::class, $addresseeId));
|
||||
}
|
||||
|
||||
$em->persist($notification);
|
||||
$em->flush();
|
||||
$em->refresh($notification);
|
||||
|
||||
$this->toDelete[] = [Notification::class, $notification->getId()];
|
||||
|
||||
$this->assertEquals($senderId, $notification->getSender()->getId());
|
||||
$this->assertCount(count($addressesIds), $notification->getUnreadBy());
|
||||
|
||||
$unreadIds = $notification->getUnreadBy()->map(static function (User $u) { return $u->getId(); });
|
||||
|
||||
foreach ($addressesIds as $addresseeId) {
|
||||
$this->assertContains($addresseeId, $unreadIds);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user