Skip to content

Commit 7e107e2

Browse files
author
Volodymyr Kublytskyi
authored
Merge pull request #1795 from magento-engcom/2.2-develop-prs
[EngCom] Public Pull Requests - 2.2-develop
2 parents 82caf48 + fd23e7c commit 7e107e2

File tree

19 files changed

+624
-213
lines changed

19 files changed

+624
-213
lines changed

app/code/Magento/Catalog/Block/Product/ListProduct.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,13 +334,21 @@ public function prepareSortableFieldsByCategory($category)
334334
public function getIdentities()
335335
{
336336
$identities = [];
337-
foreach ($this->_getProductCollection() as $item) {
338-
$identities = array_merge($identities, $item->getIdentities());
339-
}
337+
340338
$category = $this->getLayer()->getCurrentCategory();
341339
if ($category) {
342340
$identities[] = Product::CACHE_PRODUCT_CATEGORY_TAG . '_' . $category->getId();
343341
}
342+
343+
//Check if category page shows only static block (No products)
344+
if ($category->getData('display_mode') == Category::DM_PAGE) {
345+
return $identities;
346+
}
347+
348+
foreach ($this->_getProductCollection() as $item) {
349+
$identities = array_merge($identities, $item->getIdentities());
350+
}
351+
344352
return $identities;
345353
}
346354

app/code/Magento/Catalog/Test/Unit/Block/Product/ListProductTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public function testGetIdentities()
192192
->will($this->returnValue($this->toolbarMock));
193193

194194
$this->assertEquals(
195-
[$productTag, $categoryTag],
195+
[$categoryTag, $productTag],
196196
$this->block->getIdentities()
197197
);
198198
$this->assertEquals(
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Customer\Model\Plugin;
8+
9+
use Magento\Customer\Model\Session;
10+
use Magento\Framework\Data\Form\FormKey as DataFormKey;
11+
use Magento\PageCache\Observer\FlushFormKey;
12+
13+
class CustomerFlushFormKey
14+
{
15+
/**
16+
* @var Session
17+
*/
18+
private $session;
19+
20+
/**
21+
* @var DataFormKey
22+
*/
23+
private $dataFormKey;
24+
25+
/**
26+
* Initialize dependencies.
27+
*
28+
* @param Session $session
29+
* @param DataFormKey $dataFormKey
30+
*/
31+
public function __construct(Session $session, DataFormKey $dataFormKey)
32+
{
33+
$this->session = $session;
34+
$this->dataFormKey = $dataFormKey;
35+
}
36+
37+
/**
38+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
39+
* @param FlushFormKey $subject
40+
* @param callable $proceed
41+
* @param $args
42+
*/
43+
public function aroundExecute(FlushFormKey $subject, callable $proceed, ...$args)
44+
{
45+
$currentFormKey = $this->dataFormKey->getFormKey();
46+
$proceed(...$args);
47+
$beforeParams = $this->session->getBeforeRequestParams();
48+
if ($beforeParams['form_key'] == $currentFormKey) {
49+
$beforeParams['form_key'] = $this->dataFormKey->getFormKey();
50+
$this->session->setBeforeRequestParams($beforeParams);
51+
}
52+
}
53+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Customer\Test\Unit\Model\Plugin;
7+
8+
use Magento\Customer\Model\Plugin\CustomerFlushFormKey;
9+
use Magento\Customer\Model\Session;
10+
use Magento\Framework\App\PageCache\FormKey as CookieFormKey;
11+
use Magento\Framework\Data\Form\FormKey as DataFormKey;
12+
use Magento\Framework\Event\Observer;
13+
use Magento\PageCache\Observer\FlushFormKey;
14+
use PHPUnit\Framework\TestCase;
15+
use PHPUnit_Framework_MockObject_MockObject as MockObject;
16+
17+
class CustomerFlushFormKeyTest extends TestCase
18+
{
19+
/**
20+
* @var CookieFormKey | MockObject
21+
*/
22+
private $cookieFormKey;
23+
24+
/**
25+
* @var Session | MockObject
26+
*/
27+
private $customerSession;
28+
29+
/**
30+
* @var DataFormKey | MockObject
31+
*/
32+
private $dataFormKey;
33+
34+
protected function setUp()
35+
{
36+
37+
/** @var CookieFormKey | MockObject */
38+
$this->cookieFormKey = $this->getMockBuilder(CookieFormKey::class)
39+
->disableOriginalConstructor()
40+
->getMock();
41+
42+
/** @var DataFormKey | MockObject */
43+
$this->dataFormKey = $this->getMockBuilder(DataFormKey::class)
44+
->disableOriginalConstructor()
45+
->getMock();
46+
47+
/** @var Session | MockObject */
48+
$this->customerSession = $this->getMockBuilder(Session::class)
49+
->disableOriginalConstructor()
50+
->setMethods(['getBeforeRequestParams', 'setBeforeRequestParams'])
51+
->getMock();
52+
}
53+
54+
/**
55+
* @dataProvider aroundFlushFormKeyProvider
56+
* @param $beforeFormKey
57+
* @param $currentFormKey
58+
* @param $getFormKeyTimes
59+
* @param $setBeforeParamsTimes
60+
*/
61+
public function testAroundFlushFormKey(
62+
$beforeFormKey,
63+
$currentFormKey,
64+
$getFormKeyTimes,
65+
$setBeforeParamsTimes
66+
) {
67+
$observerDto = new Observer();
68+
$observer = new FlushFormKey($this->cookieFormKey, $this->dataFormKey);
69+
$plugin = new CustomerFlushFormKey($this->customerSession, $this->dataFormKey);
70+
71+
$beforeParams['form_key'] = $beforeFormKey;
72+
73+
$this->dataFormKey->expects($this->exactly($getFormKeyTimes))
74+
->method('getFormKey')
75+
->willReturn($currentFormKey);
76+
77+
$this->customerSession->expects($this->once())
78+
->method('getBeforeRequestParams')
79+
->willReturn($beforeParams);
80+
81+
$this->customerSession->expects($this->exactly($setBeforeParamsTimes))
82+
->method('setBeforeRequestParams')
83+
->with($beforeParams);
84+
85+
$proceed = function ($observerDto) use ($observer) {
86+
return $observer->execute($observerDto);
87+
};
88+
89+
$plugin->aroundExecute($observer, $proceed, $observerDto);
90+
}
91+
92+
/**
93+
* Data provider for testAroundFlushFormKey
94+
*
95+
* @return array
96+
*/
97+
public function aroundFlushFormKeyProvider()
98+
{
99+
return [
100+
['form_key_value', 'form_key_value', 2, 1],
101+
['form_old_key_value', 'form_key_value', 1, 0],
102+
[null, 'form_key_value', 1, 0]
103+
];
104+
}
105+
}

app/code/Magento/Customer/etc/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,9 @@
323323
<type name="Magento\Framework\App\Action\AbstractAction">
324324
<plugin name="customerNotification" type="Magento\Customer\Model\Plugin\CustomerNotification"/>
325325
</type>
326+
<type name="Magento\PageCache\Observer\FlushFormKey">
327+
<plugin name="customerFlushFormKey" type="Magento\Customer\Model\Plugin\CustomerFlushFormKey"/>
328+
</type>
326329
<type name="Magento\Customer\Model\Customer\NotificationStorage">
327330
<arguments>
328331
<argument name="cache" xsi:type="object">Magento\Customer\Model\Cache\Type\Notification</argument>

app/code/Magento/Deploy/Model/Mode.php

Lines changed: 31 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use Magento\Deploy\App\Mode\ConfigProvider;
1010
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
use Magento\Framework\App\Console\MaintenanceModeEnabler;
1112
use Magento\Framework\App\DeploymentConfig\Reader;
1213
use Magento\Framework\App\DeploymentConfig\Writer;
1314
use Magento\Framework\App\Filesystem\DirectoryList;
@@ -49,11 +50,6 @@ class Mode
4950
*/
5051
private $reader;
5152

52-
/**
53-
* @var MaintenanceMode
54-
*/
55-
private $maintenanceMode;
56-
5753
/**
5854
* @var Filesystem
5955
*/
@@ -78,16 +74,24 @@ class Mode
7874
*/
7975
private $emulatedAreaProcessor;
8076

77+
/**
78+
* @var MaintenanceModeEnabler
79+
*/
80+
private $maintenanceModeEnabler;
81+
8182
/**
8283
* @param InputInterface $input
8384
* @param OutputInterface $output
8485
* @param Writer $writer
8586
* @param Reader $reader
86-
* @param MaintenanceMode $maintenanceMode
87+
* @param MaintenanceMode $maintenanceMode deprecated, use $maintenanceModeEnabler instead
8788
* @param Filesystem $filesystem
8889
* @param ConfigProvider $configProvider
8990
* @param ProcessorFacadeFactory $processorFacadeFactory
9091
* @param EmulatedAdminhtmlAreaProcessor $emulatedAreaProcessor
92+
* @param MaintenanceModeEnabler $maintenanceModeEnabler
93+
*
94+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
9195
*/
9296
public function __construct(
9397
InputInterface $input,
@@ -98,13 +102,13 @@ public function __construct(
98102
Filesystem $filesystem,
99103
ConfigProvider $configProvider = null,
100104
ProcessorFacadeFactory $processorFacadeFactory = null,
101-
EmulatedAdminhtmlAreaProcessor $emulatedAreaProcessor = null
105+
EmulatedAdminhtmlAreaProcessor $emulatedAreaProcessor = null,
106+
MaintenanceModeEnabler $maintenanceModeEnabler = null
102107
) {
103108
$this->input = $input;
104109
$this->output = $output;
105110
$this->writer = $writer;
106111
$this->reader = $reader;
107-
$this->maintenanceMode = $maintenanceMode;
108112
$this->filesystem = $filesystem;
109113

110114
$this->configProvider =
@@ -113,6 +117,8 @@ public function __construct(
113117
$processorFacadeFactory ?: ObjectManager::getInstance()->get(ProcessorFacadeFactory::class);
114118
$this->emulatedAreaProcessor =
115119
$emulatedAreaProcessor ?: ObjectManager::getInstance()->get(EmulatedAdminhtmlAreaProcessor::class);
120+
$this->maintenanceModeEnabler =
121+
$maintenanceModeEnabler ?: ObjectManager::getInstance()->get(MaintenanceModeEnabler::class);
116122
}
117123

118124
/**
@@ -123,19 +129,23 @@ public function __construct(
123129
*/
124130
public function enableProductionMode()
125131
{
126-
$this->enableMaintenanceMode($this->output);
127-
$previousMode = $this->getMode();
128-
try {
129-
// We have to turn on production mode before generation.
130-
// We need this to enable generation of the "min" files.
131-
$this->setStoreMode(State::MODE_PRODUCTION);
132-
$this->filesystem->regenerateStatic($this->output);
133-
} catch (LocalizedException $e) {
134-
// We have to return store mode to previous state in case of error.
135-
$this->setStoreMode($previousMode);
136-
throw $e;
137-
}
138-
$this->disableMaintenanceMode($this->output);
132+
$this->maintenanceModeEnabler->executeInMaintenanceMode(
133+
function () {
134+
$previousMode = $this->getMode();
135+
try {
136+
// We have to turn on production mode before generation.
137+
// We need this to enable generation of the "min" files.
138+
$this->setStoreMode(State::MODE_PRODUCTION);
139+
$this->filesystem->regenerateStatic($this->output);
140+
} catch (LocalizedException $e) {
141+
// We have to return store mode to previous state in case of error.
142+
$this->setStoreMode($previousMode);
143+
throw $e;
144+
}
145+
},
146+
$this->output,
147+
false
148+
);
139149
}
140150

141151
/**
@@ -218,28 +228,4 @@ private function saveAppConfigs($mode)
218228
$this->output->writeln('Config "' . $path . ' = ' . $item['value'] . '" has been saved.');
219229
}
220230
}
221-
222-
/**
223-
* Enable maintenance mode
224-
*
225-
* @param OutputInterface $output
226-
* @return void
227-
*/
228-
protected function enableMaintenanceMode(OutputInterface $output)
229-
{
230-
$this->maintenanceMode->set(true);
231-
$output->writeln('Enabled maintenance mode');
232-
}
233-
234-
/**
235-
* Disable maintenance mode
236-
*
237-
* @param OutputInterface $output
238-
* @return void
239-
*/
240-
protected function disableMaintenanceMode(OutputInterface $output)
241-
{
242-
$this->maintenanceMode->set(false);
243-
$output->writeln('Disabled maintenance mode');
244-
}
245231
}

app/code/Magento/Deploy/Test/Unit/Model/ModeTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Deploy\Model\Filesystem;
1313
use Magento\Deploy\Model\Mode;
1414
use Magento\Framework\App\Config\ScopeConfigInterface;
15+
use Magento\Framework\App\Console\MaintenanceModeEnabler;
1516
use Magento\Framework\App\DeploymentConfig\Reader;
1617
use Magento\Framework\App\DeploymentConfig\Writer;
1718
use Magento\Framework\App\MaintenanceMode;
@@ -124,7 +125,8 @@ protected function setUp()
124125
$this->filesystemMock,
125126
$this->configProvider,
126127
$this->processorFacadeFactory,
127-
$this->emulatedAreaProcessor
128+
$this->emulatedAreaProcessor,
129+
new MaintenanceModeEnabler($this->maintenanceMock)
128130
);
129131
}
130132

0 commit comments

Comments
 (0)