mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-14 18:54:59 +00:00
Resolve "user notification preferences are not displayed correctly"
This commit is contained in:
6
.changes/unreleased/Fixed-20250910-180935.yaml
Normal file
6
.changes/unreleased/Fixed-20250910-180935.yaml
Normal file
@@ -0,0 +1,6 @@
|
||||
kind: Fixed
|
||||
body: Fix saving notification preferences in user's profile
|
||||
time: 2025-09-10T18:09:35.525979715+02:00
|
||||
custom:
|
||||
Issue: ""
|
||||
SchemaChange: No schema change
|
@@ -0,0 +1,64 @@
|
||||
<?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\Action\User\UpdateProfile;
|
||||
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Chill\MainBundle\Notification\NotificationFlagManager;
|
||||
use Chill\MainBundle\Validation\Constraint\PhonenumberConstraint;
|
||||
use libphonenumber\PhoneNumber;
|
||||
|
||||
final class UpdateProfileCommand
|
||||
{
|
||||
public array $notificationFlags = [];
|
||||
|
||||
public function __construct(
|
||||
#[PhonenumberConstraint]
|
||||
public ?PhoneNumber $phonenumber,
|
||||
) {}
|
||||
|
||||
public static function create(User $user, NotificationFlagManager $flagManager): self
|
||||
{
|
||||
$updateProfileCommand = new self($user->getPhonenumber());
|
||||
|
||||
foreach ($flagManager->getAllNotificationFlagProviders() as $provider) {
|
||||
$updateProfileCommand->setNotificationFlag(
|
||||
$provider->getFlag(),
|
||||
User::NOTIF_FLAG_IMMEDIATE_EMAIL,
|
||||
$user->isNotificationSendImmediately($provider->getFlag())
|
||||
);
|
||||
$updateProfileCommand->setNotificationFlag(
|
||||
$provider->getFlag(),
|
||||
User::NOTIF_FLAG_DAILY_DIGEST,
|
||||
$user->isNotificationDailyDigest($provider->getFlag())
|
||||
);
|
||||
}
|
||||
|
||||
return $updateProfileCommand;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User::NOTIF_FLAG_IMMEDIATE_EMAIL|User::NOTIF_FLAG_DAILY_DIGEST $kind
|
||||
*/
|
||||
private function setNotificationFlag(string $type, string $kind, bool $value): void
|
||||
{
|
||||
if (!array_key_exists($type, $this->notificationFlags)) {
|
||||
$this->notificationFlags[$type] = ['immediate_email' => true, 'daily_digest' => false];
|
||||
}
|
||||
|
||||
$k = match ($kind) {
|
||||
User::NOTIF_FLAG_IMMEDIATE_EMAIL => 'immediate_email',
|
||||
User::NOTIF_FLAG_DAILY_DIGEST => 'daily_digest',
|
||||
};
|
||||
|
||||
$this->notificationFlags[$type][$k] = $value;
|
||||
}
|
||||
}
|
@@ -0,0 +1,27 @@
|
||||
<?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\Action\User\UpdateProfile;
|
||||
|
||||
use Chill\MainBundle\Entity\User;
|
||||
|
||||
final readonly class UpdateProfileCommandHandler
|
||||
{
|
||||
public function updateProfile(User $user, UpdateProfileCommand $command): void
|
||||
{
|
||||
$user->setPhonenumber($command->phonenumber);
|
||||
|
||||
foreach ($command->notificationFlags as $flag => $values) {
|
||||
$user->setNotificationImmediately($flag, $values['immediate_email']);
|
||||
$user->setNotificationDailyDigest($flag, $values['daily_digest']);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,63 +0,0 @@
|
||||
<?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\Controller;
|
||||
|
||||
use Chill\MainBundle\Form\UserProfileType;
|
||||
use Chill\MainBundle\Security\ChillSecurity;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
|
||||
final class UserProfileController extends AbstractController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly TranslatorInterface $translator,
|
||||
private readonly ChillSecurity $security,
|
||||
private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* User profile that allows editing of phonenumber and visualization of certain data.
|
||||
*/
|
||||
#[Route(path: '/{_locale}/main/user/my-profile', name: 'chill_main_user_profile')]
|
||||
public function __invoke(Request $request)
|
||||
{
|
||||
if (!$this->security->isGranted('ROLE_USER')) {
|
||||
throw new AccessDeniedHttpException();
|
||||
}
|
||||
|
||||
$user = $this->security->getUser();
|
||||
$editForm = $this->createForm(UserProfileType::class, $user);
|
||||
|
||||
$editForm->get('notificationFlags')->setData($user->getNotificationFlags());
|
||||
|
||||
$editForm->handleRequest($request);
|
||||
|
||||
if ($editForm->isSubmitted() && $editForm->isValid()) {
|
||||
$notificationFlagsData = $editForm->get('notificationFlags')->getData();
|
||||
$user->setNotificationFlags($notificationFlagsData);
|
||||
|
||||
$em = $this->managerRegistry->getManager();
|
||||
$em->flush();
|
||||
$this->addFlash('success', $this->translator->trans('user.profile.Profile successfully updated!'));
|
||||
|
||||
return $this->redirectToRoute('chill_main_user_profile');
|
||||
}
|
||||
|
||||
return $this->render('@ChillMain/User/profile.html.twig', [
|
||||
'user' => $user,
|
||||
'form' => $editForm->createView(),
|
||||
]);
|
||||
}
|
||||
}
|
@@ -0,0 +1,75 @@
|
||||
<?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\Controller;
|
||||
|
||||
use Chill\MainBundle\Action\User\UpdateProfile\UpdateProfileCommand;
|
||||
use Chill\MainBundle\Action\User\UpdateProfile\UpdateProfileCommandHandler;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Chill\MainBundle\Form\UpdateProfileType;
|
||||
use Chill\MainBundle\Notification\NotificationFlagManager;
|
||||
use Chill\MainBundle\Security\ChillSecurity;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\Form\FormFactoryInterface;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\Session\Session;
|
||||
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Twig\Environment;
|
||||
|
||||
final readonly class UserUpdateProfileController
|
||||
{
|
||||
public function __construct(
|
||||
private TranslatorInterface $translator,
|
||||
private ChillSecurity $security,
|
||||
private EntityManagerInterface $entityManager,
|
||||
private NotificationFlagManager $notificationFlagManager,
|
||||
private FormFactoryInterface $formFactory,
|
||||
private UrlGeneratorInterface $urlGenerator,
|
||||
private Environment $twig,
|
||||
private UpdateProfileCommandHandler $updateProfileCommandHandler,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* User profile that allows editing of phonenumber and visualization of certain data.
|
||||
*/
|
||||
#[Route(path: '/{_locale}/main/user/my-profile', name: 'chill_main_user_profile')]
|
||||
public function __invoke(Request $request, Session $session)
|
||||
{
|
||||
if (!$this->security->isGranted('ROLE_USER')) {
|
||||
throw new AccessDeniedHttpException();
|
||||
}
|
||||
|
||||
$user = $this->security->getUser();
|
||||
|
||||
$command = UpdateProfileCommand::create($user, $this->notificationFlagManager);
|
||||
$editForm = $this->formFactory->create(UpdateProfileType::class, $command);
|
||||
|
||||
$editForm->handleRequest($request);
|
||||
|
||||
if ($editForm->isSubmitted() && $editForm->isValid()) {
|
||||
$this->updateProfileCommandHandler->updateProfile($user, $command);
|
||||
$this->entityManager->flush();
|
||||
$session->getFlashBag()->add('success', $this->translator->trans('user.profile.Profile successfully updated!'));
|
||||
|
||||
return new RedirectResponse($this->urlGenerator->generate('chill_main_user_profile'));
|
||||
}
|
||||
|
||||
return new Response($this->twig->render('@ChillMain/User/profile.html.twig', [
|
||||
'user' => $user,
|
||||
'form' => $editForm->createView(),
|
||||
]));
|
||||
}
|
||||
}
|
@@ -652,42 +652,66 @@ class User implements UserInterface, \Stringable, PasswordAuthenticatedUserInter
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getNotificationFlags(): array
|
||||
private function getNotificationFlagData(string $flag): array
|
||||
{
|
||||
return $this->notificationFlags;
|
||||
}
|
||||
|
||||
public function setNotificationFlags(array $notificationFlags)
|
||||
{
|
||||
$this->notificationFlags = $notificationFlags;
|
||||
}
|
||||
|
||||
public function getNotificationFlagData(string $flag): array
|
||||
{
|
||||
return $this->notificationFlags[$flag] ?? [];
|
||||
}
|
||||
|
||||
public function setNotificationFlagData(string $flag, array $data): void
|
||||
{
|
||||
$this->notificationFlags[$flag] = $data;
|
||||
return $this->notificationFlags[$flag] ?? [self::NOTIF_FLAG_IMMEDIATE_EMAIL];
|
||||
}
|
||||
|
||||
public function isNotificationSendImmediately(string $type): bool
|
||||
{
|
||||
if ([] === $this->getNotificationFlagData($type) || in_array(User::NOTIF_FLAG_IMMEDIATE_EMAIL, $this->getNotificationFlagData($type), true)) {
|
||||
return true;
|
||||
return $this->isNotificationForElement($type, self::NOTIF_FLAG_IMMEDIATE_EMAIL);
|
||||
}
|
||||
|
||||
return false;
|
||||
public function setNotificationImmediately(string $type, bool $active): void
|
||||
{
|
||||
$this->setNotificationFlagElement($type, $active, self::NOTIF_FLAG_IMMEDIATE_EMAIL);
|
||||
}
|
||||
|
||||
public function setNotificationDailyDigest(string $type, bool $active): void
|
||||
{
|
||||
$this->setNotificationFlagElement($type, $active, self::NOTIF_FLAG_DAILY_DIGEST);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param self::NOTIF_FLAG_IMMEDIATE_EMAIL|self::NOTIF_FLAG_DAILY_DIGEST $kind
|
||||
*/
|
||||
private function setNotificationFlagElement(string $type, bool $active, string $kind): void
|
||||
{
|
||||
$notificationFlags = [...$this->notificationFlags];
|
||||
$changed = false;
|
||||
|
||||
if (!isset($notificationFlags[$type])) {
|
||||
$notificationFlags[$type] = [self::NOTIF_FLAG_IMMEDIATE_EMAIL];
|
||||
$changed = true;
|
||||
}
|
||||
|
||||
if ($active) {
|
||||
if (!in_array($kind, $notificationFlags[$type], true)) {
|
||||
$notificationFlags[$type] = [...$notificationFlags[$type], $kind];
|
||||
$changed = true;
|
||||
}
|
||||
} else {
|
||||
if (in_array($kind, $notificationFlags[$type], true)) {
|
||||
$notificationFlags[$type] = array_values(
|
||||
array_filter($notificationFlags[$type], static fn ($k) => $k !== $kind)
|
||||
);
|
||||
$changed = true;
|
||||
}
|
||||
}
|
||||
|
||||
if ($changed) {
|
||||
$this->notificationFlags = [...$notificationFlags];
|
||||
}
|
||||
}
|
||||
|
||||
private function isNotificationForElement(string $type, string $kind): bool
|
||||
{
|
||||
return in_array($kind, $this->getNotificationFlagData($type), true);
|
||||
}
|
||||
|
||||
public function isNotificationDailyDigest(string $type): bool
|
||||
{
|
||||
if (in_array(User::NOTIF_FLAG_DAILY_DIGEST, $this->getNotificationFlagData($type), true)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
return $this->isNotificationForElement($type, self::NOTIF_FLAG_DAILY_DIGEST);
|
||||
}
|
||||
|
||||
public function getLocale(): string
|
||||
|
@@ -1,75 +0,0 @@
|
||||
<?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\Form\DataMapper;
|
||||
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Symfony\Component\Form\DataMapperInterface;
|
||||
|
||||
final readonly class NotificationFlagDataMapper implements DataMapperInterface
|
||||
{
|
||||
public function __construct(private array $notificationFlagProviders) {}
|
||||
|
||||
public function mapDataToForms($viewData, $forms): void
|
||||
{
|
||||
if (null === $viewData) {
|
||||
$viewData = [];
|
||||
}
|
||||
|
||||
$formsArray = iterator_to_array($forms);
|
||||
|
||||
foreach ($this->notificationFlagProviders as $flagProvider) {
|
||||
$flag = $flagProvider->getFlag();
|
||||
|
||||
if (isset($formsArray[$flag])) {
|
||||
$flagForm = $formsArray[$flag];
|
||||
|
||||
$immediateEmailChecked = in_array(User::NOTIF_FLAG_IMMEDIATE_EMAIL, $viewData[$flag] ?? [], true)
|
||||
|| !array_key_exists($flag, $viewData);
|
||||
$dailyEmailChecked = in_array(User::NOTIF_FLAG_DAILY_DIGEST, $viewData[$flag] ?? [], true);
|
||||
|
||||
if ($flagForm->has('immediate_email')) {
|
||||
$flagForm->get('immediate_email')->setData($immediateEmailChecked);
|
||||
}
|
||||
if ($flagForm->has('daily_email')) {
|
||||
$flagForm->get('daily_email')->setData($dailyEmailChecked);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function mapFormsToData($forms, &$viewData): void
|
||||
{
|
||||
$formsArray = iterator_to_array($forms);
|
||||
$viewData = [];
|
||||
|
||||
foreach ($this->notificationFlagProviders as $flagProvider) {
|
||||
$flag = $flagProvider->getFlag();
|
||||
|
||||
if (isset($formsArray[$flag])) {
|
||||
$flagForm = $formsArray[$flag];
|
||||
$viewData[$flag] = [];
|
||||
|
||||
if (true === $flagForm['immediate_email']->getData()) {
|
||||
$viewData[$flag][] = User::NOTIF_FLAG_IMMEDIATE_EMAIL;
|
||||
}
|
||||
|
||||
if (true === $flagForm['daily_email']->getData()) {
|
||||
$viewData[$flag][] = User::NOTIF_FLAG_DAILY_DIGEST;
|
||||
}
|
||||
|
||||
if ([] === $viewData[$flag]) {
|
||||
$viewData[$flag][] = User::NOTIF_FLAG_IMMEDIATE_EMAIL;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -11,11 +11,9 @@ declare(strict_types=1);
|
||||
|
||||
namespace Chill\MainBundle\Form\Type;
|
||||
|
||||
use Chill\MainBundle\Form\DataMapper\NotificationFlagDataMapper;
|
||||
use Chill\MainBundle\Notification\NotificationFlagManager;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\FormType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
@@ -30,27 +28,24 @@ class NotificationFlagsType extends AbstractType
|
||||
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
{
|
||||
$builder->setDataMapper(new NotificationFlagDataMapper($this->notificationFlagProviders));
|
||||
|
||||
foreach ($this->notificationFlagProviders as $flagProvider) {
|
||||
$flag = $flagProvider->getFlag();
|
||||
$builder->add($flag, FormType::class, [
|
||||
$flagBuilder = $builder->create($flag, options: [
|
||||
'label' => $flagProvider->getLabel(),
|
||||
'required' => false,
|
||||
'compound' => true,
|
||||
]);
|
||||
|
||||
$builder->get($flag)
|
||||
$flagBuilder
|
||||
->add('immediate_email', CheckboxType::class, [
|
||||
'label' => false,
|
||||
'required' => false,
|
||||
'mapped' => false,
|
||||
])
|
||||
->add('daily_email', CheckboxType::class, [
|
||||
->add('daily_digest', CheckboxType::class, [
|
||||
'label' => false,
|
||||
'required' => false,
|
||||
'mapped' => false,
|
||||
])
|
||||
;
|
||||
$builder->add($flagBuilder);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,6 +53,7 @@ class NotificationFlagsType extends AbstractType
|
||||
{
|
||||
$resolver->setDefaults([
|
||||
'data_class' => null,
|
||||
'compound' => true,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
@@ -11,31 +11,29 @@ declare(strict_types=1);
|
||||
|
||||
namespace Chill\MainBundle\Form;
|
||||
|
||||
use Chill\MainBundle\Action\User\UpdateProfile\UpdateProfileCommand;
|
||||
use Chill\MainBundle\Form\Type\ChillPhoneNumberType;
|
||||
use Chill\MainBundle\Form\Type\NotificationFlagsType;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
class UserProfileType extends AbstractType
|
||||
class UpdateProfileType extends AbstractType
|
||||
{
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
{
|
||||
$builder
|
||||
->add('phonenumber', ChillPhoneNumberType::class, [
|
||||
'required' => false,
|
||||
])
|
||||
->add('notificationFlags', NotificationFlagsType::class, [
|
||||
'label' => false,
|
||||
'mapped' => false,
|
||||
])
|
||||
->add('notificationFlags', NotificationFlagsType::class)
|
||||
;
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
public function configureOptions(OptionsResolver $resolver): void
|
||||
{
|
||||
$resolver->setDefaults([
|
||||
'data_class' => \Chill\MainBundle\Entity\User::class,
|
||||
'data_class' => UpdateProfileCommand::class,
|
||||
]);
|
||||
}
|
||||
}
|
@@ -64,7 +64,7 @@
|
||||
{{ form_widget(flag.immediate_email, {'label_attr': { 'class': 'checkbox-inline checkbox-switch'}}) }}
|
||||
</td>
|
||||
<td class="text-center">
|
||||
{{ form_widget(flag.daily_email, {'label_attr': { 'class': 'checkbox-inline checkbox-switch'}}) }}
|
||||
{{ form_widget(flag.daily_digest, {'label_attr': { 'class': 'checkbox-inline checkbox-switch'}}) }}
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
|
@@ -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'));
|
||||
}
|
||||
}
|
@@ -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);
|
||||
}
|
||||
}
|
@@ -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');
|
||||
|
||||
|
@@ -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 l’utilisateur
|
||||
$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 n’est 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();
|
||||
}
|
||||
}
|
||||
}
|
@@ -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'));
|
||||
}
|
||||
}
|
||||
|
@@ -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())
|
||||
|
@@ -113,3 +113,5 @@ services:
|
||||
Chill\MainBundle\Service\EntityInfo\ViewEntityInfoManager:
|
||||
arguments:
|
||||
$vienEntityInfoProviders: !tagged_iterator chill_main.entity_info_provider
|
||||
|
||||
Chill\MainBundle\Action\User\UpdateProfile\UpdateProfileCommandHandler: ~
|
||||
|
Reference in New Issue
Block a user