Skip to content

Commit 453652a

Browse files
author
Oleksandr Karpenko
committed
Merge branch 'MAGETWO-43952' into MAGETWO-42988
2 parents a5f476a + c54495d commit 453652a

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ protected function _extractAddress()
106106
array_merge($existingAddressData, $attributeValues),
107107
'\Magento\Customer\Api\Data\AddressInterface'
108108
);
109-
110109
$addressDataObject->setCustomerId($this->_getSession()->getCustomerId())
111110
->setIsDefaultBilling($this->getRequest()->getParam('default_billing', false))
112111
->setIsDefaultShipping($this->getRequest()->getParam('default_shipping', false));
@@ -118,12 +117,16 @@ protected function _extractAddress()
118117
* Retrieve existing address data
119118
*
120119
* @return array
120+
* @throws \Exception
121121
*/
122122
protected function getExistingAddressData()
123123
{
124124
$existingAddressData = [];
125125
if ($addressId = $this->getRequest()->getParam('id')) {
126126
$existingAddress = $this->_addressRepository->getById($addressId);
127+
if ($existingAddress->getCustomerId() !== $this->_getSession()->getCustomerId()) {
128+
throw new \Exception();
129+
}
127130
$existingAddressData = $this->_dataProcessor->buildOutputDataArray(
128131
$existingAddress,
129132
'\Magento\Customer\Api\Data\AddressInterface'
@@ -175,6 +178,7 @@ protected function updateRegionData(&$attributeValues)
175178
*/
176179
public function execute()
177180
{
181+
$redirectUrl = null;
178182
if (!$this->_formKeyValidator->validate($this->getRequest())) {
179183
return $this->resultRedirectFactory->create()->setPath('*/*/');
180184
}
@@ -198,11 +202,16 @@ public function execute()
198202
$this->messageManager->addError($error->getMessage());
199203
}
200204
} catch (\Exception $e) {
205+
$redirectUrl = $this->_buildUrl('*/*/index');
201206
$this->messageManager->addException($e, __('We can\'t save the address.'));
202207
}
203208

204-
$this->_getSession()->setAddressFormData($this->getRequest()->getPostValue());
205-
$url = $this->_buildUrl('*/*/edit', ['id' => $this->getRequest()->getParam('id')]);
209+
$url = $redirectUrl;
210+
if (!$redirectUrl) {
211+
$this->_getSession()->setAddressFormData($this->getRequest()->getPostValue());
212+
$url = $this->_buildUrl('*/*/edit', ['id' => $this->getRequest()->getParam('id')]);
213+
}
214+
206215
return $this->resultRedirectFactory->create()->setUrl($this->_redirect->error($url));
207216
}
208217
}

app/code/Magento/Customer/Test/Unit/Controller/Address/FormPostTest.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,10 @@ public function testExecute(
528528
],
529529
]);
530530

531-
$this->session->expects($this->once())
531+
$this->session->expects($this->atLeastOnce())
532+
->method('getCustomerId')
533+
->willReturn($customerId);
534+
$this->addressData->expects($this->once())
532535
->method('getCustomerId')
533536
->willReturn($customerId);
534537

@@ -682,11 +685,11 @@ public function testExecuteException()
682685
$this->request->expects($this->once())
683686
->method('isPost')
684687
->willReturn(true);
685-
$this->request->expects($this->exactly(2))
688+
$this->request->expects($this->once())
686689
->method('getParam')
687690
->with('id')
688691
->willReturn($addressId);
689-
$this->request->expects($this->once())
692+
$this->request->expects($this->never())
690693
->method('getPostValue')
691694
->willReturn($postValue);
692695

@@ -701,7 +704,7 @@ public function testExecuteException()
701704
->with($exception, __('We can\'t save the address.'))
702705
->willReturnSelf();
703706

704-
$this->session->expects($this->once())
707+
$this->session->expects($this->never())
705708
->method('setAddressFormData')
706709
->with($postValue)
707710
->willReturnSelf();
@@ -710,7 +713,7 @@ public function testExecuteException()
710713
->getMockForAbstractClass();
711714
$urlBuilder->expects($this->once())
712715
->method('getUrl')
713-
->with('*/*/edit', ['id' => $addressId])
716+
->with('*/*/index')
714717
->willReturn($url);
715718

716719
$this->objectManager->expects($this->once())

0 commit comments

Comments
 (0)