From f3fb186549c4ceb9704e6d151dd3db773d863964 Mon Sep 17 00:00:00 2001 From: Frederik Rommel Date: Wed, 19 Feb 2025 14:51:39 +0100 Subject: [PATCH] PAYOSWXP-164: prevent saving empty phonenumber --- .../CustomerDataPersistor.php | 35 ++-- .../CustomerDataPersistorTest.php | 153 ++++++++++++++++++ 2 files changed, 168 insertions(+), 20 deletions(-) create mode 100644 tests/Components/CustomerDataPersistor/CustomerDataPersistorTest.php diff --git a/src/Components/CustomerDataPersistor/CustomerDataPersistor.php b/src/Components/CustomerDataPersistor/CustomerDataPersistor.php index 5b2502f8..6d37f755 100644 --- a/src/Components/CustomerDataPersistor/CustomerDataPersistor.php +++ b/src/Components/CustomerDataPersistor/CustomerDataPersistor.php @@ -46,48 +46,43 @@ protected function saveCustomerData(CustomerEntity $customer, RequestDataBag $da $birthday = \is_string($birthday) ? (\DateTime::createFromFormat('Y-m-d', $birthday) ?: null) : null; if ($birthday instanceof \DateTime && $birthday->getTimestamp() !== $customer->getBirthday()?->getTimestamp()) { - $this->customerRepository->update( + $this->customerRepository->update([ [ - [ - 'id' => $customer->getId(), - 'birthday' => $birthday, - ], + 'id' => $customer->getId(), + 'birthday' => $birthday, ], - $context - ); + ], $context); } } protected function saveCustomerAddressData(CustomerAddressEntity $customerAddress, RequestDataBag $dataBag, Context $context): void { $phoneNumber = $dataBag->get(RequestConstants::PHONE); + $phoneNumber = \is_string($phoneNumber) ? trim($phoneNumber) : null; - if ($phoneNumber !== $customerAddress->getPhoneNumber()) { - $this->customerAddressRepository->update( + if (!empty($phoneNumber) && $phoneNumber !== $customerAddress->getPhoneNumber()) { + $this->customerAddressRepository->update([ [ - [ - 'id' => $customerAddress->getId(), - 'phoneNumber' => $phoneNumber, - ], + 'id' => $customerAddress->getId(), + 'phoneNumber' => $phoneNumber, ], - $context - ); + ], $context); } } protected function saveOrderData(OrderEntity $orderEntity, RequestDataBag $dataBag, Context $context): void { $phoneNumber = $dataBag->get(RequestConstants::PHONE); + $phoneNumber = \is_string($phoneNumber) ? trim($phoneNumber) : null; - $this->orderAddressRepository->update( - [ + if (!empty($phoneNumber)) { + $this->orderAddressRepository->update([ [ 'id' => $orderEntity->getBillingAddressId(), 'phoneNumber' => $phoneNumber, ], - ], - $context - ); + ], $context); + } } private function getCustomer(OrderEntity $order, Context $context): ?CustomerEntity diff --git a/tests/Components/CustomerDataPersistor/CustomerDataPersistorTest.php b/tests/Components/CustomerDataPersistor/CustomerDataPersistorTest.php new file mode 100644 index 00000000..392f4ee8 --- /dev/null +++ b/tests/Components/CustomerDataPersistor/CustomerDataPersistorTest.php @@ -0,0 +1,153 @@ +getContainer()->get(SystemConfigService::class)->set('PayonePayment.settings.saveUserEnteredDataToCustomer', true); + $this->context = $this->createSalesChannelContextWithLoggedInCustomerAndWithNavigation(); + $this->orderRepository = $this->getContainer()->get('order.repository'); + $this->customerRepository = $this->getContainer()->get('customer.repository'); + $this->customerAddressRepository = $this->getContainer()->get('customer_address.repository'); + } + + public function testIfPhoneNumberGotSaved(): void + { + $defaultBillingAddress = $this->context->getCustomer()->getDefaultBillingAddress(); + $defaultBillingAddress->setPhoneNumber('old-phone-number'); + $this->customerAddressRepository->update([[ + 'id' => $defaultBillingAddress->getId(), + 'phoneNumber' => $defaultBillingAddress->getPhoneNumber(), + ]], $this->context->getContext()); + $order = $this->getRandomOrder($this->context); + + $this->getContainer()->get(CustomerDataPersistor::class)->save($order, new RequestDataBag([ + RequestConstants::PHONE => 'test-phone-number_address', + ]), $this->context->getContext()); + + $criteria = new Criteria([$order->getId()]); + $criteria->addAssociation('billingAddress'); + /** @var OrderEntity $order */ + $order = $this->orderRepository->search($criteria, $this->context->getContext())->first(); + static::assertEquals('test-phone-number_address', $order->getBillingAddress()->getPhoneNumber()); + + /** @var CustomerAddressEntity $defaultBillingAddress */ + $defaultBillingAddress = $this->customerAddressRepository->search(new Criteria([$defaultBillingAddress->getId()]), $this->context->getContext())->first(); + static::assertEquals('test-phone-number_address', $defaultBillingAddress->getPhoneNumber()); + } + + /** + * @dataProvider phoneNumberGotNotSavedDataProvider + */ + public function testIfEmptyPhoneNumberGotNotSaved(?string $input = null): void + { + $defaultBillingAddress = $this->context->getCustomer()->getDefaultBillingAddress(); + $defaultBillingAddress->setPhoneNumber('old-phone-number'); + $this->customerAddressRepository->update([[ + 'id' => $defaultBillingAddress->getId(), + 'phoneNumber' => $defaultBillingAddress->getPhoneNumber(), + ]], $this->context->getContext()); + $order = $this->getRandomOrder($this->context); + + $this->getContainer()->get(CustomerDataPersistor::class)->save($order, new RequestDataBag([ + RequestConstants::PHONE => $input, + ]), $this->context->getContext()); + + $criteria = new Criteria([$order->getId()]); + $criteria->addAssociation('billingAddress'); + /** @var OrderEntity $order */ + $order = $this->orderRepository->search($criteria, $this->context->getContext())->first(); + static::assertEquals('old-phone-number', $order->getBillingAddress()->getPhoneNumber()); + + /** @var CustomerAddressEntity $defaultBillingAddress */ + $defaultBillingAddress = $this->customerAddressRepository->search(new Criteria([$defaultBillingAddress->getId()]), $this->context->getContext())->first(); + static::assertEquals('old-phone-number', $defaultBillingAddress->getPhoneNumber()); + } + + public function testIfBirthDayGotSaved(): void + { + $customer = $this->context->getCustomer(); + $customer->setBirthday(DateTime::createFromFormat('Y-m-d', '1950-12-20')); + $this->customerRepository->update([[ + 'id' => $customer->getId(), + 'birthday' => $customer->getBirthday(), + ]], $this->context->getContext()); + $order = $this->getRandomOrder($this->context); + + $this->getContainer()->get(CustomerDataPersistor::class)->save($order, new RequestDataBag([ + RequestConstants::BIRTHDAY => '2020-11-18', + ]), $this->context->getContext()); + + /** @var CustomerEntity $customer */ + $customer = $this->customerRepository->search(new Criteria([$customer->getId()]), $this->context->getContext())->first(); + static::assertEquals('2020-11-18', $customer->getBirthday()->format('Y-m-d')); + } + + /** + * @dataProvider birthDayGotNotSavedDataProvider + */ + public function testIfEmptyBirthDayGotNotSaved(?string $input = null): void + { + $customer = $this->context->getCustomer(); + $customer->setBirthday(DateTime::createFromFormat('Y-m-d', '1950-12-20')); + $this->customerRepository->update([[ + 'id' => $customer->getId(), + 'birthday' => $customer->getBirthday(), + ]], $this->context->getContext()); + $order = $this->getRandomOrder($this->context); + + $this->getContainer()->get(CustomerDataPersistor::class)->save($order, new RequestDataBag([ + RequestConstants::BIRTHDAY => $input, + ]), $this->context->getContext()); + + /** @var CustomerEntity $customer */ + $customer = $this->customerRepository->search(new Criteria([$customer->getId()]), $this->context->getContext())->first(); + static::assertEquals('1950-12-20', $customer->getBirthday()->format('Y-m-d')); + } + + public static function birthDayGotNotSavedDataProvider(): array + { + return [ + [null], + [''], + [' '], + ['invalid-value'], + ]; + } + + public static function phoneNumberGotNotSavedDataProvider(): array + { + return [ + [null], + [''], + [' '], + ]; + } +}