Skip to content

Commit

Permalink
Merge pull request #1795 from magento-engcom/2.2-develop-prs
Browse files Browse the repository at this point in the history
[EngCom] Public Pull Requests - 2.2-develop
  • Loading branch information
Volodymyr Kublytskyi authored Dec 1, 2017
2 parents 82caf48 + fd23e7c commit 7e107e2
Show file tree
Hide file tree
Showing 19 changed files with 624 additions and 213 deletions.
14 changes: 11 additions & 3 deletions app/code/Magento/Catalog/Block/Product/ListProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,13 +334,21 @@ public function prepareSortableFieldsByCategory($category)
public function getIdentities()
{
$identities = [];
foreach ($this->_getProductCollection() as $item) {
$identities = array_merge($identities, $item->getIdentities());
}

$category = $this->getLayer()->getCurrentCategory();
if ($category) {
$identities[] = Product::CACHE_PRODUCT_CATEGORY_TAG . '_' . $category->getId();
}

//Check if category page shows only static block (No products)
if ($category->getData('display_mode') == Category::DM_PAGE) {
return $identities;
}

foreach ($this->_getProductCollection() as $item) {
$identities = array_merge($identities, $item->getIdentities());
}

return $identities;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public function testGetIdentities()
->will($this->returnValue($this->toolbarMock));

$this->assertEquals(
[$productTag, $categoryTag],
[$categoryTag, $productTag],
$this->block->getIdentities()
);
$this->assertEquals(
Expand Down
53 changes: 53 additions & 0 deletions app/code/Magento/Customer/Model/Plugin/CustomerFlushFormKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Customer\Model\Plugin;

use Magento\Customer\Model\Session;
use Magento\Framework\Data\Form\FormKey as DataFormKey;
use Magento\PageCache\Observer\FlushFormKey;

class CustomerFlushFormKey
{
/**
* @var Session
*/
private $session;

/**
* @var DataFormKey
*/
private $dataFormKey;

/**
* Initialize dependencies.
*
* @param Session $session
* @param DataFormKey $dataFormKey
*/
public function __construct(Session $session, DataFormKey $dataFormKey)
{
$this->session = $session;
$this->dataFormKey = $dataFormKey;
}

/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @param FlushFormKey $subject
* @param callable $proceed
* @param $args
*/
public function aroundExecute(FlushFormKey $subject, callable $proceed, ...$args)
{
$currentFormKey = $this->dataFormKey->getFormKey();
$proceed(...$args);
$beforeParams = $this->session->getBeforeRequestParams();
if ($beforeParams['form_key'] == $currentFormKey) {
$beforeParams['form_key'] = $this->dataFormKey->getFormKey();
$this->session->setBeforeRequestParams($beforeParams);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Customer\Test\Unit\Model\Plugin;

use Magento\Customer\Model\Plugin\CustomerFlushFormKey;
use Magento\Customer\Model\Session;
use Magento\Framework\App\PageCache\FormKey as CookieFormKey;
use Magento\Framework\Data\Form\FormKey as DataFormKey;
use Magento\Framework\Event\Observer;
use Magento\PageCache\Observer\FlushFormKey;
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_MockObject as MockObject;

class CustomerFlushFormKeyTest extends TestCase
{
/**
* @var CookieFormKey | MockObject
*/
private $cookieFormKey;

/**
* @var Session | MockObject
*/
private $customerSession;

/**
* @var DataFormKey | MockObject
*/
private $dataFormKey;

protected function setUp()
{

/** @var CookieFormKey | MockObject */
$this->cookieFormKey = $this->getMockBuilder(CookieFormKey::class)
->disableOriginalConstructor()
->getMock();

/** @var DataFormKey | MockObject */
$this->dataFormKey = $this->getMockBuilder(DataFormKey::class)
->disableOriginalConstructor()
->getMock();

/** @var Session | MockObject */
$this->customerSession = $this->getMockBuilder(Session::class)
->disableOriginalConstructor()
->setMethods(['getBeforeRequestParams', 'setBeforeRequestParams'])
->getMock();
}

/**
* @dataProvider aroundFlushFormKeyProvider
* @param $beforeFormKey
* @param $currentFormKey
* @param $getFormKeyTimes
* @param $setBeforeParamsTimes
*/
public function testAroundFlushFormKey(
$beforeFormKey,
$currentFormKey,
$getFormKeyTimes,
$setBeforeParamsTimes
) {
$observerDto = new Observer();
$observer = new FlushFormKey($this->cookieFormKey, $this->dataFormKey);
$plugin = new CustomerFlushFormKey($this->customerSession, $this->dataFormKey);

$beforeParams['form_key'] = $beforeFormKey;

$this->dataFormKey->expects($this->exactly($getFormKeyTimes))
->method('getFormKey')
->willReturn($currentFormKey);

$this->customerSession->expects($this->once())
->method('getBeforeRequestParams')
->willReturn($beforeParams);

$this->customerSession->expects($this->exactly($setBeforeParamsTimes))
->method('setBeforeRequestParams')
->with($beforeParams);

$proceed = function ($observerDto) use ($observer) {
return $observer->execute($observerDto);
};

$plugin->aroundExecute($observer, $proceed, $observerDto);
}

/**
* Data provider for testAroundFlushFormKey
*
* @return array
*/
public function aroundFlushFormKeyProvider()
{
return [
['form_key_value', 'form_key_value', 2, 1],
['form_old_key_value', 'form_key_value', 1, 0],
[null, 'form_key_value', 1, 0]
];
}
}
3 changes: 3 additions & 0 deletions app/code/Magento/Customer/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,9 @@
<type name="Magento\Framework\App\Action\AbstractAction">
<plugin name="customerNotification" type="Magento\Customer\Model\Plugin\CustomerNotification"/>
</type>
<type name="Magento\PageCache\Observer\FlushFormKey">
<plugin name="customerFlushFormKey" type="Magento\Customer\Model\Plugin\CustomerFlushFormKey"/>
</type>
<type name="Magento\Customer\Model\Customer\NotificationStorage">
<arguments>
<argument name="cache" xsi:type="object">Magento\Customer\Model\Cache\Type\Notification</argument>
Expand Down
76 changes: 31 additions & 45 deletions app/code/Magento/Deploy/Model/Mode.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use Magento\Deploy\App\Mode\ConfigProvider;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Console\MaintenanceModeEnabler;
use Magento\Framework\App\DeploymentConfig\Reader;
use Magento\Framework\App\DeploymentConfig\Writer;
use Magento\Framework\App\Filesystem\DirectoryList;
Expand Down Expand Up @@ -49,11 +50,6 @@ class Mode
*/
private $reader;

/**
* @var MaintenanceMode
*/
private $maintenanceMode;

/**
* @var Filesystem
*/
Expand All @@ -78,16 +74,24 @@ class Mode
*/
private $emulatedAreaProcessor;

/**
* @var MaintenanceModeEnabler
*/
private $maintenanceModeEnabler;

/**
* @param InputInterface $input
* @param OutputInterface $output
* @param Writer $writer
* @param Reader $reader
* @param MaintenanceMode $maintenanceMode
* @param MaintenanceMode $maintenanceMode deprecated, use $maintenanceModeEnabler instead
* @param Filesystem $filesystem
* @param ConfigProvider $configProvider
* @param ProcessorFacadeFactory $processorFacadeFactory
* @param EmulatedAdminhtmlAreaProcessor $emulatedAreaProcessor
* @param MaintenanceModeEnabler $maintenanceModeEnabler
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function __construct(
InputInterface $input,
Expand All @@ -98,13 +102,13 @@ public function __construct(
Filesystem $filesystem,
ConfigProvider $configProvider = null,
ProcessorFacadeFactory $processorFacadeFactory = null,
EmulatedAdminhtmlAreaProcessor $emulatedAreaProcessor = null
EmulatedAdminhtmlAreaProcessor $emulatedAreaProcessor = null,
MaintenanceModeEnabler $maintenanceModeEnabler = null
) {
$this->input = $input;
$this->output = $output;
$this->writer = $writer;
$this->reader = $reader;
$this->maintenanceMode = $maintenanceMode;
$this->filesystem = $filesystem;

$this->configProvider =
Expand All @@ -113,6 +117,8 @@ public function __construct(
$processorFacadeFactory ?: ObjectManager::getInstance()->get(ProcessorFacadeFactory::class);
$this->emulatedAreaProcessor =
$emulatedAreaProcessor ?: ObjectManager::getInstance()->get(EmulatedAdminhtmlAreaProcessor::class);
$this->maintenanceModeEnabler =
$maintenanceModeEnabler ?: ObjectManager::getInstance()->get(MaintenanceModeEnabler::class);
}

/**
Expand All @@ -123,19 +129,23 @@ public function __construct(
*/
public function enableProductionMode()
{
$this->enableMaintenanceMode($this->output);
$previousMode = $this->getMode();
try {
// We have to turn on production mode before generation.
// We need this to enable generation of the "min" files.
$this->setStoreMode(State::MODE_PRODUCTION);
$this->filesystem->regenerateStatic($this->output);
} catch (LocalizedException $e) {
// We have to return store mode to previous state in case of error.
$this->setStoreMode($previousMode);
throw $e;
}
$this->disableMaintenanceMode($this->output);
$this->maintenanceModeEnabler->executeInMaintenanceMode(
function () {
$previousMode = $this->getMode();
try {
// We have to turn on production mode before generation.
// We need this to enable generation of the "min" files.
$this->setStoreMode(State::MODE_PRODUCTION);
$this->filesystem->regenerateStatic($this->output);
} catch (LocalizedException $e) {
// We have to return store mode to previous state in case of error.
$this->setStoreMode($previousMode);
throw $e;
}
},
$this->output,
false
);
}

/**
Expand Down Expand Up @@ -218,28 +228,4 @@ private function saveAppConfigs($mode)
$this->output->writeln('Config "' . $path . ' = ' . $item['value'] . '" has been saved.');
}
}

/**
* Enable maintenance mode
*
* @param OutputInterface $output
* @return void
*/
protected function enableMaintenanceMode(OutputInterface $output)
{
$this->maintenanceMode->set(true);
$output->writeln('Enabled maintenance mode');
}

/**
* Disable maintenance mode
*
* @param OutputInterface $output
* @return void
*/
protected function disableMaintenanceMode(OutputInterface $output)
{
$this->maintenanceMode->set(false);
$output->writeln('Disabled maintenance mode');
}
}
4 changes: 3 additions & 1 deletion app/code/Magento/Deploy/Test/Unit/Model/ModeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Magento\Deploy\Model\Filesystem;
use Magento\Deploy\Model\Mode;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Console\MaintenanceModeEnabler;
use Magento\Framework\App\DeploymentConfig\Reader;
use Magento\Framework\App\DeploymentConfig\Writer;
use Magento\Framework\App\MaintenanceMode;
Expand Down Expand Up @@ -124,7 +125,8 @@ protected function setUp()
$this->filesystemMock,
$this->configProvider,
$this->processorFacadeFactory,
$this->emulatedAreaProcessor
$this->emulatedAreaProcessor,
new MaintenanceModeEnabler($this->maintenanceMock)
);
}

Expand Down
Loading

0 comments on commit 7e107e2

Please sign in to comment.