Skip to content

Commit 8187bf7

Browse files
committed
Merge remote-tracking branch 'adobe-commerce-tier-4/ACP2E-3296' into Tier4-Kings-PR-11-22-2024
2 parents 88660e7 + ba07827 commit 8187bf7

File tree

5 files changed

+83
-28
lines changed

5 files changed

+83
-28
lines changed

app/code/Magento/Quote/Model/CustomerManagement.php

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2014 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

@@ -10,6 +10,7 @@
1010
use Magento\Customer\Api\AccountManagementInterface as AccountManagement;
1111
use Magento\Customer\Api\AddressRepositoryInterface as CustomerAddressRepository;
1212
use Magento\Customer\Api\CustomerRepositoryInterface as CustomerRepository;
13+
use Magento\Customer\Api\Data\AddressInterfaceFactory;
1314
use Magento\Customer\Model\AddressFactory;
1415
use Magento\Framework\App\ObjectManager;
1516
use Magento\Framework\Validator\Exception as ValidatorException;
@@ -46,24 +47,32 @@ class CustomerManagement
4647
*/
4748
private $addressFactory;
4849

50+
/**
51+
* @var AddressInterfaceFactory
52+
*/
53+
private $customerAddressFactory;
54+
4955
/**
5056
* CustomerManagement constructor.
5157
* @param CustomerRepository $customerRepository
5258
* @param CustomerAddressRepository $customerAddressRepository
5359
* @param AccountManagement $accountManagement
60+
* @param AddressInterfaceFactory $customerAddressFactory
5461
* @param ValidatorFactory|null $validatorFactory
5562
* @param AddressFactory|null $addressFactory
5663
*/
5764
public function __construct(
5865
CustomerRepository $customerRepository,
5966
CustomerAddressRepository $customerAddressRepository,
6067
AccountManagement $accountManagement,
68+
AddressInterfaceFactory $customerAddressFactory,
6169
ValidatorFactory $validatorFactory = null,
6270
AddressFactory $addressFactory = null
6371
) {
6472
$this->customerRepository = $customerRepository;
6573
$this->customerAddressRepository = $customerAddressRepository;
6674
$this->accountManagement = $accountManagement;
75+
$this->customerAddressFactory = $customerAddressFactory;
6776
$this->validatorFactory = $validatorFactory ?: ObjectManager::getInstance()
6877
->get(ValidatorFactory::class);
6978
$this->addressFactory = $addressFactory ?: ObjectManager::getInstance()
@@ -150,18 +159,29 @@ public function validateAddresses(QuoteEntity $quote)
150159
$quote->getShippingAddress()->getCustomerAddressId()
151160
);
152161
}
153-
if (!empty($addresses)) {
154-
foreach ($addresses as $address) {
155-
$validator = $this->validatorFactory->createValidator('customer_address', 'save');
156-
$addressModel = $this->addressFactory->create();
157-
$addressModel->updateData($address);
158-
if (!$validator->isValid($addressModel)) {
159-
throw new ValidatorException(
160-
null,
161-
null,
162-
$validator->getMessages()
163-
);
164-
}
162+
if (empty($addresses) && $quote->getCustomerIsGuest()) {
163+
$billingAddress = $quote->getBillingAddress();
164+
$customerAddress = $this->customerAddressFactory->create();
165+
$customerAddress->setFirstname($billingAddress->getFirstname());
166+
$customerAddress->setLastname($billingAddress->getLastname());
167+
$customerAddress->setStreet($billingAddress->getStreet());
168+
$customerAddress->setCity($billingAddress->getCity());
169+
$customerAddress->setPostcode($billingAddress->getPostcode());
170+
$customerAddress->setTelephone($billingAddress->getTelephone());
171+
$customerAddress->setCountryId($billingAddress->getCountryId());
172+
$customerAddress->setCustomAttributes($billingAddress->getCustomAttributes());
173+
$addresses[] = $customerAddress;
174+
}
175+
foreach ($addresses as $address) {
176+
$validator = $this->validatorFactory->createValidator('customer_address', 'save');
177+
$addressModel = $this->addressFactory->create();
178+
$addressModel->updateData($address);
179+
if (!$validator->isValid($addressModel)) {
180+
throw new ValidatorException(
181+
null,
182+
null,
183+
$validator->getMessages()
184+
);
165185
}
166186
}
167187
}

app/code/Magento/Quote/Model/QuoteManagement.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2014 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

@@ -565,10 +565,10 @@ protected function submitQuote(QuoteEntity $quote, $orderData = [])
565565
if (!$quote->getCustomerIsGuest()) {
566566
if ($quote->getCustomerId()) {
567567
$this->_prepareCustomerQuote($quote);
568-
$this->customerManagement->validateAddresses($quote);
569568
}
570569
$this->customerManagement->populateCustomerInfo($quote);
571570
}
571+
$this->customerManagement->validateAddresses($quote);
572572
$addresses = [];
573573
$quote->reserveOrderId();
574574
if ($quote->isVirtual()) {

app/code/Magento/Quote/Test/Unit/Model/CustomerManagementTest.php

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2014 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

@@ -11,9 +11,11 @@
1111
use Magento\Customer\Api\AddressRepositoryInterface;
1212
use Magento\Customer\Api\CustomerRepositoryInterface;
1313
use Magento\Customer\Api\Data\AddressInterface;
14+
use Magento\Customer\Api\Data\AddressInterfaceFactory;
1415
use Magento\Customer\Api\Data\CustomerInterface;
1516
use Magento\Customer\Model\AddressFactory;
1617
use Magento\Framework\Validator;
18+
use Magento\Framework\Validator\Exception as ValidatorException;
1719
use Magento\Framework\Validator\Factory;
1820
use Magento\Quote\Model\CustomerManagement;
1921
use Magento\Quote\Model\Quote;
@@ -76,6 +78,11 @@ class CustomerManagementTest extends TestCase
7678
*/
7779
private $addressFactoryMock;
7880

81+
/**
82+
* @var MockObject
83+
*/
84+
private $customerAddressFactoryMock;
85+
7986
protected function setUp(): void
8087
{
8188
$this->customerRepositoryMock = $this->getMockForAbstractClass(
@@ -107,7 +114,8 @@ protected function setUp(): void
107114
);
108115
$this->quoteMock = $this->getMockBuilder(Quote::class)
109116
->addMethods(['getPasswordHash'])
110-
->onlyMethods(['getId', 'getCustomer', 'getBillingAddress', 'getShippingAddress', 'setCustomer'])
117+
->onlyMethods(['getId', 'getCustomer', 'getBillingAddress', 'getShippingAddress', 'setCustomer',
118+
'getCustomerIsGuest'])
111119
->disableOriginalConstructor()
112120
->getMock();
113121
$this->quoteAddressMock = $this->createMock(Address::class);
@@ -136,10 +144,20 @@ protected function setUp(): void
136144
$this->validatorFactoryMock = $this->getMockBuilder(Factory::class)
137145
->disableOriginalConstructor()
138146
->getMock();
147+
$this->customerAddressFactoryMock = $this->getMockForAbstractClass(
148+
AddressInterfaceFactory::class,
149+
[],
150+
'',
151+
false,
152+
true,
153+
true,
154+
['create']
155+
);
139156
$this->customerManagement = new CustomerManagement(
140157
$this->customerRepositoryMock,
141158
$this->customerAddressRepositoryMock,
142159
$this->accountManagementMock,
160+
$this->customerAddressFactoryMock,
143161
$this->validatorFactoryMock,
144162
$this->addressFactoryMock
145163
);
@@ -249,19 +267,35 @@ public function testValidateAddresses()
249267

250268
public function testValidateAddressesNotSavedInAddressBook()
251269
{
270+
$this->expectException(ValidatorException::class);
271+
$this->quoteMock->method('getCustomerIsGuest')->willReturn(true);
272+
$this->quoteAddressMock->method('getStreet')->willReturn(['test']);
273+
$this->quoteAddressMock->method('getCustomAttributes')->willReturn(['test']);
274+
$this->customerAddressFactoryMock->method('create')
275+
->willReturn($this->customerAddressMock);
276+
$addressMock = $this->getMockBuilder(Address::class)
277+
->disableOriginalConstructor()
278+
->getMock();
279+
$this->addressFactoryMock->expects($this->exactly(1))->method('create')->willReturn($addressMock);
252280
$this->quoteMock
253-
->expects($this->once())
281+
->expects($this->atMost(2))
254282
->method('getBillingAddress')
255283
->willReturn($this->quoteAddressMock);
256284
$this->quoteMock
257285
->expects($this->once())
258286
->method('getShippingAddress')
259287
->willReturn($this->quoteAddressMock);
260288
$this->quoteAddressMock->expects($this->any())->method('getCustomerAddressId')->willReturn(null);
289+
$validatorMock = $this->getMockBuilder(Validator::class)
290+
->disableOriginalConstructor()
291+
->getMock();
261292
$this->validatorFactoryMock
262-
->expects($this->never())
293+
->expects($this->exactly(1))
263294
->method('createValidator')
264-
->with('customer_address', 'save', null);
295+
->with('customer_address', 'save', null)
296+
->willReturn($validatorMock);
297+
$validatorMock->expects($this->exactly(1))->method('isValid')->with($addressMock)->willReturn(false);
298+
$validatorMock->expects($this->exactly(1))->method('getMessages')->willReturn([]);
265299
$this->customerManagement->validateAddresses($this->quoteMock);
266300
}
267301
}

dev/tests/integration/framework/Magento/TestFramework/ApplicationStateComparator/_files/state-filter-list.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2023 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

@@ -41,6 +41,7 @@
4141
Magento\Framework\Logger\Handler\Base::class => [ // TODO: remove this after ACPT-1034 is fixed
4242
'stream' => null,
4343
],
44+
Magento\Framework\Validator\AbstractValidator::class => ['_defaultTranslator' => null],
4445
],
4546
'services' => [ // Note: These apply only to the service names that match.
4647
Magento\Framework\ObjectManager\ConfigInterface::class => ['_mergedArguments' => null],

dev/tests/integration/framework/Magento/TestFramework/ApplicationStateComparator/_files/state-skip-list.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2023 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

@@ -81,7 +81,7 @@
8181
// Magento\Customer\Model\ResourceModel\AddressRepository::class => null,
8282
// Magento\Customer\Model\CustomerRegistry::class => null,
8383
// Magento\Customer\Model\ResourceModel\Address\Relation::class => null,
84-
// Magento\Customer\Model\ResourceModel\Address::class => null,
84+
Magento\Customer\Model\ResourceModel\Address::class => null,
8585
// Magento\Customer\Model\AttributeMetadataConverter::class => null,
8686
Magento\Customer\Model\Metadata\CustomerMetadata::class => null, // TODO?
8787
// Magento\Customer\Model\Metadata\AttributeMetadataCache::class => null,

0 commit comments

Comments
 (0)