From fe74769a79c3964ded1d3aab79315a84e619a1c5 Mon Sep 17 00:00:00 2001 From: KrasnoshchokBohdan Date: Tue, 20 May 2025 18:18:44 +0300 Subject: [PATCH 1/5] magento/magento2#39915: Guest Prefix Not Saved to Quote Address 2.4.8 --- app/code/Magento/Quote/Model/CustomerManagement.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/code/Magento/Quote/Model/CustomerManagement.php b/app/code/Magento/Quote/Model/CustomerManagement.php index d2b2bb58159bf..498741072341c 100644 --- a/app/code/Magento/Quote/Model/CustomerManagement.php +++ b/app/code/Magento/Quote/Model/CustomerManagement.php @@ -162,6 +162,7 @@ public function validateAddresses(QuoteEntity $quote) if (empty($addresses) && $quote->getCustomerIsGuest()) { $billingAddress = $quote->getBillingAddress(); $customerAddress = $this->customerAddressFactory->create(); + $customerAddress->setPrefix($billingAddress->getPrefix()); $customerAddress->setFirstname($billingAddress->getFirstname()); $customerAddress->setMiddlename($billingAddress?->getMiddlename()); $customerAddress->setLastname($billingAddress->getLastname()); From cb889474e2f6082d46bdb4d26f3f5e67d4fa6619 Mon Sep 17 00:00:00 2001 From: KrasnoshchokBohdan <111579196+KrasnoshchokBohdan@users.noreply.github.com> Date: Mon, 26 May 2025 11:44:30 +0300 Subject: [PATCH 2/5] Update app/code/Magento/Quote/Model/CustomerManagement.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- app/code/Magento/Quote/Model/CustomerManagement.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Quote/Model/CustomerManagement.php b/app/code/Magento/Quote/Model/CustomerManagement.php index 498741072341c..00898fc6edec7 100644 --- a/app/code/Magento/Quote/Model/CustomerManagement.php +++ b/app/code/Magento/Quote/Model/CustomerManagement.php @@ -162,7 +162,7 @@ public function validateAddresses(QuoteEntity $quote) if (empty($addresses) && $quote->getCustomerIsGuest()) { $billingAddress = $quote->getBillingAddress(); $customerAddress = $this->customerAddressFactory->create(); - $customerAddress->setPrefix($billingAddress->getPrefix()); + $customerAddress->setPrefix($billingAddress?->getPrefix()); $customerAddress->setFirstname($billingAddress->getFirstname()); $customerAddress->setMiddlename($billingAddress?->getMiddlename()); $customerAddress->setLastname($billingAddress->getLastname()); From 5bae5617964acf972385da61f788fa77eb011145 Mon Sep 17 00:00:00 2001 From: KrasnoshchokBohdan Date: Fri, 4 Jul 2025 23:46:38 +0300 Subject: [PATCH 3/5] magento/magento2#39915: Guest Prefix Not Saved to Quote Address 2.4.8 - Add unit tests for `validateAddresses` with and without prefix --- .../Unit/Model/CustomerManagementTest.php | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/app/code/Magento/Quote/Test/Unit/Model/CustomerManagementTest.php b/app/code/Magento/Quote/Test/Unit/Model/CustomerManagementTest.php index b4916b053d2fd..911d391826dda 100644 --- a/app/code/Magento/Quote/Test/Unit/Model/CustomerManagementTest.php +++ b/app/code/Magento/Quote/Test/Unit/Model/CustomerManagementTest.php @@ -269,8 +269,53 @@ public function testValidateAddressesNotSavedInAddressBook() { $this->expectException(ValidatorException::class); $this->quoteMock->method('getCustomerIsGuest')->willReturn(true); + $this->quoteAddressMock->method('getPrefix')->willReturn(null); $this->quoteAddressMock->method('getStreet')->willReturn(['test']); $this->quoteAddressMock->method('getCustomAttributes')->willReturn(['test']); + $this->customerAddressMock->expects($this->once()) + ->method('setPrefix') + ->with(null) + ->willReturnSelf(); + $this->customerAddressFactoryMock->method('create') + ->willReturn($this->customerAddressMock); + $addressMock = $this->getMockBuilder(Address::class) + ->disableOriginalConstructor() + ->getMock(); + $this->addressFactoryMock->expects($this->exactly(1))->method('create')->willReturn($addressMock); + $this->quoteMock + ->expects($this->atMost(2)) + ->method('getBillingAddress') + ->willReturn($this->quoteAddressMock); + $this->quoteMock + ->expects($this->once()) + ->method('getShippingAddress') + ->willReturn($this->quoteAddressMock); + $this->quoteAddressMock->expects($this->any())->method('getCustomerAddressId')->willReturn(null); + $validatorMock = $this->getMockBuilder(Validator::class) + ->disableOriginalConstructor() + ->getMock(); + $this->validatorFactoryMock + ->expects($this->exactly(1)) + ->method('createValidator') + ->with('customer_address', 'save', null) + ->willReturn($validatorMock); + $validatorMock->expects($this->exactly(1))->method('isValid')->with($addressMock)->willReturn(false); + $validatorMock->expects($this->exactly(1))->method('getMessages')->willReturn([]); + $this->customerManagement->validateAddresses($this->quoteMock); + } + + public function testValidateAddressesNotSavedInAddressBookWithPrefix() + { + $this->expectException(ValidatorException::class); + $this->quoteMock->method('getCustomerIsGuest')->willReturn(true); + $this->quoteAddressMock->method('getPrefix')->willReturn('Mr.'); + $this->quoteAddressMock->method('getStreet')->willReturn(['test']); + $this->quoteAddressMock->method('getCustomAttributes')->willReturn(['test']); + $this->customerAddressMock->expects($this->once()) + ->method('setPrefix') + ->with('Mr.') + ->willReturnSelf(); + $this->customerAddressFactoryMock->method('create') ->willReturn($this->customerAddressMock); $addressMock = $this->getMockBuilder(Address::class) From ee6d0f35390999c17d2b19dfc1f773b6d543a021 Mon Sep 17 00:00:00 2001 From: KrasnoshchokBohdan Date: Sat, 12 Jul 2025 16:01:29 +0300 Subject: [PATCH 4/5] magento/magento2#39915: Guest Prefix Not Saved to Quote Address 2.4.8 Update CustomerManagement unit tests to fix guest prefix handling --- .../Unit/Model/CustomerManagementTest.php | 74 ++++++++----------- 1 file changed, 31 insertions(+), 43 deletions(-) diff --git a/app/code/Magento/Quote/Test/Unit/Model/CustomerManagementTest.php b/app/code/Magento/Quote/Test/Unit/Model/CustomerManagementTest.php index 57665cc9526e9..b5ff4386c54b7 100644 --- a/app/code/Magento/Quote/Test/Unit/Model/CustomerManagementTest.php +++ b/app/code/Magento/Quote/Test/Unit/Model/CustomerManagementTest.php @@ -20,6 +20,7 @@ use Magento\Quote\Model\CustomerManagement; use Magento\Quote\Model\Quote; use Magento\Quote\Model\Quote\Address; +use PHPUnit\Framework\MockObject\Exception; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Magento\Customer\Api\Data\RegionInterfaceFactory; @@ -291,10 +292,12 @@ public function testValidateAddressesNotSavedInAddressBook() $this->quoteAddressMock->method('getPrefix')->willReturn(null); $this->quoteAddressMock->method('getStreet')->willReturn(['test']); $this->quoteAddressMock->method('getCustomAttributes')->willReturn(['test']); - $this->customerAddressMock->expects($this->once()) + + $this->customerAddressMock->expects($this->atLeastOnce()) ->method('setPrefix') ->with(null) ->willReturnSelf(); + $this->customerAddressFactoryMock->method('create') ->willReturn($this->customerAddressMock); $addressMock = $this->getMockBuilder(Address::class) @@ -326,32 +329,15 @@ public function testValidateAddressesNotSavedInAddressBook() public function testValidateAddressesNotSavedInAddressBookWithPrefix() { $this->expectException(ValidatorException::class); - $this->quoteMock->method('getCustomerIsGuest')->willReturn(true); - $this->quoteAddressMock->method('getPrefix')->willReturn('Mr.'); - $this->quoteAddressMock->method('getStreet')->willReturn(['test']); - $this->quoteAddressMock->method('getCustomAttributes')->willReturn(['test']); - $this->customerAddressMock->expects($this->once()) - ->method('setPrefix') - ->with('Mr.') - ->willReturnSelf(); - $this->customerAddressFactoryMock->method('create') - ->willReturn($this->customerAddressMock); - $addressMock = $this->getMockBuilder(Address::class) - ->disableOriginalConstructor() - ->getMock(); - $this->addressFactoryMock->expects($this->exactly(1))->method('create')->willReturn($addressMock); - $this->quoteMock - ->expects($this->atMost(2)) - ->method('getBillingAddress') - ->willReturn($this->quoteAddressMock); - $this->quoteMock - $this->quoteMock->method('getBillingAddress')->willReturn($this->quoteAddressMock); - $this->quoteMock->method('getShippingAddress')->willReturn($this->quoteAddressMock); - $this->quoteAddressMock->method('getCustomerAddressId')->willReturn(null); + $regionData = [ + 'region' => 'California', + 'region_code' => 'CA', + 'region_id' => 12, + ]; - // Set up billing address data - $this->quoteAddressMock->method('getPrefix')->willReturn('Mr'); + $this->quoteMock->method('getCustomerIsGuest')->willReturn(true); + $this->quoteAddressMock->method('getPrefix')->willReturn('Mr.'); $this->quoteAddressMock->method('getFirstname')->willReturn('John'); $this->quoteAddressMock->method('getMiddlename')->willReturn('Q'); $this->quoteAddressMock->method('getLastname')->willReturn('Public'); @@ -366,6 +352,10 @@ public function testValidateAddressesNotSavedInAddressBookWithPrefix() $this->quoteAddressMock->method('getVatId')->willReturn('US123456789'); $this->quoteAddressMock->method('getRegion')->willReturn($regionData); $this->quoteAddressMock->method('getCustomAttributes')->willReturn(['custom_attr' => 'value']); + $this->quoteAddressMock->method('getCustomerAddressId')->willReturn(null); + + $this->quoteMock->method('getBillingAddress')->willReturn($this->quoteAddressMock); + $this->quoteMock->method('getShippingAddress')->willReturn($this->quoteAddressMock); // Region setup $regionMock = $this->createMock(RegionInterface::class); @@ -376,24 +366,22 @@ public function testValidateAddressesNotSavedInAddressBookWithPrefix() // Customer address object to be created $this->customerAddressFactoryMock->method('create')->willReturn($this->customerAddressMock); - $this->customerAddressMock->expects($this->once())->method('setPrefix')->with('Mr'); - $this->customerAddressMock->expects($this->once())->method('setFirstname')->with('John'); - $this->customerAddressMock->expects($this->once())->method('setMiddlename')->with('Q'); - $this->customerAddressMock->expects($this->once())->method('setLastname')->with('Public'); - $this->customerAddressMock->expects($this->once())->method('setSuffix')->with('Jr'); - $this->customerAddressMock->expects($this->once())->method('setCompany')->with('Acme Inc.'); - $this->customerAddressMock->expects($this->once())->method('setStreet')->with(['123 Main St']); - $this->customerAddressMock->expects($this->once())->method('setCountryId')->with('US'); - $this->customerAddressMock->expects($this->once())->method('setCity')->with('Los Angeles'); - $this->customerAddressMock->expects($this->once())->method('setPostcode')->with('90001'); - $this->customerAddressMock->expects($this->once())->method('setTelephone')->with('1234567890'); - $this->customerAddressMock->expects($this->once())->method('setFax')->with('9876543210'); - $this->customerAddressMock->expects($this->once())->method('setVatId')->with('US123456789'); - $this->customerAddressMock->expects($this->once())->method('setRegion')->with($regionMock); - $this->customerAddressMock - ->expects($this->once()) - ->method('setCustomAttributes') - ->with(['custom_attr' => 'value']); + + $this->customerAddressMock->expects($this->atLeastOnce())->method('setPrefix')->with('Mr.'); + $this->customerAddressMock->expects($this->atLeastOnce())->method('setFirstname')->with('John'); + $this->customerAddressMock->expects($this->atLeastOnce())->method('setMiddlename')->with('Q'); + $this->customerAddressMock->expects($this->atLeastOnce())->method('setLastname')->with('Public'); + $this->customerAddressMock->expects($this->atLeastOnce())->method('setSuffix')->with('Jr'); + $this->customerAddressMock->expects($this->atLeastOnce())->method('setCompany')->with('Acme Inc.'); + $this->customerAddressMock->expects($this->atLeastOnce())->method('setStreet')->with(['123 Main St']); + $this->customerAddressMock->expects($this->atLeastOnce())->method('setCountryId')->with('US'); + $this->customerAddressMock->expects($this->atLeastOnce())->method('setCity')->with('Los Angeles'); + $this->customerAddressMock->expects($this->atLeastOnce())->method('setPostcode')->with('90001'); + $this->customerAddressMock->expects($this->atLeastOnce())->method('setTelephone')->with('1234567890'); + $this->customerAddressMock->expects($this->atLeastOnce())->method('setFax')->with('9876543210'); + $this->customerAddressMock->expects($this->atLeastOnce())->method('setVatId')->with('US123456789'); + $this->customerAddressMock->expects($this->atLeastOnce())->method('setRegion')->with($regionMock); + $this->customerAddressMock->expects($this->atLeastOnce())->method('setCustomAttributes')->with(['custom_attr' => 'value']); // Validator to fail $validatorMock = $this->createMock(Validator::class); From 5be90e6ea6601f6b8ddd5b43beedd6fc19df7f20 Mon Sep 17 00:00:00 2001 From: KrasnoshchokBohdan Date: Tue, 15 Jul 2025 11:17:16 +0300 Subject: [PATCH 5/5] magento/magento2#39915: Guest Prefix Not Saved to Quote Address 2.4.8 Refactor CustomerManagementTest to remove unused region data. Removed unused region data array in CustomerManagementTest to clean up the code. Adjusted formatting for setCustomAttributes mock expectation for better readability and consistency. --- .../Quote/Test/Unit/Model/CustomerManagementTest.php | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/app/code/Magento/Quote/Test/Unit/Model/CustomerManagementTest.php b/app/code/Magento/Quote/Test/Unit/Model/CustomerManagementTest.php index b5ff4386c54b7..21f297ab1e277 100644 --- a/app/code/Magento/Quote/Test/Unit/Model/CustomerManagementTest.php +++ b/app/code/Magento/Quote/Test/Unit/Model/CustomerManagementTest.php @@ -282,12 +282,6 @@ public function testValidateAddressesNotSavedInAddressBook() { $this->expectException(ValidatorException::class); - $regionData = [ - 'region' => 'California', - 'region_code' => 'CA', - 'region_id' => 12, - ]; - $this->quoteMock->method('getCustomerIsGuest')->willReturn(true); $this->quoteAddressMock->method('getPrefix')->willReturn(null); $this->quoteAddressMock->method('getStreet')->willReturn(['test']); @@ -381,7 +375,9 @@ public function testValidateAddressesNotSavedInAddressBookWithPrefix() $this->customerAddressMock->expects($this->atLeastOnce())->method('setFax')->with('9876543210'); $this->customerAddressMock->expects($this->atLeastOnce())->method('setVatId')->with('US123456789'); $this->customerAddressMock->expects($this->atLeastOnce())->method('setRegion')->with($regionMock); - $this->customerAddressMock->expects($this->atLeastOnce())->method('setCustomAttributes')->with(['custom_attr' => 'value']); + $this->customerAddressMock->expects($this->atLeastOnce()) + ->method('setCustomAttributes') + ->with(['custom_attr' => 'value']); // Validator to fail $validatorMock = $this->createMock(Validator::class);