fix loading of unread addressees in notification

This commit is contained in:
2021-12-28 19:40:38 +01:00
parent d5d64cdf65
commit b323ddae05
3 changed files with 166 additions and 10 deletions

View File

@@ -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);
}
}
}