Skip to content

Commit d6f8b63

Browse files
committed
MAGETWO-48286: "Create/Edit Customer" Admin forms do not support data validation without refreshing
1 parent 798a842 commit d6f8b63

File tree

7 files changed

+254
-93
lines changed

7 files changed

+254
-93
lines changed

app/code/Magento/Customer/Block/Adminhtml/Edit/Tab/Newsletter.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public function initForm()
148148

149149
$fieldset = $form->addFieldset('base_fieldset', ['legend' => __('Newsletter Information')]);
150150

151-
$fieldset->addField(
151+
$subscriptionField = $fieldset->addField(
152152
'subscription',
153153
'checkbox',
154154
[
@@ -160,11 +160,19 @@ public function initForm()
160160
);
161161

162162
if ($this->customerAccountManagement->isReadOnly($customerId)) {
163-
$form->getElement('subscription')->setReadonly(true, true);
163+
$subscriptionField->setReadonly(true, true);
164164
}
165165
$isSubscribed = $subscriber->isSubscribed();
166166
$form->setValues(['subscription' => $isSubscribed ? 'true' : 'false']);
167-
$form->getElement('subscription')->setIsChecked($isSubscribed);
167+
$subscriptionField->setIsChecked($isSubscribed);
168+
169+
$data = $this->_backendSession->getCustomerFormData();
170+
if (!empty($data)) {
171+
$dataCustomerId = isset($data['customer']['entity_id']) ? $data['customer']['entity_id'] : null;
172+
if (isset($data['subscription']) && $dataCustomerId == $customerId) {
173+
$subscriptionField->setIsChecked($data['subscription']);
174+
}
175+
}
168176

169177
$changedDate = $this->getStatusChangedDate();
170178
if ($changedDate) {

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

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -49,77 +49,6 @@ public function execute()
4949
}
5050
}
5151
$customerData['customer_id'] = $customerId;
52-
53-
// set entered data if was error when we do save
54-
$data = $this->_getSession()->getCustomerData(true);
55-
56-
// restore data from SESSION
57-
if ($data && (!isset(
58-
$data['customer_id']
59-
) || isset(
60-
$data['customer_id']
61-
) && $data['customer_id'] == $customerId)
62-
) {
63-
$request = clone $this->getRequest();
64-
$request->setParams($data);
65-
66-
if (isset($data['account']) && is_array($data['account'])) {
67-
$customerForm = $this->_formFactory->create(
68-
'customer',
69-
'adminhtml_customer',
70-
$customerData['account'],
71-
true
72-
);
73-
$formData = $customerForm->extractData($request, 'account');
74-
$customerData['account'] = $customerForm->restoreData($formData);
75-
$customer = $this->customerDataFactory->create();
76-
$this->dataObjectHelper->populateWithArray(
77-
$customer,
78-
$customerData['account'],
79-
'\Magento\Customer\Api\Data\CustomerInterface'
80-
);
81-
}
82-
83-
if (isset($data['address']) && is_array($data['address'])) {
84-
foreach (array_keys($data['address']) as $addressId) {
85-
if ($addressId == '_template_') {
86-
continue;
87-
}
88-
89-
try {
90-
$address = $this->addressRepository->getById($addressId);
91-
if (empty($customerId) || $address->getCustomerId() != $customerId) {
92-
//reinitialize address data object
93-
$address = $this->addressDataFactory->create();
94-
}
95-
} catch (NoSuchEntityException $e) {
96-
$address = $this->addressDataFactory->create();
97-
$address->setId($addressId);
98-
}
99-
if (!empty($customerId)) {
100-
$address->setCustomerId($customerId);
101-
}
102-
$address->setIsDefaultBilling(
103-
!empty($data['account'][CustomerInterface::DEFAULT_BILLING]) &&
104-
$data['account'][CustomerInterface::DEFAULT_BILLING] == $addressId
105-
);
106-
$address->setIsDefaultShipping(
107-
!empty($data['account'][CustomerInterface::DEFAULT_SHIPPING]) &&
108-
$data['account'][CustomerInterface::DEFAULT_SHIPPING] == $addressId
109-
);
110-
$requestScope = sprintf('address/%s', $addressId);
111-
$addressForm = $this->_formFactory->create(
112-
'customer_address',
113-
'adminhtml_customer_address',
114-
$this->addressMapper->toFlatArray($address)
115-
);
116-
$formData = $addressForm->extractData($request, $requestScope);
117-
$customerData['address'][$addressId] = $addressForm->restoreData($formData);
118-
$customerData['address'][$addressId][\Magento\Customer\Api\Data\AddressInterface::ID] = $addressId;
119-
}
120-
}
121-
}
122-
12352
$this->_getSession()->setCustomerData($customerData);
12453

12554
$resultPage = $this->resultPageFactory->create();

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ public function execute()
357357
'adminhtml_customer_save_after',
358358
['customer' => $customer, 'request' => $request]
359359
);
360-
$this->_getSession()->unsCustomerData();
360+
$this->_getSession()->unsCustomerFormData();
361361
// Done Saving customer, finish save action
362362
$this->_coreRegistry->register(RegistryConstants::CURRENT_CUSTOMER_ID, $customerId);
363363
$this->messageManager->addSuccess(__('You saved the customer.'));
@@ -368,15 +368,15 @@ public function execute()
368368
$messages = $exception->getMessage();
369369
}
370370
$this->_addSessionErrorMessages($messages);
371-
$this->_getSession()->setCustomerData($originalRequestData);
371+
$this->_getSession()->setCustomerFormData($originalRequestData);
372372
$returnToEdit = true;
373373
} catch (LocalizedException $exception) {
374374
$this->_addSessionErrorMessages($exception->getMessage());
375-
$this->_getSession()->setCustomerData($originalRequestData);
375+
$this->_getSession()->setCustomerFormData($originalRequestData);
376376
$returnToEdit = true;
377377
} catch (\Exception $exception) {
378378
$this->messageManager->addException($exception, __('Something went wrong while saving the customer.'));
379-
$this->_getSession()->setCustomerData($originalRequestData);
379+
$this->_getSession()->setCustomerFormData($originalRequestData);
380380
$returnToEdit = true;
381381
}
382382
}

app/code/Magento/Customer/Model/Customer/DataProvider.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Magento\Eav\Model\Entity\Type;
1010
use Magento\Customer\Model\Address;
1111
use Magento\Customer\Model\Customer;
12+
use Magento\Framework\App\ObjectManager;
13+
use Magento\Framework\Session\SessionManagerInterface;
1214
use Magento\Ui\Component\Form\Field;
1315
use Magento\Ui\DataProvider\EavValidationRules;
1416
use Magento\Customer\Model\ResourceModel\Customer\Collection;
@@ -71,6 +73,11 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
7173
*/
7274
protected $eavValidationRules;
7375

76+
/**
77+
* @var SessionManagerInterface
78+
*/
79+
protected $session;
80+
7481
/**
7582
* Constructor
7683
*
@@ -109,6 +116,19 @@ public function __construct(
109116
);
110117
}
111118

119+
/**
120+
* Get session object
121+
*
122+
* @return SessionManagerInterface
123+
*/
124+
protected function getSession()
125+
{
126+
if ($this->session === null) {
127+
$this->session = ObjectManager::getInstance()->get('Magento\Framework\Session\SessionManagerInterface');
128+
}
129+
return $this->session;
130+
}
131+
112132
/**
113133
* Get data
114134
*
@@ -135,6 +155,12 @@ public function getData()
135155
$this->loadedData[$customer->getId()] = $result;
136156
}
137157

158+
$data = $this->getSession()->getCustomerFormData();
159+
if (!empty($data)) {
160+
$customerId = isset($data['customer']['entity_id']) ? $data['customer']['entity_id'] : null;
161+
$this->loadedData[$customerId] = $data;
162+
}
163+
138164
return $this->loadedData;
139165
}
140166

@@ -195,7 +221,7 @@ protected function prepareAddressData($addressId, array &$addresses, array $cust
195221
) {
196222
$addresses[$addressId]['default_shipping'] = $customer['default_shipping'];
197223
}
198-
if (isset($addresses[$addressId]['street'])) {
224+
if (isset($addresses[$addressId]['street']) && !is_array($addresses[$addressId]['street'])) {
199225
$addresses[$addressId]['street'] = explode("\n", $addresses[$addressId]['street']);
200226
}
201227
}

app/code/Magento/Customer/Test/Unit/Block/Adminhtml/Edit/Tab/NewsletterTest.php

Lines changed: 86 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Customer\Test\Unit\Block\Adminhtml\Edit\Tab;
77

8+
use Magento\Backend\Model\Session;
89
use Magento\Customer\Controller\RegistryConstants;
910

1011
class NewsletterTest extends \PHPUnit_Framework_TestCase
@@ -44,6 +45,11 @@ class NewsletterTest extends \PHPUnit_Framework_TestCase
4445
*/
4546
protected $urlBuilderMock;
4647

48+
/**
49+
* @var Session|\PHPUnit_Framework_MockObject_MockObject
50+
*/
51+
protected $backendSessionMock;
52+
4753
public function setUp()
4854
{
4955
$this->contextMock = $this->getMock('\Magento\Backend\Block\Template\Context', [], [], '', false);
@@ -64,7 +70,12 @@ public function setUp()
6470
false
6571
);
6672
$this->urlBuilderMock = $this->getMock('\Magento\Framework\UrlInterface', [], [], '', false);
73+
$this->backendSessionMock = $this->getMockBuilder('Magento\Backend\Model\Session')
74+
->setMethods(['getCustomerFormData'])
75+
->disableOriginalConstructor()
76+
->getMock();
6777
$this->contextMock->expects($this->once())->method('getUrlBuilder')->willReturn($this->urlBuilderMock);
78+
$this->contextMock->expects($this->once())->method('getBackendSession')->willReturn($this->backendSessionMock);
6879

6980
$this->model = new \Magento\Customer\Block\Adminhtml\Edit\Tab\Newsletter(
7081
$this->contextMock,
@@ -84,28 +95,98 @@ public function testInitFormCanNotShowTab()
8495

8596
public function testInitForm()
8697
{
98+
$customerId = 1;
99+
87100
$subscriberMock = $this->getMock('\Magento\Newsletter\Model\Subscriber', [], [], '', false);
88101
$fieldsetMock = $this->getMock('\Magento\Framework\Data\Form\Element\Fieldset', [], [], '', false);
89-
$elementMock = $this->getMock('\Magento\Framework\Data\Form\Element\AbstractElement', [], [], '', false);
102+
$elementMock = $this->getMock('Magento\Framework\Data\Form\Element\Checkbox', ['setIsChecked'], [], '', false);
90103
$formMock = $this->getMock(
91104
'\Magento\Framework\Data\Form',
92105
['setHtmlIdPrefix', 'addFieldset', 'setValues', 'getElement', 'setForm', 'setParent', 'setBaseUrl'],
93106
[],
94107
'',
95108
false
96109
);
97-
$this->registryMock->expects($this->atLeastOnce())->method('registry')->willReturn($subscriberMock);
110+
$this->registryMock->expects($this->exactly(3))
111+
->method('registry')
112+
->willReturnMap(
113+
[
114+
[RegistryConstants::CURRENT_CUSTOMER_ID, $customerId],
115+
['subscriber', $subscriberMock],
116+
]
117+
);
98118
$this->formFactoryMock->expects($this->once())->method('create')->willReturn($formMock);
99119
$formMock->expects($this->once())->method('setHtmlIdPrefix')->with('_newsletter');
100120
$this->subscriberFactoryMock->expects($this->once())->method('create')->willReturn($subscriberMock);
101-
$subscriberMock->expects($this->once())->method('loadByCustomerId')->with($subscriberMock)->willReturnSelf();
121+
$subscriberMock->expects($this->once())->method('loadByCustomerId')->with($customerId)->willReturnSelf();
102122
$this->registryMock->expects($this->once())->method('register')->with('subscriber', $subscriberMock);
103123
$formMock->expects($this->once())->method('addFieldset')->willReturn($fieldsetMock);
104-
$this->accountManagementMock->expects($this->once())->method('isReadOnly')->with($subscriberMock)
124+
$fieldsetMock->expects($this->once())->method('addField')->willReturn($elementMock);
125+
$this->accountManagementMock->expects($this->once())->method('isReadOnly')->with($customerId)
105126
->willReturn(false);
106127
$subscriberMock->expects($this->once())->method('isSubscribed')->willReturn(true);
107-
$formMock->expects($this->once())->method('getElement')->willReturn($elementMock);
108128
$this->urlBuilderMock->expects($this->once())->method('getBaseUrl')->willReturn('domain.com');
129+
130+
$this->backendSessionMock->expects($this->once())->method('getCustomerFormData')->willReturn(null);
131+
132+
$elementMock->expects($this->once())
133+
->method('setIsChecked')
134+
->with(true);
135+
136+
$this->assertSame($this->model, $this->model->initForm());
137+
}
138+
139+
public function testInitFormWithCustomerFormData()
140+
{
141+
$customerId = 1;
142+
143+
$subscriberMock = $this->getMock('\Magento\Newsletter\Model\Subscriber', [], [], '', false);
144+
$fieldsetMock = $this->getMock('\Magento\Framework\Data\Form\Element\Fieldset', [], [], '', false);
145+
$elementMock = $this->getMock('Magento\Framework\Data\Form\Element\Checkbox', ['setIsChecked'], [], '', false);
146+
$formMock = $this->getMock(
147+
'\Magento\Framework\Data\Form',
148+
['setHtmlIdPrefix', 'addFieldset', 'setValues', 'getElement', 'setForm', 'setParent', 'setBaseUrl'],
149+
[],
150+
'',
151+
false
152+
);
153+
$this->registryMock->expects($this->exactly(3))
154+
->method('registry')
155+
->willReturnMap(
156+
[
157+
[RegistryConstants::CURRENT_CUSTOMER_ID, $customerId],
158+
['subscriber', $subscriberMock],
159+
]
160+
);
161+
$this->formFactoryMock->expects($this->once())->method('create')->willReturn($formMock);
162+
$formMock->expects($this->once())->method('setHtmlIdPrefix')->with('_newsletter');
163+
$this->subscriberFactoryMock->expects($this->once())->method('create')->willReturn($subscriberMock);
164+
$subscriberMock->expects($this->once())->method('loadByCustomerId')->with($customerId)->willReturnSelf();
165+
$formMock->expects($this->once())->method('addFieldset')->willReturn($fieldsetMock);
166+
$fieldsetMock->expects($this->once())->method('addField')->willReturn($elementMock);
167+
$this->accountManagementMock->expects($this->once())->method('isReadOnly')->with($customerId)
168+
->willReturn(false);
169+
$subscriberMock->expects($this->once())->method('isSubscribed')->willReturn(false);
170+
$this->urlBuilderMock->expects($this->once())->method('getBaseUrl')->willReturn('domain.com');
171+
172+
$this->backendSessionMock->expects($this->once())
173+
->method('getCustomerFormData')
174+
->willReturn([
175+
'customer' => [
176+
'entity_id' => $customerId,
177+
],
178+
'subscription' => true,
179+
]);
180+
181+
$elementMock->expects($this->exactly(2))
182+
->method('setIsChecked')
183+
->willReturnMap(
184+
[
185+
[false],
186+
[true],
187+
]
188+
);
189+
109190
$this->assertSame($this->model, $this->model->initForm());
110191
}
111192
}

0 commit comments

Comments
 (0)