Resolve "user notification preferences are not displayed correctly"

This commit is contained in:
2025-09-10 16:28:45 +00:00
parent 2a280b814f
commit 1195b54a68
17 changed files with 529 additions and 185 deletions

View File

@@ -0,0 +1,85 @@
<?php
declare(strict_types=1);
/*
* 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.
*/
namespace Action\User\UpdateProfile;
use Chill\MainBundle\Action\User\UpdateProfile\UpdateProfileCommand;
use Chill\MainBundle\Action\User\UpdateProfile\UpdateProfileCommandHandler;
use Chill\MainBundle\Entity\User;
use libphonenumber\PhoneNumber;
use PHPUnit\Framework\TestCase;
/**
* @internal
*
* @coversNothing
*/
final class UpdateProfileCommandHandlerTest extends TestCase
{
public function testUpdateProfileWithNullPhoneAndFlags(): void
{
$user = new User();
// Pre-set some flags to opposite values to check they are updated
$flag = 'tickets';
$user->setNotificationImmediately($flag, true);
$user->setNotificationDailyDigest($flag, true);
$command = new UpdateProfileCommand(null);
$command->notificationFlags = [
$flag => [
'immediate_email' => false,
'daily_digest' => false,
],
];
(new UpdateProfileCommandHandler())->updateProfile($user, $command);
self::assertNull($user->getPhonenumber(), 'Phone should be set to null');
self::assertFalse($user->isNotificationSendImmediately($flag));
self::assertFalse($user->isNotificationDailyDigest($flag));
}
public function testUpdateProfileWithPhoneAndMultipleFlags(): void
{
$user = new User();
$phone = new PhoneNumber();
$phone->setCountryCode(33); // France
$phone->setNationalNumber(612345678);
$command = new UpdateProfileCommand($phone);
$command->notificationFlags = [
'reports' => [
'immediate_email' => true,
'daily_digest' => false,
],
'activities' => [
'immediate_email' => false,
'daily_digest' => true,
],
];
(new UpdateProfileCommandHandler())->updateProfile($user, $command);
// Phone assigned
self::assertInstanceOf(PhoneNumber::class, $user->getPhonenumber());
self::assertSame(33, $user->getPhonenumber()->getCountryCode());
self::assertSame('612345678', (string) $user->getPhonenumber()->getNationalNumber());
// Flags applied
self::assertTrue($user->isNotificationSendImmediately('reports'));
self::assertFalse($user->isNotificationDailyDigest('reports'));
self::assertFalse($user->isNotificationSendImmediately('activities'));
self::assertTrue($user->isNotificationDailyDigest('activities'));
}
}

View File

@@ -0,0 +1,103 @@
<?php
declare(strict_types=1);
/*
* 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.
*/
namespace Action\User\UpdateProfile;
use Chill\MainBundle\Action\User\UpdateProfile\UpdateProfileCommand;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Notification\FlagProviders\NotificationFlagProviderInterface;
use Chill\MainBundle\Notification\NotificationFlagManager;
use libphonenumber\PhoneNumber;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Translation\TranslatableMessage;
/**
* @internal
*
* @coversNothing
*/
final class UpdateProfileCommandTest extends TestCase
{
public function testCreateTransfersPhonenumberAndNotificationFlags(): void
{
$user = new User();
// set a phone number
$phone = new PhoneNumber();
$phone->setCountryCode(32); // Belgium
$phone->setNationalNumber(471234567);
$user->setPhonenumber($phone);
// configure notification flags on the user via helpers
$flagA = 'foo';
$flagB = 'bar';
// For tickets: immediate true, daily false
$user->setNotificationImmediately($flagA, true);
$user->setNotificationDailyDigest($flagA, false);
// For reports: immediate false, daily true
$user->setNotificationImmediately($flagB, false);
$user->setNotificationDailyDigest($flagB, true);
// a third flag not explicitly set to validate default behavior from User
$flagC = 'foobar'; // by default immediate-email is true, daily-digest is false per User::getNotificationFlagData
$manager = $this->createNotificationFlagManager([$flagA, $flagB, $flagC]);
$command = UpdateProfileCommand::create($user, $manager);
// phone number transferred
self::assertInstanceOf(PhoneNumber::class, $command->phonenumber);
self::assertSame($phone->getCountryCode(), $command->phonenumber->getCountryCode());
self::assertSame($phone->getNationalNumber(), $command->phonenumber->getNationalNumber());
// flags transferred consistently
self::assertArrayHasKey($flagA, $command->notificationFlags);
self::assertArrayHasKey($flagB, $command->notificationFlags);
self::assertArrayHasKey($flagC, $command->notificationFlags);
self::assertSame([
'immediate_email' => true,
'daily_digest' => false,
], $command->notificationFlags[$flagA]);
self::assertSame([
'immediate_email' => false,
'daily_digest' => true,
], $command->notificationFlags[$flagB]);
// default from User::getNotificationFlagData -> immediate true, daily false
self::assertSame([
'immediate_email' => true,
'daily_digest' => false,
], $command->notificationFlags[$flagC]);
}
private function createNotificationFlagManager(array $flags): NotificationFlagManager
{
$providers = array_map(fn (string $flag) => new class ($flag) implements NotificationFlagProviderInterface {
public function __construct(private readonly string $flag) {}
public function getFlag(): string
{
return $this->flag;
}
public function getLabel(): TranslatableMessage
{
return new TranslatableMessage($this->flag);
}
}, $flags);
return new NotificationFlagManager($providers);
}
}

View File

@@ -96,11 +96,13 @@ final class NotificationTest extends KernelTestCase
$this->assertTrue($user->isNotificationSendImmediately($notification->getType()), 'Should return true when no notification flags are set, by default immediate email');
// immediate-email preference
$user->setNotificationFlags(['test_notification_type' => [User::NOTIF_FLAG_IMMEDIATE_EMAIL, User::NOTIF_FLAG_DAILY_DIGEST]]);
$user->setNotificationImmediately('test_notification_type', true);
$user->setNotificationDailyDigest('test_notification_type', true);
$this->assertTrue($user->isNotificationSendImmediately($notification->getType()), 'Should return true when preferences contain immediate-email');
// daily-email preference
$user->setNotificationFlags(['test_notification_type' => [User::NOTIF_FLAG_DAILY_DIGEST]]);
$user->setNotificationDailyDigest('test_notification_type', true);
$user->setNotificationImmediately('test_notification_type', false);
$this->assertFalse($user->isNotificationSendImmediately($notification->getType()), 'Should return false when preference is daily-email only');
$this->assertTrue($user->isNotificationDailyDigest($notification->getType()), 'Should return true when preference is daily-email');

View File

@@ -0,0 +1,82 @@
<?php
declare(strict_types=1);
/*
* 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.
*/
namespace Chill\MainBundle\Tests\Entity;
use Chill\MainBundle\Entity\User;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
/**
* @internal
*
* @coversNothing
*/
class UserNotificationFlagsPersistenceTest extends KernelTestCase
{
public function testFlushPersistsNotificationFlagsChanges(): void
{
self::bootKernel();
$em = self::getContainer()->get('doctrine')->getManager();
$user = new User();
$user->setUsername('user_'.bin2hex(random_bytes(4)));
$user->setLabel('Test User');
$user->setPassword('secret');
// Étape 1: créer et persister lutilisateur
$em->persist($user);
$em->flush();
$id = $user->getId();
self::assertNotNull($id, 'User should have an ID after flush');
try {
// Sanity check: par défaut, pas de daily digest pour "alerts"
self::assertFalse($user->isNotificationDailyDigest('alerts'));
// Étape 2: activer le daily digest -> setNotificationFlagElement réassigne la propriété
$user->setNotificationDailyDigest('alerts', true);
$em->flush(); // persist le changement
$em->clear(); // simule un nouveau cycle de requête
// Étape 3: recharger depuis la base et vérifier la persistance
/** @var User $reloaded */
$reloaded = $em->find(User::class, $id);
self::assertNotNull($reloaded);
self::assertTrue(
$reloaded->isNotificationDailyDigest('alerts'),
'Daily digest flag should be persisted'
);
// Étape 4: modifier via setNotificationFlagData (remplacement du tableau)
// Cette méthode doit réassigner la propriété (copie -> réassignation)
$reloaded->setNotificationImmediately('alerts', true);
$reloaded->setNotificationDailyDigest('alerts', false);
$em->flush();
$em->clear();
/** @var User $reloaded2 */
$reloaded2 = $em->find(User::class, $id);
self::assertNotNull($reloaded2);
// Le daily digest nest plus actif, seul immediate-email est présent
self::assertFalse($reloaded2->isNotificationDailyDigest('alerts'));
self::assertTrue($reloaded2->isNotificationSendImmediately('alerts'));
} finally {
// Nettoyage
$managed = $em->find(User::class, $id);
if (null !== $managed) {
$em->remove($managed);
$em->flush();
}
$em->clear();
}
}
}

View File

@@ -99,4 +99,22 @@ class UserTest extends TestCase
$user->setAbsenceEnd(null);
self::assertFalse($user->isAbsent(), 'Should not be absent if start is null');
}
public function testSetNotification(): void
{
$user = new User();
self::assertTrue($user->isNotificationSendImmediately('dummy'));
self::assertFalse($user->isNotificationDailyDigest('dummy'));
$user->setNotificationImmediately('dummy', false);
self::assertFalse($user->isNotificationSendImmediately('dummy'));
$user->setNotificationDailyDigest('dummy', true);
self::assertTrue($user->isNotificationDailyDigest('dummy'));
$user->setNotificationImmediately('dummy', true);
self::assertTrue($user->isNotificationSendImmediately('dummy'));
self::assertTrue($user->isNotificationDailyDigest('dummy'));
}
}

View File

@@ -144,7 +144,7 @@ class NotificationMailerTest extends TestCase
$idProperty->setValue($user, 456);
// Set notification flags for the user
$user->setNotificationFlags(['test_notification_type' => [User::NOTIF_FLAG_IMMEDIATE_EMAIL]]);
$user->setNotificationImmediately('test_notification_type', true);
$messageBus = $this->createMock(MessageBusInterface::class);
$messageBus->expects($this->once())