Skip to content

Commit 60abc87

Browse files
committed
MAGETWO-96522: Fix action behavior
1 parent d9259f4 commit 60abc87

File tree

9 files changed

+111
-26
lines changed

9 files changed

+111
-26
lines changed

app/code/Magento/Checkout/Controller/Cart/CouponPost.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ public function execute()
7272
if (!$this->getRequest()->isPost()) {
7373
throw new \Magento\Framework\Exception\NotFoundException(__('Page not found.'));
7474
}
75+
if (!$this->_formKeyValidator->validate($this->getRequest())) {
76+
return $this->_goBack();
77+
}
7578

7679
$couponCode = $this->getRequest()->getParam('remove') == 1
7780
? ''

app/code/Magento/Checkout/Test/Unit/Controller/Cart/CouponPostTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\Checkout\Test\Unit\Controller\Cart;
77

88
use Magento\Checkout\Controller\Cart\Index;
9+
use Magento\Framework\Data\Form\FormKey\Validator;
910

1011
/**
1112
* Test for \Magento\Checkout\Controller\Cart\CouponPost
@@ -84,6 +85,11 @@ class CouponPostTest extends \PHPUnit_Framework_TestCase
8485
*/
8586
private $redirectFactory;
8687

88+
/**
89+
* @var Validator|\PHPUnit_Framework_MockObject_MockObject
90+
*/
91+
private $formKeyValidatorMock;
92+
8793
/**
8894
* @var \PHPUnit_Framework_MockObject_MockObject
8995
*/
@@ -166,6 +172,8 @@ protected function setUp()
166172
->getMock();
167173
$this->quoteRepository = $this->getMock(\Magento\Quote\Api\CartRepositoryInterface::class);
168174
$this->shippingAddress = $this->getMock(\Magento\Quote\Model\Quote\Address::class, [], [], '', false);
175+
$this->formKeyValidatorMock = $this->getMock(Validator::class, [], [], '', false);
176+
$this->formKeyValidatorMock->expects($this->once())->method('validate')->willReturn(true);
169177

170178
$objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
171179

@@ -176,7 +184,8 @@ protected function setUp()
176184
'checkoutSession' => $this->checkoutSession,
177185
'cart' => $this->cart,
178186
'couponFactory' => $this->couponFactory,
179-
'quoteRepository' => $this->quoteRepository
187+
'quoteRepository' => $this->quoteRepository,
188+
'formKeyValidator' => $this->formKeyValidatorMock,
180189
]
181190
);
182191
}

app/code/Magento/Checkout/view/frontend/templates/cart/coupon.phtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
</div>
2929
</div>
3030
<div class="actions-toolbar">
31+
<?php echo $block->getBlockHtml('formkey')?>
3132
<?php if (!strlen($block->getCouponCode())): ?>
3233
<div class="primary">
3334
<button class="action apply primary" type="button" value="<?php /* @escapeNotVerified */ echo __('Apply Discount') ?>">

app/code/Magento/Customer/Controller/Account/ForgotPasswordPost.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use Magento\Customer\Model\AccountManagement;
1111
use Magento\Customer\Model\Session;
1212
use Magento\Framework\App\Action\Context;
13+
use Magento\Framework\App\ObjectManager;
14+
use Magento\Framework\Data\Form\FormKey\Validator;
1315
use Magento\Framework\Escaper;
1416
use Magento\Framework\Exception\NoSuchEntityException;
1517
use Magento\Framework\Exception\SecurityViolationException;
@@ -31,33 +33,50 @@ class ForgotPasswordPost extends \Magento\Customer\Controller\AbstractAccount
3133
*/
3234
protected $session;
3335

36+
/**
37+
* @var Validator
38+
*/
39+
private $formKeyValidator;
40+
3441
/**
3542
* @param Context $context
3643
* @param Session $customerSession
3744
* @param AccountManagementInterface $customerAccountManagement
3845
* @param Escaper $escaper
46+
* @param Validator|null $formKeyValidator
3947
*/
4048
public function __construct(
4149
Context $context,
4250
Session $customerSession,
4351
AccountManagementInterface $customerAccountManagement,
44-
Escaper $escaper
52+
Escaper $escaper,
53+
Validator $formKeyValidator = null
4554
) {
4655
$this->session = $customerSession;
4756
$this->customerAccountManagement = $customerAccountManagement;
4857
$this->escaper = $escaper;
58+
$this->formKeyValidator = $formKeyValidator ?: ObjectManager::getInstance()->get(Validator::class);
4959
parent::__construct($context);
5060
}
5161

5262
/**
5363
* Forgot customer password action
5464
*
5565
* @return \Magento\Framework\Controller\Result\Redirect
66+
* @throws \Magento\Framework\Exception\NotFoundException
5667
*/
5768
public function execute()
5869
{
5970
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
6071
$resultRedirect = $this->resultRedirectFactory->create();
72+
73+
if (!$this->getRequest()->isPost()) {
74+
throw new \Magento\Framework\Exception\NotFoundException(__('Page not found.'));
75+
}
76+
if (!$this->formKeyValidator->validate($this->getRequest())) {
77+
return $resultRedirect->setPath('*/*/forgotpassword');
78+
}
79+
6180
$email = (string)$this->getRequest()->getPost('email');
6281
if ($email) {
6382
$validator = new \Zend\Validator\EmailAddress();

app/code/Magento/Customer/Test/Unit/Controller/Account/ForgotPasswordPostTest.php

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Framework\App\Request\Http as Request;
1414
use Magento\Framework\Controller\Result\Redirect as ResultRedirect;
1515
use Magento\Framework\Controller\Result\RedirectFactory as ResultRedirectFactory;
16+
use Magento\Framework\Data\Form\FormKey\Validator;
1617
use Magento\Framework\Escaper;
1718
use Magento\Framework\Exception\NoSuchEntityException;
1819
use Magento\Framework\Message\ManagerInterface;
@@ -67,26 +68,42 @@ class ForgotPasswordPostTest extends \PHPUnit_Framework_TestCase
6768
*/
6869
protected $messageManager;
6970

71+
/**
72+
* @var Validator|\PHPUnit_Framework_MockObject_MockObject
73+
*/
74+
private $formKeyValidatorMock;
75+
7076
protected function setUp()
7177
{
7278
$this->prepareContext();
7379

74-
$this->session = $this->getMockBuilder('Magento\Customer\Model\Session')
80+
$this->session = $this->getMockBuilder(\Magento\Customer\Model\Session::class)
7581
->disableOriginalConstructor()
7682
->getMock();
7783

78-
$this->accountManagement = $this->getMockBuilder('Magento\Customer\Api\AccountManagementInterface')
84+
$this->accountManagement = $this->getMockBuilder(\Magento\Customer\Api\AccountManagementInterface::class)
7985
->getMockForAbstractClass();
8086

81-
$this->escaper = $this->getMockBuilder('Magento\Framework\Escaper')
87+
$this->escaper = $this->getMockBuilder(\Magento\Framework\Escaper::class)
8288
->disableOriginalConstructor()
8389
->getMock();
90+
$this->formKeyValidatorMock = $this->getMockBuilder(Validator::class)
91+
->disableOriginalConstructor()
92+
->setMethods(['validate'])
93+
->getMock();
94+
95+
$this->request->expects($this->once())->method('isPost')->willReturn(true);
96+
$this->formKeyValidatorMock->expects($this->once())
97+
->method('validate')
98+
->with($this->request)
99+
->willReturn(true);
84100

85101
$this->controller = new ForgotPasswordPost(
86102
$this->context,
87103
$this->session,
88104
$this->accountManagement,
89-
$this->escaper
105+
$this->escaper,
106+
$this->formKeyValidatorMock
90107
);
91108
}
92109

@@ -212,26 +229,24 @@ public function testExecuteException()
212229

213230
protected function prepareContext()
214231
{
215-
$this->resultRedirect = $this->getMockBuilder('Magento\Framework\Controller\Result\Redirect')
232+
$this->resultRedirect = $this->getMockBuilder(\Magento\Framework\Controller\Result\Redirect::class)
216233
->disableOriginalConstructor()
217234
->getMock();
218235

219-
$this->resultRedirectFactory = $this->getMockBuilder('Magento\Framework\Controller\Result\RedirectFactory')
236+
$this->resultRedirectFactory = $this->getMockBuilder(\Magento\Framework\Controller\Result\RedirectFactory::class)
220237
->disableOriginalConstructor()
221238
->getMock();
222239

223-
$this->context = $this->getMockBuilder('Magento\Framework\App\Action\Context')
240+
$this->context = $this->getMockBuilder(\Magento\Framework\App\Action\Context::class)
224241
->disableOriginalConstructor()
225242
->getMock();
226243

227-
$this->request = $this->getMockBuilder('Magento\Framework\App\Request\Http')
244+
$this->request = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
228245
->disableOriginalConstructor()
229-
->setMethods([
230-
'getPost',
231-
])
246+
->setMethods(['getPost', 'isPost'])
232247
->getMock();
233248

234-
$this->messageManager = $this->getMockBuilder('Magento\Framework\Message\ManagerInterface')
249+
$this->messageManager = $this->getMockBuilder(\Magento\Framework\Message\ManagerInterface::class)
235250
->getMockForAbstractClass();
236251

237252
$this->resultRedirectFactory->expects($this->any())

app/code/Magento/Customer/view/frontend/templates/form/forgotpassword.phtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<?php echo $block->getChildHtml('form_additional_info'); ?>
2626
</fieldset>
2727
<div class="actions-toolbar">
28+
<?php echo $block->getBlockHtml('formkey')?>
2829
<div class="primary">
2930
<button type="submit" class="action submit primary"><span><?php echo $block->escapeHtml(__('Reset My Password')) ?></span></button>
3031
</div>

app/code/Magento/Sales/Controller/Guest/View.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
namespace Magento\Sales\Controller\Guest;
77

88
use Magento\Framework\App\Action;
9+
use Magento\Framework\App\ObjectManager;
10+
use Magento\Framework\Data\Form\FormKey\Validator;
911
use Magento\Sales\Helper\Guest as GuestHelper;
1012
use Magento\Framework\View\Result\PageFactory;
1113
use Magento\Framework\Controller\ResultInterface;
@@ -22,26 +24,42 @@ class View extends Action\Action
2224
*/
2325
protected $resultPageFactory;
2426

27+
/**
28+
* @var Validator
29+
*/
30+
private $formKeyValidator;
31+
2532
/**
2633
* @param \Magento\Framework\App\Action\Context $context
27-
* @param \Magento\Sales\Helper\Guest $guestHelper
28-
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
34+
* @param GuestHelper $guestHelper
35+
* @param PageFactory $resultPageFactory
36+
* @param Validator|null $formKeyValidator
2937
*/
3038
public function __construct(
3139
Action\Context $context,
3240
GuestHelper $guestHelper,
33-
PageFactory $resultPageFactory
41+
PageFactory $resultPageFactory,
42+
Validator $formKeyValidator = null
3443
) {
3544
$this->guestHelper = $guestHelper;
3645
$this->resultPageFactory = $resultPageFactory;
46+
$this->formKeyValidator = $formKeyValidator ?: ObjectManager::getInstance()->get(Validator::class);
3747
parent::__construct($context);
3848
}
3949

4050
/**
4151
* @return \Magento\Framework\Controller\ResultInterface
52+
* @throws \Magento\Framework\Exception\NotFoundException
4253
*/
4354
public function execute()
4455
{
56+
if (!$this->getRequest()->isPost()) {
57+
throw new \Magento\Framework\Exception\NotFoundException(__('Page not found.'));
58+
}
59+
if (!$this->formKeyValidator->validate($this->getRequest())) {
60+
return $this->resultRedirectFactory->create()->setPath('*/*/form/');
61+
}
62+
4563
$result = $this->guestHelper->loadValidOrder($this->getRequest());
4664
if ($result instanceof ResultInterface) {
4765
return $result;

app/code/Magento/Sales/Test/Unit/Controller/Guest/ViewTest.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Sales\Test\Unit\Controller\Guest;
77

8+
use Magento\Framework\Data\Form\FormKey\Validator;
89
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
910

1011
class ViewTest extends \PHPUnit_Framework_TestCase
@@ -49,40 +50,57 @@ class ViewTest extends \PHPUnit_Framework_TestCase
4950
*/
5051
protected $resultPageMock;
5152

53+
/**
54+
* @var Validator|\PHPUnit_Framework_MockObject_MockObject
55+
*/
56+
private $formKeyValidatorMock;
57+
5258
/**
5359
* @return void
5460
*/
5561
protected function setUp()
5662
{
57-
$this->requestMock = $this->getMockBuilder('Magento\Framework\App\RequestInterface')
58-
->getMock();
59-
$this->guestHelperMock = $this->getMockBuilder('Magento\Sales\Helper\Guest')
63+
$this->requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
64+
->setMethods(['isPost'])
65+
->getMockForAbstractClass();
66+
$this->guestHelperMock = $this->getMockBuilder(\Magento\Sales\Helper\Guest::class)
6067
->disableOriginalConstructor()
6168
->getMock();
62-
$this->resultRedirectMock = $this->getMockBuilder('Magento\Framework\Controller\Result\Redirect')
69+
$this->resultRedirectMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Redirect::class)
6370
->disableOriginalConstructor()
6471
->getMock();
65-
$this->resultPageFactoryMock = $this->getMockBuilder('Magento\Framework\View\Result\PageFactory')
72+
$this->resultPageFactoryMock = $this->getMockBuilder(\Magento\Framework\View\Result\PageFactory::class)
6673
->disableOriginalConstructor()
6774
->setMethods(['create'])
6875
->getMock();
69-
$this->resultPageMock = $this->getMockBuilder('Magento\Framework\View\Result\Page')
76+
$this->resultPageMock = $this->getMockBuilder(\Magento\Framework\View\Result\Page::class)
7077
->disableOriginalConstructor()
7178
->getMock();
79+
$this->formKeyValidatorMock = $this->getMockBuilder(Validator::class)
80+
->disableOriginalConstructor()
81+
->setMethods(['validate'])
82+
->getMock();
83+
84+
$this->requestMock->expects($this->once())->method('isPost')->willReturn(true);
85+
$this->formKeyValidatorMock->expects($this->once())
86+
->method('validate')
87+
->with($this->requestMock)
88+
->willReturn(true);
7289

7390
$this->objectManagerHelper = new ObjectManagerHelper($this);
7491
$this->context = $this->objectManagerHelper->getObject(
75-
'Magento\Framework\App\Action\Context',
92+
\Magento\Framework\App\Action\Context::class,
7693
[
7794
'request' => $this->requestMock
7895
]
7996
);
8097
$this->viewController = $this->objectManagerHelper->getObject(
81-
'Magento\Sales\Controller\Guest\View',
98+
\Magento\Sales\Controller\Guest\View::class,
8299
[
83100
'context' => $this->context,
84101
'guestHelper' => $this->guestHelperMock,
85-
'resultPageFactory' => $this->resultPageFactoryMock
102+
'resultPageFactory' => $this->resultPageFactoryMock,
103+
'formKeyValidator' => $this->formKeyValidatorMock,
86104
]
87105
);
88106
}

app/code/Magento/Sales/view/frontend/templates/guest/form.phtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
</div>
5757
</fieldset>
5858
<div class="actions-toolbar">
59+
<?php echo $block->getBlockHtml('formkey')?>
5960
<div class="primary">
6061
<button type="submit" title="<?php /* @escapeNotVerified */ echo __('Continue') ?>" class="action submit primary">
6162
<span><?php /* @escapeNotVerified */ echo __('Continue') ?></span>

0 commit comments

Comments
 (0)