Skip to content

Commit e59257a

Browse files
author
Sergey Semenov
committed
MAGETWO-50350: Yes/No attribute values do not get saved on the Customer account
1 parent 67caf84 commit e59257a

File tree

3 files changed

+100
-6
lines changed

3 files changed

+100
-6
lines changed

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

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Customer\Model\Customer;
77

8+
use Magento\Eav\Api\Data\AttributeInterface;
89
use Magento\Eav\Model\Config;
910
use Magento\Eav\Model\Entity\Type;
1011
use Magento\Customer\Model\Address;
@@ -151,6 +152,8 @@ protected function getAttributesMeta(Type $entityType)
151152
$attributes = $entityType->getAttributeCollection();
152153
/* @var \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute */
153154
foreach ($attributes as $attribute) {
155+
$this->processFrontendInput($attribute, $meta);
156+
154157
$code = $attribute->getAttributeCode();
155158
// use getDataUsingMethod, since some getters are defined and apply additional processing of returning value
156159
foreach ($this->metaProperties as $metaName => $origName) {
@@ -161,9 +164,10 @@ protected function getAttributesMeta(Type $entityType)
161164
? $this->formElement[$value]
162165
: $value;
163166
}
164-
if ($attribute->usesSource()) {
165-
$meta[$code]['arguments']['data']['config']['options'] = $attribute->getSource()->getAllOptions();
166-
}
167+
}
168+
169+
if ($attribute->usesSource()) {
170+
$meta[$code]['arguments']['data']['config']['options'] = $attribute->getSource()->getAllOptions();
167171
}
168172

169173
$rules = $this->eavValidationRules->build($attribute, $meta[$code]['arguments']['data']['config']);
@@ -175,6 +179,25 @@ protected function getAttributesMeta(Type $entityType)
175179
return $meta;
176180
}
177181

182+
/**
183+
* Process attributes by frontend input type
184+
*
185+
* @param AttributeInterface $attribute
186+
* @param array $meta
187+
* @return array
188+
*/
189+
private function processFrontendInput(AttributeInterface $attribute, array &$meta)
190+
{
191+
$code = $attribute->getAttributeCode();
192+
if ($attribute->getFrontendInput() === 'boolean') {
193+
$meta[$code]['arguments']['data']['config']['prefer'] = 'toggle';
194+
$meta[$code]['arguments']['data']['config']['valueMap'] = [
195+
'true' => '1',
196+
'false' => '0',
197+
];
198+
}
199+
}
200+
178201
/**
179202
* Prepare address data
180203
*

app/code/Magento/Customer/Test/Unit/Model/Customer/DataProviderTest.php

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,29 @@ public function getAttributesMetaDataProvider()
121121
],
122122
],
123123
],
124+
'test-code-boolean' => [
125+
'arguments' => [
126+
'data' => [
127+
'config' => [
128+
'dataType' => 'frontend_input',
129+
'formElement' => 'frontend_input',
130+
'visible' => 'is_visible',
131+
'required' => 'is_required',
132+
'label' => 'frontend_label',
133+
'sortOrder' => 'sort_order',
134+
'notice' => 'note',
135+
'default' => 'default_value',
136+
'size' => 'multiline_count',
137+
'componentType' => Field::NAME,
138+
'prefer' => 'toggle',
139+
'valueMap' => [
140+
'true' => 1,
141+
'false' => 0,
142+
],
143+
],
144+
],
145+
],
146+
],
124147
],
125148
],
126149
'address' => [
@@ -144,6 +167,29 @@ public function getAttributesMetaDataProvider()
144167
],
145168
],
146169
],
170+
'test-code-boolean' => [
171+
'arguments' => [
172+
'data' => [
173+
'config' => [
174+
'dataType' => 'frontend_input',
175+
'formElement' => 'frontend_input',
176+
'visible' => 'is_visible',
177+
'required' => 'is_required',
178+
'label' => 'frontend_label',
179+
'sortOrder' => 'sort_order',
180+
'notice' => 'note',
181+
'default' => 'default_value',
182+
'size' => 'multiline_count',
183+
'componentType' => Field::NAME,
184+
'prefer' => 'toggle',
185+
'valueMap' => [
186+
'true' => 1,
187+
'false' => 0,
188+
],
189+
],
190+
],
191+
],
192+
],
147193
],
148194
],
149195
]
@@ -237,7 +283,7 @@ protected function getAttributeMock()
237283
->method('getAllOptions')
238284
->willReturn(self::OPTIONS_RESULT);
239285

240-
$attributeMock->expects($this->once())
286+
$attributeMock->expects($this->exactly(2))
241287
->method('getAttributeCode')
242288
->willReturn(self::ATTRIBUTE_CODE);
243289

@@ -255,11 +301,35 @@ function ($origName) {
255301
->method('getSource')
256302
->willReturn($sourceMock);
257303

304+
$attributeBooleanMock = $this->getMockBuilder('Magento\Eav\Model\Entity\Attribute\AbstractAttribute')
305+
->setMethods(['getAttributeCode', 'getDataUsingMethod', 'usesSource', 'getFrontendInput'])
306+
->disableOriginalConstructor()
307+
->getMockForAbstractClass();
308+
$attributeBooleanMock->expects($this->exactly(2))
309+
->method('getAttributeCode')
310+
->willReturn('test-code-boolean');
311+
$attributeBooleanMock->expects($this->once())
312+
->method('getFrontendInput')
313+
->willReturn('boolean');
314+
$attributeBooleanMock->expects($this->any())
315+
->method('getDataUsingMethod')
316+
->willReturnCallback(
317+
function ($origName) {
318+
return $origName;
319+
}
320+
);
321+
$attributeBooleanMock->expects($this->once())
322+
->method('usesSource')
323+
->willReturn(false);
324+
258325
$this->eavValidationRulesMock->expects($this->any())
259326
->method('build')
260-
->with($attributeMock, $this->logicalNot($this->isEmpty()));
327+
->willReturnMap([
328+
[$attributeMock, $this->logicalNot($this->isEmpty()), []],
329+
[$attributeBooleanMock, $this->logicalNot($this->isEmpty()), []],
330+
]);
261331

262-
return [$attributeMock];
332+
return [$attributeMock, $attributeBooleanMock];
263333
}
264334

265335
public function testGetData()

app/code/Magento/Customer/view/base/ui_component/customer_form.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@
148148
<item name="dataType" xsi:type="string">boolean</item>
149149
<item name="formElement" xsi:type="string">checkbox</item>
150150
<item name="source" xsi:type="string">customer</item>
151+
<item name="prefer" xsi:type="string">checkbox</item>
151152
<item name="description" xsi:type="string" translate="true">Disable Automatic Group Change Based on VAT ID</item>
152153
<item name="valueMap" xsi:type="array">
153154
<item name="true" xsi:type="string">1</item>

0 commit comments

Comments
 (0)