Skip to content

Commit

Permalink
Bug #10951 [Account] fix sync username with customer email
Browse files Browse the repository at this point in the history
if applied, this commit will

-make sync behaviour works on both customer and shop user,
resulting in username will never by setting username value whenever
shopuser or customer are inserted or updated.
  • Loading branch information
hatem20 committed Jan 27, 2020
1 parent fd2b3ad commit e9b92f9
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ Feature: Adding a new customer account
And I specify their password as "killSauron"
And I save my changes
Then I should be notified that it has been successfully edited
And I should not see create account option
And the customer "f.baggins@example.com" should appear in the store
And this customer should have an account created
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\UnitOfWork;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\ShopUserInterface;

/**
* Keeps user's username synchronized with email.
Expand All @@ -35,26 +36,34 @@ public function onFlush(OnFlushEventArgs $onFlushEventArgs)

private function processEntities(array $entities, EntityManagerInterface $entityManager, UnitOfWork $unitOfWork): void
{
foreach ($entities as $customer) {
if (!$customer instanceof CustomerInterface) {
continue;
foreach ($entities as $entity) {
if ($entity instanceof ShopUserInterface) {
/** @var CustomerInterface $customer */
$customer = $entity->getCustomer();
$this->updateUsername($entity, $customer, $entityManager, $unitOfWork);
} elseif ($entity instanceof CustomerInterface) {
/** @var ShopUserInterface $user */
$user = $entity->getUser();
$this->updateUsername($user, $entity, $entityManager, $unitOfWork);
}
}
}

$user = $customer->getUser();
if (null === $user) {
continue;
}
private function updateUsername(?ShopUserInterface $user, ?CustomerInterface $customer, EntityManagerInterface $entityManager, UnitOfWork $unitOfWork): void
{
if (!$user || !$customer) {
return;
}

if ($customer->getEmail() === $user->getUsername() && $customer->getEmailCanonical() === $user->getUsernameCanonical()) {
continue;
}
if ($customer->getEmail() === $user->getUsername() && $customer->getEmailCanonical() === $user->getUsernameCanonical()) {
return;
}

$user->setUsername($customer->getEmail());
$user->setUsernameCanonical($customer->getEmailCanonical());
$user->setUsername($customer->getEmail());
$user->setUsernameCanonical($customer->getEmailCanonical());

/** @var ClassMetadata $userMetadata */
$userMetadata = $entityManager->getClassMetadata(get_class($user));
$unitOfWork->recomputeSingleEntityChangeSet($userMetadata, $user);
}
/** @var ClassMetadata $userMetadata */
$userMetadata = $entityManager->getClassMetadata(get_class($user));
$unitOfWork->recomputeSingleEntityChangeSet($userMetadata, $user);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,33 @@ function it_does_nothing_when_there_are_other_objects_than_customer(

$this->onFlush($onFlushEventArgs);
}

function it_sets_usernames_on_user_create(
OnFlushEventArgs $onFlushEventArgs,
EntityManager $entityManager,
UnitOfWork $unitOfWork,
CustomerInterface $customer,
ShopUserInterface $user,
ClassMetadata $userMetadata
): void {
$onFlushEventArgs->getEntityManager()->willReturn($entityManager);
$entityManager->getUnitOfWork()->willReturn($unitOfWork);

$unitOfWork->getScheduledEntityInsertions()->willReturn([$user]);
$unitOfWork->getScheduledEntityUpdates()->willReturn([]);

$user->getUsername()->willReturn(null);
$user->getUsernameCanonical()->willReturn(null);
$user->getCustomer()->willReturn($customer);
$customer->getEmail()->willReturn('customer+extra@email.com');
$customer->getEmailCanonical()->willReturn('customer@email.com');

$user->setUsername('customer+extra@email.com')->shouldBeCalled();
$user->setUsernameCanonical('customer@email.com')->shouldBeCalled();

$entityManager->getClassMetadata(get_class($user->getWrappedObject()))->willReturn($userMetadata);
$unitOfWork->recomputeSingleEntityChangeSet($userMetadata, $user)->shouldBeCalled();

$this->onFlush($onFlushEventArgs);
}
}

0 comments on commit e9b92f9

Please sign in to comment.