Skip to content

Commit

Permalink
Merge branch 'main' into develop #3147
Browse files Browse the repository at this point in the history
  • Loading branch information
sfinx13 committed Oct 22, 2024
2 parents e934c6a + 56c305a commit 0be7f64
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 29 deletions.
3 changes: 2 additions & 1 deletion src/Command/ImportGridAffectationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$csvData = $this->csvParser->parseAsDict($toFile);
$checkErrors = $this->gridAffectationLoader->validate(
$csvData,
$isModeUpdate
$territory,
$isModeUpdate,
);

if (\count($checkErrors) > 0) {
Expand Down
7 changes: 6 additions & 1 deletion src/Entity/Partner.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
use Symfony\Component\Validator\Constraints as Assert;

#[ORM\Entity(repositoryClass: PartnerRepository::class)]
#[UniqueEntity('email', ignoreNull: true)]
#[UniqueEntity(
fields: ['email', 'territory', 'isArchive'],
message: 'L\'e-mail générique existe déjà pour ce territoire. Veuillez saisir un autre e-mail partenaire.',
errorPath: 'email',
ignoreNull: true)
]
class Partner implements EntityHistoryInterface
{
use TimestampableTrait;
Expand Down
50 changes: 27 additions & 23 deletions src/Service/Import/GridAffectation/GridAffectationLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,18 @@ class GridAffectationLoader
];

public function __construct(
private PartnerFactory $partnerFactory,
private PartnerManager $partnerManager,
private UserFactory $userFactory,
private UserManager $userManager,
private ManagerInterface $manager,
private ValidatorInterface $validator,
private LoggerInterface $logger,
private EntityManagerInterface $entityManager,
private readonly PartnerFactory $partnerFactory,
private readonly PartnerManager $partnerManager,
private readonly UserFactory $userFactory,
private readonly UserManager $userManager,
private readonly ManagerInterface $manager,
private readonly ValidatorInterface $validator,
private readonly LoggerInterface $logger,
private readonly EntityManagerInterface $entityManager,
) {
}

public function validate(array $data, bool $isModeUpdate = false): array
public function validate(array $data, Territory $territory, bool $isModeUpdate = false): array
{
$errors = [];
$mailPartners = [];
Expand All @@ -72,7 +72,7 @@ public function validate(array $data, bool $isModeUpdate = false): array
$item[GridAffectationHeader::PARTNER_TYPE]
);
}
// if partner has an email, it should be valid and not existing for another partner
// if partner has an email, it should be valid and not existing in the same territory
$emailPartner = trim($item[GridAffectationHeader::PARTNER_EMAIL]);
if (!empty($emailPartner)) {
$violations = $this->validator->validate($emailPartner, $emailConstraint);
Expand All @@ -85,15 +85,19 @@ public function validate(array $data, bool $isModeUpdate = false): array
}

if (!$isModeUpdate) {
/** @var Partner $partnerToCreate */
$partnerToCreate = $this->partnerManager->findOneBy(['email' => $emailPartner]);
if (null !== $partnerToCreate) {
/** @var Partner $partnerToCheck */
$partnerToCheck = $this->partnerManager->findOneBy([
'email' => $emailPartner,
'isArchive' => false,
'territory' => $territory]
);
if (null !== $partnerToCheck) {
$errors[] = \sprintf(
'line %d : Partenaire déjà existant avec (%s) dans %s, nom : %s',
'line %d : E-mail partenaire déjà existant dans le territoire avec (%s) dans %s, nom : %s',
$numLine,
$emailPartner,
$partnerToCreate->getTerritory()->getName(),
$partnerToCreate->getNom()
$partnerToCheck->getTerritory()->getName(),
$partnerToCheck->getNom()
);
}
}
Expand Down Expand Up @@ -121,19 +125,19 @@ public function validate(array $data, bool $isModeUpdate = false): array
$errors[] = \sprintf('line %d : E-mail incorrect pour un utilisateur : %s', $numLine, $emailUser);
}

/** @var User $userToCreate */
$userToCreate = $this->userManager->findOneBy(['email' => $emailUser]);
/** @var User $userToCheck */
$userToCheck = $this->userManager->findOneBy(['email' => $emailUser]);
if (!$isModeUpdate
&& null !== $userToCreate
&& !\in_array('ROLE_USAGER', $userToCreate->getRoles())
&& null !== $userToCheck
&& !\in_array('ROLE_USAGER', $userToCheck->getRoles())
) {
$errors[] = \sprintf(
'line %d : Utilisateur déjà existant avec (%s) dans %s, partenaire : %s, rôle : %s',
$numLine,
$emailUser,
$userToCreate->getTerritory()->getName(),
$userToCreate->getPartner()->getNom(),
$userToCreate->getRoleLabel()
$userToCheck->getTerritory()->getName(),
$userToCheck->getPartner()->getNom(),
$userToCheck->getRoleLabel()
);
}
// store user mail to check duplicates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected function setUp(): void
public function testLoadValidPartnersAndUserInCreateMode(): void
{
$territory = $this->entityManager->getRepository(Territory::class)->findOneBy(['isActive' => 0]);
$errors = $this->gridAffectationLoader->validate($this->provideValidData());
$errors = $this->gridAffectationLoader->validate($this->provideValidData(), $territory);
$this->assertCount(0, $errors);

$this->gridAffectationLoader->load($territory, $this->provideValidData(), []);
Expand Down Expand Up @@ -104,7 +104,7 @@ public function testLoadValidPartnerAndUserInUpdateMode(): void
'Rôle' => self::FIXTURE_ROLE_USER,
];

$errors = $this->gridAffectationLoader->validate($data, true);
$errors = $this->gridAffectationLoader->validate($data, $territory, true);
$this->assertCount(0, $errors);
$this->gridAffectationLoader->load($territory, $data, []);
$metaData = $this->gridAffectationLoader->getMetadata();
Expand All @@ -120,13 +120,15 @@ public function testLoadValidPartnerAndUserInUpdateMode(): void

public function testValidateWithErrors(): void
{
$territory = $this->entityManager->getRepository(Territory::class)->findOneBy(['zip' => 13]);

$errors = [
'line 3 : E-mail incorrect pour un partenaire : arshistologe.fr',
'line 5 : Type incorrect pour Random Type --> Random Type',
'line 5 : Rôle incorrect pour [email protected] --> Fake role',
'line 6 : Type incorrect pour Random Type --> Random Type',
'line 6 : E-mail incorrect pour un utilisateur : john.doe@',
'line 7 : Partenaire déjà existant avec ([email protected]) dans Bouches-du-Rhône, nom : Partenaire 13-01',
'line 7 : E-mail partenaire déjà existant dans le territoire avec ([email protected]) dans Bouches-du-Rhône, nom : Partenaire 13-01',
'line 8 : E-mail manquant pour Margaretta Borer, partenaire ADIL',
'line 9 : Nom de partenaire manquant',
'line 10 : Utilisateur déjà existant avec ([email protected]) dans Bouches-du-Rhône, partenaire : Partenaire 13-06 ESABORA ARS, rôle : Agent',
Expand All @@ -137,7 +139,7 @@ public function testValidateWithErrors(): void

$this->assertEquals(
$errors,
$this->gridAffectationLoader->validate($this->provideInvalidDataWithDuplicatePartnersAndUsers())
$this->gridAffectationLoader->validate($this->provideInvalidDataWithDuplicatePartnersAndUsers(), $territory)
);
}

Expand Down
26 changes: 26 additions & 0 deletions tests/Unit/Entity/PartnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,30 @@ public function testPartnerSCHSCompetenceRSDWithSpecificInseeCanSyncWithOilhi():

$this->assertTrue($partner->canSyncWithOilhi($signalement));
}

/**
* @dataProvider provideDataForTestPartnerWithEmail
*/
public function testCreatePartnerNoValidWithEmailExistInTerritory(int $zip, int $countErrors): void
{
$entityManager = self::getContainer()->get('doctrine')->getManager();
$territory = $entityManager->getRepository(Territory::class)->find($zip);
$partner = (new Partner())
->setNom('Random partner')
->setEmail('[email protected]')
->setType(PartnerType::COMMUNE_SCHS)
->setCompetence([Qualification::VISITES])
->setTerritory($territory);

$validator = self::getContainer()->get('validator');
$errors = $validator->validate($partner);
$this->assertEquals($countErrors, $errors->count());
}

public function provideDataForTestPartnerWithEmail(): \Generator
{
yield 'Create partner not valid with email exists in territory' => [13, 1];

yield 'Create partner valid with email exists in territory' => [1, 0];
}
}

0 comments on commit 0be7f64

Please sign in to comment.