9
9
use Magento \Framework \Registry ;
10
10
use Magento \Customer \Model \Attribute ;
11
11
use Magento \ImportExport \Model \Export ;
12
+ use Magento \ImportExport \Model \Import ;
12
13
use Magento \TestFramework \Helper \Bootstrap ;
13
14
use Magento \Framework \ObjectManagerInterface ;
14
15
use Magento \Store \Model \StoreManagerInterface ;
20
21
21
22
/**
22
23
* Tests for customer export model.
24
+ *
25
+ * @magentoAppArea adminhtml
23
26
*/
24
27
class CustomerTest extends \PHPUnit \Framework \TestCase
25
28
{
@@ -33,6 +36,16 @@ class CustomerTest extends \PHPUnit\Framework\TestCase
33
36
*/
34
37
private $ objectManager ;
35
38
39
+ /**
40
+ * @var array
41
+ */
42
+ private $ attributeValues ;
43
+
44
+ /**
45
+ * @var array
46
+ */
47
+ private $ attributeTypes ;
48
+
36
49
/**
37
50
* @inheritdoc
38
51
*/
@@ -49,10 +62,13 @@ protected function setUp()
49
62
*/
50
63
public function testExport ()
51
64
{
52
- $ expectedAttributes = [];
53
- /** @var $collection Collection */
65
+ /** @var Collection $collection */
54
66
$ 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 */
56
72
foreach ($ collection as $ attribute ) {
57
73
$ expectedAttributes [] = $ attribute ->getAttributeCode ();
58
74
}
@@ -72,10 +88,10 @@ public function testExport()
72
88
73
89
$ this ->assertNotEmpty ($ lines ['data ' ], 'No data was exported. ' );
74
90
75
- /** @var $customers CustomerModel[] */
91
+ /** @var CustomerModel[] $customers */
76
92
$ customers = $ this ->objectManager ->create (CustomerCollection::class)->getItems ();
77
93
foreach ($ customers as $ customer ) {
78
- $ data = $ customer -> getData ( );
94
+ $ data = $ this -> processCustomerData ( $ customer , $ expectedAttributes );
79
95
$ exportData = $ lines ['data ' ][$ data ['email ' ]];
80
96
$ exportData = $ this ->unsetDuplicateData ($ exportData );
81
97
array_walk (
@@ -91,6 +107,96 @@ function (&$value) {
91
107
}
92
108
}
93
109
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
+
94
200
/**
95
201
* Unset non-useful or duplicate data from exported file data.
96
202
*
@@ -172,14 +278,10 @@ public function testFilterAttributeCollection()
172
278
public function testFilterEntityCollection ()
173
279
{
174
280
$ createdAtDate = '2038-01-01 ' ;
175
-
176
- /** @var $objectManager ObjectManagerInterface */
177
- $ objectManager = $ this ->objectManager ;
178
-
179
281
/**
180
282
* Change created_at date of first customer for future filter test.
181
283
*/
182
- $ customers = $ objectManager ->get (Registry::class)
284
+ $ customers = $ this -> objectManager ->get (Registry::class)
183
285
->registry ('_fixture/Magento_ImportExport_Customer_Collection ' );
184
286
$ customers [0 ]->setCreatedAt ($ createdAtDate );
185
287
$ customers [0 ]->save ();
@@ -239,6 +341,7 @@ protected function _csvToArray($content, $entityId = null)
239
341
}
240
342
}
241
343
}
344
+
242
345
return $ data ;
243
346
}
244
347
}
0 commit comments