Skip to content

Commit f7cc155

Browse files
committed
MC-20624: Automate MC-11459
1 parent 8ba8c5b commit f7cc155

File tree

1 file changed

+113
-10
lines changed
  • dev/tests/integration/testsuite/Magento/CustomerImportExport/Model/Export

1 file changed

+113
-10
lines changed

dev/tests/integration/testsuite/Magento/CustomerImportExport/Model/Export/CustomerTest.php

Lines changed: 113 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Framework\Registry;
1010
use Magento\Customer\Model\Attribute;
1111
use Magento\ImportExport\Model\Export;
12+
use Magento\ImportExport\Model\Import;
1213
use Magento\TestFramework\Helper\Bootstrap;
1314
use Magento\Framework\ObjectManagerInterface;
1415
use Magento\Store\Model\StoreManagerInterface;
@@ -20,6 +21,8 @@
2021

2122
/**
2223
* Tests for customer export model.
24+
*
25+
* @magentoAppArea adminhtml
2326
*/
2427
class CustomerTest extends \PHPUnit\Framework\TestCase
2528
{
@@ -33,6 +36,16 @@ class CustomerTest extends \PHPUnit\Framework\TestCase
3336
*/
3437
private $objectManager;
3538

39+
/**
40+
* @var array
41+
*/
42+
private $attributeValues;
43+
44+
/**
45+
* @var array
46+
*/
47+
private $attributeTypes;
48+
3649
/**
3750
* @inheritdoc
3851
*/
@@ -49,10 +62,13 @@ protected function setUp()
4962
*/
5063
public function testExport()
5164
{
52-
$expectedAttributes = [];
53-
/** @var $collection Collection */
65+
/** @var Collection $collection */
5466
$collection = $this->objectManager->create(Collection::class);
55-
/** @var $attribute Attribute */
67+
$this->initAttributeValues($collection);
68+
$this->initAttributeTypes($collection);
69+
70+
$expectedAttributes = [];
71+
/** @var Attribute $attribute */
5672
foreach ($collection as $attribute) {
5773
$expectedAttributes[] = $attribute->getAttributeCode();
5874
}
@@ -72,10 +88,10 @@ public function testExport()
7288

7389
$this->assertNotEmpty($lines['data'], 'No data was exported.');
7490

75-
/** @var $customers CustomerModel[] */
91+
/** @var CustomerModel[] $customers */
7692
$customers = $this->objectManager->create(CustomerCollection::class)->getItems();
7793
foreach ($customers as $customer) {
78-
$data = $customer->getData();
94+
$data = $this->processCustomerData($customer, $expectedAttributes);
7995
$exportData = $lines['data'][$data['email']];
8096
$exportData = $this->unsetDuplicateData($exportData);
8197
array_walk(
@@ -91,6 +107,96 @@ function (&$value) {
91107
}
92108
}
93109

110+
/**
111+
* Initialize attribute option values.
112+
*
113+
* @param Collection $attributeCollection
114+
* @return $this
115+
*/
116+
private function initAttributeValues(Collection $attributeCollection): CustomerTest
117+
{
118+
/** @var Attribute $attribute */
119+
foreach ($attributeCollection as $attribute) {
120+
$this->attributeValues[$attribute->getAttributeCode()] = $this->_model->getAttributeOptions($attribute);
121+
}
122+
123+
return $this;
124+
}
125+
126+
/**
127+
* Initialize attribute types.
128+
*
129+
* @param \Magento\Customer\Model\ResourceModel\Attribute\Collection $attributeCollection
130+
* @return $this
131+
*/
132+
private function initAttributeTypes(Collection $attributeCollection): CustomerTest
133+
{
134+
/** @var Attribute $attribute */
135+
foreach ($attributeCollection as $attribute) {
136+
$this->attributeTypes[$attribute->getAttributeCode()] = $attribute->getFrontendInput();
137+
}
138+
139+
return $this;
140+
}
141+
142+
/**
143+
* Format Customer data as same as export data.
144+
*
145+
* @param CustomerModel $item
146+
* @param array $expectedAttributes
147+
* @return array
148+
*/
149+
private function processCustomerData(CustomerModel $item, array $expectedAttributes): array
150+
{
151+
$data = [];
152+
foreach ($expectedAttributes as $attributeCode) {
153+
$attributeValue = $item->getData($attributeCode);
154+
155+
if ($this->isMultiselect($attributeCode)) {
156+
$values = [];
157+
$attributeValue = explode(Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR, $attributeValue);
158+
foreach ($attributeValue as $value) {
159+
$values[] = $this->getAttributeValueById($attributeCode, $value);
160+
}
161+
$data[$attributeCode] = implode(Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR, $values);
162+
} else {
163+
$data[$attributeCode] = $this->getAttributeValueById($attributeCode, $attributeValue);
164+
}
165+
}
166+
167+
return $data;
168+
}
169+
170+
/**
171+
* Check that attribute is multiselect type by attribute code.
172+
*
173+
* @param string $attributeCode
174+
* @return bool
175+
*/
176+
private function isMultiselect(string $attributeCode): bool
177+
{
178+
return isset($this->attributeTypes[$attributeCode])
179+
&& $this->attributeTypes[$attributeCode] === 'multiselect';
180+
}
181+
182+
/**
183+
* Return attribute value by id.
184+
*
185+
* @param string $attributeCode
186+
* @param int|string $valueId
187+
* @return mixed
188+
*/
189+
private function getAttributeValueById(string $attributeCode, $valueId)
190+
{
191+
if (isset($this->attributeValues[$attributeCode])
192+
&& isset($this->attributeValues[$attributeCode][$valueId])
193+
) {
194+
return $this->attributeValues[$attributeCode][$valueId];
195+
}
196+
197+
return $valueId;
198+
}
199+
94200
/**
95201
* Unset non-useful or duplicate data from exported file data.
96202
*
@@ -172,14 +278,10 @@ public function testFilterAttributeCollection()
172278
public function testFilterEntityCollection()
173279
{
174280
$createdAtDate = '2038-01-01';
175-
176-
/** @var $objectManager ObjectManagerInterface */
177-
$objectManager = $this->objectManager;
178-
179281
/**
180282
* Change created_at date of first customer for future filter test.
181283
*/
182-
$customers = $objectManager->get(Registry::class)
284+
$customers = $this->objectManager->get(Registry::class)
183285
->registry('_fixture/Magento_ImportExport_Customer_Collection');
184286
$customers[0]->setCreatedAt($createdAtDate);
185287
$customers[0]->save();
@@ -239,6 +341,7 @@ protected function _csvToArray($content, $entityId = null)
239341
}
240342
}
241343
}
344+
242345
return $data;
243346
}
244347
}

0 commit comments

Comments
 (0)