Skip to content

Commit 092f01f

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-52338' into BUGS
2 parents eb4e0c9 + 3fc2286 commit 092f01f

File tree

4 files changed

+212
-7
lines changed

4 files changed

+212
-7
lines changed

app/code/Magento/Customer/Controller/Address/Delete.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function execute()
1515
{
1616
$addressId = $this->getRequest()->getParam('id', false);
1717

18-
if ($addressId) {
18+
if ($addressId && $this->_formKeyValidator->validate($this->getRequest())) {
1919
try {
2020
$address = $this->_addressRepository->getById($addressId);
2121
if ($address->getCustomerId() === $this->_getSession()->getCustomerId()) {
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Customer\Test\Unit\Controller\Address;
7+
8+
use Magento\Customer\Controller\Address\Delete;
9+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
10+
11+
/**
12+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
13+
*/
14+
class DeleteTest extends \PHPUnit_Framework_TestCase
15+
{
16+
/** @var Delete */
17+
protected $model;
18+
19+
/** @var \Magento\Framework\App\Action\Context */
20+
protected $context;
21+
22+
/** @var \Magento\Customer\Model\Session|\PHPUnit_Framework_MockObject_MockObject */
23+
protected $sessionMock;
24+
25+
/** @var \Magento\Framework\Data\Form\FormKey\Validator|\PHPUnit_Framework_MockObject_MockObject */
26+
protected $validatorMock;
27+
28+
/** @var \Magento\Customer\Api\AddressRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */
29+
protected $addressRepositoryMock;
30+
31+
/** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
32+
protected $request;
33+
34+
/** @var \Magento\Customer\Api\Data\AddressInterface|\PHPUnit_Framework_MockObject_MockObject */
35+
protected $address;
36+
37+
/** @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
38+
protected $messageManager;
39+
40+
/** @var \Magento\Framework\Controller\Result\RedirectFactory|\PHPUnit_Framework_MockObject_MockObject */
41+
protected $resultRedirectFactory;
42+
43+
/** @var \Magento\Framework\Controller\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject */
44+
protected $resultRedirect;
45+
46+
protected function setUp()
47+
{
48+
$this->sessionMock = $this->getMockBuilder('Magento\Customer\Model\Session')
49+
->disableOriginalConstructor()
50+
->getMock();
51+
$this->validatorMock = $this->getMockBuilder('Magento\Framework\Data\Form\FormKey\Validator')
52+
->disableOriginalConstructor()
53+
->getMock();
54+
$formFactoryMock = $this->getMockBuilder('Magento\Customer\Model\Metadata\FormFactory')
55+
->disableOriginalConstructor()
56+
->getMock();
57+
$this->addressRepositoryMock = $this->getMockBuilder('Magento\Customer\Api\AddressRepositoryInterface')
58+
->getMockForAbstractClass();
59+
$addressInterfaceFactoryMock = $this->getMockBuilder('Magento\Customer\Api\Data\AddressInterfaceFactory')
60+
->disableOriginalConstructor()
61+
->setMethods(['create'])
62+
->getMock();
63+
$regionInterfaceFactoryMock = $this->getMockBuilder('Magento\Customer\Api\Data\RegionInterfaceFactory')
64+
->disableOriginalConstructor()
65+
->setMethods(['create'])
66+
->getMock();
67+
$dataObjectProcessorMock = $this->getMockBuilder('Magento\Framework\Reflection\DataObjectProcessor')
68+
->disableOriginalConstructor()
69+
->getMock();
70+
$dataObjectHelperMock = $this->getMockBuilder('Magento\Framework\Api\DataObjectHelper')
71+
->disableOriginalConstructor()
72+
->getMock();
73+
$forwardFactoryMock = $this->getMockBuilder('Magento\Framework\Controller\Result\ForwardFactory')
74+
->disableOriginalConstructor()
75+
->setMethods(['create'])
76+
->getMock();
77+
$pageFactoryMock = $this->getMockBuilder('Magento\Framework\View\Result\PageFactory')
78+
->disableOriginalConstructor()
79+
->getMock();
80+
$this->request = $this->getMockBuilder('Magento\Framework\App\RequestInterface')
81+
->getMockForAbstractClass();
82+
$this->address = $this->getMockBuilder('Magento\Customer\Api\Data\AddressInterface')
83+
->getMockForAbstractClass();
84+
$this->messageManager = $this->getMockBuilder('Magento\Framework\Message\ManagerInterface')
85+
->getMockForAbstractClass();
86+
$this->resultRedirectFactory = $this->getMockBuilder('Magento\Framework\Controller\Result\RedirectFactory')
87+
->disableOriginalConstructor()
88+
->getMock();
89+
$this->resultRedirect = $this->getMockBuilder('Magento\Framework\Controller\Result\Redirect')
90+
->disableOriginalConstructor()
91+
->getMock();
92+
93+
$objectManager = new ObjectManagerHelper($this);
94+
$this->context = $objectManager->getObject(
95+
'Magento\Framework\App\Action\Context',
96+
[
97+
'request' => $this->request,
98+
'messageManager' => $this->messageManager,
99+
'resultRedirectFactory' => $this->resultRedirectFactory,
100+
]
101+
);
102+
103+
$this->model = new Delete(
104+
$this->context,
105+
$this->sessionMock,
106+
$this->validatorMock,
107+
$formFactoryMock,
108+
$this->addressRepositoryMock,
109+
$addressInterfaceFactoryMock,
110+
$regionInterfaceFactoryMock,
111+
$dataObjectProcessorMock,
112+
$dataObjectHelperMock,
113+
$forwardFactoryMock,
114+
$pageFactoryMock
115+
);
116+
}
117+
118+
public function testExecute()
119+
{
120+
$addressId = 1;
121+
$customerId = 2;
122+
123+
$this->resultRedirectFactory->expects($this->once())
124+
->method('create')
125+
->willReturn($this->resultRedirect);
126+
$this->request->expects($this->once())
127+
->method('getParam')
128+
->with('id', false)
129+
->willReturn($addressId);
130+
$this->validatorMock->expects($this->once())
131+
->method('validate')
132+
->with($this->request)
133+
->willReturn(true);
134+
$this->addressRepositoryMock->expects($this->once())
135+
->method('getById')
136+
->with($addressId)
137+
->willReturn($this->address);
138+
$this->sessionMock->expects($this->once())
139+
->method('getCustomerId')
140+
->willReturn($customerId);
141+
$this->address->expects($this->once())
142+
->method('getCustomerId')
143+
->willReturn($customerId);
144+
$this->addressRepositoryMock->expects($this->once())
145+
->method('deleteById')
146+
->with($addressId);
147+
$this->messageManager->expects($this->once())
148+
->method('addSuccess')
149+
->with(__('You deleted the address.'));
150+
$this->resultRedirect->expects($this->once())
151+
->method('setPath')
152+
->with('*/*/index')
153+
->willReturnSelf();
154+
$this->assertSame($this->resultRedirect, $this->model->execute());
155+
}
156+
157+
public function testExecuteWithException()
158+
{
159+
$addressId = 1;
160+
$customerId = 2;
161+
162+
$this->resultRedirectFactory->expects($this->once())
163+
->method('create')
164+
->willReturn($this->resultRedirect);
165+
$this->request->expects($this->once())
166+
->method('getParam')
167+
->with('id', false)
168+
->willReturn($addressId);
169+
$this->validatorMock->expects($this->once())
170+
->method('validate')
171+
->with($this->request)
172+
->willReturn(true);
173+
$this->addressRepositoryMock->expects($this->once())
174+
->method('getById')
175+
->with($addressId)
176+
->willReturn($this->address);
177+
$this->sessionMock->expects($this->once())
178+
->method('getCustomerId')
179+
->willReturn($customerId);
180+
$this->address->expects($this->once())
181+
->method('getCustomerId')
182+
->willReturn(34);
183+
$exception = new \Exception('Exception');
184+
$this->messageManager->expects($this->once())
185+
->method('addError')
186+
->with(__('We can\'t delete the address right now.'))
187+
->willThrowException($exception);
188+
$this->messageManager->expects($this->once())
189+
->method('addException')
190+
->with($exception, __('We can\'t delete the address right now.'));
191+
$this->resultRedirect->expects($this->once())
192+
->method('setPath')
193+
->with('*/*/index')
194+
->willReturnSelf();
195+
$this->assertSame($this->resultRedirect, $this->model->execute());
196+
}
197+
}

app/code/Magento/Customer/view/frontend/web/address.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,12 @@ define([
6161
actions: {
6262
confirm: function() {
6363
if (typeof $(e.target).parent().data('address') !== 'undefined') {
64-
window.location = self.options.deleteUrlPrefix + $(e.target).parent().data('address');
64+
window.location = self.options.deleteUrlPrefix + $(e.target).parent().data('address')
65+
+ '/form_key/' + $.mage.cookies.get('form_key');
6566
}
6667
else {
67-
window.location = self.options.deleteUrlPrefix + $(e.target).data('address');
68+
window.location = self.options.deleteUrlPrefix + $(e.target).data('address')
69+
+ '/form_key/' + $.mage.cookies.get('form_key');
6870
}
6971
}
7072
}

dev/tests/integration/testsuite/Magento/Customer/Controller/AddressTest.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@
55
*/
66
namespace Magento\Customer\Controller;
77

8+
use Magento\Customer\Api\AccountManagementInterface;
9+
use Magento\Framework\Data\Form\FormKey;
810
use Magento\TestFramework\Helper\Bootstrap;
911

1012
class AddressTest extends \Magento\TestFramework\TestCase\AbstractController
1113
{
12-
/** @var \Magento\Customer\Api\AccountManagementInterface */
14+
/** @var AccountManagementInterface */
1315
private $accountManagement;
1416

17+
/** @var FormKey */
18+
private $formKey;
19+
1520
protected function setUp()
1621
{
1722
parent::setUp();
@@ -20,9 +25,8 @@ protected function setUp()
2025
'Magento\Customer\Model\Session',
2126
[$logger]
2227
);
23-
$this->accountManagement = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
24-
'Magento\Customer\Api\AccountManagementInterface'
25-
);
28+
$this->accountManagement = Bootstrap::getObjectManager()->create(AccountManagementInterface::class);
29+
$this->formKey = Bootstrap::getObjectManager()->create(FormKey::class);
2630
$customer = $this->accountManagement->authenticate('customer@example.com', 'password');
2731
$session->setCustomerDataAsLoggedIn($customer);
2832
}
@@ -152,6 +156,7 @@ public function testFailedFormPostAction()
152156
public function testDeleteAction()
153157
{
154158
$this->getRequest()->setParam('id', 1);
159+
$this->getRequest()->setParam('form_key', $this->formKey->getFormKey());
155160
// we are overwriting the address coming from the fixture
156161
$this->dispatch('customer/address/delete');
157162

@@ -169,6 +174,7 @@ public function testDeleteAction()
169174
public function testWrongAddressDeleteAction()
170175
{
171176
$this->getRequest()->setParam('id', 555);
177+
$this->getRequest()->setParam('form_key', $this->formKey->getFormKey());
172178
// we are overwriting the address coming from the fixture
173179
$this->dispatch('customer/address/delete');
174180

0 commit comments

Comments
 (0)