Skip to content

Commit aed0545

Browse files
committed
Merge remote-tracking branch 'mainline/2.2-develop' into 2.2-develop-pr35
2 parents 5392108 + be89e9c commit aed0545

File tree

8 files changed

+257
-48
lines changed

8 files changed

+257
-48
lines changed

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

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace Magento\Persistent\Observer;
1010

1111
use Magento\Framework\Event\ObserverInterface;
12+
use Magento\Quote\Model\Quote;
1213

1314
/**
1415
* Observer of expired session
@@ -71,6 +72,11 @@ class CheckExpirePersistentQuoteObserver implements ObserverInterface
7172
*/
7273
private $checkoutPagePath = 'checkout';
7374

75+
/**
76+
* @var Quote
77+
*/
78+
private $quote;
79+
7480
/**
7581
* @param \Magento\Persistent\Helper\Session $persistentSession
7682
* @param \Magento\Persistent\Helper\Data $persistentData
@@ -110,23 +116,65 @@ public function execute(\Magento\Framework\Event\Observer $observer)
110116
return;
111117
}
112118

119+
//clear persistent when persistent data is disabled
120+
if ($this->isPersistentQuoteOutdated()) {
121+
$this->_eventManager->dispatch('persistent_session_expired');
122+
$this->quoteManager->expire();
123+
$this->_checkoutSession->clearQuote();
124+
return;
125+
}
126+
113127
if ($this->_persistentData->isEnabled() &&
114128
!$this->_persistentSession->isPersistent() &&
115129
!$this->_customerSession->isLoggedIn() &&
116130
$this->_checkoutSession->getQuoteId() &&
117131
!$this->isRequestFromCheckoutPage($this->request) &&
118132
// persistent session does not expire on onepage checkout page
119-
(
120-
$this->_checkoutSession->getQuote()->getIsPersistent() ||
121-
$this->_checkoutSession->getQuote()->getCustomerIsGuest()
122-
)
133+
$this->isNeedToExpireSession()
123134
) {
124135
$this->_eventManager->dispatch('persistent_session_expired');
125136
$this->quoteManager->expire();
126137
$this->_customerSession->setCustomerId(null)->setCustomerGroupId(null);
127138
}
128139
}
129140

141+
/**
142+
* Checks if current quote marked as persistent and Persistence Functionality is disabled.
143+
*
144+
* @return bool
145+
*/
146+
private function isPersistentQuoteOutdated(): bool
147+
{
148+
if (!$this->_persistentData->isEnabled() && !$this->_customerSession->isLoggedIn()
149+
&& $this->_checkoutSession->getQuoteId()) {
150+
return (bool)$this->getQuote()->getIsPersistent();
151+
}
152+
return false;
153+
}
154+
155+
/**
156+
* Condition checker
157+
*
158+
* @return bool
159+
*/
160+
private function isNeedToExpireSession(): bool
161+
{
162+
return $this->getQuote()->getIsPersistent() || $this->getQuote()->getCustomerIsGuest();
163+
}
164+
165+
/**
166+
* Getter for Quote with micro optimization
167+
*
168+
* @return Quote
169+
*/
170+
private function getQuote(): Quote
171+
{
172+
if ($this->quote === null) {
173+
$this->quote = $this->_checkoutSession->getQuote();
174+
}
175+
return $this->quote;
176+
}
177+
130178
/**
131179
* Check current request is coming from onepage checkout page.
132180
*

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function testExecuteWhenPersistentIsNotEnabled()
112112
->method('canProcess')
113113
->with($this->observerMock)
114114
->will($this->returnValue(true));
115-
$this->persistentHelperMock->expects($this->once())->method('isEnabled')->will($this->returnValue(false));
115+
$this->persistentHelperMock->expects($this->exactly(2))->method('isEnabled')->will($this->returnValue(false));
116116
$this->eventManagerMock->expects($this->never())->method('dispatch');
117117
$this->model->execute($this->observerMock);
118118
}
@@ -139,7 +139,9 @@ public function testExecuteWhenPersistentIsEnabled(
139139
->method('canProcess')
140140
->with($this->observerMock)
141141
->will($this->returnValue(true));
142-
$this->persistentHelperMock->expects($this->once())->method('isEnabled')->will($this->returnValue(true));
142+
$this->persistentHelperMock->expects($this->atLeastOnce())
143+
->method('isEnabled')
144+
->will($this->returnValue(true));
143145
$this->sessionMock->expects($this->once())->method('isPersistent')->will($this->returnValue(false));
144146
$this->checkoutSessionMock
145147
->method('getQuote')

app/code/Magento/Reports/Block/Adminhtml/Grid.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,8 @@ protected function _prepareCollection()
172172
* Validate from and to date
173173
*/
174174
try {
175-
$from = $this->_localeDate->date($this->getFilter('report_from'), null, false, false);
176-
$to = $this->_localeDate->date($this->getFilter('report_to'), null, false, false);
177-
175+
$from = $this->_localeDate->date($this->getFilter('report_from'), null, true, false);
176+
$to = $this->_localeDate->date($this->getFilter('report_to'), null, true, false);
178177
$collection->setInterval($from, $to);
179178
} catch (\Exception $e) {
180179
$this->_errors[] = __('Invalid date specified');

app/code/Magento/Reports/Model/ResourceModel/Report/Collection.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ class Collection extends \Magento\Framework\Data\Collection
2020
/**
2121
* From value
2222
*
23-
* @var \DateTime
23+
* @var \DateTimeInterface
2424
*/
2525
protected $_from;
2626

2727
/**
2828
* To value
2929
*
30-
* @var \DateTime
30+
* @var \DateTimeInterface
3131
*/
3232
protected $_to;
3333

@@ -121,8 +121,8 @@ public function setPeriod($period)
121121
*/
122122
public function setInterval(\DateTimeInterface $fromDate, \DateTimeInterface $toDate)
123123
{
124-
$this->_from = new \DateTime($fromDate->format('Y-m-d'), $fromDate->getTimezone());
125-
$this->_to = new \DateTime($toDate->format('Y-m-d'), $toDate->getTimezone());
124+
$this->_from = $fromDate;
125+
$this->_to = $toDate;
126126

127127
return $this;
128128
}
@@ -139,8 +139,8 @@ protected function _getIntervals()
139139
if (!$this->_from && !$this->_to) {
140140
return $this->_intervals;
141141
}
142-
$dateStart = $this->_from;
143-
$dateEnd = $this->_to;
142+
$dateStart = new \DateTime($this->_from->format('Y-m-d'), $this->_from->getTimezone());
143+
$dateEnd = new \DateTime($this->_to->format('Y-m-d'), $this->_to->getTimezone());
144144

145145
$firstInterval = true;
146146
while ($dateStart <= $dateEnd) {
@@ -175,11 +175,7 @@ protected function _getIntervals()
175175
protected function _getDayInterval(\DateTime $dateStart)
176176
{
177177
$interval = [
178-
'period' => $this->_localeDate->formatDateTime(
179-
$dateStart,
180-
\IntlDateFormatter::SHORT,
181-
\IntlDateFormatter::NONE
182-
),
178+
'period' => $this->_localeDate->formatDate($dateStart, \IntlDateFormatter::SHORT),
183179
'start' => $this->_localeDate->convertConfigTimeToUtc($dateStart->format('Y-m-d 00:00:00')),
184180
'end' => $this->_localeDate->convertConfigTimeToUtc($dateStart->format('Y-m-d 23:59:59')),
185181
];

app/code/Magento/Reports/Test/Unit/Model/ResourceModel/Report/CollectionTest.php

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,49 @@
66

77
namespace Magento\Reports\Test\Unit\Model\ResourceModel\Report;
88

9+
use Magento\Framework\Data\Collection\EntityFactory;
10+
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
911
use Magento\Reports\Model\ResourceModel\Report\Collection;
12+
use Magento\Reports\Model\ResourceModel\Report\Collection\Factory as ReportCollectionFactory;
1013

14+
/**
15+
* Class CollectionTest
16+
*
17+
* @covers \Magento\Reports\Model\ResourceModel\Report\Collection
18+
*/
1119
class CollectionTest extends \PHPUnit\Framework\TestCase
1220
{
1321
/**
14-
* @var \Magento\Reports\Model\ResourceModel\Report\Collection
22+
* @var Collection
1523
*/
1624
protected $collection;
1725

1826
/**
19-
* @var \Magento\Framework\Data\Collection\EntityFactory|\PHPUnit_Framework_MockObject_MockObject
27+
* @var EntityFactory|\PHPUnit_Framework_MockObject_MockObject
2028
*/
2129
protected $entityFactoryMock;
2230

2331
/**
24-
* @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface|\PHPUnit_Framework_MockObject_MockObject
32+
* @var TimezoneInterface|\PHPUnit_Framework_MockObject_MockObject
2533
*/
2634
protected $timezoneMock;
2735

2836
/**
29-
* @var \Magento\Reports\Model\ResourceModel\Report\Collection\Factory|\PHPUnit_Framework_MockObject_MockObject
37+
* @var ReportCollectionFactory|\PHPUnit_Framework_MockObject_MockObject
3038
*/
3139
protected $factoryMock;
3240

3341
/**
34-
* {@inheritDoc}
42+
* @inheritDoc
3543
*/
3644
protected function setUp()
3745
{
38-
$this->entityFactoryMock = $this->getMockBuilder(\Magento\Framework\Data\Collection\EntityFactory::class)
39-
->disableOriginalConstructor()
40-
->getMock();
41-
$this->timezoneMock = $this->getMockBuilder(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class)
42-
->getMock();
43-
$this->factoryMock = $this->getMockBuilder(
44-
\Magento\Reports\Model\ResourceModel\Report\Collection\Factory::class
45-
)->disableOriginalConstructor()
46-
->getMock();
47-
48-
$this->timezoneMock
49-
->expects($this->any())
50-
->method('formatDateTime')
51-
->will($this->returnCallback([$this, 'formatDateTime']));
46+
$this->entityFactoryMock = $this->createMock(EntityFactory::class);
47+
$this->timezoneMock = $this->createMock(TimezoneInterface::class);
48+
$this->factoryMock = $this->createMock(ReportCollectionFactory::class);
49+
50+
$this->timezoneMock->method('formatDate')
51+
->will($this->returnCallback([$this, 'formatDate']));
5252

5353
$this->collection = new Collection(
5454
$this->entityFactoryMock,
@@ -131,7 +131,7 @@ public function testGetReports($period, $fromDate, $toDate, $size)
131131
public function testLoadData()
132132
{
133133
$this->assertInstanceOf(
134-
\Magento\Reports\Model\ResourceModel\Report\Collection::class,
134+
Collection::class,
135135
$this->collection->loadData()
136136
);
137137
}
@@ -182,14 +182,11 @@ public function intervalsDataProvider()
182182
}
183183

184184
/**
185+
* @param \DateTimeInterface $dateStart
185186
* @return string
186187
*/
187-
public function formatDateTime()
188+
public function formatDate(\DateTimeInterface $dateStart): string
188189
{
189-
$args = func_get_args();
190-
191-
$dateStart = $args[0];
192-
193190
$formatter = new \IntlDateFormatter(
194191
"en_US",
195192
\IntlDateFormatter::SHORT,

app/code/Magento/Store/App/Action/Plugin/Context.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Magento\Framework\App\Http\Context as HttpContext;
1010
use Magento\Framework\Exception\NoSuchEntityException;
1111
use Magento\Framework\Exception\NotFoundException;
12-
use Magento\Framework\Phrase;
1312
use Magento\Store\Api\Data\StoreInterface;
1413
use Magento\Store\Api\StoreCookieManagerInterface;
1514
use Magento\Store\Api\StoreResolverInterface;
@@ -128,15 +127,17 @@ private function processInvalidStoreRequested(
128127
* Update context accordingly to the store found.
129128
*
130129
* @param StoreInterface $store
130+
* @throws \Magento\Framework\Exception\LocalizedException
131131
*/
132132
private function updateContext(StoreInterface $store)
133133
{
134134
/** @var StoreInterface $defaultStore */
135135
$defaultStore = $this->storeManager->getWebsite()->getDefaultStore();
136+
136137
$this->httpContext->setValue(
137138
StoreManagerInterface::CONTEXT_STORE,
138139
$store->getCode(),
139-
$defaultStore->getCode()
140+
$store->isUseStoreInUrl() ? $store->getCode() : $defaultStore->getCode()
140141
);
141142

142143
$this->httpContext->setValue(

0 commit comments

Comments
 (0)