Skip to content

Commit 21a6730

Browse files
committed
Merge remote-tracking branch 'origin/MC-37109' into 2.4-develop-pr41
2 parents 0cb2ac8 + 9c764e2 commit 21a6730

File tree

6 files changed

+92
-27
lines changed

6 files changed

+92
-27
lines changed

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

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Persistent\Model;
77

8+
use Magento\Customer\Api\Data\CustomerInterfaceFactory;
89
use Magento\Customer\Api\Data\GroupInterface;
910
use Magento\Framework\App\ObjectManager;
1011
use Magento\Persistent\Helper\Data;
@@ -64,21 +65,28 @@ class QuoteManager
6465
*/
6566
private $cartExtensionFactory;
6667

68+
/**
69+
* @var CustomerInterfaceFactory
70+
*/
71+
private $customerDataFactory;
72+
6773
/**
6874
* @param \Magento\Persistent\Helper\Session $persistentSession
6975
* @param Data $persistentData
7076
* @param \Magento\Checkout\Model\Session $checkoutSession
7177
* @param CartRepositoryInterface $quoteRepository
7278
* @param CartExtensionFactory|null $cartExtensionFactory
7379
* @param ShippingAssignmentProcessor|null $shippingAssignmentProcessor
80+
* @param CustomerInterfaceFactory|null $customerDataFactory
7481
*/
7582
public function __construct(
7683
\Magento\Persistent\Helper\Session $persistentSession,
7784
Data $persistentData,
7885
\Magento\Checkout\Model\Session $checkoutSession,
7986
CartRepositoryInterface $quoteRepository,
8087
?CartExtensionFactory $cartExtensionFactory = null,
81-
?ShippingAssignmentProcessor $shippingAssignmentProcessor = null
88+
?ShippingAssignmentProcessor $shippingAssignmentProcessor = null,
89+
?CustomerInterfaceFactory $customerDataFactory = null
8290
) {
8391
$this->persistentSession = $persistentSession;
8492
$this->persistentData = $persistentData;
@@ -88,6 +96,8 @@ public function __construct(
8896
?? ObjectManager::getInstance()->get(CartExtensionFactory::class);
8997
$this->shippingAssignmentProcessor = $shippingAssignmentProcessor
9098
?? ObjectManager::getInstance()->get(ShippingAssignmentProcessor::class);
99+
$this->customerDataFactory = $customerDataFactory
100+
?? ObjectManager::getInstance()->get(CustomerInterfaceFactory::class);
91101
}
92102

93103
/**
@@ -109,14 +119,11 @@ public function setGuest($checkQuote = false)
109119
$quote->getPaymentsCollection()->walk('delete');
110120
$quote->getAddressesCollection()->walk('delete');
111121
$this->_setQuotePersistent = false;
122+
$this->cleanCustomerData($quote);
112123
$quote->setIsActive(true)
113-
->setCustomerId(null)
114-
->setCustomerEmail(null)
115-
->setCustomerFirstname(null)
116-
->setCustomerLastname(null)
117-
->setCustomerGroupId(GroupInterface::NOT_LOGGED_IN_ID)
118124
->setIsPersistent(false)
119125
->removeAllAddresses();
126+
120127
//Create guest addresses
121128
$quote->getShippingAddress();
122129
$quote->getBillingAddress();
@@ -129,6 +136,27 @@ public function setGuest($checkQuote = false)
129136
$this->persistentSession->setSession(null);
130137
}
131138

139+
/**
140+
* Clear customer data in quote
141+
*
142+
* @param Quote $quote
143+
*/
144+
private function cleanCustomerData($quote)
145+
{
146+
/**
147+
* Set empty customer object in quote to avoid restore customer id
148+
* @see Quote::beforeSave()
149+
*/
150+
if ($quote->getCustomerId()) {
151+
$quote->setCustomer($this->customerDataFactory->create());
152+
}
153+
$quote->setCustomerId(null)
154+
->setCustomerEmail(null)
155+
->setCustomerFirstname(null)
156+
->setCustomerLastname(null)
157+
->setCustomerGroupId(GroupInterface::NOT_LOGGED_IN_ID);
158+
}
159+
132160
/**
133161
* Emulate guest cart with persistent cart
134162
*

app/code/Magento/Persistent/Observer/MakePersistentQuoteGuestObserver.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
<?php
22
/**
3-
*
43
* Copyright © Magento, Inc. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
7-
86
namespace Magento\Persistent\Observer;
97

108
use Magento\Framework\Event\ObserverInterface;
119

1210
/**
13-
* Make persistent quote to be guest
11+
* Make persistent quote to be guest
1412
*
1513
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
1614
*/
@@ -38,26 +36,26 @@ class MakePersistentQuoteGuestObserver implements ObserverInterface
3836
protected $_persistentData = null;
3937

4038
/**
41-
* @var \Magento\Persistent\Model\QuoteManager
39+
* @var \Magento\Checkout\Model\Session
4240
*/
43-
protected $quoteManager;
41+
private $checkoutSession;
4442

4543
/**
4644
* @param \Magento\Persistent\Helper\Session $persistentSession
4745
* @param \Magento\Persistent\Helper\Data $persistentData
4846
* @param \Magento\Customer\Model\Session $customerSession
49-
* @param \Magento\Persistent\Model\QuoteManager $quoteManager
47+
* @param \Magento\Checkout\Model\Session $checkoutSession
5048
*/
5149
public function __construct(
5250
\Magento\Persistent\Helper\Session $persistentSession,
5351
\Magento\Persistent\Helper\Data $persistentData,
5452
\Magento\Customer\Model\Session $customerSession,
55-
\Magento\Persistent\Model\QuoteManager $quoteManager
53+
\Magento\Checkout\Model\Session $checkoutSession
5654
) {
5755
$this->_persistentSession = $persistentSession;
5856
$this->_persistentData = $persistentData;
5957
$this->_customerSession = $customerSession;
60-
$this->quoteManager = $quoteManager;
58+
$this->checkoutSession = $checkoutSession;
6159
}
6260

6361
/**
@@ -74,7 +72,7 @@ public function execute(\Magento\Framework\Event\Observer $observer)
7472
if (($this->_persistentSession->isPersistent() && !$this->_customerSession->isLoggedIn())
7573
|| $this->_persistentData->isShoppingCartPersist()
7674
) {
77-
$this->quoteManager->setGuest(true);
75+
$this->checkoutSession->clearQuote()->clearStorage();
7876
}
7977
}
8078
}

app/code/Magento/Persistent/Observer/RemoveGuestPersistenceOnEmptyCartObserver.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
/**
1212
* Observer to remove persistent session if guest empties persistent cart previously created and added to by customer.
13+
*
14+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
1315
*/
1416
class RemoveGuestPersistenceOnEmptyCartObserver implements ObserverInterface
1517
{
@@ -96,6 +98,8 @@ public function execute(\Magento\Framework\Event\Observer $observer)
9698
}
9799

98100
if (!$cart || $cart->getItemsCount() == 0) {
101+
$this->customerSession->setCustomerId(null)
102+
->setCustomerGroupId(null);
99103
$this->quoteManager->setGuest();
100104
}
101105
}

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
namespace Magento\Persistent\Test\Unit\Model;
1010

1111
use Magento\Checkout\Model\Session;
12+
use Magento\Customer\Api\Data\CustomerInterface;
13+
use Magento\Customer\Api\Data\CustomerInterfaceFactory;
1214
use Magento\Customer\Model\GroupManagement;
1315
use Magento\Eav\Model\Entity\Collection\AbstractCollection;
1416
use Magento\Persistent\Helper\Data;
@@ -78,6 +80,11 @@ class QuoteManagerTest extends TestCase
7880
*/
7981
private $shippingAssignmentProcessor;
8082

83+
/**
84+
* @var CustomerInterfaceFactory|MockObject
85+
*/
86+
private $customerDataFactory;
87+
8188
protected function setUp(): void
8289
{
8390
$this->persistentSessionMock = $this->createMock(\Magento\Persistent\Helper\Session::class);
@@ -124,21 +131,24 @@ protected function setUp(): void
124131
'getItemsQty',
125132
'getExtensionAttributes',
126133
'setExtensionAttributes',
127-
'__wakeup'
134+
'__wakeup',
135+
'setCustomer'
128136
])
129137
->disableOriginalConstructor()
130138
->getMock();
131139

132140
$this->cartExtensionFactory = $this->createPartialMock(CartExtensionFactory::class, ['create']);
133141
$this->shippingAssignmentProcessor = $this->createPartialMock(ShippingAssignmentProcessor::class, ['create']);
142+
$this->customerDataFactory = $this->createMock(CustomerInterfaceFactory::class);
134143

135144
$this->model = new QuoteManager(
136145
$this->persistentSessionMock,
137146
$this->persistentDataMock,
138147
$this->checkoutSessionMock,
139148
$this->quoteRepositoryMock,
140149
$this->cartExtensionFactory,
141-
$this->shippingAssignmentProcessor
150+
$this->shippingAssignmentProcessor,
151+
$this->customerDataFactory
142152
);
143153
}
144154

@@ -189,6 +199,7 @@ public function testSetGuestWhenShoppingCartAndQuoteAreNotPersistent()
189199

190200
public function testSetGuest()
191201
{
202+
$customerId = 22;
192203
$this->checkoutSessionMock->expects($this->once())
193204
->method('getQuote')->willReturn($this->quoteMock);
194205
$this->quoteMock->expects($this->once())->method('getId')->willReturn(11);
@@ -220,6 +231,7 @@ public function testSetGuest()
220231
->method('getShippingAddress')->willReturn($quoteAddressMock);
221232
$this->quoteMock->expects($this->once())
222233
->method('getBillingAddress')->willReturn($quoteAddressMock);
234+
$this->quoteMock->method('getCustomerId')->willReturn($customerId);
223235
$this->quoteMock->expects($this->once())->method('collectTotals')->willReturn($this->quoteMock);
224236
$this->quoteRepositoryMock->expects($this->once())->method('save')->with($this->quoteMock);
225237
$this->persistentSessionMock->expects($this->once())
@@ -229,7 +241,6 @@ public function testSetGuest()
229241
$this->quoteMock->expects($this->once())->method('isVirtual')->willReturn(false);
230242
$this->quoteMock->expects($this->once())->method('getItemsQty')->willReturn(1);
231243
$extensionAttributes = $this->getMockBuilder(CartExtensionInterface::class)
232-
->addMethods(['getShippingAssignments', 'setShippingAssignments'])
233244
->getMockForAbstractClass();
234245
$shippingAssignment = $this->createMock(ShippingAssignmentInterface::class);
235246
$extensionAttributes->expects($this->once())
@@ -248,6 +259,11 @@ public function testSetGuest()
248259
$this->quoteMock->expects($this->once())
249260
->method('setExtensionAttributes')
250261
->with($extensionAttributes);
262+
$customerMock = $this->createMock(CustomerInterface::class);
263+
$this->customerDataFactory->method('create')->willReturn($customerMock);
264+
$this->quoteMock->expects($this->once())
265+
->method('setCustomer')
266+
->with($customerMock);
251267
$this->model->setGuest(false);
252268
}
253269

app/code/Magento/Persistent/Test/Unit/Observer/MakePersistentQuoteGuestObserverTest.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
<?php
22
/**
3-
*
43
* Copyright © Magento, Inc. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
76
declare(strict_types=1);
87

98
namespace Magento\Persistent\Test\Unit\Observer;
109

10+
use Magento\Checkout\Model\Session as CheckoutSession;
1111
use Magento\Framework\Event;
1212
use Magento\Framework\Event\Observer;
1313
use Magento\Persistent\Controller\Index;
1414
use Magento\Persistent\Helper\Data;
1515
use Magento\Persistent\Helper\Session;
16-
use Magento\Persistent\Model\QuoteManager;
1716
use Magento\Persistent\Observer\MakePersistentQuoteGuestObserver;
1817
use PHPUnit\Framework\MockObject\MockObject;
1918
use PHPUnit\Framework\TestCase;
@@ -48,10 +47,10 @@ class MakePersistentQuoteGuestObserverTest extends TestCase
4847
/**
4948
* @var MockObject
5049
*/
51-
protected $quoteManagerMock;
50+
protected $checkoutSession;
5251

5352
/**
54-
* @var MockObject
53+
* @var CheckoutSession|MockObject
5554
*/
5655
protected $eventManagerMock;
5756

@@ -60,14 +59,17 @@ class MakePersistentQuoteGuestObserverTest extends TestCase
6059
*/
6160
protected $actionMock;
6261

62+
/**
63+
* @inheritdoc
64+
*/
6365
protected function setUp(): void
6466
{
6567
$this->actionMock = $this->createMock(Index::class);
6668
$this->observerMock = $this->createMock(Observer::class);
6769
$this->sessionHelperMock = $this->createMock(Session::class);
6870
$this->helperMock = $this->createMock(Data::class);
6971
$this->customerSessionMock = $this->createMock(\Magento\Customer\Model\Session::class);
70-
$this->quoteManagerMock = $this->createMock(QuoteManager::class);
72+
$this->checkoutSession = $this->createMock(CheckoutSession::class);
7173
$this->eventManagerMock =
7274
$this->getMockBuilder(Event::class)
7375
->addMethods(['getControllerAction'])
@@ -81,7 +83,7 @@ protected function setUp(): void
8183
$this->sessionHelperMock,
8284
$this->helperMock,
8385
$this->customerSessionMock,
84-
$this->quoteManagerMock
86+
$this->checkoutSession
8587
);
8688
}
8789

@@ -94,7 +96,8 @@ public function testExecute()
9496
$this->sessionHelperMock->expects($this->once())->method('isPersistent')->willReturn(true);
9597
$this->customerSessionMock->expects($this->once())->method('isLoggedIn')->willReturn(false);
9698
$this->helperMock->expects($this->never())->method('isShoppingCartPersist');
97-
$this->quoteManagerMock->expects($this->once())->method('setGuest')->with(true);
99+
$this->checkoutSession->expects($this->once())->method('clearQuote')->willReturnSelf();
100+
$this->checkoutSession->expects($this->once())->method('clearStorage')->willReturnSelf();
98101
$this->model->execute($this->observerMock);
99102
}
100103

@@ -107,7 +110,8 @@ public function testExecuteWhenShoppingCartIsPersist()
107110
$this->sessionHelperMock->expects($this->once())->method('isPersistent')->willReturn(true);
108111
$this->customerSessionMock->expects($this->once())->method('isLoggedIn')->willReturn(true);
109112
$this->helperMock->expects($this->once())->method('isShoppingCartPersist')->willReturn(true);
110-
$this->quoteManagerMock->expects($this->once())->method('setGuest')->with(true);
113+
$this->checkoutSession->expects($this->once())->method('clearQuote')->willReturnSelf();
114+
$this->checkoutSession->expects($this->once())->method('clearStorage')->willReturnSelf();
111115
$this->model->execute($this->observerMock);
112116
}
113117

@@ -120,7 +124,8 @@ public function testExecuteWhenShoppingCartIsNotPersist()
120124
$this->sessionHelperMock->expects($this->once())->method('isPersistent')->willReturn(true);
121125
$this->customerSessionMock->expects($this->once())->method('isLoggedIn')->willReturn(true);
122126
$this->helperMock->expects($this->once())->method('isShoppingCartPersist')->willReturn(false);
123-
$this->quoteManagerMock->expects($this->never())->method('setGuest');
127+
$this->checkoutSession->expects($this->never())->method('clearQuote')->willReturnSelf();
128+
$this->checkoutSession->expects($this->never())->method('clearStorage')->willReturnSelf();
124129
$this->model->execute($this->observerMock);
125130
}
126131
}

app/code/Magento/Persistent/Test/Unit/Observer/RemoveGuestPersistenceOnEmptyCartObserverTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@ public function testExecuteWithEmptyCart()
137137
->with($customerId)
138138
->willReturn($quoteMock);
139139
$quoteMock->expects($this->once())->method('getItemsCount')->willReturn($emptyCount);
140+
$this->customerSessionMock->expects($this->once())
141+
->method('setCustomerId')
142+
->with(null)
143+
->willReturnSelf();
144+
$this->customerSessionMock->expects($this->once())
145+
->method('setCustomerGroupId')
146+
->with(null);
140147
$this->quoteManagerMock->expects($this->once())->method('setGuest');
141148

142149
$this->model->execute($this->observerMock);
@@ -160,6 +167,13 @@ public function testExecuteWithNonexistentCart()
160167
->method('getActiveForCustomer')
161168
->with($customerId)
162169
->willThrowException($exception);
170+
$this->customerSessionMock->expects($this->once())
171+
->method('setCustomerId')
172+
->with(null)
173+
->willReturnSelf();
174+
$this->customerSessionMock->expects($this->once())
175+
->method('setCustomerGroupId')
176+
->with(null);
163177
$this->quoteManagerMock->expects($this->once())->method('setGuest');
164178

165179
$this->model->execute($this->observerMock);

0 commit comments

Comments
 (0)