From 895e1be9efc9381c7febc660d93755e464c59689 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Thu, 11 Sep 2025 11:44:53 +0200 Subject: [PATCH] Fix fixtures - Inject parameters directly instead of getting from container - User PasswordHasher instead of EncoderFactory - set parameters directly and correctly encode values to json --- .../DataFixtures/ORM/LoadCountries.php | 8 +++++++- .../DataFixtures/ORM/LoadLanguages.php | 9 ++++++++- .../DataFixtures/ORM/LoadUsers.php | 20 ++++++++++--------- .../DataFixtures/ORM/LoadCustomFields.php | 6 +++--- .../Service/Import/SocialWorkMetadata.php | 19 ++++-------------- 5 files changed, 33 insertions(+), 29 deletions(-) diff --git a/src/Bundle/ChillMainBundle/DataFixtures/ORM/LoadCountries.php b/src/Bundle/ChillMainBundle/DataFixtures/ORM/LoadCountries.php index 5cb3f12e4..9d8e39aef 100644 --- a/src/Bundle/ChillMainBundle/DataFixtures/ORM/LoadCountries.php +++ b/src/Bundle/ChillMainBundle/DataFixtures/ORM/LoadCountries.php @@ -16,6 +16,7 @@ use Doctrine\Bundle\FixturesBundle\Fixture; use Doctrine\Common\DataFixtures\AbstractFixture; use Doctrine\Common\DataFixtures\OrderedFixtureInterface; use Doctrine\Persistence\ObjectManager; +use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; /** @@ -23,6 +24,11 @@ use Doctrine\Persistence\ObjectManager; */ class LoadCountries extends Fixture implements OrderedFixtureInterface { + private array $availableLanguages; + public function __construct(private readonly ParameterBagInterface $parameterBag) + { + $this->availableLanguages = $this->parameterBag->get('chill_main.available_languages'); + } public function getOrder(): int { return 20; @@ -32,7 +38,7 @@ class LoadCountries extends Fixture implements OrderedFixtureInterface { echo "loading countries... \n"; - $languages = $this->container->getParameter('chill_main.available_languages'); + $languages = $this->availableLanguages; foreach (LoadCountriesCommand::prepareCountryList($languages) as $country) { $manager->persist($country); diff --git a/src/Bundle/ChillMainBundle/DataFixtures/ORM/LoadLanguages.php b/src/Bundle/ChillMainBundle/DataFixtures/ORM/LoadLanguages.php index 206cfd6fd..69f042f55 100644 --- a/src/Bundle/ChillMainBundle/DataFixtures/ORM/LoadLanguages.php +++ b/src/Bundle/ChillMainBundle/DataFixtures/ORM/LoadLanguages.php @@ -15,6 +15,7 @@ use Chill\MainBundle\Entity\Language; use Doctrine\Bundle\FixturesBundle\Fixture; use Doctrine\Common\DataFixtures\OrderedFixtureInterface; use Doctrine\Persistence\ObjectManager; +use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; /** * Load languages into database. @@ -29,6 +30,12 @@ class LoadLanguages extends Fixture implements OrderedFixtureInterface // This array contains regional code to not exclude private array $regionalVersionToInclude = ['ro_MD']; + private array $availableLanguages; + public function __construct(private readonly ParameterBagInterface $parameterBag) + { + $this->availableLanguages = $this->parameterBag->get('chill_main.available_languages'); + } + public function getOrder(): int { return 10; @@ -62,7 +69,7 @@ class LoadLanguages extends Fixture implements OrderedFixtureInterface { $names = []; - foreach ($this->container->getParameter('chill_main.available_languages') as $lang) { + foreach ($this->availableLanguages as $lang) { $names[$lang] = \Symfony\Component\Intl\Languages::getName($languageCode, $lang); } diff --git a/src/Bundle/ChillMainBundle/DataFixtures/ORM/LoadUsers.php b/src/Bundle/ChillMainBundle/DataFixtures/ORM/LoadUsers.php index f1479d360..70de02aa3 100644 --- a/src/Bundle/ChillMainBundle/DataFixtures/ORM/LoadUsers.php +++ b/src/Bundle/ChillMainBundle/DataFixtures/ORM/LoadUsers.php @@ -19,7 +19,7 @@ use Chill\MainBundle\Entity\User; use Doctrine\Bundle\FixturesBundle\Fixture; use Doctrine\Common\DataFixtures\OrderedFixtureInterface; use Doctrine\Persistence\ObjectManager; -use Symfony\Component\Security\Core\Encoder\EncoderFactory; +use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; /** * Load fixtures users into database. @@ -53,6 +53,14 @@ class LoadUsers extends Fixture implements OrderedFixtureInterface 'centerB_permission_group_social', ], ], ]; + + private UserPasswordHasherInterface $passwordHasher; + + public function __construct(UserPasswordHasherInterface $passwordHasher) + { + $this->passwordHasher = $passwordHasher; + } + public function getOrder(): int { return 1000; @@ -74,17 +82,11 @@ class LoadUsers extends Fixture implements OrderedFixtureInterface $defaultEncoder = new \Symfony\Component\PasswordHasher\Hasher\MessageDigestPasswordHasher('sha512', true, 5000); - $encoderFactory = new EncoderFactory([ - User::class => $defaultEncoder, - ]); + $hashedPassword = $this->passwordHasher->hashPassword($user, 'password'); $user ->setUsername($username) - ->setPassword( - $encoderFactory - ->getEncoder($user) - ->encodePassword('password', $user->getSalt()) - ) + ->setPassword($hashedPassword) ->setEmail(sprintf('%s@chill.social', \str_replace(' ', '', (string) $username))); foreach ($params['groupCenterRefs'] as $groupCenterRef) { diff --git a/src/Bundle/ChillPersonBundle/DataFixtures/ORM/LoadCustomFields.php b/src/Bundle/ChillPersonBundle/DataFixtures/ORM/LoadCustomFields.php index 9d39f2ac0..0ebd81bdf 100644 --- a/src/Bundle/ChillPersonBundle/DataFixtures/ORM/LoadCustomFields.php +++ b/src/Bundle/ChillPersonBundle/DataFixtures/ORM/LoadCustomFields.php @@ -52,12 +52,12 @@ class LoadCustomFields extends AbstractFixture implements OrderedFixtureInterfac $manager->flush(); } - private function createCustomFieldChoice(): \Chill\CustomFieldsBundle\CustomFields\CustomFieldChoice + private function createCustomFieldChoice(): CustomFieldChoice { return $this->customFieldChoice; } - private function createCustomFieldText(): \Chill\CustomFieldsBundle\CustomFields\CustomFieldText + private function createCustomFieldText(): CustomFieldText { return $this->customFieldText; } @@ -65,7 +65,7 @@ class LoadCustomFields extends AbstractFixture implements OrderedFixtureInterfac private function loadData(ObjectManager $manager): void { $personIds = $this->entityManager - ->createQuery('SELECT person.id FROM ChillPersonBundle:Person person') + ->createQuery('SELECT person.id FROM '.Person::class.' person') ->getScalarResult(); // get possible values for cfGroup diff --git a/src/Bundle/ChillPersonBundle/Service/Import/SocialWorkMetadata.php b/src/Bundle/ChillPersonBundle/Service/Import/SocialWorkMetadata.php index 3086ee31a..3c035b907 100644 --- a/src/Bundle/ChillPersonBundle/Service/Import/SocialWorkMetadata.php +++ b/src/Bundle/ChillPersonBundle/Service/Import/SocialWorkMetadata.php @@ -84,20 +84,9 @@ final readonly class SocialWorkMetadata implements SocialWorkMetadataInterface $temporaryJsonCriterias[] = [$field, $key, $value, sprintf(':placeholder_%s_%s', $field, $key)]; } - $jsonParameters = array_reduce( - $temporaryJsonCriterias, - static function (array $carry, array $row): array { - [,, $value, $placeholder] = $row; - - return array_merge( - $carry, - [ - $placeholder => sprintf('"%s"', $value), - ] - ); - }, - [] - ); + foreach ($temporaryJsonCriterias as [$field, $key, $value, $placeholder]) { + $qb->setParameter(ltrim($placeholder, ':'), json_encode($value)); + } $jsonPredicates = array_map( static function (array $row) use ($expr): Comparison { @@ -121,7 +110,7 @@ final readonly class SocialWorkMetadata implements SocialWorkMetadataInterface return $qb ->select('s') ->where(...$jsonPredicates) - ->setParameters($jsonParameters) +// ->setParameters($jsonParameters) ->getQuery() ->getResult(); }