Skip to content

Commit

Permalink
PAYOSWXP-164: prevent saving empty phonenumber
Browse files Browse the repository at this point in the history
  • Loading branch information
rommelfreddy committed Feb 19, 2025
1 parent bca44d1 commit f3fb186
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 20 deletions.
35 changes: 15 additions & 20 deletions src/Components/CustomerDataPersistor/CustomerDataPersistor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
153 changes: 153 additions & 0 deletions tests/Components/CustomerDataPersistor/CustomerDataPersistorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php

declare(strict_types=1);

namespace PayonePayment\Components\CustomerDataPersistor;

use DateTime;
use PayonePayment\RequestConstants;
use PayonePayment\TestCaseBase\PayoneTestBehavior;
use PHPUnit\Framework\TestCase;
use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressEntity;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\System\SystemConfig\SystemConfigService;

class CustomerDataPersistorTest extends TestCase
{
use PayoneTestBehavior;

private SalesChannelContext $context;

private EntityRepository $orderRepository;

private EntityRepository $customerRepository;

private EntityRepository $customerAddressRepository;

protected function setUp(): void
{
$this->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],
[''],
[' '],
];
}
}

0 comments on commit f3fb186

Please sign in to comment.