Skip to content

Commit 342dc53

Browse files
MAGETWO-98620: Shipping quote in cart not persisted for Guest customers when Persistent Shopping Cart is enabled
1 parent 4a28abb commit 342dc53

File tree

6 files changed

+118
-9
lines changed

6 files changed

+118
-9
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public function setGuest($checkQuote = false)
8787
->setCustomerLastname(null)
8888
->setCustomerGroupId(\Magento\Customer\Api\Data\GroupInterface::NOT_LOGGED_IN_ID)
8989
->setIsPersistent(false)
90+
->setCustomerIsGuest(true)
9091
->removeAllAddresses();
9192
//Create guest addresses
9293
$quote->getShippingAddress();

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
<?php
22
/**
3-
*
43
* Copyright © Magento, Inc. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
76
namespace Magento\Persistent\Observer;
87

98
use Magento\Framework\Event\ObserverInterface;
109

10+
/**
11+
* Observer of expired session
12+
*/
1113
class CheckExpirePersistentQuoteObserver implements ObserverInterface
1214
{
1315
/**
@@ -107,8 +109,12 @@ public function execute(\Magento\Framework\Event\Observer $observer)
107109
!$this->_persistentSession->isPersistent() &&
108110
!$this->_customerSession->isLoggedIn() &&
109111
$this->_checkoutSession->getQuoteId() &&
110-
!$this->isRequestFromCheckoutPage($this->request)
112+
!$this->isRequestFromCheckoutPage($this->request) &&
111113
// persistent session does not expire on onepage checkout page
114+
(
115+
$this->_checkoutSession->getQuote()->getIsPersistent() ||
116+
$this->_checkoutSession->getQuote()->getCustomerIsGuest()
117+
)
112118
) {
113119
$this->_eventManager->dispatch('persistent_session_expired');
114120
$this->quoteManager->expire();

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

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

98
use Magento\Framework\Event\ObserverInterface;
109

10+
/**
11+
* Observer for setting "is_persistent" value to quote
12+
*/
1113
class SetQuotePersistentDataObserver implements ObserverInterface
1214
{
1315
/**
@@ -73,8 +75,8 @@ public function execute(\Magento\Framework\Event\Observer $observer)
7375
}
7476

7577
if ((
76-
($this->_persistentSession->isPersistent() && !$this->_customerSession->isLoggedIn())
77-
&& !$this->_persistentData->isShoppingCartPersist()
78+
($this->_persistentSession->isPersistent())
79+
&& $this->_persistentData->isShoppingCartPersist()
7880
)
7981
&& $this->quoteManager->isPersistent()
8082
) {
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/testSchema.xsd">
10+
<test name="ShippingQuotePersistedForGuestTest">
11+
<annotations>
12+
<features value="Persistent"/>
13+
<stories value="Guest checkout"/>
14+
<title value="Estimate Shipping and Tax block sections on shipping cart saving correctly for Guest."/>
15+
<description value="Verify that 'Estimate Shipping and Tax' block sections on shipping cart saving correctly for Guest after switching to another page. And check that the shopping cart is cleared after reset persistent cookie."/>
16+
<severity value="CRITICAL"/>
17+
<testCaseId value="MAGETWO-99025"/>
18+
<useCaseId value="MAGETWO-98620"/>
19+
<group value="persistent"/>
20+
</annotations>
21+
<before>
22+
<!--Enabled The Persistent Shopping Cart feature -->
23+
<createData entity="PersistentConfigEnabled" stepKey="enablePersistent"/>
24+
<createData entity="PersistentLogoutClearDisable" stepKey="persistentLogoutClearDisable"/>
25+
<!--Create simple product-->
26+
<createData entity="SimpleProduct2" stepKey="createProduct">
27+
<field key="price">150</field>
28+
</createData>
29+
<!--Create customer-->
30+
<createData entity="Simple_US_Customer" stepKey="createCustomer">
31+
<field key="firstname">John1</field>
32+
<field key="lastname">Doe1</field>
33+
</createData>
34+
</before>
35+
<after>
36+
<!--Revert persistent configuration to default-->
37+
<createData entity="PersistentConfigDefault" stepKey="setDefaultPersistentState"/>
38+
<deleteData createDataKey="createProduct" stepKey="deleteProduct"/>
39+
<deleteData createDataKey="createCustomer" stepKey="deleteCustomer"/>
40+
<actionGroup ref="logout" stepKey="logout"/>
41+
</after>
42+
<!--Step 1: Login as a Customer with remember me checked-->
43+
<actionGroup ref="CustomerLoginOnStorefrontWithRememberMeChecked" stepKey="loginToStorefrontAccountWithRememberMeChecked">
44+
<argument name="Customer" value="$$createCustomer$$"/>
45+
</actionGroup>
46+
<!--Step 2: Open the Product Page and add the product to shopping cart-->
47+
<amOnPage url="{{StorefrontProductPage.url($$createProduct.custom_attributes[url_key]$$)}}" stepKey="navigateToProductPageAsLoggedUser"/>
48+
<actionGroup ref="addToCartFromStorefrontProductPage" stepKey="addProductToCartAsLoggedUser">
49+
<argument name="productName" value="$$createProduct.name$$"/>
50+
</actionGroup>
51+
<!--Step 3: Log out, reset persistent cookie and go to homepage-->
52+
<amOnPage url="{{StorefrontCustomerSignOutPage.url}}" stepKey="signOut"/>
53+
<waitForLoadingMaskToDisappear stepKey="waitSignOutPage"/>
54+
<resetCookie userInput="persistent_shopping_cart" stepKey="resetPersistentCookie"/>
55+
<amOnPage url="{{StorefrontHomePage.url}}" stepKey="amOnHomePageAfterResetPersistentCookie"/>
56+
<waitForPageLoad stepKey="waitHomePageLoadAfterResetCookie"/>
57+
<!--Check that the minicart is empty-->
58+
<actionGroup ref="assertMiniCartEmpty" after="waitHomePageLoadAfterResetCookie" stepKey="seeMinicartEmpty"/>
59+
<!--Step 4: Add the product to shopping cart and open cart-->
60+
<amOnPage url="{{StorefrontProductPage.url($$createProduct.custom_attributes[url_key]$$)}}" stepKey="navigateToProductPageAsGuestUser"/>
61+
<actionGroup ref="addToCartFromStorefrontProductPage" stepKey="addProductToCartAsGuestUser">
62+
<argument name="productName" value="$$createProduct.name$$"/>
63+
</actionGroup>
64+
<actionGroup ref="clickViewAndEditCartFromMiniCart" stepKey="goToShoppingCartBeforeChangeShippingAndTaxSection"/>
65+
<!--Step 5: Open Estimate Shipping and Tax block and fill the sections-->
66+
<conditionalClick selector="{{CheckoutCartSummarySection.estimateShippingAndTax}}" dependentSelector="{{CheckoutCartSummarySection.country}}" visible="false" stepKey="expandEstimateShippingAndTax" />
67+
<waitForLoadingMaskToDisappear stepKey="waitForLoadingMask"/>
68+
<selectOption selector="{{CheckoutCartSummarySection.country}}" userInput="{{US_Address_CA.country}}" stepKey="selectUSCountry"/>
69+
<selectOption selector="{{CheckoutCartSummarySection.stateProvince}}" userInput="{{US_Address_CA.state}}" stepKey="selectCaliforniaRegion"/>
70+
<fillField selector="{{CheckoutCartSummarySection.postcode}}" userInput="{{US_Address_CA.postcode}}" stepKey="inputPostCode"/>
71+
<!--Step 6: Go to Homepage-->
72+
<amOnPage url="{{StorefrontHomePage.url}}" stepKey="goToHomePageAfterChangingShippingAndTaxSection"/>
73+
<!--Step 7: Go to shopping cart and check "Estimate Shipping and Tax" fields values are saved-->
74+
<actionGroup ref="clickViewAndEditCartFromMiniCart" after="goToHomePageAfterChangingShippingAndTaxSection" stepKey="goToShoppingCartAfterChangingShippingAndTaxSection"/>
75+
<conditionalClick selector="{{CheckoutCartSummarySection.estimateShippingAndTax}}" dependentSelector="{{CheckoutCartSummarySection.country}}" visible="false" stepKey="expandEstimateShippingAndTaxAfterChanging" />
76+
<seeOptionIsSelected selector="{{CheckoutCartSummarySection.country}}" userInput="{{US_Address_CA.country}}" stepKey="checkCustomerCountry" />
77+
<seeOptionIsSelected selector="{{CheckoutCartSummarySection.stateProvince}}" userInput="{{US_Address_CA.state}}" stepKey="checkCustomerRegion" />
78+
<grabValueFrom selector="{{CheckoutCartSummarySection.postcode}}" stepKey="grabTextPostCode"/>
79+
<assertEquals message="Customer postcode is invalid" stepKey="checkCustomerPostcode">
80+
<expectedResult type="string">{{US_Address_CA.postcode}}</expectedResult>
81+
<actualResult type="variable">grabTextPostCode</actualResult>
82+
</assertEquals>
83+
</test>
84+
</tests>

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
<?php
22
/**
3-
*
43
* Copyright © Magento, Inc. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
76

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

9+
use Magento\Quote\Model\Quote;
10+
11+
/**
12+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
13+
*/
1014
class CheckExpirePersistentQuoteObserverTest extends \PHPUnit\Framework\TestCase
1115
{
1216
/**
@@ -54,6 +58,11 @@ class CheckExpirePersistentQuoteObserverTest extends \PHPUnit\Framework\TestCase
5458
*/
5559
private $requestMock;
5660

61+
/**
62+
* @var \PHPUnit_Framework_MockObject_MockObject|Quote
63+
*/
64+
private $quoteMock;
65+
5766
/**
5867
* @inheritdoc
5968
*/
@@ -83,6 +92,10 @@ protected function setUp()
8392
$this->checkoutSessionMock,
8493
$this->requestMock
8594
);
95+
$this->quoteMock = $this->getMockBuilder(Quote::class)
96+
->setMethods(['getCustomerIsGuest', 'getIsPersistent'])
97+
->disableOriginalConstructor()
98+
->getMock();
8699
}
87100

88101
public function testExecuteWhenCanNotApplyPersistentData()
@@ -133,6 +146,11 @@ public function testExecuteWhenPersistentIsEnabled(
133146
->willReturn(true);
134147
$this->persistentHelperMock->expects($this->once())->method('isEnabled')->willReturn(true);
135148
$this->sessionMock->expects($this->once())->method('isPersistent')->willReturn(false);
149+
$this->checkoutSessionMock
150+
->method('getQuote')
151+
->willReturn($this->quoteMock);
152+
$this->quoteMock->method('getCustomerIsGuest')->willReturn(true);
153+
$this->quoteMock->method('getIsPersistent')->willReturn(true);
136154
$this->customerSessionMock
137155
->expects($this->atLeastOnce())
138156
->method('isLoggedIn')

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ public function testExecuteWhenQuoteNotExist()
8383
->method('getEvent')
8484
->will($this->returnValue($this->eventManagerMock));
8585
$this->eventManagerMock->expects($this->once())->method('getQuote');
86-
$this->customerSessionMock->expects($this->never())->method('isLoggedIn');
8786
$this->model->execute($this->observerMock);
8887
}
8988

@@ -98,8 +97,7 @@ public function testExecuteWhenSessionIsPersistent()
9897
->expects($this->once())
9998
->method('getQuote')
10099
->will($this->returnValue($this->quoteMock));
101-
$this->customerSessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(false));
102-
$this->helperMock->expects($this->once())->method('isShoppingCartPersist')->will($this->returnValue(false));
100+
$this->helperMock->expects($this->once())->method('isShoppingCartPersist')->will($this->returnValue(true));
103101
$this->quoteManagerMock->expects($this->once())->method('isPersistent')->will($this->returnValue(true));
104102
$this->quoteMock->expects($this->once())->method('setIsPersistent')->with(true);
105103
$this->model->execute($this->observerMock);

0 commit comments

Comments
 (0)