Skip to content

Commit 82da60a

Browse files
Merge branch 'AC-9672' into cia-2.4.7-develop-bugfix-03012024
2 parents 179f2c6 + 630dfd2 commit 82da60a

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,6 +3,9 @@
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;
@@ -27,6 +30,7 @@
2730
use Magento\Framework\Api\SearchCriteriaInterface;
2831
use Magento\Framework\App\ObjectManager;
2932
use Magento\Framework\Event\ManagerInterface;
33+
use Magento\Framework\Exception\InputException;
3034
use Magento\Framework\Exception\LocalizedException;
3135
use Magento\Framework\Exception\NoSuchEntityException;
3236
use Magento\Store\Model\StoreManagerInterface;
@@ -195,12 +199,19 @@ public function __construct(
195199
* @throws \Magento\Framework\Exception\LocalizedException
196200
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
197201
* @SuppressWarnings(PHPMD.NPathComplexity)
202+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
198203
*/
199204
public function save(CustomerInterface $customer, $passwordHash = null)
200205
{
201206
/** @var NewOperation|null $delegatedNewOperation */
202207
$delegatedNewOperation = !$customer->getId() ? $this->delegatedStorage->consumeNewOperation() : null;
203208
$prevCustomerData = $prevCustomerDataArr = null;
209+
if ($customer->getDefaultBilling()) {
210+
$this->validateDefaultAddress($customer, CustomerInterface::DEFAULT_BILLING);
211+
}
212+
if ($customer->getDefaultShipping()) {
213+
$this->validateDefaultAddress($customer, CustomerInterface::DEFAULT_SHIPPING);
214+
}
204215
if ($customer->getId()) {
205216
$prevCustomerData = $this->getById($customer->getId());
206217
$prevCustomerDataArr = $this->prepareCustomerData($prevCustomerData->__toArray());
@@ -228,7 +239,7 @@ public function save(CustomerInterface $customer, $passwordHash = null)
228239
$prevCustomerData ? $prevCustomerData->getStoreId() : $this->storeManager->getStore()->getId()
229240
);
230241
}
231-
$this->validateGroupId($customer->getGroupId());
242+
$this->validateGroupId((int)$customer->getGroupId());
232243
$this->setCustomerGroupId($customerModel, $customerArr, $prevCustomerDataArr);
233244
// Need to use attribute set or future updates can cause data loss
234245
if (!$customerModel->getAttributeSetId()) {
@@ -553,4 +564,34 @@ private function prepareCustomerData(array $customerData): array
553564
}
554565
return $customerData;
555566
}
567+
568+
/**
569+
* To validate default address
570+
*
571+
* @param CustomerInterface $customer
572+
* @param string $defaultAddressType
573+
* @return void
574+
* @throws InputException
575+
*/
576+
private function validateDefaultAddress(
577+
CustomerInterface $customer,
578+
string $defaultAddressType
579+
): void {
580+
$addressId = $defaultAddressType === CustomerInterface::DEFAULT_BILLING ? $customer->getDefaultBilling()
581+
: $customer->getDefaultShipping();
582+
if ($customer->getAddresses()) {
583+
foreach ($customer->getAddresses() as $address) {
584+
if ((int) $addressId === (int) $address->getId()) {
585+
return;
586+
}
587+
}
588+
589+
throw new InputException(
590+
__(
591+
'The %fieldName value is invalid. Set the correct value and try again.',
592+
['fieldName' => $defaultAddressType]
593+
)
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)