Skip to content

Commit 8c14c08

Browse files
committed
ACP2E-2470: Persistent shopping cart cleared during checkout step
1 parent e4d5225 commit 8c14c08

File tree

5 files changed

+31
-57
lines changed

5 files changed

+31
-57
lines changed

app/code/Magento/Customer/Model/Customer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ public function getDataModel()
357357
\Magento\Customer\Api\Data\CustomerInterface::class
358358
);
359359
$customerDataObject->setAddresses($addressesData)
360-
->setId($this->getId() ? (int) $this->getId() : null);
360+
->setId($this->getId());
361361
return $customerDataObject;
362362
}
363363

app/code/Magento/Persistent/Model/Plugin/ConvertCustomerCartToGuest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
use Magento\Quote\Model\Quote;
2525
use Magento\Quote\Model\QuoteManagement;
2626

27+
/**
28+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
29+
*/
2730
class ConvertCustomerCartToGuest
2831
{
2932
/**

app/code/Magento/Persistent/Model/QuoteManager.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,16 @@
2323
class QuoteManager
2424
{
2525
/**
26-
* Persistent session
27-
*
2826
* @var \Magento\Persistent\Helper\Session
2927
*/
3028
protected $persistentSession;
3129

3230
/**
33-
* Checkout session
34-
*
3531
* @var \Magento\Checkout\Model\Session
3632
*/
3733
protected $checkoutSession;
3834

3935
/**
40-
* Persistent data
41-
*
4236
* @var Data
4337
*/
4438
protected $persistentData;

app/code/Magento/Persistent/Test/Unit/Model/QuoteManagerTest.php

Lines changed: 26 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -343,30 +343,38 @@ public function testExpire(): void
343343
*/
344344
public function testConvertCustomerCartToGuest(): void
345345
{
346-
$quoteId = 1;
347346
$addressArgs = ['customerAddressId' => null];
348347
$customerIdArgs = ['customerId' => null];
349-
$emailArgs = ['email' => null];
350-
351-
$this->checkoutSessionMock->expects($this->once())
352-
->method('getQuoteId')->willReturn($quoteId);
353-
$this->quoteMock->expects($this->once())->method('getId')->willReturn($quoteId);
354-
$this->quoteRepositoryMock->expects($this->once())->method('get')->with($quoteId)->willReturn($this->quoteMock);
348+
$email = '[email protected]';
349+
$firstname = 'Firstname';
350+
$lastname = 'Lastname';
351+
352+
$billingAddressMock = $this->createMock(Address::class);
353+
$billingAddressMock->method('getEmail')->willReturn($email);
354+
$billingAddressMock->method('getFirstname')->willReturn($firstname);
355+
$billingAddressMock->method('getLastname')->willReturn($lastname);
356+
$this->quoteMock->method('getBillingAddress')->willReturn($billingAddressMock);
355357
$this->quoteMock->expects($this->once())
356-
->method('setIsActive')->with(true)->willReturn($this->quoteMock);
358+
->method('setCustomerId')
359+
->with(null)
360+
->willReturn($this->quoteMock);
357361
$this->quoteMock->expects($this->once())
358-
->method('setCustomerId')->with(null)->willReturn($this->quoteMock);
362+
->method('setCustomerEmail')->with($email)
363+
->willReturn($this->quoteMock);
359364
$this->quoteMock->expects($this->once())
360-
->method('setCustomerEmail')->with(null)->willReturn($this->quoteMock);
365+
->method('setCustomerFirstname')
366+
->with($firstname)
367+
->willReturn($this->quoteMock);
361368
$this->quoteMock->expects($this->once())
362-
->method('setCustomerFirstname')->with(null)->willReturn($this->quoteMock);
369+
->method('setCustomerLastname')->with($lastname)
370+
->willReturn($this->quoteMock);
363371
$this->quoteMock->expects($this->once())
364-
->method('setCustomerLastname')->with(null)->willReturn($this->quoteMock);
365-
$this->quoteMock->expects($this->never())->method('setCustomerGroupId')
372+
->method('setCustomerGroupId')
373+
->with(0)
366374
->willReturn($this->quoteMock);
367375
$this->quoteMock->expects($this->once())
368376
->method('setIsPersistent')->with(false)->willReturn($this->quoteMock);
369-
$this->quoteMock->expects($this->exactly(3))
377+
$this->quoteMock->expects($this->exactly(2))
370378
->method('getAddressesCollection')->willReturn($this->abstractCollectionMock);
371379
$customerMock = $this->createMock(CustomerInterface::class);
372380
$customerMock->expects($this->once())
@@ -375,49 +383,18 @@ public function testConvertCustomerCartToGuest(): void
375383
->willReturnSelf();
376384
$this->quoteMock->expects($this->once())
377385
->method('getCustomer')->willReturn($customerMock);
378-
$this->abstractCollectionMock->expects($this->exactly(3))->method('walk')->with(
386+
$this->abstractCollectionMock->expects($this->exactly(2))->method('walk')->with(
379387
$this->logicalOr(
380388
$this->equalTo('setCustomerAddressId'),
381389
$this->equalTo($addressArgs),
382390
$this->equalTo('setCustomerId'),
383-
$this->equalTo($customerIdArgs),
384-
$this->equalTo('setEmail'),
385-
$this->equalTo($emailArgs)
391+
$this->equalTo($customerIdArgs)
386392
)
387393
);
388394
$this->quoteMock->expects($this->once())->method('collectTotals')->willReturn($this->quoteMock);
389-
$this->persistentSessionMock->expects($this->once())
390-
->method('getSession')->willReturn($this->sessionMock);
391-
$this->sessionMock->expects($this->once())
392-
->method('removePersistentCookie')->willReturn($this->sessionMock);
393395
$this->quoteRepositoryMock->expects($this->once())->method('save')->with($this->quoteMock);
394396

395-
$this->model->convertCustomerCartToGuest();
396-
}
397-
398-
/**
399-
* @return void
400-
*/
401-
public function testConvertCustomerCartToGuestWithEmptyQuote(): void
402-
{
403-
$this->checkoutSessionMock->expects($this->once())
404-
->method('getQuoteId')->willReturn(null);
405-
$this->quoteRepositoryMock->expects($this->once())->method('get')->with(null)->willReturn(null);
406-
$this->model->convertCustomerCartToGuest();
407-
}
408-
409-
/**
410-
* @return void
411-
*/
412-
public function testConvertCustomerCartToGuestWithEmptyQuoteId(): void
413-
{
414-
$this->checkoutSessionMock->expects($this->once())
415-
->method('getQuoteId')->willReturn(1);
416-
$quoteWithNoId = $this->quoteMock = $this->createMock(Quote::class);
417-
$quoteWithNoId->expects($this->once())->method('getId')->willReturn(null);
418-
$this->quoteRepositoryMock->expects($this->once())->method('get')->with(1)->willReturn($quoteWithNoId);
419-
$this->quoteMock->expects($this->once())->method('getId')->willReturn(1);
420-
$this->model->convertCustomerCartToGuest();
397+
$this->model->convertCustomerCartToGuest($this->quoteMock);
421398
}
422399

423400
/**
@@ -429,7 +406,7 @@ private function getExtensionAttributesMock(): MockObject
429406
{
430407
$extensionMockBuilder = $this->getMockBuilder(CartExtensionInterface::class);
431408
try {
432-
$extensionMockBuilder->addMethods(['setShippingAssignments']);
409+
$extensionMockBuilder->onlyMethods(['setShippingAssignments']);
433410
} catch (RuntimeException $e) {
434411
// do nothing as CartExtensionInterface already generated and has 'setShippingAssignments' method.
435412
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public function validateForCart(CartInterface $cart, AddressInterface $address):
156156
if ($cart->getCustomerIsGuest()) {
157157
$this->doValidateForGuestQuoteAddress($address, $cart);
158158
}
159-
$this->doValidate($address, $cart->getCustomer()->getId());
159+
$this->doValidate($address, !$cart->getCustomer()->getId() ? null : (int) $cart->getCustomer()->getId());
160160
}
161161

162162
/**

0 commit comments

Comments
 (0)