Skip to content

Commit 1d0ef5b

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-92137' into 2.3-develop-pr23
2 parents c20d777 + f93c916 commit 1d0ef5b

File tree

8 files changed

+283
-22
lines changed

8 files changed

+283
-22
lines changed

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

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,28 +50,45 @@ class CheckExpirePersistentQuoteObserver implements ObserverInterface
5050
*/
5151
protected $_persistentData = null;
5252

53+
/**
54+
* Request
55+
*
56+
* @var \Magento\Framework\App\RequestInterface
57+
*/
58+
private $request;
59+
60+
/**
61+
* Checkout Page path
62+
*
63+
* @var string
64+
*/
65+
private $checkoutPagePath = 'checkout';
66+
5367
/**
5468
* @param \Magento\Persistent\Helper\Session $persistentSession
5569
* @param \Magento\Persistent\Helper\Data $persistentData
5670
* @param \Magento\Persistent\Model\QuoteManager $quoteManager
5771
* @param \Magento\Framework\Event\ManagerInterface $eventManager
5872
* @param \Magento\Customer\Model\Session $customerSession
5973
* @param \Magento\Checkout\Model\Session $checkoutSession
74+
* @param \Magento\Framework\App\RequestInterface $request
6075
*/
6176
public function __construct(
6277
\Magento\Persistent\Helper\Session $persistentSession,
6378
\Magento\Persistent\Helper\Data $persistentData,
6479
\Magento\Persistent\Model\QuoteManager $quoteManager,
6580
\Magento\Framework\Event\ManagerInterface $eventManager,
6681
\Magento\Customer\Model\Session $customerSession,
67-
\Magento\Checkout\Model\Session $checkoutSession
82+
\Magento\Checkout\Model\Session $checkoutSession,
83+
\Magento\Framework\App\RequestInterface $request
6884
) {
6985
$this->_persistentSession = $persistentSession;
7086
$this->quoteManager = $quoteManager;
7187
$this->_customerSession = $customerSession;
7288
$this->_checkoutSession = $checkoutSession;
7389
$this->_eventManager = $eventManager;
7490
$this->_persistentData = $persistentData;
91+
$this->request = $request;
7592
}
7693

7794
/**
@@ -90,12 +107,32 @@ public function execute(\Magento\Framework\Event\Observer $observer)
90107
!$this->_persistentSession->isPersistent() &&
91108
!$this->_customerSession->isLoggedIn() &&
92109
$this->_checkoutSession->getQuoteId() &&
93-
!$observer->getControllerAction() instanceof \Magento\Checkout\Controller\Onepage
94-
// persistent session does not expire on onepage checkout page to not spoil customer group id
110+
!$this->isRequestFromCheckoutPage($this->request)
111+
// persistent session does not expire on onepage checkout page
95112
) {
96113
$this->_eventManager->dispatch('persistent_session_expired');
97114
$this->quoteManager->expire();
98115
$this->_customerSession->setCustomerId(null)->setCustomerGroupId(null);
99116
}
100117
}
118+
119+
/**
120+
* Check current request is coming from onepage checkout page.
121+
*
122+
* @param \Magento\Framework\App\RequestInterface $request
123+
* @return bool
124+
*/
125+
private function isRequestFromCheckoutPage(\Magento\Framework\App\RequestInterface $request): bool
126+
{
127+
$requestUri = (string)$request->getRequestUri();
128+
$refererUri = (string)$request->getServer('HTTP_REFERER');
129+
130+
/** @var bool $isCheckoutPage */
131+
$isCheckoutPage = (
132+
false !== strpos($requestUri, $this->checkoutPagePath) ||
133+
false !== strpos($refererUri, $this->checkoutPagePath)
134+
);
135+
136+
return $isCheckoutPage;
137+
}
101138
}

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

Lines changed: 100 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,39 @@ class CheckExpirePersistentQuoteObserverTest extends \PHPUnit\Framework\TestCase
4949
*/
5050
protected $eventManagerMock;
5151

52+
/**
53+
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\RequestInterface
54+
*/
55+
private $requestMock;
56+
57+
/**
58+
* @inheritdoc
59+
*/
5260
protected function setUp()
5361
{
5462
$this->sessionMock = $this->createMock(\Magento\Persistent\Helper\Session::class);
5563
$this->customerSessionMock = $this->createMock(\Magento\Customer\Model\Session::class);
5664
$this->persistentHelperMock = $this->createMock(\Magento\Persistent\Helper\Data::class);
57-
$this->observerMock
58-
= $this->createPartialMock(\Magento\Framework\Event\Observer::class, ['getControllerAction',
59-
'__wakeUp']);
65+
$this->observerMock = $this->createPartialMock(
66+
\Magento\Framework\Event\Observer::class,
67+
['getControllerAction','__wakeUp']
68+
);
6069
$this->quoteManagerMock = $this->createMock(\Magento\Persistent\Model\QuoteManager::class);
6170
$this->eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
6271
$this->checkoutSessionMock = $this->createMock(\Magento\Checkout\Model\Session::class);
72+
$this->requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
73+
->disableOriginalConstructor()
74+
->setMethods(['getRequestUri', 'getServer'])
75+
->getMockForAbstractClass();
76+
6377
$this->model = new \Magento\Persistent\Observer\CheckExpirePersistentQuoteObserver(
6478
$this->sessionMock,
6579
$this->persistentHelperMock,
6680
$this->quoteManagerMock,
6781
$this->eventManagerMock,
6882
$this->customerSessionMock,
69-
$this->checkoutSessionMock
83+
$this->checkoutSessionMock,
84+
$this->requestMock
7085
);
7186
}
7287

@@ -76,7 +91,7 @@ public function testExecuteWhenCanNotApplyPersistentData()
7691
->expects($this->once())
7792
->method('canProcess')
7893
->with($this->observerMock)
79-
->will($this->returnValue(false));
94+
->willReturn(false);
8095
$this->persistentHelperMock->expects($this->never())->method('isEnabled');
8196
$this->model->execute($this->observerMock);
8297
}
@@ -87,31 +102,97 @@ public function testExecuteWhenPersistentIsNotEnabled()
87102
->expects($this->once())
88103
->method('canProcess')
89104
->with($this->observerMock)
90-
->will($this->returnValue(true));
91-
$this->persistentHelperMock->expects($this->once())->method('isEnabled')->will($this->returnValue(false));
105+
->willReturn(true);
106+
$this->persistentHelperMock->expects($this->once())->method('isEnabled')->willReturn(false);
92107
$this->eventManagerMock->expects($this->never())->method('dispatch');
93108
$this->model->execute($this->observerMock);
94109
}
95110

96-
public function testExecuteWhenPersistentIsEnabled()
97-
{
111+
/**
112+
* Test method \Magento\Persistent\Observer\CheckExpirePersistentQuoteObserver::execute when persistent is enabled.
113+
*
114+
* @param string $refererUri
115+
* @param string $requestUri
116+
* @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $expireCounter
117+
* @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $dispatchCounter
118+
* @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $setCustomerIdCounter
119+
* @return void
120+
* @dataProvider requestDataProvider
121+
*/
122+
public function testExecuteWhenPersistentIsEnabled(
123+
string $refererUri,
124+
string $requestUri,
125+
\PHPUnit_Framework_MockObject_Matcher_InvokedCount $expireCounter,
126+
\PHPUnit_Framework_MockObject_Matcher_InvokedCount $dispatchCounter,
127+
\PHPUnit_Framework_MockObject_Matcher_InvokedCount $setCustomerIdCounter
128+
): void {
98129
$this->persistentHelperMock
99130
->expects($this->once())
100131
->method('canProcess')
101132
->with($this->observerMock)
102-
->will($this->returnValue(true));
103-
$this->persistentHelperMock->expects($this->once())->method('isEnabled')->will($this->returnValue(true));
104-
$this->sessionMock->expects($this->once())->method('isPersistent')->will($this->returnValue(false));
105-
$this->customerSessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(false));
106-
$this->checkoutSessionMock->expects($this->once())->method('getQuoteId')->will($this->returnValue(10));
107-
$this->observerMock->expects($this->once())->method('getControllerAction');
108-
$this->eventManagerMock->expects($this->once())->method('dispatch');
109-
$this->quoteManagerMock->expects($this->once())->method('expire');
133+
->willReturn(true);
134+
$this->persistentHelperMock->expects($this->once())->method('isEnabled')->willReturn(true);
135+
$this->sessionMock->expects($this->once())->method('isPersistent')->willReturn(false);
110136
$this->customerSessionMock
111-
->expects($this->once())
137+
->expects($this->atLeastOnce())
138+
->method('isLoggedIn')
139+
->willReturn(false);
140+
$this->checkoutSessionMock
141+
->expects($this->atLeastOnce())
142+
->method('getQuoteId')
143+
->willReturn(10);
144+
$this->eventManagerMock->expects($dispatchCounter)->method('dispatch');
145+
$this->quoteManagerMock->expects($expireCounter)->method('expire');
146+
$this->customerSessionMock
147+
->expects($setCustomerIdCounter)
112148
->method('setCustomerId')
113149
->with(null)
114-
->will($this->returnSelf());
150+
->willReturnSelf();
151+
$this->requestMock->expects($this->atLeastOnce())->method('getRequestUri')->willReturn($refererUri);
152+
$this->requestMock
153+
->expects($this->atLeastOnce())
154+
->method('getServer')
155+
->with('HTTP_REFERER')
156+
->willReturn($requestUri);
115157
$this->model->execute($this->observerMock);
116158
}
159+
160+
/**
161+
* Request Data Provider
162+
*
163+
* @return array
164+
*/
165+
public function requestDataProvider()
166+
{
167+
return [
168+
[
169+
'refererUri' => 'checkout',
170+
'requestUri' => 'index',
171+
'expireCounter' => $this->never(),
172+
'dispatchCounter' => $this->never(),
173+
'setCustomerIdCounter' => $this->never(),
174+
],
175+
[
176+
'refererUri' => 'checkout',
177+
'requestUri' => 'checkout',
178+
'expireCounter' => $this->never(),
179+
'dispatchCounter' => $this->never(),
180+
'setCustomerIdCounter' => $this->never(),
181+
],
182+
[
183+
'refererUri' => 'index',
184+
'requestUri' => 'checkout',
185+
'expireCounter' => $this->never(),
186+
'dispatchCounter' => $this->never(),
187+
'setCustomerIdCounter' => $this->never(),
188+
],
189+
[
190+
'refererUri' => 'index',
191+
'requestUri' => 'index',
192+
'expireCounter' => $this->once(),
193+
'dispatchCounter' => $this->once(),
194+
'setCustomerIdCounter' => $this->once(),
195+
],
196+
];
197+
}
117198
}

dev/tests/acceptance/tests/functional/Magento/FunctionalTest/Catalog/ActionGroup/AddProductToCartActionGroup.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@
1414
<amOnPage url="/{{product.name}}.html" stepKey="navigateProductPage"/>
1515
<click selector="{{StorefrontProductPageSection.addToCartBtn}}" stepKey="addToCart"/>
1616
<waitForElementVisible selector="{{StorefrontProductPageSection.successMsg}}" time="30" stepKey="waitForProductAdded"/>
17+
<see selector="{{StorefrontCategoryMainSection.SuccessMsg}}" userInput="You added {{product.name}} to your shopping cart." stepKey="seeAddedToCartMessage"/>
1718
</actionGroup>
1819
</actionGroups>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
<pages xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../vendor/magento/magento2-functional-testing-framework/src/Magento/FunctionalTestingFramework/Page/etc/PageObject.xsd">
10+
<page name="GuestCheckoutReviewAndPaymentsPage" url="/checkout/#payment" area="storefront" module="Magento_Checkout">
11+
<section name="CheckoutPaymentSection"/>
12+
</page>
13+
</pages>

dev/tests/acceptance/tests/functional/Magento/FunctionalTest/Checkout/Section/CheckoutShippingGuestInfoSection.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,7 @@
1717
<element name="region" type="select" selector="select[name=region_id]"/>
1818
<element name="postcode" type="input" selector="input[name=postcode]"/>
1919
<element name="telephone" type="input" selector="input[name=telephone]"/>
20+
<element name="next" type="button" selector="button.button.action.continue.primary" timeout="30"/>
21+
<element name="firstShippingMethod" type="radio" selector=".row:nth-of-type(1) .col-method .radio"/>
2022
</section>
2123
</sections>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
<entities xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../vendor/magento/magento2-functional-testing-framework/src/Magento/FunctionalTestingFramework/DataGenerator/etc/dataProfileSchema.xsd">
10+
<entity name="PersistentConfigDefault" type="persistent_config_state">
11+
<requiredEntity type="persistent_options_enabled">persistentDefaultState</requiredEntity>
12+
</entity>
13+
<entity name="persistentDefaultState" type="persistent_options_enabled">
14+
<data key="value">0</data>
15+
</entity>
16+
17+
<entity name="PersistentConfigEnabled" type="persistent_config_state">
18+
<requiredEntity type="persistent_options_enabled">persistentEnabledState</requiredEntity>
19+
</entity>
20+
<entity name="persistentEnabledState" type="persistent_options_enabled">
21+
<data key="value">1</data>
22+
</entity>
23+
</entities>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
<operations xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../vendor/magento/magento2-functional-testing-framework/src/Magento/FunctionalTestingFramework/DataGenerator/etc/dataOperation.xsd">
10+
<operation name="CreatePersistentConfigState" dataType="persistent_config_state" type="create" auth="adminFormKey" url="/admin/system_config/save/section/persistent/" method="POST">
11+
<object key="groups" dataType="persistent_config_state">
12+
<object key="options" dataType="persistent_config_state">
13+
<object key="fields" dataType="persistent_config_state">
14+
<object key="enabled" dataType="persistent_options_enabled">
15+
<field key="value">string</field>
16+
</object>
17+
</object>
18+
</object>
19+
</object>
20+
</operation>
21+
</operations>

0 commit comments

Comments
 (0)