Skip to content

Commit

Permalink
ACP2E-2470: Persistent shopping cart cleared during checkout step
Browse files Browse the repository at this point in the history
  • Loading branch information
dhorytskyi committed Apr 27, 2024
1 parent e4d5225 commit 8c14c08
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 57 deletions.
2 changes: 1 addition & 1 deletion app/code/Magento/Customer/Model/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ public function getDataModel()
\Magento\Customer\Api\Data\CustomerInterface::class
);
$customerDataObject->setAddresses($addressesData)
->setId($this->getId() ? (int) $this->getId() : null);
->setId($this->getId());
return $customerDataObject;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
use Magento\Quote\Model\Quote;
use Magento\Quote\Model\QuoteManagement;

/**
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
*/
class ConvertCustomerCartToGuest
{
/**
Expand Down
6 changes: 0 additions & 6 deletions app/code/Magento/Persistent/Model/QuoteManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,16 @@
class QuoteManager
{
/**
* Persistent session
*
* @var \Magento\Persistent\Helper\Session
*/
protected $persistentSession;

/**
* Checkout session
*
* @var \Magento\Checkout\Model\Session
*/
protected $checkoutSession;

/**
* Persistent data
*
* @var Data
*/
protected $persistentData;
Expand Down
75 changes: 26 additions & 49 deletions app/code/Magento/Persistent/Test/Unit/Model/QuoteManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,30 +343,38 @@ public function testExpire(): void
*/
public function testConvertCustomerCartToGuest(): void
{
$quoteId = 1;
$addressArgs = ['customerAddressId' => null];
$customerIdArgs = ['customerId' => null];
$emailArgs = ['email' => null];

$this->checkoutSessionMock->expects($this->once())
->method('getQuoteId')->willReturn($quoteId);
$this->quoteMock->expects($this->once())->method('getId')->willReturn($quoteId);
$this->quoteRepositoryMock->expects($this->once())->method('get')->with($quoteId)->willReturn($this->quoteMock);
$email = 'test@example.com';
$firstname = 'Firstname';
$lastname = 'Lastname';

$billingAddressMock = $this->createMock(Address::class);
$billingAddressMock->method('getEmail')->willReturn($email);
$billingAddressMock->method('getFirstname')->willReturn($firstname);
$billingAddressMock->method('getLastname')->willReturn($lastname);
$this->quoteMock->method('getBillingAddress')->willReturn($billingAddressMock);
$this->quoteMock->expects($this->once())
->method('setIsActive')->with(true)->willReturn($this->quoteMock);
->method('setCustomerId')
->with(null)
->willReturn($this->quoteMock);
$this->quoteMock->expects($this->once())
->method('setCustomerId')->with(null)->willReturn($this->quoteMock);
->method('setCustomerEmail')->with($email)
->willReturn($this->quoteMock);
$this->quoteMock->expects($this->once())
->method('setCustomerEmail')->with(null)->willReturn($this->quoteMock);
->method('setCustomerFirstname')
->with($firstname)
->willReturn($this->quoteMock);
$this->quoteMock->expects($this->once())
->method('setCustomerFirstname')->with(null)->willReturn($this->quoteMock);
->method('setCustomerLastname')->with($lastname)
->willReturn($this->quoteMock);
$this->quoteMock->expects($this->once())
->method('setCustomerLastname')->with(null)->willReturn($this->quoteMock);
$this->quoteMock->expects($this->never())->method('setCustomerGroupId')
->method('setCustomerGroupId')
->with(0)
->willReturn($this->quoteMock);
$this->quoteMock->expects($this->once())
->method('setIsPersistent')->with(false)->willReturn($this->quoteMock);
$this->quoteMock->expects($this->exactly(3))
$this->quoteMock->expects($this->exactly(2))
->method('getAddressesCollection')->willReturn($this->abstractCollectionMock);
$customerMock = $this->createMock(CustomerInterface::class);
$customerMock->expects($this->once())
Expand All @@ -375,49 +383,18 @@ public function testConvertCustomerCartToGuest(): void
->willReturnSelf();
$this->quoteMock->expects($this->once())
->method('getCustomer')->willReturn($customerMock);
$this->abstractCollectionMock->expects($this->exactly(3))->method('walk')->with(
$this->abstractCollectionMock->expects($this->exactly(2))->method('walk')->with(
$this->logicalOr(
$this->equalTo('setCustomerAddressId'),
$this->equalTo($addressArgs),
$this->equalTo('setCustomerId'),
$this->equalTo($customerIdArgs),
$this->equalTo('setEmail'),
$this->equalTo($emailArgs)
$this->equalTo($customerIdArgs)
)
);
$this->quoteMock->expects($this->once())->method('collectTotals')->willReturn($this->quoteMock);
$this->persistentSessionMock->expects($this->once())
->method('getSession')->willReturn($this->sessionMock);
$this->sessionMock->expects($this->once())
->method('removePersistentCookie')->willReturn($this->sessionMock);
$this->quoteRepositoryMock->expects($this->once())->method('save')->with($this->quoteMock);

$this->model->convertCustomerCartToGuest();
}

/**
* @return void
*/
public function testConvertCustomerCartToGuestWithEmptyQuote(): void
{
$this->checkoutSessionMock->expects($this->once())
->method('getQuoteId')->willReturn(null);
$this->quoteRepositoryMock->expects($this->once())->method('get')->with(null)->willReturn(null);
$this->model->convertCustomerCartToGuest();
}

/**
* @return void
*/
public function testConvertCustomerCartToGuestWithEmptyQuoteId(): void
{
$this->checkoutSessionMock->expects($this->once())
->method('getQuoteId')->willReturn(1);
$quoteWithNoId = $this->quoteMock = $this->createMock(Quote::class);
$quoteWithNoId->expects($this->once())->method('getId')->willReturn(null);
$this->quoteRepositoryMock->expects($this->once())->method('get')->with(1)->willReturn($quoteWithNoId);
$this->quoteMock->expects($this->once())->method('getId')->willReturn(1);
$this->model->convertCustomerCartToGuest();
$this->model->convertCustomerCartToGuest($this->quoteMock);
}

/**
Expand All @@ -429,7 +406,7 @@ private function getExtensionAttributesMock(): MockObject
{
$extensionMockBuilder = $this->getMockBuilder(CartExtensionInterface::class);
try {
$extensionMockBuilder->addMethods(['setShippingAssignments']);
$extensionMockBuilder->onlyMethods(['setShippingAssignments']);
} catch (RuntimeException $e) {
// do nothing as CartExtensionInterface already generated and has 'setShippingAssignments' method.
}
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Quote/Model/QuoteAddressValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public function validateForCart(CartInterface $cart, AddressInterface $address):
if ($cart->getCustomerIsGuest()) {
$this->doValidateForGuestQuoteAddress($address, $cart);
}
$this->doValidate($address, $cart->getCustomer()->getId());
$this->doValidate($address, !$cart->getCustomer()->getId() ? null : (int) $cart->getCustomer()->getId());
}

/**
Expand Down

0 comments on commit 8c14c08

Please sign in to comment.