Skip to content

Commit

Permalink
Fixed static tests and backported unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Volodymyr Kublytskyi committed Nov 28, 2017
1 parent 5368727 commit f79d97e
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

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

class CustomerFlushFormKey
Expand Down Expand Up @@ -36,13 +37,15 @@ public function __construct(Session $session, DataFormKey $dataFormKey)
/**
* @param FlushFormKey $subject
* @param callable $proceed
* @param array $args
* @param Observer $observer
* @return void
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function aroundExecute(FlushFormKey $subject, callable $proceed, ...$args)
public function aroundExecute(FlushFormKey $subject, callable $proceed, Observer $observer)
{
$currentFormKey = $this->dataFormKey->getFormKey();
$proceed(...$args);
$proceed($observer);
$beforeParams = $this->session->getBeforeRequestParams();
if ($beforeParams['form_key'] == $currentFormKey) {
$beforeParams['form_key'] = $this->dataFormKey->getFormKey();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php
/**
* Copyright © 2013-2017 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 as 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 [
'valid form key' => ['form_key_value', 'form_key_value', 2, 1],
'corrupted form key' => ['form_old_key_value', 'form_key_value', 1, 0],
'missed form key' => [null, 'form_key_value', 1, 0]
];
}
}

0 comments on commit f79d97e

Please sign in to comment.