Skip to content

Commit 0b600de

Browse files
committed
AC-9672: Fix for customer address extraction
1 parent d8db009 commit 0b600de

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed

app/code/Magento/Customer/Model/ResourceModel/CustomerRepository.php

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
7+
declare(strict_types=1);
8+
69
namespace Magento\Customer\Model\ResourceModel;
710

811
use Magento\Customer\Api\CustomerMetadataInterface;
912
use Magento\Customer\Api\CustomerRepositoryInterface;
1013
use Magento\Customer\Api\Data\CustomerInterface;
1114
use Magento\Customer\Api\Data\CustomerSearchResultsInterfaceFactory;
1215
use Magento\Customer\Api\GroupRepositoryInterface;
16+
use Magento\Customer\Model\Address\AbstractAddress;
1317
use Magento\Customer\Model\Customer as CustomerModel;
1418
use Magento\Customer\Model\Customer\NotificationStorage;
1519
use Magento\Customer\Model\CustomerFactory;
@@ -27,6 +31,7 @@
2731
use Magento\Framework\Api\SearchCriteriaInterface;
2832
use Magento\Framework\App\ObjectManager;
2933
use Magento\Framework\Event\ManagerInterface;
34+
use Magento\Framework\Exception\InputException;
3035
use Magento\Framework\Exception\LocalizedException;
3136
use Magento\Framework\Exception\NoSuchEntityException;
3237
use Magento\Store\Model\StoreManagerInterface;
@@ -195,12 +200,19 @@ public function __construct(
195200
* @throws \Magento\Framework\Exception\LocalizedException
196201
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
197202
* @SuppressWarnings(PHPMD.NPathComplexity)
203+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
198204
*/
199205
public function save(CustomerInterface $customer, $passwordHash = null)
200206
{
201207
/** @var NewOperation|null $delegatedNewOperation */
202208
$delegatedNewOperation = !$customer->getId() ? $this->delegatedStorage->consumeNewOperation() : null;
203209
$prevCustomerData = $prevCustomerDataArr = null;
210+
if ($customer->getDefaultBilling()) {
211+
$this->validateDefaultAddress($customer, AbstractAddress::TYPE_BILLING);
212+
}
213+
if ($customer->getDefaultShipping()) {
214+
$this->validateDefaultAddress($customer, AbstractAddress::TYPE_SHIPPING);
215+
}
204216
if ($customer->getId()) {
205217
$prevCustomerData = $this->getById($customer->getId());
206218
$prevCustomerDataArr = $this->prepareCustomerData($prevCustomerData->__toArray());
@@ -228,7 +240,7 @@ public function save(CustomerInterface $customer, $passwordHash = null)
228240
$prevCustomerData ? $prevCustomerData->getStoreId() : $this->storeManager->getStore()->getId()
229241
);
230242
}
231-
$this->validateGroupId($customer->getGroupId());
243+
$this->validateGroupId((int)$customer->getGroupId());
232244
$this->setCustomerGroupId($customerModel, $customerArr, $prevCustomerDataArr);
233245
// Need to use attribute set or future updates can cause data loss
234246
if (!$customerModel->getAttributeSetId()) {
@@ -553,4 +565,33 @@ private function prepareCustomerData(array $customerData): array
553565
}
554566
return $customerData;
555567
}
568+
569+
/**
570+
* To validate default address
571+
*
572+
* @param CustomerInterface $customer
573+
* @param string $defaultAddressType
574+
* @return void
575+
* @throws InputException
576+
*/
577+
private function validateDefaultAddress(
578+
CustomerInterface $customer,
579+
string $defaultAddressType
580+
): void {
581+
$addressId = $defaultAddressType === AbstractAddress::TYPE_BILLING ? $customer->getDefaultBilling()
582+
: $customer->getDefaultShipping();
583+
584+
if ($customer->getAddresses()) {
585+
foreach ($customer->getAddresses() as $address) {
586+
if ((int) $addressId === (int) $address->getId()) {
587+
return;
588+
}
589+
}
590+
591+
throw InputException::invalidFieldValue(
592+
$defaultAddressType === AbstractAddress::TYPE_BILLING ? 'default_billing' : 'default_shipping',
593+
$addressId
594+
);
595+
}
596+
}
556597
}

dev/tests/api-functional/testsuite/Magento/Customer/Api/AccountManagementTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ public function testActivateCustomer()
265265
$customerData[Customer::ID],
266266
[
267267
'id' => $customerData[Customer::ID],
268+
'addresses' => $customerData[Customer::KEY_ADDRESSES],
268269
'confirmation' => CustomerHelper::CONFIRMATION
269270
]
270271
);

dev/tests/integration/testsuite/Magento/Customer/Model/AccountManagement/CreateAccountTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,8 @@ public function testCreateNewCustomerWithPasswordHashWithNotAllowedCountry(): vo
421421
$customerData = $this->customerRepository->getById($customerId);
422422
$customerData->getAddresses()[1]->setRegion(null)->setCountryId($allowedCountryIdForSecondWebsite)
423423
->setRegionId(null);
424+
$customerData->getAddresses()[1]->setIsDefaultBilling(true);
425+
$customerData->getAddresses()[1]->setIsDefaultShipping(true);
424426
$customerData->setStoreId($store->getId())->setWebsiteId($store->getWebsiteId())->setId(null);
425427
$password = $this->random->getRandomString(8);
426428
$passwordHash = $this->encryptor->getHash($password, true);

dev/tests/integration/testsuite/Magento/Quote/Model/Quote/AddressTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public function testSameAsBillingWhenCustomerHasNoDefaultShippingAddress($unsetI
172172
/** @var AddressRepositoryInterface $addressRepository */
173173
$addressRepository = Bootstrap::getObjectManager()
174174
->create(AddressRepositoryInterface::class);
175-
$this->_customer->setDefaultShipping(-1)
175+
$this->_customer->setDefaultShipping(1)
176176
->setAddresses(
177177
[
178178
$addressRepository->getById($this->_address->getId()),
@@ -211,7 +211,7 @@ public function testSameAsBillingWhenCustomerHasDefaultShippingAddress()
211211
/** @var AddressRepositoryInterface $addressRepository */
212212
$addressRepository = Bootstrap::getObjectManager()
213213
->create(AddressRepositoryInterface::class);
214-
$this->_customer->setDefaultShipping(2)
214+
$this->_customer->setDefaultShipping(1)
215215
->setAddresses([$addressRepository->getById($this->_address->getId())]);
216216
$this->_customer = $this->customerRepository->save($this->_customer);
217217
// we should save the customer data in order to be able to use it

0 commit comments

Comments
 (0)