Skip to content

Commit 73b48b4

Browse files
Merge pull request #2516 from magento-panda/PANDA-FIXES-2.2
Fixed issues: - MAGETWO-90053: Refreshing page deletes shipping address on guest checkout if module-persistent is enabled
2 parents 029d0e6 + 9f0915c commit 73b48b4

File tree

14 files changed

+329
-21
lines changed

14 files changed

+329
-21
lines changed

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

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
* Copyright © Magento, Inc. All rights reserved.
55
* See COPYING.txt for license details.
66
*/
7+
8+
declare(strict_types=1);
9+
710
namespace Magento\Persistent\Observer;
811

912
use Magento\Framework\Event\ObserverInterface;
@@ -50,28 +53,45 @@ class CheckExpirePersistentQuoteObserver implements ObserverInterface
5053
*/
5154
protected $_persistentData = null;
5255

56+
/**
57+
* Request
58+
*
59+
* @var \Magento\Framework\App\RequestInterface
60+
*/
61+
private $request;
62+
63+
/**
64+
* Checkout Page path
65+
*
66+
* @var string
67+
*/
68+
private $checkoutPagePath = 'checkout';
69+
5370
/**
5471
* @param \Magento\Persistent\Helper\Session $persistentSession
5572
* @param \Magento\Persistent\Helper\Data $persistentData
5673
* @param \Magento\Persistent\Model\QuoteManager $quoteManager
5774
* @param \Magento\Framework\Event\ManagerInterface $eventManager
5875
* @param \Magento\Customer\Model\Session $customerSession
5976
* @param \Magento\Checkout\Model\Session $checkoutSession
77+
* @param \Magento\Framework\App\RequestInterface $request
6078
*/
6179
public function __construct(
6280
\Magento\Persistent\Helper\Session $persistentSession,
6381
\Magento\Persistent\Helper\Data $persistentData,
6482
\Magento\Persistent\Model\QuoteManager $quoteManager,
6583
\Magento\Framework\Event\ManagerInterface $eventManager,
6684
\Magento\Customer\Model\Session $customerSession,
67-
\Magento\Checkout\Model\Session $checkoutSession
85+
\Magento\Checkout\Model\Session $checkoutSession,
86+
\Magento\Framework\App\RequestInterface $request
6887
) {
6988
$this->_persistentSession = $persistentSession;
7089
$this->quoteManager = $quoteManager;
7190
$this->_customerSession = $customerSession;
7291
$this->_checkoutSession = $checkoutSession;
7392
$this->_eventManager = $eventManager;
7493
$this->_persistentData = $persistentData;
94+
$this->request = $request;
7595
}
7696

7797
/**
@@ -90,12 +110,32 @@ public function execute(\Magento\Framework\Event\Observer $observer)
90110
!$this->_persistentSession->isPersistent() &&
91111
!$this->_customerSession->isLoggedIn() &&
92112
$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
113+
!$this->isRequestFromCheckoutPage($this->request)
114+
// persistent session does not expire on onepage checkout page
95115
) {
96116
$this->_eventManager->dispatch('persistent_session_expired');
97117
$this->quoteManager->expire();
98118
$this->_customerSession->setCustomerId(null)->setCustomerGroupId(null);
99119
}
100120
}
121+
122+
/**
123+
* Check current request is coming from onepage checkout page.
124+
*
125+
* @param \Magento\Framework\App\RequestInterface $request
126+
* @return bool
127+
*/
128+
private function isRequestFromCheckoutPage(\Magento\Framework\App\RequestInterface $request): bool
129+
{
130+
$requestUri = (string)$request->getRequestUri();
131+
$refererUri = (string)$request->getServer('HTTP_REFERER');
132+
133+
/** @var bool $isCheckoutPage */
134+
$isCheckoutPage = (
135+
false !== strpos($requestUri, $this->checkoutPagePath) ||
136+
false !== strpos($refererUri, $this->checkoutPagePath)
137+
);
138+
139+
return $isCheckoutPage;
140+
}
101141
}

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

Lines changed: 86 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,35 @@ 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+
5257
protected function setUp()
5358
{
5459
$this->sessionMock = $this->createMock(\Magento\Persistent\Helper\Session::class);
5560
$this->customerSessionMock = $this->createMock(\Magento\Customer\Model\Session::class);
5661
$this->persistentHelperMock = $this->createMock(\Magento\Persistent\Helper\Data::class);
5762
$this->observerMock
5863
= $this->createPartialMock(\Magento\Framework\Event\Observer::class, ['getControllerAction',
59-
'__wakeUp']);
64+
'__wakeUp']);
6065
$this->quoteManagerMock = $this->createMock(\Magento\Persistent\Model\QuoteManager::class);
6166
$this->eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
6267
$this->checkoutSessionMock = $this->createMock(\Magento\Checkout\Model\Session::class);
68+
$this->requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
69+
->disableOriginalConstructor()
70+
->setMethods(['getRequestUri', 'getServer'])
71+
->getMockForAbstractClass();
72+
6373
$this->model = new \Magento\Persistent\Observer\CheckExpirePersistentQuoteObserver(
6474
$this->sessionMock,
6575
$this->persistentHelperMock,
6676
$this->quoteManagerMock,
6777
$this->eventManagerMock,
6878
$this->customerSessionMock,
69-
$this->checkoutSessionMock
79+
$this->checkoutSessionMock,
80+
$this->requestMock
7081
);
7182
}
7283

@@ -93,25 +104,90 @@ public function testExecuteWhenPersistentIsNotEnabled()
93104
$this->model->execute($this->observerMock);
94105
}
95106

96-
public function testExecuteWhenPersistentIsEnabled()
97-
{
107+
/**
108+
* Test method \Magento\Persistent\Observer\CheckExpirePersistentQuoteObserver::execute when persistent is enabled
109+
*
110+
* @param $refererUri
111+
* @param $requestUri
112+
* @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $expireCounter
113+
* @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $dispatchCounter
114+
* @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $setCustomerIdCounter
115+
* @dataProvider requestDataProvider
116+
*/
117+
public function testExecuteWhenPersistentIsEnabled(
118+
$refererUri,
119+
$requestUri,
120+
\PHPUnit_Framework_MockObject_Matcher_InvokedCount $expireCounter,
121+
\PHPUnit_Framework_MockObject_Matcher_InvokedCount $dispatchCounter,
122+
\PHPUnit_Framework_MockObject_Matcher_InvokedCount $setCustomerIdCounter
123+
) {
98124
$this->persistentHelperMock
99125
->expects($this->once())
100126
->method('canProcess')
101127
->with($this->observerMock)
102128
->will($this->returnValue(true));
103129
$this->persistentHelperMock->expects($this->once())->method('isEnabled')->will($this->returnValue(true));
104130
$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');
110131
$this->customerSessionMock
111-
->expects($this->once())
132+
->expects($this->atLeastOnce())
133+
->method('isLoggedIn')
134+
->will($this->returnValue(false));
135+
$this->checkoutSessionMock
136+
->expects($this->atLeastOnce())
137+
->method('getQuoteId')
138+
->will($this->returnValue(10));
139+
$this->eventManagerMock->expects($dispatchCounter)->method('dispatch');
140+
$this->quoteManagerMock->expects($expireCounter)->method('expire');
141+
$this->customerSessionMock
142+
->expects($setCustomerIdCounter)
112143
->method('setCustomerId')
113144
->with(null)
114145
->will($this->returnSelf());
146+
$this->requestMock->expects($this->atLeastOnce())->method('getRequestUri')->willReturn($refererUri);
147+
$this->requestMock
148+
->expects($this->atLeastOnce())
149+
->method('getServer')
150+
->with('HTTP_REFERER')
151+
->willReturn($requestUri);
115152
$this->model->execute($this->observerMock);
116153
}
154+
155+
/**
156+
* Request Data Provider
157+
*
158+
* @return array
159+
*/
160+
public function requestDataProvider()
161+
{
162+
return [
163+
[
164+
'refererUri' => 'checkout',
165+
'requestUri' => 'index',
166+
'expireCounter' => $this->never(),
167+
'dispatchCounter' => $this->never(),
168+
'setCustomerIdCounter' => $this->never(),
169+
],
170+
[
171+
'refererUri' => 'checkout',
172+
'requestUri' => 'checkout',
173+
'expireCounter' => $this->never(),
174+
'dispatchCounter' => $this->never(),
175+
'setCustomerIdCounter' => $this->never(),
176+
],
177+
[
178+
'refererUri' => 'index',
179+
'requestUri' => 'checkout',
180+
'expireCounter' => $this->never(),
181+
'dispatchCounter' => $this->never(),
182+
'setCustomerIdCounter' => $this->never(),
183+
],
184+
[
185+
'refererUri' => 'index',
186+
'requestUri' => 'index',
187+
'expireCounter' => $this->once(),
188+
'dispatchCounter' => $this->once(),
189+
'setCustomerIdCounter' => $this->once(),
190+
],
191+
];
192+
}
117193
}

dev/tests/acceptance/tests/functional/Magento/FunctionalTest/Catalog/Test/StorefrontPurchaseProductCustomOptionsDifferentStoreViewsTest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<severity value="CRITICAL"/>
1717
<testCaseId value="MAGETWO-77831"/>
1818
<group value="product"/>
19+
<group value="skip"/><!-- MAGETWO-91215 -->
1920
</annotations>
2021

2122
<before>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10+
xsi:noNamespaceSchemaLocation="../../../../../../vendor/magento/magento2-functional-testing-framework/src/Magento/FunctionalTestingFramework/Test/etc/actionGroupSchema.xsd">
11+
<actionGroup name="NavigateToCheckoutActionGroup">
12+
<click selector="{{StorefrontMiniCartSection.show}}" stepKey="clickCart"/>
13+
<click selector="{{StorefrontMiniCartSection.goToCheckout}}" stepKey="goToCheckout"/>
14+
<waitForPageLoad stepKey="waitForPageLoad2"/>
15+
</actionGroup>
16+
</actionGroups>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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="GuestCheckoutPage" url="/checkout/#payment" area="storefront" module="Checkout">
11+
<section name="PaymentMethodSection"/>
12+
<section name="ShipToSection"/>
13+
</page>
14+
</pages>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
<sections xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10+
xsi:noNamespaceSchemaLocation="../../../../../../vendor/magento/magento2-functional-testing-framework/src/Magento/FunctionalTestingFramework/Page/etc/SectionObject.xsd">
11+
<section name="PaymentMethodSection">
12+
<element name="billingAddress" type="text" selector=".checkout-billing-address"/>
13+
</section>
14+
</sections>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
<sections xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10+
xsi:noNamespaceSchemaLocation="../../../../../../vendor/magento/magento2-functional-testing-framework/src/Magento/FunctionalTestingFramework/Page/etc/SectionObject.xsd">
11+
<section name="ShipToSection">
12+
<element name="shippingInformation" type="text" selector=".shipping-information-content"/>
13+
</section>
14+
</sections>

dev/tests/acceptance/tests/functional/Magento/FunctionalTest/Checkout/Test/StorefrontGuestCheckoutTest.xml

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,14 @@
3030
<deleteData createDataKey="createProduct" stepKey="deleteProduct"/>
3131
</after>
3232

33-
<amOnPage url="{{StorefrontCategoryPage.url($$createCategory.name$$)}}" stepKey="onCategoryPage"/>
34-
<waitForPageLoad stepKey="waitForPageLoad1"/>
35-
<moveMouseOver selector="{{StorefrontCategoryMainSection.ProductItemInfo}}" stepKey="hoverProduct"/>
36-
<click selector="{{StorefrontCategoryMainSection.AddToCartBtn}}" stepKey="addToCart"/>
37-
<waitForElementVisible selector="{{StorefrontCategoryMainSection.SuccessMsg}}" time="30" stepKey="waitForProductAdded"/>
33+
<!-- Add simple product to cart -->
34+
<actionGroup stepKey="addProductToCart1" ref="AddSimpleProductToCart">
35+
<argument name="product" value="$$createProduct$$"/>
36+
</actionGroup>
3837
<see selector="{{StorefrontCategoryMainSection.SuccessMsg}}" userInput="You added $$createProduct.name$$ to your shopping cart." stepKey="seeAddedToCartMessage"/>
3938
<see selector="{{StorefrontMiniCartSection.quantity}}" userInput="1" stepKey="seeCartQuantity"/>
40-
<click selector="{{StorefrontMiniCartSection.show}}" stepKey="clickCart"/>
41-
<click selector="{{StorefrontMiniCartSection.goToCheckout}}" stepKey="goToCheckout"/>
42-
<waitForPageLoad stepKey="waitForPageLoad2"/>
39+
<!-- Navigate to checkout -->
40+
<actionGroup stepKey="addProductNavigateToCheckout" ref="NavigateToCheckoutActionGroup"/>
4341
<fillField selector="{{GuestCheckoutShippingSection.email}}" userInput="{{CustomerEntityOne.email}}" stepKey="enterEmail"/>
4442
<fillField selector="{{GuestCheckoutShippingSection.firstName}}" userInput="{{CustomerEntityOne.firstname}}" stepKey="enterFirstName"/>
4543
<fillField selector="{{GuestCheckoutShippingSection.lastName}}" userInput="{{CustomerEntityOne.lastname}}" stepKey="enterLastName"/>
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)