Skip to content

Commit 03f7ae1

Browse files
committed
Merge branch 'ACP2E-2684' of https://github.com/magento-l3/magento2ce into PR-12-19-2023
2 parents a69b45b + e828eca commit 03f7ae1

File tree

2 files changed

+62
-15
lines changed

2 files changed

+62
-15
lines changed

app/code/Magento/Customer/Model/Address/AbstractAddress.php

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,9 @@ public function setData($key, $value = null)
314314
} elseif (is_array($value) && $this->isAddressMultilineAttribute($key)) {
315315
$value = $this->_implodeArrayValues($value);
316316
} elseif (self::CUSTOM_ATTRIBUTES === $key && is_array($value)) {
317-
foreach ($value as &$attribute) {
318-
$attribute = is_array($attribute) ? $attribute : $attribute->__toArray();
319-
$attribute = $this->processCustomAttribute($attribute);
317+
$value = $this->filterCustomAttributes([self::CUSTOM_ATTRIBUTES => $value])[self::CUSTOM_ATTRIBUTES];
318+
foreach ($value as $attribute) {
319+
$this->processCustomAttribute($attribute);
320320
}
321321
}
322322

@@ -748,22 +748,16 @@ protected function isFaxRequired()
748748
}
749749

750750
/**
751-
* Unify attribute format.
751+
* Normalize custom attribute value
752752
*
753-
* @param array $attribute
754-
* @return array
753+
* @param \Magento\Framework\Api\AttributeInterface $attribute
754+
* @return void
755755
*/
756-
private function processCustomAttribute(array $attribute): array
756+
private function processCustomAttribute(\Magento\Framework\Api\AttributeInterface $attribute): void
757757
{
758-
if (isset($attribute['attribute_code']) &&
759-
isset($attribute['value']) &&
760-
is_array($attribute['value']) &&
761-
$this->isAddressMultilineAttribute($attribute['attribute_code'])
762-
) {
763-
$attribute['value'] = $this->_implodeArrayValues($attribute['value']);
758+
if (is_array($attribute->getValue()) && $this->isAddressMultilineAttribute($attribute->getAttributeCode())) {
759+
$attribute->setValue($this->_implodeArrayValues($attribute->getValue()));
764760
}
765-
766-
return $attribute;
767761
}
768762

769763
/**

app/code/Magento/Customer/Test/Unit/Model/Address/AbstractAddressTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use Magento\Directory\Model\RegionFactory;
1818
use Magento\Directory\Model\ResourceModel\Region\Collection;
1919
use Magento\Eav\Model\Config;
20+
use Magento\Framework\Api\AttributeInterface;
21+
use Magento\Framework\Api\AttributeValue;
2022
use Magento\Framework\Data\Collection\AbstractDb;
2123
use Magento\Framework\DataObject;
2224
use Magento\Framework\Model\Context;
@@ -438,6 +440,57 @@ public function getStreetFullDataProvider()
438440
];
439441
}
440442

443+
/**
444+
* @return void
445+
*/
446+
public function testSetCustomerAttributes(): void
447+
{
448+
$model = $this->getMockBuilder(AbstractAddress::class)
449+
->onlyMethods(['getCustomAttributesCodes'])
450+
->disableOriginalConstructor()
451+
->getMockForAbstractClass();
452+
$customAttributeFactory = $this->createMock(\Magento\Customer\Model\AttributeFactory::class);
453+
$customAttributeFactory->method('create')
454+
->willReturnCallback(
455+
function ($data) {
456+
return new AttributeValue($data);
457+
}
458+
);
459+
$data = [
460+
'customer_attribute1' => new AttributeValue([
461+
'attribute_code' => 'customer_attribute1',
462+
'value' => 'customer_attribute1_value'
463+
]),
464+
'customer_attribute2' => new AttributeValue([
465+
'attribute_code' => 'customer_attribute2',
466+
'value' => ['customer_attribute2_value1', 'customer_attribute2_value2']
467+
])
468+
];
469+
$model->method('getCustomAttributesCodes')->willReturn(array_keys($data));
470+
$this->objectManager->setBackwardCompatibleProperty(
471+
$model,
472+
'customAttributeFactory',
473+
$customAttributeFactory
474+
);
475+
$model->setData('custom_attributes', $data);
476+
$this->assertEquals(
477+
[
478+
[
479+
'attribute_code' => 'customer_attribute1',
480+
'value' => 'customer_attribute1_value'
481+
],
482+
[
483+
'attribute_code' => 'customer_attribute2',
484+
'value' => "customer_attribute2_value1\ncustomer_attribute2_value2"
485+
]
486+
],
487+
array_map(
488+
fn ($attr) => ['attribute_code' => $attr->getAttributeCode(), 'value' => $attr->getValue()],
489+
$model->getCustomAttributes()
490+
)
491+
);
492+
}
493+
441494
protected function tearDown(): void
442495
{
443496
$this->objectManager->setBackwardCompatibleProperty(

0 commit comments

Comments
 (0)