From e8690d249fff6acac5b02054daff7833cc2ec893 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Wed, 12 Jul 2023 10:42:46 +0200 Subject: [PATCH 01/45] FIX [duplicate][delete] fix error messages for treating the accompaning_period_work linked to the person that will be deleted --- .../ChillPersonBundle/Actions/Remove/PersonMove.php | 10 ++++++++-- .../views/PersonDuplicate/_sidepane.html.twig | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php b/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php index 4e2f7468e..76a6f5b8d 100644 --- a/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php @@ -133,8 +133,14 @@ class PersonMove $conditions = []; - foreach ($mapping['joinColumns'] as $columns) { - $conditions[] = sprintf('%s = %d', $columns['name'], $from->getId()); + if (array_key_exists('joinTable', $mapping)) { + foreach ($mapping['joinTable']['joinColumns'] as $columns) { + $conditions[] = sprintf('%s = %d', $columns['referencedColumnName'], $from->getId()); + } + } elseif (array_key_exists('joinColumns', $mapping)) { + foreach ($mapping['joinColumns'] as $columns) { + $conditions[] = sprintf('%s = %d', $columns['name'], $from->getId()); + } } return sprintf( diff --git a/src/Bundle/ChillPersonBundle/Resources/views/PersonDuplicate/_sidepane.html.twig b/src/Bundle/ChillPersonBundle/Resources/views/PersonDuplicate/_sidepane.html.twig index 1e770062a..b8edcc437 100644 --- a/src/Bundle/ChillPersonBundle/Resources/views/PersonDuplicate/_sidepane.html.twig +++ b/src/Bundle/ChillPersonBundle/Resources/views/PersonDuplicate/_sidepane.html.twig @@ -4,7 +4,7 @@
  • {{ 'gender'|trans }}: {{ person.gender|trans }}
  • {{ 'maritalStatus'|trans }}: - {% if person.maritalStatus.name %}{{ person.maritalStatus.name|localize_translatable_string }}{% endif %}
  • + {% if person.maritalStatus %}{{ person.maritalStatus.name|localize_translatable_string }}{% endif %}
  • {{ 'birthdate'|trans }}: {% if person.birthdate is not null %}{{ person.birthdate|format_date('short') }}{% endif %}
  • {{ 'placeOfBirth'|trans }}: From d825a8509bcf8aaf0df4037231c167495c6f3a41 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Wed, 12 Jul 2023 10:42:46 +0200 Subject: [PATCH 02/45] FIX [duplicate][delete] fix error messages for treating the accompaning_period_work linked to the person that will be deleted --- .../ChillPersonBundle/Actions/Remove/PersonMove.php | 10 ++++++++-- .../views/PersonDuplicate/_sidepane.html.twig | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php b/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php index 4e2f7468e..76a6f5b8d 100644 --- a/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php @@ -133,8 +133,14 @@ class PersonMove $conditions = []; - foreach ($mapping['joinColumns'] as $columns) { - $conditions[] = sprintf('%s = %d', $columns['name'], $from->getId()); + if (array_key_exists('joinTable', $mapping)) { + foreach ($mapping['joinTable']['joinColumns'] as $columns) { + $conditions[] = sprintf('%s = %d', $columns['referencedColumnName'], $from->getId()); + } + } elseif (array_key_exists('joinColumns', $mapping)) { + foreach ($mapping['joinColumns'] as $columns) { + $conditions[] = sprintf('%s = %d', $columns['name'], $from->getId()); + } } return sprintf( diff --git a/src/Bundle/ChillPersonBundle/Resources/views/PersonDuplicate/_sidepane.html.twig b/src/Bundle/ChillPersonBundle/Resources/views/PersonDuplicate/_sidepane.html.twig index 1e770062a..b8edcc437 100644 --- a/src/Bundle/ChillPersonBundle/Resources/views/PersonDuplicate/_sidepane.html.twig +++ b/src/Bundle/ChillPersonBundle/Resources/views/PersonDuplicate/_sidepane.html.twig @@ -4,7 +4,7 @@
  • {{ 'gender'|trans }}: {{ person.gender|trans }}
  • {{ 'maritalStatus'|trans }}: - {% if person.maritalStatus.name %}{{ person.maritalStatus.name|localize_translatable_string }}{% endif %}
  • + {% if person.maritalStatus %}{{ person.maritalStatus.name|localize_translatable_string }}{% endif %}
  • {{ 'birthdate'|trans }}: {% if person.birthdate is not null %}{{ person.birthdate|format_date('short') }}{% endif %}
  • {{ 'placeOfBirth'|trans }}: From fcd4c3b92eef7963b6ce0d1096648852ccbec88a Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Wed, 12 Jul 2023 11:12:22 +0200 Subject: [PATCH 03/45] DX add changie --- .changes/unreleased/Fixed-20230712-111206.yaml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changes/unreleased/Fixed-20230712-111206.yaml diff --git a/.changes/unreleased/Fixed-20230712-111206.yaml b/.changes/unreleased/Fixed-20230712-111206.yaml new file mode 100644 index 000000000..7bec6095e --- /dev/null +++ b/.changes/unreleased/Fixed-20230712-111206.yaml @@ -0,0 +1,5 @@ +kind: Fixed +body: reinstate the fusion of duplicate persons +time: 2023-07-12T11:12:06.673925762+02:00 +custom: + Issue: "107" From 04235409769d55a3b5277e23b6c1f2e31a184218 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Wed, 12 Jul 2023 11:17:25 +0200 Subject: [PATCH 04/45] FIX [duplicate][personpicker] use dynamic person picker to assign duplicate manually --- .../Form/PersonFindManuallyDuplicateType.php | 4 ++-- .../views/PersonDuplicate/find_manually.html.twig | 12 ++++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Form/PersonFindManuallyDuplicateType.php b/src/Bundle/ChillPersonBundle/Form/PersonFindManuallyDuplicateType.php index b9dfad240..1d7697332 100644 --- a/src/Bundle/ChillPersonBundle/Form/PersonFindManuallyDuplicateType.php +++ b/src/Bundle/ChillPersonBundle/Form/PersonFindManuallyDuplicateType.php @@ -11,7 +11,7 @@ declare(strict_types=1); namespace Chill\PersonBundle\Form; -use Chill\PersonBundle\Form\Type\PickPersonType; +use Chill\PersonBundle\Form\Type\PickPersonDynamicType; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\HiddenType; use Symfony\Component\Form\FormBuilderInterface; @@ -21,7 +21,7 @@ class PersonFindManuallyDuplicateType extends AbstractType public function buildForm(FormBuilderInterface $builder, array $options) { $builder - ->add('person', PickPersonType::class, [ + ->add('person', PickPersonDynamicType::class, [ 'label' => 'Find duplicate', 'mapped' => false, ]) diff --git a/src/Bundle/ChillPersonBundle/Resources/views/PersonDuplicate/find_manually.html.twig b/src/Bundle/ChillPersonBundle/Resources/views/PersonDuplicate/find_manually.html.twig index 8769ae6da..982690f00 100644 --- a/src/Bundle/ChillPersonBundle/Resources/views/PersonDuplicate/find_manually.html.twig +++ b/src/Bundle/ChillPersonBundle/Resources/views/PersonDuplicate/find_manually.html.twig @@ -8,9 +8,9 @@ {% block content %}
    - +

    {{ 'Désigner un dossier doublon'|trans }}

    - + {{ form_start(form) }} {{ form_rest(form) }} @@ -29,3 +29,11 @@
    {% endblock %} + +{% block js %} + {{ encore_entry_script_tags('mod_pickentity_type') }} +{% endblock %} + +{% block css %} + {{ encore_entry_link_tags('mod_pickentity_type') }} +{% endblock %} From e0cdf06e9987eb9ff1d7c853497d327a7b55b1a8 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Thu, 13 Jul 2023 15:30:33 +0200 Subject: [PATCH 05/45] FIX [duplicate] adding test and handlers to handle fusion of doublons with special cases - after codewithme session --- ...AccompanyingPeriodParticipationHandler.php | 35 +++++ .../PersonMoveCenterHistoryHandler.php | 45 ++++++ .../Handler/PersonMoveHouseholdHandler.php | 38 +++++ .../Actions/Remove/PersonMove.php | 69 +++++---- .../Actions/Remove/PersonMoveManager.php | 48 ++++++ .../Remove/PersonMoveSqlHandlerInterface.php | 19 +++ .../ChillPersonBundle/ChillPersonBundle.php | 3 + .../Tests/Action/Remove/PersonMoveTest.php | 138 ++++++++++++++++++ .../config/services/actions.yaml | 14 +- 9 files changed, 377 insertions(+), 32 deletions(-) create mode 100644 src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveAccompanyingPeriodParticipationHandler.php create mode 100644 src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php create mode 100644 src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveHouseholdHandler.php create mode 100644 src/Bundle/ChillPersonBundle/Actions/Remove/PersonMoveManager.php create mode 100644 src/Bundle/ChillPersonBundle/Actions/Remove/PersonMoveSqlHandlerInterface.php create mode 100644 src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveAccompanyingPeriodParticipationHandler.php b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveAccompanyingPeriodParticipationHandler.php new file mode 100644 index 000000000..83d8f201a --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveAccompanyingPeriodParticipationHandler.php @@ -0,0 +1,35 @@ +getId(), $from->getId(), $to->getId()); + + $deleteSql = sprintf(<<getId()); + + return [$sqlInsert, $deleteSql]; + } +} diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php new file mode 100644 index 000000000..25767d175 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php @@ -0,0 +1,45 @@ +getId(), $from->getId(), $to->getId()); + + $updateSql = sprintf(<<getId()); + } + +} diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveHouseholdHandler.php b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveHouseholdHandler.php new file mode 100644 index 000000000..07035f569 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveHouseholdHandler.php @@ -0,0 +1,38 @@ +getId(), $from->getId(), $to->getId()); + + $deleteSql = sprintf(<<getId()); + + return [$sqlInsert, $deleteSql]; + } + +} diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php b/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php index 76a6f5b8d..b0530d6c3 100644 --- a/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php @@ -35,22 +35,11 @@ use function in_array; */ class PersonMove { - /** - * @var EntityManagerInterface - */ - protected $em; - - /** - * @var EventDispatcherInterface - */ - protected $eventDispatcher; - public function __construct( - EntityManagerInterface $em, - EventDispatcherInterface $eventDispatcher + private EntityManagerInterface $em, + private PersonMoveManager $personMoveManager, + private EventDispatcherInterface $eventDispatcher ) { - $this->em = $em; - $this->eventDispatcher = $eventDispatcher; } /** @@ -88,9 +77,16 @@ class PersonMove } foreach ($metadata->getAssociationMappings() as $field => $mapping) { + + if ($this->personMoveManager->hasHandler($metadata->getName(), $field)) { + $sqls = array_merge($sqls, $this->personMoveManager->getSqls($metadata->getName(), $field, $from, $to)); + continue; + } + if (in_array($mapping['sourceEntity'], $this->getIgnoredEntities(), true)) { continue; } + if (Person::class === $mapping['targetEntity'] and true === $mapping['isOwningSide']) { if (in_array($mapping['sourceEntity'], $toDelete, true)) { $sql = $this->createDeleteSQL($metadata, $from, $field); @@ -101,18 +97,10 @@ class PersonMove ['to' => $to->getId(), 'original_action' => 'move'] ); $this->eventDispatcher->dispatch(ActionEvent::DELETE, $event); + $sqls = array_merge($sqls, $event->getPreSql(), [$event->getSqlStatement()], $event->getPostSql()); } else { - $sql = $this->createMoveSQL($metadata, $from, $to, $field); - $event = new ActionEvent( - $from->getId(), - $metadata->getName(), - $sql, - ['to' => $to->getId(), 'original_action' => 'move'] - ); - $this->eventDispatcher->dispatch(ActionEvent::MOVE, $event); + $sqls = array_merge($sqls, $this->createMoveSQLs($metadata, $from, $to, $field)); } - - $sqls = array_merge($sqls, $event->getPreSql(), [$event->getSqlStatement()], $event->getPostSql()); } } } @@ -150,7 +138,7 @@ class PersonMove ); } - private function createMoveSQL(ClassMetadata $metadata, Person $from, Person $to, $field): string + private function createMoveSQLs($metadata, Person $from, Person $to, $field): array { $mapping = $metadata->getAssociationMapping($field); @@ -160,9 +148,34 @@ class PersonMove $tableName = ''; if (array_key_exists('joinTable', $mapping)) { + // there is a join_table: we have to find conflict $tableName = (null !== ($mapping['joinTable']['schema'] ?? null) ? $mapping['joinTable']['schema'] . '.' : '') . $mapping['joinTable']['name']; + $sqlInsert = sprintf( + "INSERT INTO %s (%s, %s) SELECT %d, %s FROM %s WHERE %s = %d ON CONFLICT DO NOTHING", + $tableName, + $mapping['joinTable']['inverseJoinColumns'][0]['name'], // person_id + $mapping['joinTable']['joinColumns'][0]['name'], // something_else_id + $to->getId(), + $mapping['joinTable']['joinColumns'][0]['name'], // something_else_id + $tableName, + $mapping['joinTable']['inverseJoinColumns'][0]['name'], // person_id + $from->getId() + + ); + $deleteSql = sprintf( + "DELETE FROM %s WHERE %s = %d", + $tableName, + $mapping['joinTable']['inverseJoinColumns'][0]['name'], // person_id + $from->getId() + + ); + + return [ + $sqlInsert, $deleteSql + ]; + foreach ($mapping['joinTable']['inverseJoinColumns'] as $columns) { $sets[] = sprintf('%s = %d', $columns['name'], $to->getId()); } @@ -176,18 +189,17 @@ class PersonMove $sets[] = sprintf('%s = %d', $columns['name'], $to->getId()); } - foreach ($mapping['joinColumns'] as $columns) { $conditions[] = sprintf('%s = %d', $columns['name'], $from->getId()); } } - return sprintf( + return [sprintf( 'UPDATE %s SET %s WHERE %s', $tableName, implode(' ', $sets), implode(' AND ', $conditions) - ); + )]; } /** @@ -198,7 +210,6 @@ class PersonMove { return [ Person\PersonCenterHistory::class, - HouseholdMember::class, AccompanyingPeriodParticipation::class, AccompanyingPeriod\AccompanyingPeriodWork::class, Relationship::class diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMoveManager.php b/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMoveManager.php new file mode 100644 index 000000000..a7bd1188a --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMoveManager.php @@ -0,0 +1,48 @@ + + */ + private iterable $handlers, + ) + { + } + + /** + * @param class-string $className + * @param string $field + * @return bool + */ + public function hasHandler(string $className, string $field): bool + { + foreach ($this->handlers as $handler) { + if ($handler->supports($className, $field)) { + return true; + } + } + + return false; + } + + /** + * @param class-string $className + * @return array + */ + public function getSqls(string $className, string $field, Person $from, Person $to): array + { + foreach ($this->handlers as $handler) { + if ($handler->supports($className, $field)) { + return $handler->getSqls($className, $field, $from, $to); + } + } + return []; + } + +} diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMoveSqlHandlerInterface.php b/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMoveSqlHandlerInterface.php new file mode 100644 index 000000000..9958f59df --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMoveSqlHandlerInterface.php @@ -0,0 +1,19 @@ + + */ + public function getSqls(string $className, string $field, Person $from, Person $to): array; +} diff --git a/src/Bundle/ChillPersonBundle/ChillPersonBundle.php b/src/Bundle/ChillPersonBundle/ChillPersonBundle.php index 4233a7914..43c38aa36 100644 --- a/src/Bundle/ChillPersonBundle/ChillPersonBundle.php +++ b/src/Bundle/ChillPersonBundle/ChillPersonBundle.php @@ -11,6 +11,7 @@ declare(strict_types=1); namespace Chill\PersonBundle; +use Chill\PersonBundle\Actions\Remove\PersonMoveSqlHandlerInterface; use Chill\PersonBundle\DependencyInjection\CompilerPass\AccompanyingPeriodTimelineCompilerPass; use Chill\PersonBundle\Service\EntityInfo\AccompanyingPeriodInfoUnionQueryPartInterface; use Chill\PersonBundle\Widget\PersonListWidgetFactory; @@ -29,5 +30,7 @@ class ChillPersonBundle extends Bundle $container->addCompilerPass(new AccompanyingPeriodTimelineCompilerPass()); $container->registerForAutoconfiguration(AccompanyingPeriodInfoUnionQueryPartInterface::class) ->addTag('chill_person.accompanying_period_info_part'); + $container->registerForAutoconfiguration(PersonMoveSqlHandlerInterface::class) + ->addTag('chill_person.person_move_handler'); } } diff --git a/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php b/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php new file mode 100644 index 000000000..88869bf7d --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php @@ -0,0 +1,138 @@ + + */ + private static $entitiesToDelete = []; + + public function setUp(): void + { + self::bootKernel(); + $this->em = self::$container->get(EntityManagerInterface::class); + $this->personMoveManager = self::$container->get(PersonMoveManager::class); + $this->eventDispatcher = self::$container->get(EventDispatcherInterface::class); + } + + public static function tearDownAfterClass(): void + { + self::bootKernel(); + $em = self::$container->get(EntityManagerInterface::class); + + foreach (self::$entitiesToDelete as list($class, $id)) { + $entity = $em->find($class, $id); + + if (null !== $entity) { + $em->remove($entity); + } + } + + $em->flush(); + } + + /** + * @dataProvider dataProviderMovePerson + */ + public function testMovePerson(Person $personA, Person $personB, string $message): void + { + $move = new PersonMove($this->em, $this->personMoveManager, $this->eventDispatcher); + $sqls = $move->getSQL($personA, $personB); + //$conn = $this->em->getConnection(); + $this->em->getConnection()->transactional(function (Connection $conn) use ($personA, $personB, $sqls) { + foreach ($sqls as $sql) { + $conn->executeStatement($sql); + } + }); + + $personA = $this->em->find(Person::class, $personA->getId()); + $personB = $this->em->find(Person::class, $personB->getId()); + + self::assertNull($personA?->getId(), $message); + self::assertNotNull($personB?->getId(), $message); + } + + public function dataProviderMovePerson(): iterable + { + $this->setUp(); + + $personA = new Person(); + $personB = new Person(); + + $this->em->persist($personA); + $this->em->persist($personB); + + self::$entitiesToDelete[] = [Person::class, $personA]; + self::$entitiesToDelete[] = [Person::class, $personB]; + + yield [$personA, $personB, "move 2 people without any associated data"]; + + $personA = new Person(); + $personB = new Person(); + + $activity = new Activity(); + $activity->setDate(new \DateTime('today')); + $activity->addPerson($personA); + $activity->addPerson($personB); + + $this->em->persist($personA); + $this->em->persist($personB); + $this->em->persist($activity); + + self::$entitiesToDelete[] = [Person::class, $personA]; + self::$entitiesToDelete[] = [Person::class, $personB]; + self::$entitiesToDelete[] = [Activity::class, $activity]; + + yield [$personA, $personB, "move 2 people having an activity"]; + + $personA = new Person(); + $personB = new Person(); + $household = new Household(); + $household->addMember( + $memberA = (new HouseholdMember())->setPerson($personA)->setShareHousehold(true) + ->setStartDate(new \DateTimeImmutable('2023-01-01')) + ); + $household->addMember( + $memberB = (new HouseholdMember())->setPerson($personB)->setShareHousehold(true) + ->setStartDate(new \DateTimeImmutable('2023-01-01')) + ); + + + + $this->em->persist($personA); + $this->em->persist($personB); + $this->em->persist($household); + $this->em->persist($memberA); + $this->em->persist($memberB); + + self::$entitiesToDelete[] = [Person::class, $personA]; + self::$entitiesToDelete[] = [Person::class, $personB]; + self::$entitiesToDelete[] = [HouseholdMember::class, $memberA]; + self::$entitiesToDelete[] = [HouseholdMember::class, $memberB]; + self::$entitiesToDelete[] = [Household::class, $household]; + + yield [$personA, $personB, "move 2 people having the same household at the same time"]; + + $this->em->flush(); + $this->em->clear(); + } +} diff --git a/src/Bundle/ChillPersonBundle/config/services/actions.yaml b/src/Bundle/ChillPersonBundle/config/services/actions.yaml index e4c6c6621..d6e2c80a5 100644 --- a/src/Bundle/ChillPersonBundle/config/services/actions.yaml +++ b/src/Bundle/ChillPersonBundle/config/services/actions.yaml @@ -1,5 +1,13 @@ services: - Chill\PersonBundle\Actions\Remove\PersonMove: + _defaults: + autowire: true + autoconfigure: true + + Chill\PersonBundle\Actions\Remove\PersonMove: ~ + + Chill\PersonBundle\Actions\Remove\PersonMoveManager: arguments: - $em: '@Doctrine\ORM\EntityManagerInterface' - $eventDispatcher: '@Symfony\Component\EventDispatcher\EventDispatcherInterface' \ No newline at end of file + $handlers: !tagged_iterator chill_person.person_move_handler + + Chill\PersonBundle\Actions\Remove\Handler\: + resource: '../../Actions/Remove/Handler' From 860e076b89bf052f4b6a26bad155268b2d361a53 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Thu, 13 Jul 2023 16:18:39 +0200 Subject: [PATCH 06/45] [test] add dataprovider for testing move person where both are participating in the same parcours --- .../PersonMoveCenterHistoryHandler.php | 45 ------------------- .../Tests/Action/Remove/PersonMoveTest.php | 19 ++++++++ 2 files changed, 19 insertions(+), 45 deletions(-) delete mode 100644 src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php deleted file mode 100644 index 25767d175..000000000 --- a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php +++ /dev/null @@ -1,45 +0,0 @@ -getId(), $from->getId(), $to->getId()); - - $updateSql = sprintf(<<getId()); - } - -} diff --git a/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php b/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php index 88869bf7d..1a49783c8 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php @@ -5,6 +5,8 @@ namespace Action\Remove; use Chill\ActivityBundle\Entity\Activity; use Chill\PersonBundle\Actions\Remove\PersonMove; use Chill\PersonBundle\Actions\Remove\PersonMoveManager; +use Chill\PersonBundle\Entity\AccompanyingPeriod; +use Chill\PersonBundle\Entity\AccompanyingPeriodParticipation; use Chill\PersonBundle\Entity\Household\Household; use Chill\PersonBundle\Entity\Household\HouseholdMember; use Chill\PersonBundle\Entity\Person; @@ -132,6 +134,23 @@ class PersonMoveTest extends KernelTestCase yield [$personA, $personB, "move 2 people having the same household at the same time"]; + $personA = new Person(); + $personB = new Person(); + $parcours = new AccompanyingPeriod(); + + $parcours->addPerson($personA); + $parcours->addPerson($personB); + + $this->em->persist($personA); + $this->em->persist($personB); + $this->em->persist($parcours); + + self::$entitiesToDelete[] = [Person::class, $personA]; + self::$entitiesToDelete[] = [Person::class, $personB]; + self::$entitiesToDelete[] = [AccompanyingPeriod::class, $parcours]; + + yield [$personA, $personB, "move 2 people participating to the same parcours"]; + $this->em->flush(); $this->em->clear(); } From bb187f5463681493707eb3129a37884debaaa1ee Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Mon, 17 Jul 2023 07:50:10 +0200 Subject: [PATCH 07/45] WIP started center_history query for duplicate person merge --- ...AccompanyingPeriodParticipationHandler.php | 9 ++-- .../PersonMoveCenterHistoryHandler.php | 45 +++++++++++++++++ .../Actions/Remove/PersonMove.php | 49 +++++++++---------- .../Tests/Action/Remove/PersonMoveTest.php | 9 ++-- 4 files changed, 79 insertions(+), 33 deletions(-) create mode 100644 src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveAccompanyingPeriodParticipationHandler.php b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveAccompanyingPeriodParticipationHandler.php index 83d8f201a..5acd67126 100644 --- a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveAccompanyingPeriodParticipationHandler.php +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveAccompanyingPeriodParticipationHandler.php @@ -2,9 +2,11 @@ namespace Chill\PersonBundle\Actions\Remove\Handler; +use Chill\PersonBundle\Actions\Remove\PersonMoveSqlHandlerInterface; use Chill\PersonBundle\Entity\AccompanyingPeriodParticipation; +use Chill\PersonBundle\Entity\Person; -class PersonMoveAccompanyingPeriodParticipationHandler +class PersonMoveAccompanyingPeriodParticipationHandler implements PersonMoveSqlHandlerInterface { public function supports(string $className, string $field): bool { @@ -13,6 +15,7 @@ class PersonMoveAccompanyingPeriodParticipationHandler public function getSqls(string $className, string $field, Person $from, Person $to): array { + var_dump(__METHOD__); $insertSql = sprintf(<<getId(), $from->getId(), $to->getId()); @@ -30,6 +33,6 @@ class PersonMoveAccompanyingPeriodParticipationHandler DELETE FROM chill_person_accompanying_period_participation WHERE person_id = %d SQL, $from->getId()); - return [$sqlInsert, $deleteSql]; + return [$insertSql, $deleteSql]; } } diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php new file mode 100644 index 000000000..25767d175 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php @@ -0,0 +1,45 @@ +getId(), $from->getId(), $to->getId()); + + $updateSql = sprintf(<<getId()); + } + +} diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php b/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php index b0530d6c3..ecf17d3b4 100644 --- a/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php @@ -78,9 +78,9 @@ class PersonMove foreach ($metadata->getAssociationMappings() as $field => $mapping) { - if ($this->personMoveManager->hasHandler($metadata->getName(), $field)) { - $sqls = array_merge($sqls, $this->personMoveManager->getSqls($metadata->getName(), $field, $from, $to)); - continue; + if ($this->personMoveManager->hasHandler($metadata->getName(), $field)) { + $sqls = array_merge($sqls, $this->personMoveManager->getSqls($metadata->getName(), $field, $from, $to)); + continue; } if (in_array($mapping['sourceEntity'], $this->getIgnoredEntities(), true)) { @@ -152,29 +152,28 @@ class PersonMove $tableName = (null !== ($mapping['joinTable']['schema'] ?? null) ? $mapping['joinTable']['schema'] . '.' : '') . $mapping['joinTable']['name']; - $sqlInsert = sprintf( - "INSERT INTO %s (%s, %s) SELECT %d, %s FROM %s WHERE %s = %d ON CONFLICT DO NOTHING", - $tableName, - $mapping['joinTable']['inverseJoinColumns'][0]['name'], // person_id - $mapping['joinTable']['joinColumns'][0]['name'], // something_else_id - $to->getId(), - $mapping['joinTable']['joinColumns'][0]['name'], // something_else_id - $tableName, - $mapping['joinTable']['inverseJoinColumns'][0]['name'], // person_id - $from->getId() + $sqlInsert = sprintf( + "INSERT INTO %s (%s, %s) SELECT %d, %s FROM %s WHERE %s = %d ON CONFLICT DO NOTHING", + $tableName, + $mapping['joinTable']['inverseJoinColumns'][0]['name'], // person_id + $mapping['joinTable']['joinColumns'][0]['name'], // something_else_id + $to->getId(), + $mapping['joinTable']['joinColumns'][0]['name'], // something_else_id + $tableName, + $mapping['joinTable']['inverseJoinColumns'][0]['name'], // person_id + $from->getId() + ); - ); - $deleteSql = sprintf( - "DELETE FROM %s WHERE %s = %d", - $tableName, - $mapping['joinTable']['inverseJoinColumns'][0]['name'], // person_id - $from->getId() + $deleteSql = sprintf( + "DELETE FROM %s WHERE %s = %d", + $tableName, + $mapping['joinTable']['inverseJoinColumns'][0]['name'], // person_id + $from->getId() + ); - ); - - return [ - $sqlInsert, $deleteSql - ]; + return [ + $sqlInsert, $deleteSql + ]; foreach ($mapping['joinTable']['inverseJoinColumns'] as $columns) { $sets[] = sprintf('%s = %d', $columns['name'], $to->getId()); @@ -183,6 +182,7 @@ class PersonMove foreach ($mapping['joinTable']['inverseJoinColumns'] as $columns) { $conditions[] = sprintf('%s = %d', $columns['name'], $from->getId()); } + } elseif (array_key_exists('joinColumns', $mapping)) { $tableName = $this->getTableName($metadata); foreach ($mapping['joinColumns'] as $columns) { @@ -210,7 +210,6 @@ class PersonMove { return [ Person\PersonCenterHistory::class, - AccompanyingPeriodParticipation::class, AccompanyingPeriod\AccompanyingPeriodWork::class, Relationship::class ]; diff --git a/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php b/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php index 1a49783c8..05fc98aa6 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php @@ -59,12 +59,13 @@ class PersonMoveTest extends KernelTestCase { $move = new PersonMove($this->em, $this->personMoveManager, $this->eventDispatcher); $sqls = $move->getSQL($personA, $personB); - //$conn = $this->em->getConnection(); - $this->em->getConnection()->transactional(function (Connection $conn) use ($personA, $personB, $sqls) { + $conn = $this->em->getConnection(); +// $this->em->getConnection()->transactional(function (Connection $conn) use ($personA, $personB, $sqls) { foreach ($sqls as $sql) { +// var_dump($sql); $conn->executeStatement($sql); } - }); +// }); $personA = $this->em->find(Person::class, $personA->getId()); $personB = $this->em->find(Person::class, $personB->getId()); @@ -118,8 +119,6 @@ class PersonMoveTest extends KernelTestCase ->setStartDate(new \DateTimeImmutable('2023-01-01')) ); - - $this->em->persist($personA); $this->em->persist($personB); $this->em->persist($household); From f02d97ddb093c523e9a5be52bd08c177d82a9cc2 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Thu, 27 Jul 2023 10:18:11 +0200 Subject: [PATCH 08/45] FEATURE [centerHistory] attempt to complete test for centerHistory --- ...AccompanyingPeriodParticipationHandler.php | 1 - .../PersonMoveCenterHistoryHandler.php | 59 +++++++++++-------- .../Tests/Action/Remove/PersonMoveTest.php | 52 +++++++++++++++- 3 files changed, 87 insertions(+), 25 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveAccompanyingPeriodParticipationHandler.php b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveAccompanyingPeriodParticipationHandler.php index 5acd67126..e164dcaf8 100644 --- a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveAccompanyingPeriodParticipationHandler.php +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveAccompanyingPeriodParticipationHandler.php @@ -15,7 +15,6 @@ class PersonMoveAccompanyingPeriodParticipationHandler implements PersonMoveSqlH public function getSqls(string $className, string $field, Person $from, Person $to): array { - var_dump(__METHOD__); $insertSql = sprintf(<<getId(), $from->getId(), $to->getId()); + $sqlStatements = []; - $updateSql = sprintf(<<em->getRepository(Person\PersonCenterHistory::class)->findBy(['person' => $from->getId()]); +// $centerHistoriesA = $from->getCenterHistory()->toArray(); +// var_dump($centerHistoriesA); + $datesArrayA = array_map(fn($centerHistory) => $centerHistory->getStartDate(), $centerHistoriesA); + if (!count($datesArrayA) === 0) { + $oldestDateA = min($datesArrayA); + } + $centerHistoriesB = $this->em->getRepository(Person\PersonCenterHistory::class)->findBy(['person' => $to->getId()]); +// $centerHistoriesB = $to->getCenterHistory()->toArray(); +// var_dump($centerHistoriesB); + $datesArrayB = array_map(fn($centerHistory) => $centerHistory->getStartDate(), $centerHistoriesB); + + if (!count($datesArrayB) === 0) { + $oldestDateB = min($datesArrayB); + + $indexOldestCenter = array_search(min($centerHistoriesB), $centerHistoriesB); + $oldestCenterHistoryB = $centerHistoriesB[$indexOldestCenter]; + } + + $sqlDelete = sprintf("delete FROM chill_person_person_center_history WHERE person_id = %d", $from->getId()); + + $sqlStatements = [$sqlDelete]; + + if ($oldestDateA <= $oldestDateB) { + $sqlInsert = sprintf("update chill_person_person_center_history set startDate = '%s'::date WHERE id = %d", $oldestDateA->format('Y-m-d'), $oldestCenterHistoryB->getId()); + + $sqlStatements = [$sqlInsert, $sqlDelete]; + } + + return $sqlStatements; - SQL, $to->getId()); } } diff --git a/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php b/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php index 05fc98aa6..fa6c5faa9 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php @@ -3,6 +3,7 @@ namespace Action\Remove; use Chill\ActivityBundle\Entity\Activity; +use Chill\MainBundle\Entity\Center; use Chill\PersonBundle\Actions\Remove\PersonMove; use Chill\PersonBundle\Actions\Remove\PersonMoveManager; use Chill\PersonBundle\Entity\AccompanyingPeriod; @@ -62,7 +63,6 @@ class PersonMoveTest extends KernelTestCase $conn = $this->em->getConnection(); // $this->em->getConnection()->transactional(function (Connection $conn) use ($personA, $personB, $sqls) { foreach ($sqls as $sql) { -// var_dump($sql); $conn->executeStatement($sql); } // }); @@ -74,6 +74,56 @@ class PersonMoveTest extends KernelTestCase self::assertNotNull($personB?->getId(), $message); } + public function testMovePersonCenterHistory(): void + { + $personA = new Person(); + $personB = new Person(); + $centerA = ($this->em->getRepository(Center::class))->find(1); + $centerB = ($this->em->getRepository(Center::class))->find(2); + + $personCenterHistoryAFirst = (new Person\PersonCenterHistory())->setPerson($personA)->setCenter($centerA)->setStartDate(new \DateTimeImmutable('2023-01-01'))->setEndDate(new \DateTimeImmutable('2023-06-30')); + $personCenterHistoryASecond = (new Person\PersonCenterHistory())->setPerson($personA)->setCenter($centerB)->setStartDate(new \DateTimeImmutable('2023-06-30'))->setEndDate(new \DateTimeImmutable('2023-09-30')); + + $personCenterHistoryBFirst = (new Person\PersonCenterHistory())->setPerson($personB)->setCenter($centerA)->setStartDate(new \DateTimeImmutable('2023-01-01'))->setEndDate(new \DateTimeImmutable('2023-07-15')); + $personCenterHistoryBSecond = (new Person\PersonCenterHistory())->setPerson($personB)->setCenter($centerB)->setStartDate(new \DateTimeImmutable('2023-07-15'))->setEndDate(new \DateTimeImmutable('2023-09-30')); + + $this->em->persist($personA); + $this->em->persist($personB); + $this->em->persist($personCenterHistoryAFirst); + $this->em->persist($personCenterHistoryASecond); + $this->em->persist($personCenterHistoryBFirst); + $this->em->persist($personCenterHistoryBSecond); + + $this->em->flush(); + $this->em->clear(); + + $move = new PersonMove($this->em, $this->personMoveManager, $this->eventDispatcher); + $sqls = $move->getSQL($personA, $personB); + $conn = $this->em->getConnection(); +// $this->em->getConnection()->transactional(function (Connection $conn) use ($personA, $personB, $sqls) { + foreach ($sqls as $sql) { + $conn->executeStatement($sql); + } +// }); + + $personA = $this->em->find(Person::class, $personA->getId()); + $personB = $this->em->find(Person::class, $personB->getId()); + $remainingCenterHistory = $this->em->getRepository(Person\PersonCenterHistory::class)->findBy(['person' => $personB]); + + $message = 'Move persons with overlapping center histories'; + + self::assertNull($personA?->getId(), $message); + self::assertNotNull($personB?->getId(), $message); + self::assertEquals(new \DateTimeImmutable('2023-01-01'), $remainingCenterHistory->getStartDate()); + + self::$entitiesToDelete[] = [Person::class, $personA]; + self::$entitiesToDelete[] = [Person::class, $personB]; + self::$entitiesToDelete[] = [Person\PersonCenterHistory::class, $personCenterHistoryAFirst]; + self::$entitiesToDelete[] = [Person\PersonCenterHistory::class, $personCenterHistoryASecond]; + self::$entitiesToDelete[] = [Person\PersonCenterHistory::class, $personCenterHistoryBFirst]; + self::$entitiesToDelete[] = [Person\PersonCenterHistory::class, $personCenterHistoryBSecond]; + } + public function dataProviderMovePerson(): iterable { $this->setUp(); From 2921073da3ed8dea09ba7d6a2e5348f707a0a629 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Thu, 27 Jul 2023 10:34:19 +0200 Subject: [PATCH 09/45] [centerHistory] add conditions to be sure at least one center history exists per person --- .../Remove/Handler/PersonMoveCenterHistoryHandler.php | 7 ++++--- .../Tests/Action/Remove/PersonMoveTest.php | 8 ++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php index 1c1b2f8f0..53ec57843 100644 --- a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php @@ -20,10 +20,11 @@ class PersonMoveCenterHistoryHandler implements PersonMoveSqlHandlerInterface public function getSqls(string $className, string $field, Person $from, Person $to): array { $sqlStatements = []; + $oldestDateA = null; + $oldestDateB = null; $centerHistoriesA = $this->em->getRepository(Person\PersonCenterHistory::class)->findBy(['person' => $from->getId()]); // $centerHistoriesA = $from->getCenterHistory()->toArray(); -// var_dump($centerHistoriesA); $datesArrayA = array_map(fn($centerHistory) => $centerHistory->getStartDate(), $centerHistoriesA); if (!count($datesArrayA) === 0) { $oldestDateA = min($datesArrayA); @@ -31,7 +32,6 @@ class PersonMoveCenterHistoryHandler implements PersonMoveSqlHandlerInterface $centerHistoriesB = $this->em->getRepository(Person\PersonCenterHistory::class)->findBy(['person' => $to->getId()]); // $centerHistoriesB = $to->getCenterHistory()->toArray(); -// var_dump($centerHistoriesB); $datesArrayB = array_map(fn($centerHistory) => $centerHistory->getStartDate(), $centerHistoriesB); if (!count($datesArrayB) === 0) { @@ -45,12 +45,13 @@ class PersonMoveCenterHistoryHandler implements PersonMoveSqlHandlerInterface $sqlStatements = [$sqlDelete]; - if ($oldestDateA <= $oldestDateB) { + if ((null !== $oldestDateA && null !== $oldestDateB) && $oldestDateA <= $oldestDateB) { $sqlInsert = sprintf("update chill_person_person_center_history set startDate = '%s'::date WHERE id = %d", $oldestDateA->format('Y-m-d'), $oldestCenterHistoryB->getId()); $sqlStatements = [$sqlInsert, $sqlDelete]; } + var_dump($sqlStatements); return $sqlStatements; } diff --git a/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php b/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php index fa6c5faa9..613baaf44 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php @@ -61,11 +61,11 @@ class PersonMoveTest extends KernelTestCase $move = new PersonMove($this->em, $this->personMoveManager, $this->eventDispatcher); $sqls = $move->getSQL($personA, $personB); $conn = $this->em->getConnection(); -// $this->em->getConnection()->transactional(function (Connection $conn) use ($personA, $personB, $sqls) { + $this->em->getConnection()->transactional(function (Connection $conn) use ($personA, $personB, $sqls) { foreach ($sqls as $sql) { $conn->executeStatement($sql); } -// }); + }); $personA = $this->em->find(Person::class, $personA->getId()); $personB = $this->em->find(Person::class, $personB->getId()); @@ -100,11 +100,11 @@ class PersonMoveTest extends KernelTestCase $move = new PersonMove($this->em, $this->personMoveManager, $this->eventDispatcher); $sqls = $move->getSQL($personA, $personB); $conn = $this->em->getConnection(); -// $this->em->getConnection()->transactional(function (Connection $conn) use ($personA, $personB, $sqls) { + $this->em->getConnection()->transactional(function (Connection $conn) use ($personA, $personB, $sqls) { foreach ($sqls as $sql) { $conn->executeStatement($sql); } -// }); + }); $personA = $this->em->find(Person::class, $personA->getId()); $personB = $this->em->find(Person::class, $personB->getId()); From 7758d62f866c5862968e618c99ab59ccd153661f Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Thu, 27 Jul 2023 10:47:42 +0200 Subject: [PATCH 10/45] FIX remove var_dump --- .../Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php index 53ec57843..2798fd99e 100644 --- a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php @@ -51,7 +51,6 @@ class PersonMoveCenterHistoryHandler implements PersonMoveSqlHandlerInterface $sqlStatements = [$sqlInsert, $sqlDelete]; } - var_dump($sqlStatements); return $sqlStatements; } From fd79692f6df24c0aae361f64ecc6e96eb186eaa6 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Thu, 3 Aug 2023 11:38:13 +0200 Subject: [PATCH 11/45] FEATURE [relationships] Add personMoveRelationHandler and test for this --- .../Handler/PersonMoveRelationHandler.php | 38 +++++++++++++++++++ .../Tests/Action/Remove/PersonMoveTest.php | 30 +++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveRelationHandler.php diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveRelationHandler.php b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveRelationHandler.php new file mode 100644 index 000000000..3454fce24 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveRelationHandler.php @@ -0,0 +1,38 @@ +getId(), $from->getId(), $to->getId(), $from->getId(), $from->getId(), $to->getId()); + + $deleteSql = sprintf(<<getId(), $from->getId()); + + return [$insertSql, $deleteSql]; + } +} diff --git a/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php b/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php index 613baaf44..d0ebf9a85 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php @@ -4,6 +4,7 @@ namespace Action\Remove; use Chill\ActivityBundle\Entity\Activity; use Chill\MainBundle\Entity\Center; +use Chill\MainBundle\Entity\User; use Chill\PersonBundle\Actions\Remove\PersonMove; use Chill\PersonBundle\Actions\Remove\PersonMoveManager; use Chill\PersonBundle\Entity\AccompanyingPeriod; @@ -11,6 +12,9 @@ use Chill\PersonBundle\Entity\AccompanyingPeriodParticipation; use Chill\PersonBundle\Entity\Household\Household; use Chill\PersonBundle\Entity\Household\HouseholdMember; use Chill\PersonBundle\Entity\Person; +use Chill\PersonBundle\Entity\Relationships\Relation; +use Chill\PersonBundle\Entity\Relationships\Relationship; +use Chill\PersonBundle\Repository\PersonRepository; use Doctrine\DBAL\Connection; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; @@ -200,6 +204,32 @@ class PersonMoveTest extends KernelTestCase yield [$personA, $personB, "move 2 people participating to the same parcours"]; + $personA = new Person(); + $personB = new Person(); + $relationship = new Relationship(); + $relation = new Relation(); + $user = new User(); + + $relationship->setRelation($relation); + $relationship->setToPerson($personA); + $relationship->setFromPerson($personB); + $relationship->setReverse(false); + $relationship->setCreatedBy($user); + + $this->em->persist($personA); + $this->em->persist($personB); + $this->em->persist($relation); + $this->em->persist($user); + $this->em->persist($relationship); + + self::$entitiesToDelete[] = [Person::class, $personA]; + self::$entitiesToDelete[] = [Person::class, $personB]; + self::$entitiesToDelete[] = [Relation::class, $relation]; + self::$entitiesToDelete[] = [User::class, $user]; + self::$entitiesToDelete[] = [Relationship::class, $relationship]; + + yield [$personA, $personB, "move 2 people with a relationship"]; + $this->em->flush(); $this->em->clear(); } From 3af065e5307342171d21c68c755520838bdb688c Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Thu, 3 Aug 2023 11:40:47 +0200 Subject: [PATCH 12/45] WIP [centerHistory] Try to get test to work for personCenterHistory --- .../PersonMoveCenterHistoryHandler.php | 10 +++--- .../ChillPersonBundle/Entity/Person.php | 9 +++++ .../Tests/Action/Remove/PersonMoveTest.php | 33 ++++++++++++++----- 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php index 2798fd99e..4d4203a0b 100644 --- a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php @@ -4,11 +4,11 @@ namespace Chill\PersonBundle\Actions\Remove\Handler; use Chill\PersonBundle\Actions\Remove\PersonMoveSqlHandlerInterface; use Chill\PersonBundle\Entity\Person; -use Doctrine\ORM\EntityManagerInterface; +use Chill\PersonBundle\Repository\Person\PersonCenterHistoryRepository; class PersonMoveCenterHistoryHandler implements PersonMoveSqlHandlerInterface { - public function __construct(private EntityManagerInterface $em) + public function __construct(private PersonCenterHistoryRepository $centerHistoryRepository) { } @@ -23,15 +23,13 @@ class PersonMoveCenterHistoryHandler implements PersonMoveSqlHandlerInterface $oldestDateA = null; $oldestDateB = null; - $centerHistoriesA = $this->em->getRepository(Person\PersonCenterHistory::class)->findBy(['person' => $from->getId()]); -// $centerHistoriesA = $from->getCenterHistory()->toArray(); + $centerHistoriesA = $this->centerHistoryRepository->findBy(['person' => $from]); $datesArrayA = array_map(fn($centerHistory) => $centerHistory->getStartDate(), $centerHistoriesA); if (!count($datesArrayA) === 0) { $oldestDateA = min($datesArrayA); } - $centerHistoriesB = $this->em->getRepository(Person\PersonCenterHistory::class)->findBy(['person' => $to->getId()]); -// $centerHistoriesB = $to->getCenterHistory()->toArray(); + $centerHistoriesB = $this->centerHistoryRepository->findBy(['person' => $to]); $datesArrayB = array_map(fn($centerHistory) => $centerHistory->getStartDate(), $centerHistoriesB); if (!count($datesArrayB) === 0) { diff --git a/src/Bundle/ChillPersonBundle/Entity/Person.php b/src/Bundle/ChillPersonBundle/Entity/Person.php index af8e7f92b..81e603fbc 100644 --- a/src/Bundle/ChillPersonBundle/Entity/Person.php +++ b/src/Bundle/ChillPersonBundle/Entity/Person.php @@ -1606,6 +1606,15 @@ class Person implements HasCenterInterface, TrackCreationInterface, TrackUpdateI return $this; } + public function addCenterHistory(PersonCenterHistory $newCenterHistory): self + { + if (!$this->centerHistory->contains($newCenterHistory)) { + $this->centerHistory[] = $newCenterHistory; + $newCenterHistory->setPerson($this); + } + return $this; + } + /** * @return Person */ diff --git a/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php b/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php index d0ebf9a85..64b39a20b 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php @@ -85,20 +85,38 @@ class PersonMoveTest extends KernelTestCase $centerA = ($this->em->getRepository(Center::class))->find(1); $centerB = ($this->em->getRepository(Center::class))->find(2); - $personCenterHistoryAFirst = (new Person\PersonCenterHistory())->setPerson($personA)->setCenter($centerA)->setStartDate(new \DateTimeImmutable('2023-01-01'))->setEndDate(new \DateTimeImmutable('2023-06-30')); - $personCenterHistoryASecond = (new Person\PersonCenterHistory())->setPerson($personA)->setCenter($centerB)->setStartDate(new \DateTimeImmutable('2023-06-30'))->setEndDate(new \DateTimeImmutable('2023-09-30')); - - $personCenterHistoryBFirst = (new Person\PersonCenterHistory())->setPerson($personB)->setCenter($centerA)->setStartDate(new \DateTimeImmutable('2023-01-01'))->setEndDate(new \DateTimeImmutable('2023-07-15')); - $personCenterHistoryBSecond = (new Person\PersonCenterHistory())->setPerson($personB)->setCenter($centerB)->setStartDate(new \DateTimeImmutable('2023-07-15'))->setEndDate(new \DateTimeImmutable('2023-09-30')); - $this->em->persist($personA); $this->em->persist($personB); + + $personCenterHistoryAFirst = (new Person\PersonCenterHistory())->setCenter($centerA)->setStartDate(new \DateTimeImmutable('2023-01-01'))->setEndDate(new \DateTimeImmutable('2023-06-30')); + $personCenterHistoryASecond = (new Person\PersonCenterHistory())->setCenter($centerB)->setStartDate(new \DateTimeImmutable('2023-06-30'))->setEndDate(new \DateTimeImmutable('2023-09-30')); + $personCenterHistoryBFirst = (new Person\PersonCenterHistory())->setCenter($centerA)->setStartDate(new \DateTimeImmutable('2023-01-01'))->setEndDate(new \DateTimeImmutable('2023-07-15')); + $personCenterHistoryBSecond = (new Person\PersonCenterHistory())->setCenter($centerB)->setStartDate(new \DateTimeImmutable('2023-07-15'))->setEndDate(new \DateTimeImmutable('2023-09-30')); + +// $personA->getCenterHistory()->add($personCenterHistoryAFirst); +// $personA->getCenterHistory()->add($personCenterHistoryASecond); +// $personB->getCenterHistory()->add($personCenterHistoryBFirst); +// $personB->getCenterHistory()->add($personCenterHistoryBSecond); + +// $personCenterHistoryAFirst->setPerson($personA); +// $personCenterHistoryASecond->setPerson($personA); +// $personCenterHistoryBFirst->setPerson($personB); +// $personCenterHistoryBSecond->setPerson($personB); + $this->em->persist($personCenterHistoryAFirst); $this->em->persist($personCenterHistoryASecond); $this->em->persist($personCenterHistoryBFirst); $this->em->persist($personCenterHistoryBSecond); + $personA->addCenterHistory($personCenterHistoryAFirst); + $personA->addCenterHistory($personCenterHistoryASecond); + $personB->addCenterHistory($personCenterHistoryBFirst); + $personB->addCenterHistory($personCenterHistoryBSecond); + $this->em->flush(); +// $this->em->refresh($personA); +// $this->em->refresh($personB); + $this->em->clear(); $move = new PersonMove($this->em, $this->personMoveManager, $this->eventDispatcher); @@ -112,13 +130,10 @@ class PersonMoveTest extends KernelTestCase $personA = $this->em->find(Person::class, $personA->getId()); $personB = $this->em->find(Person::class, $personB->getId()); - $remainingCenterHistory = $this->em->getRepository(Person\PersonCenterHistory::class)->findBy(['person' => $personB]); - $message = 'Move persons with overlapping center histories'; self::assertNull($personA?->getId(), $message); self::assertNotNull($personB?->getId(), $message); - self::assertEquals(new \DateTimeImmutable('2023-01-01'), $remainingCenterHistory->getStartDate()); self::$entitiesToDelete[] = [Person::class, $personA]; self::$entitiesToDelete[] = [Person::class, $personB]; From 256579af892ac55b65a6ee12b943a1e9b7a1a539 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Thu, 3 Aug 2023 11:41:36 +0200 Subject: [PATCH 13/45] FIX Assign the correct id_seq for PersonMoveAccompanyingPeriodParticipationHandler --- .../PersonMoveAccompanyingPeriodParticipationHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveAccompanyingPeriodParticipationHandler.php b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveAccompanyingPeriodParticipationHandler.php index e164dcaf8..cb18bd4cd 100644 --- a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveAccompanyingPeriodParticipationHandler.php +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveAccompanyingPeriodParticipationHandler.php @@ -17,7 +17,7 @@ class PersonMoveAccompanyingPeriodParticipationHandler implements PersonMoveSqlH { $insertSql = sprintf(<< Date: Thu, 7 Sep 2023 16:07:23 +0200 Subject: [PATCH 14/45] Make test personMove work for centers --- .../PersonMoveCenterHistoryHandler.php | 31 +++++---- .../Actions/Remove/PersonMove.php | 1 - .../ChillPersonBundle/Entity/Person.php | 1 + .../Tests/Action/Remove/PersonMoveTest.php | 65 +++++++++++-------- 4 files changed, 58 insertions(+), 40 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php index 4d4203a0b..220adcfc1 100644 --- a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php @@ -5,10 +5,14 @@ namespace Chill\PersonBundle\Actions\Remove\Handler; use Chill\PersonBundle\Actions\Remove\PersonMoveSqlHandlerInterface; use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Repository\Person\PersonCenterHistoryRepository; +use Doctrine\ORM\EntityManagerInterface; class PersonMoveCenterHistoryHandler implements PersonMoveSqlHandlerInterface { - public function __construct(private PersonCenterHistoryRepository $centerHistoryRepository) + public function __construct( + private PersonCenterHistoryRepository $centerHistoryRepository, + private EntityManagerInterface $em + ) { } @@ -23,20 +27,23 @@ class PersonMoveCenterHistoryHandler implements PersonMoveSqlHandlerInterface $oldestDateA = null; $oldestDateB = null; + $oldestCenterHistoryB = null; + $oldestCenterHistoryA = null; + $centerHistoriesA = $this->centerHistoryRepository->findBy(['person' => $from]); - $datesArrayA = array_map(fn($centerHistory) => $centerHistory->getStartDate(), $centerHistoriesA); - if (!count($datesArrayA) === 0) { - $oldestDateA = min($datesArrayA); + foreach ($centerHistoriesA as $ch) { + if ($oldestDateA === null || ($ch->getStartDate() < $oldestDateA)) { + $oldestDateA = $ch->getStartDate(); + $oldestCenterHistoryA = $ch; + } } $centerHistoriesB = $this->centerHistoryRepository->findBy(['person' => $to]); - $datesArrayB = array_map(fn($centerHistory) => $centerHistory->getStartDate(), $centerHistoriesB); - - if (!count($datesArrayB) === 0) { - $oldestDateB = min($datesArrayB); - - $indexOldestCenter = array_search(min($centerHistoriesB), $centerHistoriesB); - $oldestCenterHistoryB = $centerHistoriesB[$indexOldestCenter]; + foreach ($centerHistoriesB as $ch) { + if ($oldestDateB === null || ($ch->getStartDate() < $oldestDateB)) { + $oldestDateB = $ch->getStartDate(); + $oldestCenterHistoryB = $ch; + } } $sqlDelete = sprintf("delete FROM chill_person_person_center_history WHERE person_id = %d", $from->getId()); @@ -44,7 +51,7 @@ class PersonMoveCenterHistoryHandler implements PersonMoveSqlHandlerInterface $sqlStatements = [$sqlDelete]; if ((null !== $oldestDateA && null !== $oldestDateB) && $oldestDateA <= $oldestDateB) { - $sqlInsert = sprintf("update chill_person_person_center_history set startDate = '%s'::date WHERE id = %d", $oldestDateA->format('Y-m-d'), $oldestCenterHistoryB->getId()); + $sqlInsert = sprintf("update chill_person_person_center_history set startDate = '%s' WHERE id = %d", $oldestDateA->format('Y-m-d'), $oldestCenterHistoryB->getId()); $sqlStatements = [$sqlInsert, $sqlDelete]; } diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php b/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php index ecf17d3b4..c3ecd8ec1 100644 --- a/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php @@ -209,7 +209,6 @@ class PersonMove private function getDeleteEntities(): array { return [ - Person\PersonCenterHistory::class, AccompanyingPeriod\AccompanyingPeriodWork::class, Relationship::class ]; diff --git a/src/Bundle/ChillPersonBundle/Entity/Person.php b/src/Bundle/ChillPersonBundle/Entity/Person.php index 81e603fbc..38e51a60a 100644 --- a/src/Bundle/ChillPersonBundle/Entity/Person.php +++ b/src/Bundle/ChillPersonBundle/Entity/Person.php @@ -203,6 +203,7 @@ class Person implements HasCenterInterface, TrackCreationInterface, TrackUpdateI /** * @ORM\OneToMany(targetEntity=PersonCenterHistory::class, mappedBy="person", cascade={"persist"}) + * @ORM\OrderBy({"startDate": "ASC", "id": "ASC"}) * * @var Collection|PersonCenterHistory[] */ diff --git a/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php b/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php index 64b39a20b..14268160e 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php @@ -5,6 +5,7 @@ namespace Action\Remove; use Chill\ActivityBundle\Entity\Activity; use Chill\MainBundle\Entity\Center; use Chill\MainBundle\Entity\User; +use Chill\MainBundle\Repository\CenterRepositoryInterface; use Chill\PersonBundle\Actions\Remove\PersonMove; use Chill\PersonBundle\Actions\Remove\PersonMoveManager; use Chill\PersonBundle\Entity\AccompanyingPeriod; @@ -28,6 +29,8 @@ class PersonMoveTest extends KernelTestCase private EventDispatcherInterface $eventDispatcher; + private CenterRepositoryInterface $centerRepository; + /** * @var list */ @@ -39,6 +42,7 @@ class PersonMoveTest extends KernelTestCase $this->em = self::$container->get(EntityManagerInterface::class); $this->personMoveManager = self::$container->get(PersonMoveManager::class); $this->eventDispatcher = self::$container->get(EventDispatcherInterface::class); + $this->centerRepository = self::$container->get(CenterRepositoryInterface::class); } public static function tearDownAfterClass(): void @@ -60,21 +64,22 @@ class PersonMoveTest extends KernelTestCase /** * @dataProvider dataProviderMovePerson */ - public function testMovePerson(Person $personA, Person $personB, string $message): void + public function testMovePersonSimple(Person $personA, Person $personB, string $message): void { $move = new PersonMove($this->em, $this->personMoveManager, $this->eventDispatcher); $sqls = $move->getSQL($personA, $personB); - $conn = $this->em->getConnection(); $this->em->getConnection()->transactional(function (Connection $conn) use ($personA, $personB, $sqls) { foreach ($sqls as $sql) { $conn->executeStatement($sql); } }); - $personA = $this->em->find(Person::class, $personA->getId()); + $personsByIdOfA = $this->em->createQuery("SELECT p FROM " . Person::class . " p WHERE p.id = :id") + ->setParameter('id', $personA->getId()) + ->getResult(); $personB = $this->em->find(Person::class, $personB->getId()); - self::assertNull($personA?->getId(), $message); + self::assertCount(0, $personsByIdOfA); self::assertNotNull($personB?->getId(), $message); } @@ -82,26 +87,23 @@ class PersonMoveTest extends KernelTestCase { $personA = new Person(); $personB = new Person(); - $centerA = ($this->em->getRepository(Center::class))->find(1); - $centerB = ($this->em->getRepository(Center::class))->find(2); + [$centerA, $centerB] = $this->centerRepository->findAll(); $this->em->persist($personA); $this->em->persist($personB); - $personCenterHistoryAFirst = (new Person\PersonCenterHistory())->setCenter($centerA)->setStartDate(new \DateTimeImmutable('2023-01-01'))->setEndDate(new \DateTimeImmutable('2023-06-30')); - $personCenterHistoryASecond = (new Person\PersonCenterHistory())->setCenter($centerB)->setStartDate(new \DateTimeImmutable('2023-06-30'))->setEndDate(new \DateTimeImmutable('2023-09-30')); - $personCenterHistoryBFirst = (new Person\PersonCenterHistory())->setCenter($centerA)->setStartDate(new \DateTimeImmutable('2023-01-01'))->setEndDate(new \DateTimeImmutable('2023-07-15')); - $personCenterHistoryBSecond = (new Person\PersonCenterHistory())->setCenter($centerB)->setStartDate(new \DateTimeImmutable('2023-07-15'))->setEndDate(new \DateTimeImmutable('2023-09-30')); - -// $personA->getCenterHistory()->add($personCenterHistoryAFirst); -// $personA->getCenterHistory()->add($personCenterHistoryASecond); -// $personB->getCenterHistory()->add($personCenterHistoryBFirst); -// $personB->getCenterHistory()->add($personCenterHistoryBSecond); - -// $personCenterHistoryAFirst->setPerson($personA); -// $personCenterHistoryASecond->setPerson($personA); -// $personCenterHistoryBFirst->setPerson($personB); -// $personCenterHistoryBSecond->setPerson($personB); + $personCenterHistoryAFirst = (new Person\PersonCenterHistory())->setCenter($centerA) + ->setStartDate(new \DateTimeImmutable('2023-01-01')) + ->setEndDate(new \DateTimeImmutable('2023-06-30')); + $personCenterHistoryASecond = (new Person\PersonCenterHistory())->setCenter($centerB) + ->setStartDate(new \DateTimeImmutable('2023-06-30')) + ->setEndDate(new \DateTimeImmutable('2023-09-30')); + $personCenterHistoryBFirst = (new Person\PersonCenterHistory())->setCenter($centerA) + ->setStartDate(new \DateTimeImmutable('2023-03-01')) + ->setEndDate(new \DateTimeImmutable('2023-07-15')); + $personCenterHistoryBSecond = (new Person\PersonCenterHistory())->setCenter($centerB) + ->setStartDate(new \DateTimeImmutable('2023-07-15')) + ->setEndDate(new \DateTimeImmutable('2023-09-30')); $this->em->persist($personCenterHistoryAFirst); $this->em->persist($personCenterHistoryASecond); @@ -114,27 +116,36 @@ class PersonMoveTest extends KernelTestCase $personB->addCenterHistory($personCenterHistoryBSecond); $this->em->flush(); -// $this->em->refresh($personA); -// $this->em->refresh($personB); - $this->em->clear(); $move = new PersonMove($this->em, $this->personMoveManager, $this->eventDispatcher); $sqls = $move->getSQL($personA, $personB); - $conn = $this->em->getConnection(); $this->em->getConnection()->transactional(function (Connection $conn) use ($personA, $personB, $sqls) { foreach ($sqls as $sql) { $conn->executeStatement($sql); } }); - $personA = $this->em->find(Person::class, $personA->getId()); + $personsByIdOfA = $this->em->createQuery("SELECT p FROM " . Person::class . " p WHERE p.id = :id") + ->setParameter('id', $personA->getId()) + ->getResult(); + /** @var Person $personB */ $personB = $this->em->find(Person::class, $personB->getId()); $message = 'Move persons with overlapping center histories'; - self::assertNull($personA?->getId(), $message); + $this->em->refresh($personB); + + self::assertCount(0, $personsByIdOfA); self::assertNotNull($personB?->getId(), $message); + $centerHistoriesB = $personB->getCenterHistory(); + $oldestDate = new \DateTimeImmutable('2023-01-01'); + + $this->em->refresh($centerHistoriesB->first()); + + self::assertCount(2, $centerHistoriesB); + self::assertEquals($oldestDate, $centerHistoriesB->first()->getStartDate()); + self::$entitiesToDelete[] = [Person::class, $personA]; self::$entitiesToDelete[] = [Person::class, $personB]; self::$entitiesToDelete[] = [Person\PersonCenterHistory::class, $personCenterHistoryAFirst]; @@ -223,7 +234,7 @@ class PersonMoveTest extends KernelTestCase $personB = new Person(); $relationship = new Relationship(); $relation = new Relation(); - $user = new User(); + $user = (new User())->setUsername(uniqid())->setEmail(uniqid() . '@foo.com'); $relationship->setRelation($relation); $relationship->setToPerson($personA); From c19d1ff0c75f03cb8b1ddb728ebc4b015a90d565 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Thu, 7 Sep 2023 16:08:18 +0200 Subject: [PATCH 15/45] php cs fixer --- ...AccompanyingPeriodParticipationHandler.php | 13 ++++- .../PersonMoveCenterHistoryHandler.php | 14 +++-- .../Handler/PersonMoveHouseholdHandler.php | 17 +++++-- .../Handler/PersonMoveRelationHandler.php | 13 ++++- .../Actions/Remove/PersonMove.php | 51 ++++++++++--------- .../Actions/Remove/PersonMoveManager.php | 18 +++++-- .../Remove/PersonMoveSqlHandlerInterface.php | 11 +++- .../Tests/Action/Remove/PersonMoveTest.php | 19 +++++-- 8 files changed, 110 insertions(+), 46 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveAccompanyingPeriodParticipationHandler.php b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveAccompanyingPeriodParticipationHandler.php index cb18bd4cd..53654f397 100644 --- a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveAccompanyingPeriodParticipationHandler.php +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveAccompanyingPeriodParticipationHandler.php @@ -1,5 +1,14 @@ getId(), $from->getId(), $to->getId()); - $deleteSql = sprintf(<<getId()); diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php index 220adcfc1..c4307259f 100644 --- a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php @@ -1,19 +1,25 @@ getId(), $from->getId(), $to->getId()); + SQL, $to->getId(), $from->getId(), $to->getId()); - $deleteSql = sprintf(<<getId()); - return [$sqlInsert, $deleteSql]; + return [$sqlInsert, $deleteSql]; } } diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveRelationHandler.php b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveRelationHandler.php index 3454fce24..82ca0592c 100644 --- a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveRelationHandler.php +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveRelationHandler.php @@ -1,5 +1,14 @@ getId(), $from->getId(), $to->getId(), $from->getId(), $from->getId(), $to->getId()); - $deleteSql = sprintf(<<getId(), $from->getId()); diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php b/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php index c3ecd8ec1..067727078 100644 --- a/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php @@ -37,7 +37,7 @@ class PersonMove { public function __construct( private EntityManagerInterface $em, - private PersonMoveManager $personMoveManager, + private PersonMoveManager $personMoveManager, private EventDispatcherInterface $eventDispatcher ) { } @@ -78,9 +78,9 @@ class PersonMove foreach ($metadata->getAssociationMappings() as $field => $mapping) { - if ($this->personMoveManager->hasHandler($metadata->getName(), $field)) { - $sqls = array_merge($sqls, $this->personMoveManager->getSqls($metadata->getName(), $field, $from, $to)); - continue; + if ($this->personMoveManager->hasHandler($metadata->getName(), $field)) { + $sqls = array_merge($sqls, $this->personMoveManager->getSqls($metadata->getName(), $field, $from, $to)); + continue; } if (in_array($mapping['sourceEntity'], $this->getIgnoredEntities(), true)) { @@ -148,32 +148,28 @@ class PersonMove $tableName = ''; if (array_key_exists('joinTable', $mapping)) { - // there is a join_table: we have to find conflict + // there is a join_table: we have to find conflict $tableName = (null !== ($mapping['joinTable']['schema'] ?? null) ? $mapping['joinTable']['schema'] . '.' : '') . $mapping['joinTable']['name']; $sqlInsert = sprintf( - "INSERT INTO %s (%s, %s) SELECT %d, %s FROM %s WHERE %s = %d ON CONFLICT DO NOTHING", - $tableName, - $mapping['joinTable']['inverseJoinColumns'][0]['name'], // person_id - $mapping['joinTable']['joinColumns'][0]['name'], // something_else_id - $to->getId(), - $mapping['joinTable']['joinColumns'][0]['name'], // something_else_id - $tableName, - $mapping['joinTable']['inverseJoinColumns'][0]['name'], // person_id - $from->getId() - ); + "INSERT INTO %s (%s, %s) SELECT %d, %s FROM %s WHERE %s = %d ON CONFLICT DO NOTHING", + $tableName, + $mapping['joinTable']['inverseJoinColumns'][0]['name'], // person_id + $mapping['joinTable']['joinColumns'][0]['name'], // something_else_id + $to->getId(), + $mapping['joinTable']['joinColumns'][0]['name'], // something_else_id + $tableName, + $mapping['joinTable']['inverseJoinColumns'][0]['name'], // person_id + $from->getId() + ); $deleteSql = sprintf( - "DELETE FROM %s WHERE %s = %d", - $tableName, - $mapping['joinTable']['inverseJoinColumns'][0]['name'], // person_id - $from->getId() - ); - - return [ - $sqlInsert, $deleteSql - ]; + "DELETE FROM %s WHERE %s = %d", + $tableName, + $mapping['joinTable']['inverseJoinColumns'][0]['name'], // person_id + $from->getId() + ); foreach ($mapping['joinTable']['inverseJoinColumns'] as $columns) { $sets[] = sprintf('%s = %d', $columns['name'], $to->getId()); @@ -183,7 +179,12 @@ class PersonMove $conditions[] = sprintf('%s = %d', $columns['name'], $from->getId()); } - } elseif (array_key_exists('joinColumns', $mapping)) { + return [ + $sqlInsert, $deleteSql + ]; + + } + if (array_key_exists('joinColumns', $mapping)) { $tableName = $this->getTableName($metadata); foreach ($mapping['joinColumns'] as $columns) { $sets[] = sprintf('%s = %d', $columns['name'], $to->getId()); diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMoveManager.php b/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMoveManager.php index a7bd1188a..99d22c3bb 100644 --- a/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMoveManager.php +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMoveManager.php @@ -1,5 +1,14 @@ */ private iterable $handlers, - ) - { + ) { } /** @@ -20,7 +28,7 @@ class PersonMoveManager * @param string $field * @return bool */ - public function hasHandler(string $className, string $field): bool + public function hasHandler(string $className, string $field): bool { foreach ($this->handlers as $handler) { if ($handler->supports($className, $field)) { @@ -28,14 +36,14 @@ class PersonMoveManager } } - return false; + return false; } /** * @param class-string $className * @return array */ - public function getSqls(string $className, string $field, Person $from, Person $to): array + public function getSqls(string $className, string $field, Person $from, Person $to): array { foreach ($this->handlers as $handler) { if ($handler->supports($className, $field)) { diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMoveSqlHandlerInterface.php b/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMoveSqlHandlerInterface.php index 9958f59df..c1b47127d 100644 --- a/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMoveSqlHandlerInterface.php +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMoveSqlHandlerInterface.php @@ -1,5 +1,14 @@ */ diff --git a/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php b/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php index 14268160e..19820ae1e 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Action/Remove/PersonMoveTest.php @@ -1,5 +1,14 @@ em, $this->personMoveManager, $this->eventDispatcher); $sqls = $move->getSQL($personA, $personB); $this->em->getConnection()->transactional(function (Connection $conn) use ($personA, $personB, $sqls) { - foreach ($sqls as $sql) { - $conn->executeStatement($sql); - } + foreach ($sqls as $sql) { + $conn->executeStatement($sql); + } }); $personsByIdOfA = $this->em->createQuery("SELECT p FROM " . Person::class . " p WHERE p.id = :id") From c1d70ec63e7b53f39685b1aa8982a52673fcb2a2 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Mon, 11 Sep 2023 11:23:21 +0200 Subject: [PATCH 16/45] try fix error cannot insert multiple commands into prepared statement --- ...sonMoveAccompanyingPeriodParticipationHandler.php | 4 ++-- .../Handler/PersonMoveCenterHistoryHandler.php | 9 +++++++-- .../Remove/Handler/PersonMoveHouseholdHandler.php | 4 ++-- .../Remove/Handler/PersonMoveRelationHandler.php | 2 +- .../ChillPersonBundle/Actions/Remove/PersonMove.php | 12 ++++++------ .../Controller/PersonDuplicateController.php | 1 + 6 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveAccompanyingPeriodParticipationHandler.php b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveAccompanyingPeriodParticipationHandler.php index 53654f397..1b4c07469 100644 --- a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveAccompanyingPeriodParticipationHandler.php +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveAccompanyingPeriodParticipationHandler.php @@ -34,11 +34,11 @@ class PersonMoveAccompanyingPeriodParticipationHandler implements PersonMoveSqlH WHERE person_id = %d AND (cpapp.startdate, COALESCE(cpapp.enddate, 'infinity'::date)) OVERLAPS (cpapp2.startdate, COALESCE(cpapp2.enddate, 'infinity'::date)) - ) + ); SQL, $to->getId(), $from->getId(), $to->getId()); $deleteSql = sprintf(<<<'SQL' - DELETE FROM chill_person_accompanying_period_participation WHERE person_id = %d + DELETE FROM chill_person_accompanying_period_participation WHERE person_id = %d; SQL, $from->getId()); return [$insertSql, $deleteSql]; diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php index c4307259f..e8e811f1b 100644 --- a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveCenterHistoryHandler.php @@ -52,12 +52,17 @@ class PersonMoveCenterHistoryHandler implements PersonMoveSqlHandlerInterface } } - $sqlDelete = sprintf("delete FROM chill_person_person_center_history WHERE person_id = %d", $from->getId()); + $sqlDelete = sprintf(<<<'SQL' + DELETE FROM chill_person_person_center_history WHERE person_id = %d; + SQL, $from->getId()); $sqlStatements = [$sqlDelete]; if ((null !== $oldestDateA && null !== $oldestDateB) && $oldestDateA <= $oldestDateB) { - $sqlInsert = sprintf("update chill_person_person_center_history set startDate = '%s' WHERE id = %d", $oldestDateA->format('Y-m-d'), $oldestCenterHistoryB->getId()); + + $sqlInsert = sprintf(<<<'SQL' + UPDATE chill_person_person_center_history SET startDate = '%s' WHERE id = %d; + SQL, $oldestDateA->format('Y-m-d'), $oldestCenterHistoryB->getId()); $sqlStatements = [$sqlInsert, $sqlDelete]; } diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveHouseholdHandler.php b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveHouseholdHandler.php index 2b5514b9c..d617e12ed 100644 --- a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveHouseholdHandler.php +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveHouseholdHandler.php @@ -34,11 +34,11 @@ class PersonMoveHouseholdHandler implements PersonMoveSqlHandlerInterface WHERE person_id = %d AND daterange(cphm.startdate, cphm.enddate) && daterange(cphm_inner.startdate, cphm_inner.enddate) - ) + ); SQL, $to->getId(), $from->getId(), $to->getId()); $deleteSql = sprintf(<<<'SQL' - DELETE FROM chill_person_household_members WHERE person_id = %d + DELETE FROM chill_person_household_members WHERE person_id = %d; SQL, $from->getId()); return [$sqlInsert, $deleteSql]; diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveRelationHandler.php b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveRelationHandler.php index 82ca0592c..22c65a43d 100644 --- a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveRelationHandler.php +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveRelationHandler.php @@ -34,7 +34,7 @@ class PersonMoveRelationHandler implements PersonMoveSqlHandlerInterface WHERE cpr2.fromperson_id = %d AND cpr2.toperson_id = %d OR cpr2.fromperson_id = %d AND cpr2.toperson_id = %d - ) + ); SQL, $from->getId(), $from->getId(), $to->getId(), $from->getId(), $from->getId(), $to->getId()); $deleteSql = sprintf(<<<'SQL' diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php b/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php index 067727078..8bce56c0e 100644 --- a/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php @@ -107,11 +107,11 @@ class PersonMove $personMetadata = $this->em->getClassMetadata(Person::class); $sqls[] = sprintf( - 'DELETE FROM %s WHERE id = %d', + 'DELETE FROM %s WHERE id = %d;', $this->getTableName($personMetadata), $from->getId() ); - + return $sqls; } @@ -132,7 +132,7 @@ class PersonMove } return sprintf( - 'DELETE FROM %s WHERE %s', + 'DELETE FROM %s WHERE %s;', $this->getTableName($metadata), implode(' AND ', $conditions) ); @@ -153,7 +153,7 @@ class PersonMove . $mapping['joinTable']['name']; $sqlInsert = sprintf( - "INSERT INTO %s (%s, %s) SELECT %d, %s FROM %s WHERE %s = %d ON CONFLICT DO NOTHING", + "INSERT INTO %s (%s, %s) SELECT %d, %s FROM %s WHERE %s = %d ON CONFLICT DO NOTHING;", $tableName, $mapping['joinTable']['inverseJoinColumns'][0]['name'], // person_id $mapping['joinTable']['joinColumns'][0]['name'], // something_else_id @@ -165,7 +165,7 @@ class PersonMove ); $deleteSql = sprintf( - "DELETE FROM %s WHERE %s = %d", + "DELETE FROM %s WHERE %s = %d;", $tableName, $mapping['joinTable']['inverseJoinColumns'][0]['name'], // person_id $from->getId() @@ -196,7 +196,7 @@ class PersonMove } return [sprintf( - 'UPDATE %s SET %s WHERE %s', + 'UPDATE %s SET %s WHERE %s;', $tableName, implode(' ', $sets), implode(' AND ', $conditions) diff --git a/src/Bundle/ChillPersonBundle/Controller/PersonDuplicateController.php b/src/Bundle/ChillPersonBundle/Controller/PersonDuplicateController.php index 7ade542e0..31ec67a40 100644 --- a/src/Bundle/ChillPersonBundle/Controller/PersonDuplicateController.php +++ b/src/Bundle/ChillPersonBundle/Controller/PersonDuplicateController.php @@ -120,6 +120,7 @@ class PersonDuplicateController extends Controller $connection->beginTransaction(); foreach ($sqls as $sql) { + dump($sql); $connection->executeQuery($sql); } $connection->commit(); From 77180ebb0c7c72a7fcb3a01aca688f2f643e8c50 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Tue, 12 Sep 2023 14:19:10 +0200 Subject: [PATCH 17/45] Fix error in the doublons queries --- .../Remove/Handler/PersonMoveRelationHandler.php | 10 +++++----- .../ChillPersonBundle/Actions/Remove/PersonMove.php | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveRelationHandler.php b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveRelationHandler.php index 22c65a43d..29f5f4199 100644 --- a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveRelationHandler.php +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveRelationHandler.php @@ -37,11 +37,11 @@ class PersonMoveRelationHandler implements PersonMoveSqlHandlerInterface ); SQL, $from->getId(), $from->getId(), $to->getId(), $from->getId(), $from->getId(), $to->getId()); - $deleteSql = sprintf(<<<'SQL' - DELETE FROM chill_person_relationships WHERE fromperson_id = %d; - DELETE FROM chill_person_relationships WHERE toperson_id = %d; - SQL, $from->getId(), $from->getId()); + $deleteSql = [ + sprintf("DELETE FROM chill_person_relationships WHERE fromperson_id = %d", $from->getId()), + sprintf("DELETE FROM chill_person_relationships WHERE toperson_id = %d" , $from->getId()) + ]; - return [$insertSql, $deleteSql]; + return [$insertSql, ...$deleteSql]; } } diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php b/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php index 8bce56c0e..8c155cb2a 100644 --- a/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/PersonMove.php @@ -111,7 +111,7 @@ class PersonMove $this->getTableName($personMetadata), $from->getId() ); - + return $sqls; } From c1a586dfb95b0432530633997960ef5d06849c57 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Tue, 12 Sep 2023 16:07:50 +0200 Subject: [PATCH 18/45] php cs fix --- .../Actions/Remove/Handler/PersonMoveRelationHandler.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveRelationHandler.php b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveRelationHandler.php index 29f5f4199..2c2f68ca9 100644 --- a/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveRelationHandler.php +++ b/src/Bundle/ChillPersonBundle/Actions/Remove/Handler/PersonMoveRelationHandler.php @@ -39,8 +39,8 @@ class PersonMoveRelationHandler implements PersonMoveSqlHandlerInterface $deleteSql = [ sprintf("DELETE FROM chill_person_relationships WHERE fromperson_id = %d", $from->getId()), - sprintf("DELETE FROM chill_person_relationships WHERE toperson_id = %d" , $from->getId()) - ]; + sprintf("DELETE FROM chill_person_relationships WHERE toperson_id = %d", $from->getId()) + ]; return [$insertSql, ...$deleteSql]; } From bf7f375daf29add6b9341f2b5477c63f11e78166 Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Tue, 22 Aug 2023 14:24:40 +0200 Subject: [PATCH 19/45] Fixed: [export] bad type in form of EmergencyFilter and CalendarRangeFilter --- .../Export/Filter/ACPFilters/EmergencyFilter.php | 6 +++--- .../Export/Filter/CalendarRangeFilter.php | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/EmergencyFilter.php b/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/EmergencyFilter.php index 807ba3903..66ab40e48 100644 --- a/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/EmergencyFilter.php +++ b/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/EmergencyFilter.php @@ -22,11 +22,11 @@ use Symfony\Contracts\Translation\TranslatorInterface; class EmergencyFilter implements FilterInterface { private const CHOICES = [ - 'activity is emergency' => true, - 'activity is not emergency' => false, + 'activity is emergency' => 'true', + 'activity is not emergency' => 'false', ]; - private const DEFAULT_CHOICE = false; + private const DEFAULT_CHOICE = 'false'; private TranslatorInterface $translator; diff --git a/src/Bundle/ChillCalendarBundle/Export/Filter/CalendarRangeFilter.php b/src/Bundle/ChillCalendarBundle/Export/Filter/CalendarRangeFilter.php index 5ffb7c01f..84ec77e6b 100644 --- a/src/Bundle/ChillCalendarBundle/Export/Filter/CalendarRangeFilter.php +++ b/src/Bundle/ChillCalendarBundle/Export/Filter/CalendarRangeFilter.php @@ -29,11 +29,11 @@ use Symfony\Contracts\Translation\TranslatorInterface; class CalendarRangeFilter implements FilterInterface { private const CHOICES = [ - 'Not made within a calendar range' => true, - 'Made within a calendar range' => false, + 'Not made within a calendar range' => 'true', + 'Made within a calendar range' => 'false', ]; - private const DEFAULT_CHOICE = false; + private const DEFAULT_CHOICE = 'false'; private TranslatorInterface $translator; From 52c5807cd5fe0429c4ca83fc18738164cc9ade62 Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Tue, 22 Aug 2023 14:28:35 +0200 Subject: [PATCH 20/45] add changie --- .changes/unreleased/Fixed-20230822-142809.yaml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changes/unreleased/Fixed-20230822-142809.yaml diff --git a/.changes/unreleased/Fixed-20230822-142809.yaml b/.changes/unreleased/Fixed-20230822-142809.yaml new file mode 100644 index 000000000..b22477135 --- /dev/null +++ b/.changes/unreleased/Fixed-20230822-142809.yaml @@ -0,0 +1,6 @@ +kind: Fixed +body: "Corrects a typing error in 2 filters, which caused an \nerror when trying to + reedit a saved export\n\n" +time: 2023-08-22T14:28:09.485466139+02:00 +custom: + Issue: "135" From 2cec4a770ead6f88ea8ebf39f13952f05cdba627 Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Thu, 24 Aug 2023 15:14:47 +0200 Subject: [PATCH 21/45] Fixed: [notificationMailer] invert 'if continue' condition inside foreach loop --- .changes/unreleased/Fixed-20230824-152038.yaml | 6 ++++++ .../Notification/Email/NotificationMailer.php | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 .changes/unreleased/Fixed-20230824-152038.yaml diff --git a/.changes/unreleased/Fixed-20230824-152038.yaml b/.changes/unreleased/Fixed-20230824-152038.yaml new file mode 100644 index 000000000..656bd4752 --- /dev/null +++ b/.changes/unreleased/Fixed-20230824-152038.yaml @@ -0,0 +1,6 @@ +kind: Fixed +body: Fix who receive reply notification when adding comment (recipees must receive + it, and sender not). +time: 2023-08-24T15:20:38.472382872+02:00 +custom: + Issue: "137" diff --git a/src/Bundle/ChillMainBundle/Notification/Email/NotificationMailer.php b/src/Bundle/ChillMainBundle/Notification/Email/NotificationMailer.php index 5290492b1..a652231ac 100644 --- a/src/Bundle/ChillMainBundle/Notification/Email/NotificationMailer.php +++ b/src/Bundle/ChillMainBundle/Notification/Email/NotificationMailer.php @@ -46,7 +46,7 @@ class NotificationMailer [$comment->getNotification()->getSender()] ) as $dest ) { - if (null === $dest->getEmail() || $comment->getCreatedBy() !== $dest) { + if (null !== $dest->getEmail() && $comment->getCreatedBy() === $dest) { continue; } $email = new TemplatedEmail(); From 65b585d4a9e158b3df7cb629de27e9dea17aea1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 6 Sep 2023 15:47:45 +0200 Subject: [PATCH 22/45] Add test to NotificationMailer::postPersistComment --- .../Email/NotificationMailerTest.php | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 src/Bundle/ChillMainBundle/Tests/Notification/Email/NotificationMailerTest.php diff --git a/src/Bundle/ChillMainBundle/Tests/Notification/Email/NotificationMailerTest.php b/src/Bundle/ChillMainBundle/Tests/Notification/Email/NotificationMailerTest.php new file mode 100644 index 000000000..47629b4b4 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Tests/Notification/Email/NotificationMailerTest.php @@ -0,0 +1,78 @@ +expectNotToPerformAssertions(); + $user1 = (new User())->setEmail('user1@foo.com'); + $user2 = (new User())->setEmail('user2@foo.com'); + $user3 = (new User())->setEmail('user3@foo.com'); + + $notification = new Notification(); + $notification + ->setTitle('test notification') + ->setSender($user1) + ->addAddressee($user2) + ->addAddressee($user3) + ; + + $comment = (new NotificationComment()) + ->setContent("foo bar baz") + ->setCreatedBy($user2) + ; + $notification->addComment($comment); + + $mailer = $this->prophesize(MailerInterface::class); + + // a mail only to user1 and user3 should have been sent + $mailer->send(Argument::that(function (Email $email) { + foreach ($email->getTo() as $address) { + if ($address->getAddress() === 'user1@foo.com' || $address->getAddress() === 'user3@foo.com') { + return true; + } + } + + return false; + })); + + $objectManager = $this->prophesize(EntityManagerInterface::class); + + $mailer = $this->buildNotificationMailer($mailer->reveal()); + $mailer->postPersistComment($comment, new PostPersistEventArgs($comment, $objectManager->reveal())); + } + + private function buildNotificationMailer( + MailerInterface $mailer = null, + ): NotificationMailer + { + return new NotificationMailer( + $mailer, + new NullLogger(), + new Translator('fr') + ); + } + + + +} From b4f8c7d261039e27437008974b90ab795d8ac782 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 6 Sep 2023 15:47:59 +0200 Subject: [PATCH 23/45] NotificationMailer: send to correct destinees --- .../unreleased/Fixed-20230824-152038.yaml | 6 ------ .../unreleased/Fixed-20230906-154856.yaml | 5 +++++ .../Notification/Email/NotificationMailer.php | 19 ++++++++++++------- 3 files changed, 17 insertions(+), 13 deletions(-) delete mode 100644 .changes/unreleased/Fixed-20230824-152038.yaml create mode 100644 .changes/unreleased/Fixed-20230906-154856.yaml diff --git a/.changes/unreleased/Fixed-20230824-152038.yaml b/.changes/unreleased/Fixed-20230824-152038.yaml deleted file mode 100644 index 656bd4752..000000000 --- a/.changes/unreleased/Fixed-20230824-152038.yaml +++ /dev/null @@ -1,6 +0,0 @@ -kind: Fixed -body: Fix who receive reply notification when adding comment (recipees must receive - it, and sender not). -time: 2023-08-24T15:20:38.472382872+02:00 -custom: - Issue: "137" diff --git a/.changes/unreleased/Fixed-20230906-154856.yaml b/.changes/unreleased/Fixed-20230906-154856.yaml new file mode 100644 index 000000000..73fb2dc48 --- /dev/null +++ b/.changes/unreleased/Fixed-20230906-154856.yaml @@ -0,0 +1,5 @@ +kind: Fixed +body: Do not send an email to creator twice when adding a comment to a notification +time: 2023-09-06T15:48:56.991246312+02:00 +custom: + Issue: "" diff --git a/src/Bundle/ChillMainBundle/Notification/Email/NotificationMailer.php b/src/Bundle/ChillMainBundle/Notification/Email/NotificationMailer.php index a652231ac..8facf00b9 100644 --- a/src/Bundle/ChillMainBundle/Notification/Email/NotificationMailer.php +++ b/src/Bundle/ChillMainBundle/Notification/Email/NotificationMailer.php @@ -40,13 +40,18 @@ class NotificationMailer public function postPersistComment(NotificationComment $comment, PostPersistEventArgs $eventArgs): void { - foreach ( - array_merge( - $comment->getNotification()->getAddressees()->toArray(), - [$comment->getNotification()->getSender()] - ) as $dest - ) { - if (null !== $dest->getEmail() && $comment->getCreatedBy() === $dest) { + $dests = [$comment->getNotification()->getSender(), ...$comment->getNotification()->getAddressees()->toArray()]; + + $uniqueDests = []; + foreach ($dests as $dest) { + // avoid duplication + if (in_array(spl_object_hash($dest), $uniqueDests, true)) { + continue; + } + $uniqueDests[] = spl_object_hash($dest); + + // do not send if the sender does not have any email, nor to the creator of the comment + if (null === $dest->getEmail() || $comment->getCreatedBy() === $dest) { continue; } $email = new TemplatedEmail(); From e7a9b10d0d173652b6fdd7b93d00442eb77d3a7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 6 Sep 2023 15:55:01 +0200 Subject: [PATCH 24/45] NotificationMailer: add tests when user does not have any email --- .../Email/NotificationMailerTest.php | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/Bundle/ChillMainBundle/Tests/Notification/Email/NotificationMailerTest.php b/src/Bundle/ChillMainBundle/Tests/Notification/Email/NotificationMailerTest.php index 47629b4b4..4c1ae5d42 100644 --- a/src/Bundle/ChillMainBundle/Tests/Notification/Email/NotificationMailerTest.php +++ b/src/Bundle/ChillMainBundle/Tests/Notification/Email/NotificationMailerTest.php @@ -62,6 +62,46 @@ class NotificationMailerTest extends TestCase $mailer->postPersistComment($comment, new PostPersistEventArgs($comment, $objectManager->reveal())); } + public function testPostPersistCommentDestWithNullEmail(): void + { + $this->expectNotToPerformAssertions(); + $user1 = (new User())->setEmail('user1@foo.com'); + $user2 = (new User())->setEmail('user2@foo.com'); + $user3 = (new User())->setEmail(null); + + $notification = new Notification(); + $notification + ->setTitle('test notification') + ->setSender($user1) + ->addAddressee($user2) + ->addAddressee($user3) + ; + + $comment = (new NotificationComment()) + ->setContent("foo bar baz") + ->setCreatedBy($user2) + ; + $notification->addComment($comment); + + $mailer = $this->prophesize(MailerInterface::class); + + // a mail only to user1 and user3 should have been sent + $mailer->send(Argument::that(function (Email $email) { + foreach ($email->getTo() as $address) { + if ($address->getAddress() === 'user1@foo.com') { + return true; + } + } + + return false; + })); + + $objectManager = $this->prophesize(EntityManagerInterface::class); + + $mailer = $this->buildNotificationMailer($mailer->reveal()); + $mailer->postPersistComment($comment, new PostPersistEventArgs($comment, $objectManager->reveal())); + } + private function buildNotificationMailer( MailerInterface $mailer = null, ): NotificationMailer From 6f1dd5b3f27f19c99115929b268c91324935071d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 6 Sep 2023 16:18:28 +0200 Subject: [PATCH 25/45] Fix new CS --- .../ChillEventBundle/Search/EventSearch.php | 2 +- .../Email/NotificationMailerTest.php | 30 +++++++++++++------ .../Controller/PersonController.php | 2 +- .../AccompanyingPeriodContextTest.php | 2 +- .../DocGenerator/PersonContextTest.php | 2 +- 5 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/Bundle/ChillEventBundle/Search/EventSearch.php b/src/Bundle/ChillEventBundle/Search/EventSearch.php index 6bb3d435d..504d04130 100644 --- a/src/Bundle/ChillEventBundle/Search/EventSearch.php +++ b/src/Bundle/ChillEventBundle/Search/EventSearch.php @@ -152,7 +152,7 @@ class EventSearch extends AbstractSearch $orWhere = $qb->expr()->orX(); foreach ($reachableCenters as $center) { - $n = $n+1; + $n = $n + 1; $circles = $this->authorizationHelper->getReachableScopes( $this->security->getUser(), 'CHILL_EVENT_SEE', diff --git a/src/Bundle/ChillMainBundle/Tests/Notification/Email/NotificationMailerTest.php b/src/Bundle/ChillMainBundle/Tests/Notification/Email/NotificationMailerTest.php index 4c1ae5d42..19da87b79 100644 --- a/src/Bundle/ChillMainBundle/Tests/Notification/Email/NotificationMailerTest.php +++ b/src/Bundle/ChillMainBundle/Tests/Notification/Email/NotificationMailerTest.php @@ -1,5 +1,14 @@ setSender($user1) ->addAddressee($user2) ->addAddressee($user3) - ; + ; $comment = (new NotificationComment()) ->setContent("foo bar baz") @@ -47,14 +60,14 @@ class NotificationMailerTest extends TestCase // a mail only to user1 and user3 should have been sent $mailer->send(Argument::that(function (Email $email) { - foreach ($email->getTo() as $address) { - if ($address->getAddress() === 'user1@foo.com' || $address->getAddress() === 'user3@foo.com') { - return true; - } + foreach ($email->getTo() as $address) { + if ($address->getAddress() === 'user1@foo.com' || $address->getAddress() === 'user3@foo.com') { + return true; } + } - return false; - })); + return false; + })); $objectManager = $this->prophesize(EntityManagerInterface::class); @@ -104,8 +117,7 @@ class NotificationMailerTest extends TestCase private function buildNotificationMailer( MailerInterface $mailer = null, - ): NotificationMailer - { + ): NotificationMailer { return new NotificationMailer( $mailer, new NullLogger(), diff --git a/src/Bundle/ChillPersonBundle/Controller/PersonController.php b/src/Bundle/ChillPersonBundle/Controller/PersonController.php index 2d4732289..ce6d6bb38 100644 --- a/src/Bundle/ChillPersonBundle/Controller/PersonController.php +++ b/src/Bundle/ChillPersonBundle/Controller/PersonController.php @@ -214,7 +214,7 @@ final class PersonController extends AbstractController { $person = new Person(); - $authorizedCenters =$this->authorizationHelper->getReachableCenters($this->getUser(), PersonVoter::CREATE); + $authorizedCenters = $this->authorizationHelper->getReachableCenters($this->getUser(), PersonVoter::CREATE); if (1 === count($authorizedCenters)) { $person->setCenter($authorizedCenters[0]); diff --git a/src/Bundle/ChillPersonBundle/Tests/Service/DocGenerator/AccompanyingPeriodContextTest.php b/src/Bundle/ChillPersonBundle/Tests/Service/DocGenerator/AccompanyingPeriodContextTest.php index f9f888999..b5f656702 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Service/DocGenerator/AccompanyingPeriodContextTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Service/DocGenerator/AccompanyingPeriodContextTest.php @@ -108,7 +108,7 @@ class AccompanyingPeriodContextTest extends KernelTestCase ): void { $context = $this->buildContext(); $template = new DocGeneratorTemplate(); - $template->setName(["fr" =>"test"])->setContext(AccompanyingPeriodContext::class) + $template->setName(["fr" => "test"])->setContext(AccompanyingPeriodContext::class) ->setDescription("description")->setActive(true) ->setOptions($options); diff --git a/src/Bundle/ChillPersonBundle/Tests/Service/DocGenerator/PersonContextTest.php b/src/Bundle/ChillPersonBundle/Tests/Service/DocGenerator/PersonContextTest.php index 71fce8c92..fa99a5363 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Service/DocGenerator/PersonContextTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Service/DocGenerator/PersonContextTest.php @@ -102,7 +102,7 @@ final class PersonContextTest extends KernelTestCase self::$container->get(ThirdPartyRepository::class) ); $template = new DocGeneratorTemplate(); - $template->setName(["fr" =>"test"])->setContext(AccompanyingPeriodContext::class) + $template->setName(["fr" => "test"])->setContext(AccompanyingPeriodContext::class) ->setDescription("description")->setActive(true) ->setOptions($options); From 68f2c7df25e429e96c5bb40132f3b21a724bf703 Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Thu, 24 Aug 2023 14:32:50 +0200 Subject: [PATCH 26/45] Fixed: [vuejs] fix missing translation in HouseholdMembersEditor component --- .../vuejs/HouseholdMembersEditor/components/PersonComment.vue | 2 +- .../Resources/public/vuejs/HouseholdMembersEditor/js/i18n.js | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Bundle/ChillPersonBundle/Resources/public/vuejs/HouseholdMembersEditor/components/PersonComment.vue b/src/Bundle/ChillPersonBundle/Resources/public/vuejs/HouseholdMembersEditor/components/PersonComment.vue index cb9a02d51..8f61a74d1 100644 --- a/src/Bundle/ChillPersonBundle/Resources/public/vuejs/HouseholdMembersEditor/components/PersonComment.vue +++ b/src/Bundle/ChillPersonBundle/Resources/public/vuejs/HouseholdMembersEditor/components/PersonComment.vue @@ -1,7 +1,7 @@