Skip to content

Commit 741b937

Browse files
committed
Merge branch 'MAGETWO-97549' into 2.3-develop-pr16
2 parents 57fb7d4 + a9cc22f commit 741b937

File tree

3 files changed

+118
-34
lines changed
  • app/code/Magento/Customer
  • dev/tests/integration/testsuite/Magento/Customer/Controller/Adminhtml

3 files changed

+118
-34
lines changed

app/code/Magento/Customer/Controller/Adminhtml/Index/Save.php

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,9 @@ protected function _extractCustomerAddressData(array & $extractedCustomerData)
289289
public function execute()
290290
{
291291
$returnToEdit = false;
292-
$originalRequestData = $this->getRequest()->getPostValue();
293-
294292
$customerId = $this->getCurrentCustomerId();
295293

296-
if ($originalRequestData) {
294+
if ($this->getRequest()->getPostValue()) {
297295
try {
298296
// optional fields might be set in request for future processing by observers in other modules
299297
$customerData = $this->_extractCustomerData();
@@ -364,7 +362,7 @@ public function execute()
364362
$messages = $exception->getMessage();
365363
}
366364
$this->_addSessionErrorMessages($messages);
367-
$this->_getSession()->setCustomerFormData($originalRequestData);
365+
$this->_getSession()->setCustomerFormData($this->retrieveFormattedFormData());
368366
$returnToEdit = true;
369367
} catch (\Magento\Framework\Exception\AbstractAggregateException $exception) {
370368
$errors = $exception->getErrors();
@@ -373,18 +371,19 @@ public function execute()
373371
$messages[] = $error->getMessage();
374372
}
375373
$this->_addSessionErrorMessages($messages);
376-
$this->_getSession()->setCustomerFormData($originalRequestData);
374+
$this->_getSession()->setCustomerFormData($this->retrieveFormattedFormData());
377375
$returnToEdit = true;
378376
} catch (LocalizedException $exception) {
379377
$this->_addSessionErrorMessages($exception->getMessage());
380-
$this->_getSession()->setCustomerFormData($originalRequestData);
378+
$this->_getSession()->setCustomerFormData($this->retrieveFormattedFormData());
381379
$returnToEdit = true;
382380
} catch (\Exception $exception) {
383381
$this->messageManager->addException($exception, __('Something went wrong while saving the customer.'));
384-
$this->_getSession()->setCustomerFormData($originalRequestData);
382+
$this->_getSession()->setCustomerFormData($this->retrieveFormattedFormData());
385383
$returnToEdit = true;
386384
}
387385
}
386+
388387
$resultRedirect = $this->resultRedirectFactory->create();
389388
if ($returnToEdit) {
390389
if ($customerId) {
@@ -489,4 +488,29 @@ private function disableAddressValidation($customer)
489488
$addressModel->setShouldIgnoreValidation(true);
490489
}
491490
}
491+
492+
/**
493+
* Retrieve formatted form data
494+
*
495+
* @return array
496+
*/
497+
private function retrieveFormattedFormData(): array
498+
{
499+
$originalRequestData = $this->getRequest()->getPostValue();
500+
501+
/* Customer data filtration */
502+
if (isset($originalRequestData['customer'])) {
503+
$customerData = $this->_extractData(
504+
'adminhtml_customer',
505+
CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
506+
[],
507+
'customer'
508+
);
509+
510+
$customerData = array_intersect_key($customerData, $originalRequestData['customer']);
511+
$originalRequestData['customer'] = array_merge($originalRequestData['customer'], $customerData);
512+
}
513+
514+
return $originalRequestData;
515+
}
492516
}

app/code/Magento/Customer/Test/Unit/Controller/Adminhtml/Index/SaveTest.php

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -699,22 +699,24 @@ public function testExecuteWithNewCustomerAndValidationException()
699699
'customer' => [
700700
'coolness' => false,
701701
'disable_auto_group_change' => 'false',
702+
'dob' => '3/12/1996',
702703
],
703704
'subscription' => $subscription,
704705
];
705706
$extractedData = [
706707
'coolness' => false,
707708
'disable_auto_group_change' => 'false',
709+
'dob' => '1996-03-12',
708710
];
709711

710712
/** @var AttributeMetadataInterface|\PHPUnit_Framework_MockObject_MockObject $customerFormMock */
711713
$attributeMock = $this->getMockBuilder(
712714
\Magento\Customer\Api\Data\AttributeMetadataInterface::class
713715
)->disableOriginalConstructor()->getMock();
714-
$attributeMock->expects($this->once())
716+
$attributeMock->expects($this->exactly(2))
715717
->method('getAttributeCode')
716718
->willReturn('coolness');
717-
$attributeMock->expects($this->once())
719+
$attributeMock->expects($this->exactly(2))
718720
->method('getFrontendInput')
719721
->willReturn('int');
720722
$attributes = [$attributeMock];
@@ -737,32 +739,32 @@ public function testExecuteWithNewCustomerAndValidationException()
737739
$objectMock = $this->getMockBuilder(\Magento\Framework\DataObject::class)
738740
->disableOriginalConstructor()
739741
->getMock();
740-
$objectMock->expects($this->once())
742+
$objectMock->expects($this->exactly(2))
741743
->method('getData')
742744
->with('customer')
743745
->willReturn($postValue['customer']);
744746

745-
$this->objectFactoryMock->expects($this->once())
747+
$this->objectFactoryMock->expects($this->exactly(2))
746748
->method('create')
747749
->with(['data' => $postValue])
748750
->willReturn($objectMock);
749751

750752
$customerFormMock = $this->getMockBuilder(
751753
\Magento\Customer\Model\Metadata\Form::class
752754
)->disableOriginalConstructor()->getMock();
753-
$customerFormMock->expects($this->once())
755+
$customerFormMock->expects($this->exactly(2))
754756
->method('extractData')
755757
->with($this->requestMock, 'customer')
756758
->willReturn($extractedData);
757-
$customerFormMock->expects($this->once())
759+
$customerFormMock->expects($this->exactly(2))
758760
->method('compactData')
759761
->with($extractedData)
760762
->willReturn($extractedData);
761-
$customerFormMock->expects($this->once())
763+
$customerFormMock->expects($this->exactly(2))
762764
->method('getAttributes')
763765
->willReturn($attributes);
764766

765-
$this->formFactoryMock->expects($this->once())
767+
$this->formFactoryMock->expects($this->exactly(2))
766768
->method('create')
767769
->with(
768770
CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
@@ -810,7 +812,10 @@ public function testExecuteWithNewCustomerAndValidationException()
810812

811813
$this->sessionMock->expects($this->once())
812814
->method('setCustomerFormData')
813-
->with($postValue);
815+
->with([
816+
'customer' => $extractedData,
817+
'subscription' => $subscription,
818+
]);
814819

815820
/** @var Redirect|\PHPUnit_Framework_MockObject_MockObject $redirectMock */
816821
$redirectMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Redirect::class)
@@ -841,22 +846,24 @@ public function testExecuteWithNewCustomerAndLocalizedException()
841846
'customer' => [
842847
'coolness' => false,
843848
'disable_auto_group_change' => 'false',
849+
'dob' => '3/12/1996',
844850
],
845851
'subscription' => $subscription,
846852
];
847853
$extractedData = [
848854
'coolness' => false,
849855
'disable_auto_group_change' => 'false',
856+
'dob' => '1996-03-12',
850857
];
851858

852859
/** @var AttributeMetadataInterface|\PHPUnit_Framework_MockObject_MockObject $customerFormMock */
853860
$attributeMock = $this->getMockBuilder(
854861
\Magento\Customer\Api\Data\AttributeMetadataInterface::class
855862
)->disableOriginalConstructor()->getMock();
856-
$attributeMock->expects($this->once())
863+
$attributeMock->expects($this->exactly(2))
857864
->method('getAttributeCode')
858865
->willReturn('coolness');
859-
$attributeMock->expects($this->once())
866+
$attributeMock->expects($this->exactly(2))
860867
->method('getFrontendInput')
861868
->willReturn('int');
862869
$attributes = [$attributeMock];
@@ -879,12 +886,12 @@ public function testExecuteWithNewCustomerAndLocalizedException()
879886
$objectMock = $this->getMockBuilder(\Magento\Framework\DataObject::class)
880887
->disableOriginalConstructor()
881888
->getMock();
882-
$objectMock->expects($this->once())
889+
$objectMock->expects($this->exactly(2))
883890
->method('getData')
884891
->with('customer')
885892
->willReturn($postValue['customer']);
886893

887-
$this->objectFactoryMock->expects($this->once())
894+
$this->objectFactoryMock->expects($this->exactly(2))
888895
->method('create')
889896
->with(['data' => $postValue])
890897
->willReturn($objectMock);
@@ -893,19 +900,19 @@ public function testExecuteWithNewCustomerAndLocalizedException()
893900
$customerFormMock = $this->getMockBuilder(
894901
\Magento\Customer\Model\Metadata\Form::class
895902
)->disableOriginalConstructor()->getMock();
896-
$customerFormMock->expects($this->once())
903+
$customerFormMock->expects($this->exactly(2))
897904
->method('extractData')
898905
->with($this->requestMock, 'customer')
899906
->willReturn($extractedData);
900-
$customerFormMock->expects($this->once())
907+
$customerFormMock->expects($this->exactly(2))
901908
->method('compactData')
902909
->with($extractedData)
903910
->willReturn($extractedData);
904-
$customerFormMock->expects($this->once())
911+
$customerFormMock->expects($this->exactly(2))
905912
->method('getAttributes')
906913
->willReturn($attributes);
907914

908-
$this->formFactoryMock->expects($this->once())
915+
$this->formFactoryMock->expects($this->exactly(2))
909916
->method('create')
910917
->with(
911918
CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
@@ -952,7 +959,10 @@ public function testExecuteWithNewCustomerAndLocalizedException()
952959

953960
$this->sessionMock->expects($this->once())
954961
->method('setCustomerFormData')
955-
->with($postValue);
962+
->with([
963+
'customer' => $extractedData,
964+
'subscription' => $subscription,
965+
]);
956966

957967
/** @var Redirect|\PHPUnit_Framework_MockObject_MockObject $redirectMock */
958968
$redirectMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Redirect::class)
@@ -983,22 +993,24 @@ public function testExecuteWithNewCustomerAndException()
983993
'customer' => [
984994
'coolness' => false,
985995
'disable_auto_group_change' => 'false',
996+
'dob' => '3/12/1996',
986997
],
987998
'subscription' => $subscription,
988999
];
9891000
$extractedData = [
9901001
'coolness' => false,
9911002
'disable_auto_group_change' => 'false',
1003+
'dob' => '1996-03-12',
9921004
];
9931005

9941006
/** @var AttributeMetadataInterface|\PHPUnit_Framework_MockObject_MockObject $customerFormMock */
9951007
$attributeMock = $this->getMockBuilder(
9961008
\Magento\Customer\Api\Data\AttributeMetadataInterface::class
9971009
)->disableOriginalConstructor()->getMock();
998-
$attributeMock->expects($this->once())
1010+
$attributeMock->expects($this->exactly(2))
9991011
->method('getAttributeCode')
10001012
->willReturn('coolness');
1001-
$attributeMock->expects($this->once())
1013+
$attributeMock->expects($this->exactly(2))
10021014
->method('getFrontendInput')
10031015
->willReturn('int');
10041016
$attributes = [$attributeMock];
@@ -1021,32 +1033,32 @@ public function testExecuteWithNewCustomerAndException()
10211033
$objectMock = $this->getMockBuilder(\Magento\Framework\DataObject::class)
10221034
->disableOriginalConstructor()
10231035
->getMock();
1024-
$objectMock->expects($this->once())
1036+
$objectMock->expects($this->exactly(2))
10251037
->method('getData')
10261038
->with('customer')
10271039
->willReturn($postValue['customer']);
10281040

1029-
$this->objectFactoryMock->expects($this->once())
1041+
$this->objectFactoryMock->expects($this->exactly(2))
10301042
->method('create')
10311043
->with(['data' => $postValue])
10321044
->willReturn($objectMock);
10331045

10341046
$customerFormMock = $this->getMockBuilder(
10351047
\Magento\Customer\Model\Metadata\Form::class
10361048
)->disableOriginalConstructor()->getMock();
1037-
$customerFormMock->expects($this->once())
1049+
$customerFormMock->expects($this->exactly(2))
10381050
->method('extractData')
10391051
->with($this->requestMock, 'customer')
10401052
->willReturn($extractedData);
1041-
$customerFormMock->expects($this->once())
1053+
$customerFormMock->expects($this->exactly(2))
10421054
->method('compactData')
10431055
->with($extractedData)
10441056
->willReturn($extractedData);
1045-
$customerFormMock->expects($this->once())
1057+
$customerFormMock->expects($this->exactly(2))
10461058
->method('getAttributes')
10471059
->willReturn($attributes);
10481060

1049-
$this->formFactoryMock->expects($this->once())
1061+
$this->formFactoryMock->expects($this->exactly(2))
10501062
->method('create')
10511063
->with(
10521064
CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
@@ -1095,7 +1107,10 @@ public function testExecuteWithNewCustomerAndException()
10951107

10961108
$this->sessionMock->expects($this->once())
10971109
->method('setCustomerFormData')
1098-
->with($postValue);
1110+
->with([
1111+
'customer' => $extractedData,
1112+
'subscription' => $subscription,
1113+
]);
10991114

11001115
/** @var Redirect|\PHPUnit_Framework_MockObject_MockObject $redirectMock */
11011116
$redirectMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\Redirect::class)

dev/tests/integration/testsuite/Magento/Customer/Controller/Adminhtml/IndexTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,51 @@ public function testSaveActionCoreException()
296296
$this->assertRedirect($this->stringStartsWith($this->_baseControllerUrl . 'new/key/'));
297297
}
298298

299+
/**
300+
* @magentoDataFixture Magento/Customer/_files/customer_sample.php
301+
*/
302+
public function testSaveActionCoreExceptionFormatFormData()
303+
{
304+
$post = [
305+
'customer' => [
306+
'middlename' => 'test middlename',
307+
'website_id' => 1,
308+
'firstname' => 'test firstname',
309+
'lastname' => 'test lastname',
310+
'email' => '[email protected]',
311+
'dob' => '12/3/1996',
312+
],
313+
];
314+
$postCustomerFormatted = [
315+
'middlename' => 'test middlename',
316+
'website_id' => 1,
317+
'firstname' => 'test firstname',
318+
'lastname' => 'test lastname',
319+
'email' => '[email protected]',
320+
'dob' => '1996-12-03',
321+
];
322+
323+
$this->getRequest()->setPostValue($post)->setMethod(HttpRequest::METHOD_POST);
324+
$this->dispatch('backend/customer/index/save');
325+
/*
326+
* Check that error message is set
327+
*/
328+
$this->assertSessionMessages(
329+
$this->equalTo(['A customer with the same email address already exists in an associated website.']),
330+
\Magento\Framework\Message\MessageInterface::TYPE_ERROR
331+
);
332+
333+
$customerFormData = Bootstrap::getObjectManager()
334+
->get(\Magento\Backend\Model\Session::class)
335+
->getCustomerFormData();
336+
$this->assertEquals(
337+
$postCustomerFormatted,
338+
$customerFormData['customer'],
339+
'Customer form data should be formatted'
340+
);
341+
$this->assertRedirect($this->stringStartsWith($this->_baseControllerUrl . 'new/key/'));
342+
}
343+
299344
/**
300345
* @magentoDataFixture Magento/Customer/_files/customer_sample.php
301346
*/

0 commit comments

Comments
 (0)