Skip to content

Commit 513f39d

Browse files
committed
MAGETWO-86549: Unusual behavior with the persistent shopping cart after the session is expired
- Added observer to pass customer data from persistent session to checkout session - Add automated test
1 parent ace297a commit 513f39d

File tree

8 files changed

+299
-154
lines changed

8 files changed

+299
-154
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
/**
3+
*
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Magento\Persistent\Observer;
10+
11+
use Magento\Framework\Event\ObserverInterface;
12+
13+
/**
14+
* Observer for a work with persistent data.
15+
*/
16+
class SetCheckoutSessionPersistentDataObserver implements ObserverInterface
17+
{
18+
/**
19+
* Persistent session.
20+
*
21+
* @var \Magento\Persistent\Helper\Session
22+
*/
23+
private $persistentSession = null;
24+
25+
/**
26+
* Customer session.
27+
*
28+
* @var \Magento\Customer\Model\Session
29+
*/
30+
private $customerSession;
31+
32+
/**
33+
* Persistent data.
34+
*
35+
* @var \Magento\Persistent\Helper\Data
36+
*/
37+
private $persistentData = null;
38+
39+
/**
40+
* Customer Repository.
41+
*
42+
* @var \Magento\Customer\Api\CustomerRepositoryInterface
43+
*/
44+
private $customerRepository = null;
45+
46+
/**
47+
* @param \Magento\Persistent\Helper\Session $persistentSession
48+
* @param \Magento\Customer\Model\Session $customerSession
49+
* @param \Magento\Persistent\Helper\Data $persistentData
50+
* @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
51+
*/
52+
public function __construct(
53+
\Magento\Persistent\Helper\Session $persistentSession,
54+
\Magento\Customer\Model\Session $customerSession,
55+
\Magento\Persistent\Helper\Data $persistentData,
56+
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
57+
) {
58+
$this->persistentSession = $persistentSession;
59+
$this->customerSession = $customerSession;
60+
$this->persistentData = $persistentData;
61+
$this->customerRepository = $customerRepository;
62+
}
63+
64+
/**
65+
* Pass customer data from persistent session to checkout session and set quote to be loaded even if not active.
66+
*
67+
* @param \Magento\Framework\Event\Observer $observer
68+
* @return void
69+
* @throws \Magento\Framework\Exception\NoSuchEntityException
70+
* @throws \Magento\Framework\Exception\LocalizedException
71+
*/
72+
public function execute(\Magento\Framework\Event\Observer $observer)
73+
{
74+
/** @var $checkoutSession \Magento\Checkout\Model\Session */
75+
$checkoutSession = $observer->getEvent()->getData('checkout_session');
76+
if ($this->persistentData->isShoppingCartPersist() && $this->persistentSession->isPersistent()) {
77+
$checkoutSession->setCustomerData(
78+
$this->customerRepository->getById($this->persistentSession->getSession()->getCustomerId())
79+
);
80+
}
81+
if (!(($this->persistentSession->isPersistent() && !$this->customerSession->isLoggedIn())
82+
&& !$this->persistentData->isShoppingCartPersist()
83+
)) {
84+
return;
85+
}
86+
if ($checkoutSession) {
87+
$checkoutSession->setLoadInactive();
88+
}
89+
}
90+
}

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

Lines changed: 0 additions & 78 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
9+
<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/testSchema.xsd">
11+
<test name="CheckShoppingCartBehaviorAfterSessionExpiredTest">
12+
<annotations>
13+
<features value="Persistent"/>
14+
<stories value="MAGETWO-86549 - Unusual behavior with the persistent shopping cart after the session is expired"/>
15+
<title value="Checking behavior with the persistent shopping cart after the session is expired"/>
16+
<description value="Checking behavior with the persistent shopping cart after the session is expired"/>
17+
<severity value="MAJOR"/>
18+
<testCaseId value="MAGETWO-96637"/>
19+
<group value="persistent"/>
20+
</annotations>
21+
<before>
22+
<!--Enable Persistence-->
23+
<createData entity="PersistentConfigEnabled" stepKey="enablePersistent"/>
24+
<!--Create product and customer-->
25+
<createData entity="_defaultCategory" stepKey="createCategory"/>
26+
<createData entity="_defaultProduct" stepKey="createProduct">
27+
<requiredEntity createDataKey="createCategory"/>
28+
</createData>
29+
<createData entity="Simple_US_Customer" stepKey="createCustomer"/>
30+
</before>
31+
<after>
32+
<!--Roll back configuration-->
33+
<createData entity="PersistentConfigDefault" stepKey="setDefaultPersistentState"/>
34+
<!--Delete product and customer-->
35+
<deleteData createDataKey="createCategory" stepKey="deleteCategory"/>
36+
<deleteData createDataKey="createProduct" stepKey="deleteProduct"/>
37+
<deleteData createDataKey="createCustomer" stepKey="deleteCustomer"/>
38+
</after>
39+
<!--Login as a Customer-->
40+
<actionGroup ref="CustomerLoginOnStorefront" stepKey="signUpNewUser">
41+
<argument name="customer" value="$$createCustomer$$"/>
42+
</actionGroup>
43+
<!-- Add simple product to cart -->
44+
<actionGroup ref="AddSimpleProductToCart" stepKey="addProductToCart1">
45+
<argument name="product" value="$$createProduct$$"/>
46+
</actionGroup>
47+
<actionGroup ref="StorefrontOpenCartFromMinicartActionGroup" stepKey="goToShoppingCartFromMinicart"/>
48+
<!--Reset cookies and refresh the page-->
49+
<resetCookie userInput="PHPSESSID" stepKey="resetCookieForCart"/>
50+
<reloadPage stepKey="reloadPage"/>
51+
<waitForPageLoad stepKey="waitForPageLoad"/>
52+
<!--Check product exists in cart-->
53+
<see userInput="$$createProduct.name$$" stepKey="ProductExistsInCart"/>
54+
</test>
55+
</tests>
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<?php
2+
/**
3+
*
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Magento\Persistent\Test\Unit\Observer;
10+
11+
/**
12+
* Test for SetCheckoutSessionPersistentDataObserver.
13+
*/
14+
class SetCheckoutSessionPersistentDataObserverTest extends \PHPUnit\Framework\TestCase
15+
{
16+
/**
17+
* @var \Magento\Persistent\Observer\SetCheckoutSessionPersistentDataObserver
18+
*/
19+
private $model;
20+
21+
/**
22+
* @var \Magento\Persistent\Helper\Data| \PHPUnit_Framework_MockObject_MockObject
23+
*/
24+
private $helperMock;
25+
26+
/**
27+
* @var \Magento\Persistent\Helper\Session| \PHPUnit_Framework_MockObject_MockObject
28+
*/
29+
private $sessionHelperMock;
30+
31+
/**
32+
* @var \Magento\Checkout\Model\Session| \PHPUnit_Framework_MockObject_MockObject
33+
*/
34+
private $checkoutSessionMock;
35+
36+
/**
37+
* @var \Magento\Customer\Model\Session| \PHPUnit_Framework_MockObject_MockObject
38+
*/
39+
private $customerSessionMock;
40+
41+
/**
42+
* @var \Magento\Persistent\Model\Session| \PHPUnit_Framework_MockObject_MockObject
43+
*/
44+
private $persistentSessionMock;
45+
46+
/**
47+
* @var \Magento\Customer\Api\CustomerRepositoryInterface| \PHPUnit_Framework_MockObject_MockObject
48+
*/
49+
private $customerRepositoryMock;
50+
51+
/**
52+
* @var \Magento\Framework\Event\Observer|\PHPUnit_Framework_MockObject_MockObject
53+
*/
54+
private $observerMock;
55+
56+
/**
57+
* @var \Magento\Framework\Event|\PHPUnit_Framework_MockObject_MockObject
58+
*/
59+
private $eventMock;
60+
61+
/**
62+
* @inheritdoc
63+
*/
64+
protected function setUp()
65+
{
66+
$this->helperMock = $this->createMock(\Magento\Persistent\Helper\Data::class);
67+
$this->sessionHelperMock = $this->createMock(\Magento\Persistent\Helper\Session::class);
68+
$this->checkoutSessionMock = $this->createMock(\Magento\Checkout\Model\Session::class);
69+
$this->customerSessionMock = $this->createMock(\Magento\Customer\Model\Session::class);
70+
$this->observerMock = $this->createMock(\Magento\Framework\Event\Observer::class);
71+
$this->eventMock = $this->createPartialMock(\Magento\Framework\Event::class, ['getData']);
72+
$this->persistentSessionMock = $this->createPartialMock(
73+
\Magento\Persistent\Model\Session::class,
74+
['getCustomerId']
75+
);
76+
$this->customerRepositoryMock = $this->createMock(
77+
\Magento\Customer\Api\CustomerRepositoryInterface::class
78+
);
79+
$this->model = new \Magento\Persistent\Observer\SetCheckoutSessionPersistentDataObserver(
80+
$this->sessionHelperMock,
81+
$this->customerSessionMock,
82+
$this->helperMock,
83+
$this->customerRepositoryMock
84+
);
85+
}
86+
87+
/**
88+
* Test execute method when session is not persistent.
89+
*
90+
* @return void
91+
* @throws \Magento\Framework\Exception\LocalizedException
92+
* @throws \Magento\Framework\Exception\NoSuchEntityException
93+
*/
94+
public function testExecuteWhenSessionIsNotPersistent()
95+
{
96+
$this->observerMock->expects($this->once())
97+
->method('getEvent')
98+
->willReturn($this->eventMock);
99+
$this->eventMock->expects($this->once())
100+
->method('getData')
101+
->willReturn($this->checkoutSessionMock);
102+
$this->sessionHelperMock->expects($this->once())
103+
->method('isPersistent')
104+
->willReturn(false);
105+
$this->checkoutSessionMock->expects($this->never())
106+
->method('setLoadInactive');
107+
$this->checkoutSessionMock->expects($this->never())
108+
->method('setCustomerData');
109+
$this->model->execute($this->observerMock);
110+
}
111+
112+
/**
113+
* Test execute method when session is persistent.
114+
*
115+
* @return void
116+
* @throws \Magento\Framework\Exception\LocalizedException
117+
* @throws \Magento\Framework\Exception\NoSuchEntityException
118+
*/
119+
public function testExecute()
120+
{
121+
$this->observerMock->expects($this->once())
122+
->method('getEvent')
123+
->willReturn($this->eventMock);
124+
$this->eventMock->expects($this->once())
125+
->method('getData')
126+
->willReturn($this->checkoutSessionMock);
127+
$this->sessionHelperMock->expects($this->exactly(2))
128+
->method('isPersistent')
129+
->willReturn(true);
130+
$this->customerSessionMock->expects($this->once())
131+
->method('isLoggedIn')
132+
->willReturn(false);
133+
$this->helperMock->expects($this->exactly(2))
134+
->method('isShoppingCartPersist')
135+
->willReturn(true);
136+
$this->persistentSessionMock->expects($this->once())
137+
->method('getCustomerId')
138+
->willReturn(123);
139+
$this->sessionHelperMock->expects($this->once())
140+
->method('getSession')
141+
->willReturn($this->persistentSessionMock);
142+
$this->customerRepositoryMock->expects($this->once())
143+
->method('getById')
144+
->willReturn(1);
145+
$this->checkoutSessionMock->expects($this->never())
146+
->method('setLoadInactive');
147+
$this->checkoutSessionMock->expects($this->once())
148+
->method('setCustomerData');
149+
$this->model->execute($this->observerMock);
150+
}
151+
}

0 commit comments

Comments
 (0)