Skip to content

Commit

Permalink
MAGETWO-83289: [Backport 2.3] #11825: Generate new FormKey and replac…
Browse files Browse the repository at this point in the history
…e for oldRequestParams Wishlist #12042

 - Merge Pull Request #12042 from osrecio/magento2:PR#11825_2.3
 - Merged commits:
   1. 247cfe6
   2. 3b442ad
   3. cc1602f
  • Loading branch information
Volodymyr Kublytskyi committed Dec 1, 2017
2 parents 5aaaeff + cc1602f commit 5f36d85
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 0 deletions.
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

0 comments on commit 5f36d85

Please sign in to comment.