Skip to content

magento/magento2#39915: Guest Prefix Not Saved to Quote Address 2.4.8 #39926

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions app/code/Magento/Quote/Model/CustomerManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,18 @@ 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());
$customerAddress->setStreet($billingAddress->getStreet());
$customerAddress->setCity($billingAddress->getCity());
$customerAddress->setPostcode($billingAddress->getPostcode());
$customerAddress->setTelephone($billingAddress->getTelephone());
$customerAddress->setCountryId($billingAddress->getCountryId());
$customerAddress->setCustomAttributes($billingAddress->getCustomAttributes());
$addresses[] = $customerAddress;
$addresses[] = $this->createCustomerAddressFromBilling($billingAddress);
}
foreach ($addresses as $address) {
Expand Down
86 changes: 64 additions & 22 deletions app/code/Magento/Quote/Test/Unit/Model/CustomerManagementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -281,19 +282,56 @@ 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->atLeastOnce())
->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);

$regionData = [
'region' => 'California',
'region_code' => 'CA',
'region_id' => 12,
];

$this->quoteMock->method('getCustomerIsGuest')->willReturn(true);
$this->quoteMock->method('getBillingAddress')->willReturn($this->quoteAddressMock);
$this->quoteMock->method('getShippingAddress')->willReturn($this->quoteAddressMock);
$this->quoteAddressMock->method('getCustomerAddressId')->willReturn(null);

// Set up billing address data
$this->quoteAddressMock->method('getPrefix')->willReturn('Mr');
$this->quoteAddressMock->method('getPrefix')->willReturn('Mr.');
$this->quoteAddressMock->method('getFirstname')->willReturn('John');
$this->quoteAddressMock->method('getMiddlename')->willReturn('Q');
$this->quoteAddressMock->method('getLastname')->willReturn('Public');
Expand All @@ -308,6 +346,10 @@ public function testValidateAddressesNotSavedInAddressBook()
$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);
Expand All @@ -318,22 +360,22 @@ public function testValidateAddressesNotSavedInAddressBook()

// 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())

$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']);

Expand Down