Skip to content

Commit 67d1013

Browse files
authored
Merge branch '2.4-develop' into 2.4-develop-system_file-xsd-fix
2 parents 26c3655 + f1adb44 commit 67d1013

File tree

4 files changed

+354
-40
lines changed

4 files changed

+354
-40
lines changed

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

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
use Magento\Framework\Validator\Exception as ValidatorException;
1717
use Magento\Framework\Validator\Factory as ValidatorFactory;
1818
use Magento\Quote\Model\Quote as QuoteEntity;
19+
use Magento\Customer\Api\Data\RegionInterfaceFactory;
20+
use Magento\Customer\Api\Data\AddressInterface;
21+
use Magento\Quote\Model\Quote\Address as QuoteAddress;
1922

2023
/**
2124
* Class Customer
@@ -52,12 +55,18 @@ class CustomerManagement
5255
*/
5356
private $customerAddressFactory;
5457

58+
/**
59+
* @var RegionInterfaceFactory
60+
*/
61+
private $regionFactory;
62+
5563
/**
5664
* CustomerManagement constructor.
5765
* @param CustomerRepository $customerRepository
5866
* @param CustomerAddressRepository $customerAddressRepository
5967
* @param AccountManagement $accountManagement
6068
* @param AddressInterfaceFactory $customerAddressFactory
69+
* @param RegionInterfaceFactory $regionFactory
6170
* @param ValidatorFactory|null $validatorFactory
6271
* @param AddressFactory|null $addressFactory
6372
*/
@@ -66,13 +75,15 @@ public function __construct(
6675
CustomerAddressRepository $customerAddressRepository,
6776
AccountManagement $accountManagement,
6877
AddressInterfaceFactory $customerAddressFactory,
78+
RegionInterfaceFactory $regionFactory,
6979
?ValidatorFactory $validatorFactory = null,
7080
?AddressFactory $addressFactory = null
7181
) {
7282
$this->customerRepository = $customerRepository;
7383
$this->customerAddressRepository = $customerAddressRepository;
7484
$this->accountManagement = $accountManagement;
7585
$this->customerAddressFactory = $customerAddressFactory;
86+
$this->regionFactory = $regionFactory;
7687
$this->validatorFactory = $validatorFactory ?: ObjectManager::getInstance()
7788
->get(ValidatorFactory::class);
7889
$this->addressFactory = $addressFactory ?: ObjectManager::getInstance()
@@ -161,17 +172,7 @@ public function validateAddresses(QuoteEntity $quote)
161172
}
162173
if (empty($addresses) && $quote->getCustomerIsGuest()) {
163174
$billingAddress = $quote->getBillingAddress();
164-
$customerAddress = $this->customerAddressFactory->create();
165-
$customerAddress->setFirstname($billingAddress->getFirstname());
166-
$customerAddress->setMiddlename($billingAddress?->getMiddlename());
167-
$customerAddress->setLastname($billingAddress->getLastname());
168-
$customerAddress->setStreet($billingAddress->getStreet());
169-
$customerAddress->setCity($billingAddress->getCity());
170-
$customerAddress->setPostcode($billingAddress->getPostcode());
171-
$customerAddress->setTelephone($billingAddress->getTelephone());
172-
$customerAddress->setCountryId($billingAddress->getCountryId());
173-
$customerAddress->setCustomAttributes($billingAddress->getCustomAttributes());
174-
$addresses[] = $customerAddress;
175+
$addresses[] = $this->createCustomerAddressFromBilling($billingAddress);
175176
}
176177
foreach ($addresses as $address) {
177178
$validator = $this->validatorFactory->createValidator('customer_address', 'save');
@@ -186,4 +187,43 @@ public function validateAddresses(QuoteEntity $quote)
186187
}
187188
}
188189
}
190+
191+
/**
192+
* Creates guest customer address from a billing address.
193+
*
194+
* @param QuoteAddress $billingAddress
195+
* @return AddressInterface
196+
*/
197+
private function createCustomerAddressFromBilling(QuoteAddress $billingAddress): AddressInterface
198+
{
199+
$customerAddress = $this->customerAddressFactory->create();
200+
$customerAddress->setPrefix($billingAddress?->getPrefix());
201+
$customerAddress->setFirstname($billingAddress->getFirstname());
202+
$customerAddress->setMiddlename($billingAddress?->getMiddlename());
203+
$customerAddress->setLastname($billingAddress->getLastname());
204+
$customerAddress->setSuffix($billingAddress?->getSuffix());
205+
$customerAddress->setCompany($billingAddress?->getCompany());
206+
$customerAddress->setStreet($billingAddress->getStreet());
207+
$customerAddress->setCountryId($billingAddress->getCountryId());
208+
$customerAddress->setCity($billingAddress->getCity());
209+
$customerAddress->setPostcode($billingAddress->getPostcode());
210+
$customerAddress->setTelephone($billingAddress->getTelephone());
211+
$customerAddress->setFax($billingAddress?->getFax());
212+
$customerAddress->setVatId($billingAddress?->getVatId());
213+
$regionData = $billingAddress->getRegion();
214+
if (is_array($regionData)) {
215+
$region = $this->regionFactory->create();
216+
$region->setRegion($regionData['region'] ?? null);
217+
$region->setRegionCode($regionData['region_code'] ?? null);
218+
$region->setRegionId($regionData['region_id'] ?? null);
219+
} elseif (is_string($regionData)) {
220+
$region = $this->regionFactory->create();
221+
$region->setRegion($regionData);
222+
} else {
223+
$region = null;
224+
}
225+
$customerAddress->setRegion($region);
226+
$customerAddress->setCustomAttributes($billingAddress->getCustomAttributes());
227+
return $customerAddress;
228+
}
189229
}

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

Lines changed: 76 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
use Magento\Quote\Model\Quote\Address;
2323
use PHPUnit\Framework\MockObject\MockObject;
2424
use PHPUnit\Framework\TestCase;
25+
use Magento\Customer\Api\Data\RegionInterfaceFactory;
26+
use Magento\Customer\Api\Data\RegionInterface;
2527

2628
/**
2729
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -83,6 +85,11 @@ class CustomerManagementTest extends TestCase
8385
*/
8486
private $customerAddressFactoryMock;
8587

88+
/**
89+
* @var MockObject
90+
*/
91+
private $regionFactoryMock;
92+
8693
protected function setUp(): void
8794
{
8895
$this->customerRepositoryMock = $this->getMockForAbstractClass(
@@ -153,11 +160,16 @@ protected function setUp(): void
153160
true,
154161
['create']
155162
);
163+
$this->regionFactoryMock = $this->getMockBuilder(RegionInterfaceFactory::class)
164+
->disableOriginalConstructor()
165+
->onlyMethods(['create'])
166+
->getMock();
156167
$this->customerManagement = new CustomerManagement(
157168
$this->customerRepositoryMock,
158169
$this->customerAddressRepositoryMock,
159170
$this->accountManagementMock,
160171
$this->customerAddressFactoryMock,
172+
$this->regionFactoryMock,
161173
$this->validatorFactoryMock,
162174
$this->addressFactoryMock
163175
);
@@ -268,34 +280,72 @@ public function testValidateAddresses()
268280
public function testValidateAddressesNotSavedInAddressBook()
269281
{
270282
$this->expectException(ValidatorException::class);
283+
284+
$regionData = [
285+
'region' => 'California',
286+
'region_code' => 'CA',
287+
'region_id' => 12,
288+
];
289+
271290
$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);
280-
$this->quoteMock
281-
->expects($this->atMost(2))
282-
->method('getBillingAddress')
283-
->willReturn($this->quoteAddressMock);
284-
$this->quoteMock
291+
$this->quoteMock->method('getBillingAddress')->willReturn($this->quoteAddressMock);
292+
$this->quoteMock->method('getShippingAddress')->willReturn($this->quoteAddressMock);
293+
$this->quoteAddressMock->method('getCustomerAddressId')->willReturn(null);
294+
295+
// Set up billing address data
296+
$this->quoteAddressMock->method('getPrefix')->willReturn('Mr');
297+
$this->quoteAddressMock->method('getFirstname')->willReturn('John');
298+
$this->quoteAddressMock->method('getMiddlename')->willReturn('Q');
299+
$this->quoteAddressMock->method('getLastname')->willReturn('Public');
300+
$this->quoteAddressMock->method('getSuffix')->willReturn('Jr');
301+
$this->quoteAddressMock->method('getCompany')->willReturn('Acme Inc.');
302+
$this->quoteAddressMock->method('getStreet')->willReturn(['123 Main St']);
303+
$this->quoteAddressMock->method('getCountryId')->willReturn('US');
304+
$this->quoteAddressMock->method('getCity')->willReturn('Los Angeles');
305+
$this->quoteAddressMock->method('getPostcode')->willReturn('90001');
306+
$this->quoteAddressMock->method('getTelephone')->willReturn('1234567890');
307+
$this->quoteAddressMock->method('getFax')->willReturn('9876543210');
308+
$this->quoteAddressMock->method('getVatId')->willReturn('US123456789');
309+
$this->quoteAddressMock->method('getRegion')->willReturn($regionData);
310+
$this->quoteAddressMock->method('getCustomAttributes')->willReturn(['custom_attr' => 'value']);
311+
312+
// Region setup
313+
$regionMock = $this->createMock(RegionInterface::class);
314+
$this->regionFactoryMock->method('create')->willReturn($regionMock);
315+
$regionMock->expects($this->once())->method('setRegion')->with('California')->willReturnSelf();
316+
$regionMock->expects($this->once())->method('setRegionCode')->with('CA')->willReturnSelf();
317+
$regionMock->expects($this->once())->method('setRegionId')->with(12)->willReturnSelf();
318+
319+
// Customer address object to be created
320+
$this->customerAddressFactoryMock->method('create')->willReturn($this->customerAddressMock);
321+
$this->customerAddressMock->expects($this->once())->method('setPrefix')->with('Mr');
322+
$this->customerAddressMock->expects($this->once())->method('setFirstname')->with('John');
323+
$this->customerAddressMock->expects($this->once())->method('setMiddlename')->with('Q');
324+
$this->customerAddressMock->expects($this->once())->method('setLastname')->with('Public');
325+
$this->customerAddressMock->expects($this->once())->method('setSuffix')->with('Jr');
326+
$this->customerAddressMock->expects($this->once())->method('setCompany')->with('Acme Inc.');
327+
$this->customerAddressMock->expects($this->once())->method('setStreet')->with(['123 Main St']);
328+
$this->customerAddressMock->expects($this->once())->method('setCountryId')->with('US');
329+
$this->customerAddressMock->expects($this->once())->method('setCity')->with('Los Angeles');
330+
$this->customerAddressMock->expects($this->once())->method('setPostcode')->with('90001');
331+
$this->customerAddressMock->expects($this->once())->method('setTelephone')->with('1234567890');
332+
$this->customerAddressMock->expects($this->once())->method('setFax')->with('9876543210');
333+
$this->customerAddressMock->expects($this->once())->method('setVatId')->with('US123456789');
334+
$this->customerAddressMock->expects($this->once())->method('setRegion')->with($regionMock);
335+
$this->customerAddressMock
285336
->expects($this->once())
286-
->method('getShippingAddress')
287-
->willReturn($this->quoteAddressMock);
288-
$this->quoteAddressMock->expects($this->any())->method('getCustomerAddressId')->willReturn(null);
289-
$validatorMock = $this->getMockBuilder(Validator::class)
290-
->disableOriginalConstructor()
291-
->getMock();
292-
$this->validatorFactoryMock
293-
->expects($this->exactly(1))
294-
->method('createValidator')
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([]);
337+
->method('setCustomAttributes')
338+
->with(['custom_attr' => 'value']);
339+
340+
// Validator to fail
341+
$validatorMock = $this->createMock(Validator::class);
342+
$this->validatorFactoryMock->method('createValidator')->willReturn($validatorMock);
343+
$validatorMock->expects($this->once())->method('isValid')->willReturn(false);
344+
$validatorMock->expects($this->once())->method('getMessages')->willReturn([]);
345+
346+
$addressModelMock = $this->createMock(\Magento\Customer\Model\Address::class);
347+
$this->addressFactoryMock->method('create')->willReturn($addressModelMock);
348+
299349
$this->customerManagement->validateAddresses($this->quoteMock);
300350
}
301351
}

0 commit comments

Comments
 (0)