Skip to content

Commit f1e461f

Browse files
committed
MC-36721: Unable to export customers with custom gender attribute value
- Adding attribute check for gender
1 parent 8b7d949 commit f1e461f

File tree

2 files changed

+54
-10
lines changed

2 files changed

+54
-10
lines changed

app/code/Magento/Customer/Test/Unit/Ui/Component/DataProvider/DocumentTest.php

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,16 @@ protected function setUp(): void
8080
}
8181

8282
/**
83-
* @covers \Magento\Customer\Ui\Component\DataProvider\Document::getCustomAttribute
83+
* @dataProvider getGenderAttributeDataProvider
84+
* @covers \Magento\Customer\Ui\Component\DataProvider\Document::getCustomAttribute
85+
* @param int $genderId
86+
* @param string $attributeValue
87+
* @param string $attributeLabel
8488
*/
85-
public function testGetGenderAttribute()
89+
public function testGetGenderAttribute(int $genderId, string $attributeValue, string $attributeLabel): void
8690
{
87-
$genderId = 1;
91+
$expectedResult = !empty($attributeValue) ? $attributeLabel : $genderId;
92+
8893
$this->document->setData('gender', $genderId);
8994

9095
$this->groupRepository->expects(static::never())
@@ -106,11 +111,37 @@ public function testGetGenderAttribute()
106111
->willReturn([$genderId => $option]);
107112

108113
$option->expects(static::once())
114+
->method('getValue')
115+
->willReturn($attributeValue);
116+
117+
$option->expects(static::any())
109118
->method('getLabel')
110-
->willReturn('Male');
119+
->willReturn($attributeLabel);
111120

112121
$attribute = $this->document->getCustomAttribute('gender');
113-
static::assertEquals('Male', $attribute->getValue());
122+
static::assertEquals($expectedResult, $attribute->getValue());
123+
}
124+
125+
/**
126+
* Data provider for testGetGenderAttribute
127+
* @return array
128+
*/
129+
public function getGenderAttributeDataProvider()
130+
{
131+
return [
132+
'with valid gender label and value' => [
133+
1, '1', 'Male'
134+
],
135+
'with empty gender label' => [
136+
2, '2', ''
137+
],
138+
'with empty gender value' => [
139+
3, '', 'test'
140+
],
141+
'with empty gender label and value' => [
142+
4, '', ''
143+
]
144+
];
114145
}
115146

116147
/**

app/code/Magento/Customer/Ui/Component/DataProvider/Document.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,23 @@
55
*/
66
namespace Magento\Customer\Ui\Component\DataProvider;
77

8+
use Exception;
89
use Magento\Customer\Api\CustomerMetadataInterface;
10+
use Magento\Customer\Api\Data\OptionInterface;
11+
use Magento\Customer\Api\GroupRepositoryInterface;
912
use Magento\Customer\Model\AccountManagement;
1013
use Magento\Framework\Api\AttributeValueFactory;
1114
use Magento\Framework\App\Config\ScopeConfigInterface;
12-
use Magento\Framework\Exception\NoSuchEntityException;
13-
use Magento\Customer\Api\GroupRepositoryInterface;
1415
use Magento\Framework\App\ObjectManager;
16+
use Magento\Framework\Exception\NoSuchEntityException;
1517
use Magento\Store\Model\ScopeInterface;
1618
use Magento\Store\Model\StoreManagerInterface;
1719

1820
/**
1921
* Class Document
22+
*
23+
* Set the attribute label and value for UI Component
24+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2025
*/
2126
class Document extends \Magento\Framework\View\Element\UiComponent\DataProvider\Document
2227
{
@@ -127,16 +132,23 @@ public function getCustomAttribute($attributeCode)
127132
private function setGenderValue()
128133
{
129134
$value = $this->getData(self::$genderAttributeCode);
130-
135+
131136
if (!$value) {
132137
$this->setCustomAttribute(self::$genderAttributeCode, 'N/A');
133138
return;
134139
}
135140

136141
try {
137142
$attributeMetadata = $this->customerMetadata->getAttributeMetadata(self::$genderAttributeCode);
138-
$option = $attributeMetadata->getOptions()[$value];
139-
$this->setCustomAttribute(self::$genderAttributeCode, $option->getLabel());
143+
$options = $attributeMetadata->getOptions();
144+
array_walk(
145+
$options,
146+
function (OptionInterface $option) use ($value) {
147+
if ($option->getValue() == $value) {
148+
$this->setCustomAttribute(self::$genderAttributeCode, $option->getLabel());
149+
}
150+
}
151+
);
140152
} catch (NoSuchEntityException $e) {
141153
$this->setCustomAttribute(self::$genderAttributeCode, 'N/A');
142154
}
@@ -199,6 +211,7 @@ private function setConfirmationValue()
199211
* Update lock expires value. Method set account lock text value to match what is shown in grid
200212
*
201213
* @return void
214+
* @throws Exception
202215
*/
203216
private function setAccountLockValue()
204217
{

0 commit comments

Comments
 (0)