1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Customer\Api;
use Magento\Customer\Api\Data\CustomerInterface as Customer;
use Magento\Customer\Model\Data\AttributeMetadata;
use Magento\TestFramework\TestCase\WebapiAbstract;
use Magento\TestFramework\Helper\Bootstrap;
/**
* Class CustomerMetadataTest
*/
class CustomerMetadataTest extends WebapiAbstract
{
const SERVICE_NAME = "customerCustomerMetadataV1";
const SERVICE_VERSION = "V1";
const RESOURCE_PATH = "/V1/attributeMetadata/customer";
/**
* @var CustomerMetadataInterface
*/
private $customerMetadata;
/**
* Execute per test initialization.
*/
public function setUp()
{
$this->customerMetadata = Bootstrap::getObjectManager()->create(CustomerMetadataInterface::class);
}
/**
* Test retrieval of attribute metadata for the customer entity type.
*
* @param string $attributeCode The attribute code of the requested metadata.
* @param array $expectedMetadata Expected entity metadata for the attribute code.
* @dataProvider getAttributeMetadataDataProvider
*/
public function testGetAttributeMetadata($attributeCode, $expectedMetadata)
{
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH . "/attribute/$attributeCode",
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'GetAttributeMetadata',
],
];
$requestData = [
'attributeCode' => $attributeCode,
];
$attributeMetadata = $this->_webapiCall($serviceInfo, $requestData);
$validationResult = $this->checkValidationRules($expectedMetadata, $attributeMetadata);
list($expectedMetadata, $attributeMetadata) = $validationResult;
$this->assertEquals($expectedMetadata, $attributeMetadata);
}
/**
* Data provider for testGetAttributeMetadata.
*
* @return array
*/
public function getAttributeMetadataDataProvider()
{
return [
Customer::FIRSTNAME => [
Customer::FIRSTNAME,
[
AttributeMetadata::FRONTEND_INPUT => 'text',
AttributeMetadata::INPUT_FILTER => 'trim',
AttributeMetadata::STORE_LABEL => 'First Name',
AttributeMetadata::MULTILINE_COUNT => 0,
AttributeMetadata::VALIDATION_RULES => [
['name' => 'min_text_length', 'value' => 1],
['name' => 'max_text_length', 'value' => 255],
],
AttributeMetadata::VISIBLE => true,
AttributeMetadata::REQUIRED => true,
AttributeMetadata::DATA_MODEL => '',
AttributeMetadata::OPTIONS => [],
AttributeMetadata::FRONTEND_CLASS => 'required-entry',
AttributeMetadata::USER_DEFINED => false,
AttributeMetadata::SORT_ORDER => 40,
AttributeMetadata::FRONTEND_LABEL => 'First Name',
AttributeMetadata::NOTE => '',
AttributeMetadata::SYSTEM => true,
AttributeMetadata::BACKEND_TYPE => 'static',
AttributeMetadata::IS_USED_IN_GRID => '',
AttributeMetadata::IS_VISIBLE_IN_GRID => '',
AttributeMetadata::IS_FILTERABLE_IN_GRID => '',
AttributeMetadata::IS_SEARCHABLE_IN_GRID => '',
AttributeMetadata::ATTRIBUTE_CODE => 'firstname',
],
],
Customer::GENDER => [
Customer::GENDER,
[
AttributeMetadata::FRONTEND_INPUT => 'select',
AttributeMetadata::INPUT_FILTER => '',
AttributeMetadata::STORE_LABEL => 'Gender',
AttributeMetadata::MULTILINE_COUNT => 0,
AttributeMetadata::VALIDATION_RULES => [],
AttributeMetadata::VISIBLE => false,
AttributeMetadata::REQUIRED => false,
AttributeMetadata::DATA_MODEL => '',
AttributeMetadata::OPTIONS => [
['label' => ' ', 'value' => ''],
['label' => 'Male', 'value' => '1'],
['label' => 'Female', 'value' => '2'],
['label' => 'Not Specified', 'value' => '3']
],
AttributeMetadata::FRONTEND_CLASS => '',
AttributeMetadata::USER_DEFINED => false,
AttributeMetadata::SORT_ORDER => 110,
AttributeMetadata::FRONTEND_LABEL => 'Gender',
AttributeMetadata::NOTE => '',
AttributeMetadata::SYSTEM => false,
AttributeMetadata::BACKEND_TYPE => 'static',
AttributeMetadata::IS_USED_IN_GRID => true,
AttributeMetadata::IS_VISIBLE_IN_GRID => true,
AttributeMetadata::IS_FILTERABLE_IN_GRID => true,
AttributeMetadata::IS_SEARCHABLE_IN_GRID => '',
AttributeMetadata::ATTRIBUTE_CODE => 'gender',
],
],
Customer::WEBSITE_ID => [
Customer::WEBSITE_ID,
[
AttributeMetadata::FRONTEND_INPUT => 'select',
AttributeMetadata::INPUT_FILTER => '',
AttributeMetadata::STORE_LABEL => 'Associate to Website',
AttributeMetadata::MULTILINE_COUNT => 0,
AttributeMetadata::VALIDATION_RULES => [],
AttributeMetadata::VISIBLE => true,
AttributeMetadata::REQUIRED => true,
AttributeMetadata::DATA_MODEL => '',
AttributeMetadata::OPTIONS => [
['label' => 'Main Website', 'value' => '1'],
],
AttributeMetadata::FRONTEND_CLASS => 'required-entry',
AttributeMetadata::USER_DEFINED => false,
AttributeMetadata::SORT_ORDER => 10,
AttributeMetadata::FRONTEND_LABEL => 'Associate to Website',
AttributeMetadata::NOTE => '',
AttributeMetadata::SYSTEM => true,
AttributeMetadata::BACKEND_TYPE => 'static',
AttributeMetadata::IS_USED_IN_GRID => true,
AttributeMetadata::IS_VISIBLE_IN_GRID => true,
AttributeMetadata::IS_FILTERABLE_IN_GRID => true,
AttributeMetadata::IS_SEARCHABLE_IN_GRID => false,
AttributeMetadata::ATTRIBUTE_CODE => 'website_id',
],
]
];
}
/**
* Test retrieval of all customer attribute metadata.
*/
public function testGetAllAttributesMetadata()
{
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH,
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'GetAllAttributesMetadata',
],
];
$attributeMetadata = $this->_webApiCall($serviceInfo);
$firstName = $this->getAttributeMetadataDataProvider()[Customer::FIRSTNAME][1];
$validationResult = $this->checkMultipleAttributesValidationRules($firstName, $attributeMetadata);
list($firstName, $attributeMetadata) = $validationResult;
$this->assertContains($firstName, $attributeMetadata);
$websiteId = $this->getAttributeMetadataDataProvider()[Customer::WEBSITE_ID][1];
$validationResult = $this->checkMultipleAttributesValidationRules($websiteId, $attributeMetadata);
list($websiteId, $attributeMetadata) = $validationResult;
$this->assertContains($websiteId, $attributeMetadata);
}
/**
* Test retrieval of custom customer attribute metadata.
*/
public function testGetCustomAttributesMetadata()
{
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH . '/custom',
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'GetCustomAttributesMetadata',
],
];
$attributeMetadata = $this->_webApiCall($serviceInfo);
$this->assertCount(count($this->customerMetadata->getCustomAttributesMetadata()), $attributeMetadata);
}
/**
* Test retrieval of attributes
*
* @param string $formCode Form code
* @param array $expectedMetadata The expected attribute metadata
* @dataProvider getAttributesDataProvider
*/
public function testGetAttributes($formCode, $expectedMetadata)
{
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH . "/form/$formCode",
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'GetAttributes',
],
];
$requestData = [
'formCode' => $formCode,
];
$attributeMetadataList = $this->_webApiCall($serviceInfo, $requestData);
foreach ($attributeMetadataList as $attributeMetadata) {
if (isset($attributeMetadata['attribute_code'])
&& $attributeMetadata['attribute_code'] == $expectedMetadata['attribute_code']) {
$validationResult = $this->checkValidationRules($expectedMetadata, $attributeMetadata);
list($expectedMetadata, $attributeMetadata) = $validationResult;
$this->assertEquals($expectedMetadata, $attributeMetadata);
break;
}
}
}
/**
* Data provider for testGetAttributes.
*
* @return array
*/
public function getAttributesDataProvider()
{
$attributeMetadata = $this->getAttributeMetadataDataProvider();
return [
[
'adminhtml_customer',
$attributeMetadata[Customer::FIRSTNAME][1],
],
[
'adminhtml_customer',
$attributeMetadata[Customer::GENDER][1]
]
];
}
/**
* Checks that expected and actual attribute metadata validation rules are equal
* and removes the validation rules entry from expected and actual attribute metadata
*
* @param array $expectedResult
* @param array $actualResult
* @return array
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function checkValidationRules($expectedResult, $actualResult)
{
$expectedRules = [];
$actualRules = [];
if (isset($expectedResult[AttributeMetadata::VALIDATION_RULES])) {
$expectedRules = $expectedResult[AttributeMetadata::VALIDATION_RULES];
unset($expectedResult[AttributeMetadata::VALIDATION_RULES]);
}
if (isset($actualResult[AttributeMetadata::VALIDATION_RULES])) {
$actualRules = $actualResult[AttributeMetadata::VALIDATION_RULES];
unset($actualResult[AttributeMetadata::VALIDATION_RULES]);
}
if (is_array($expectedRules) && is_array($actualRules)) {
foreach ($expectedRules as $expectedRule) {
if (isset($expectedRule['name']) && isset($expectedRule['value'])) {
$found = false;
foreach ($actualRules as $actualRule) {
if (isset($actualRule['name']) && isset($actualRule['value'])) {
if ($expectedRule['name'] == $actualRule['name']
&& $expectedRule['value'] == $actualRule['value']
) {
$found = true;
break;
}
}
}
$this->assertTrue($found);
}
}
}
return [$expectedResult, $actualResult];
}
/**
* Check specific attribute validation rules in set of multiple attributes
*
* @param array $expectedResult Set of expected attribute metadata
* @param array $actualResultSet Set of actual attribute metadata
* @return array
*/
public function checkMultipleAttributesValidationRules($expectedResult, $actualResultSet)
{
if (is_array($expectedResult) && is_array($actualResultSet)) {
if (isset($expectedResult[AttributeMetadata::ATTRIBUTE_CODE])) {
foreach ($actualResultSet as $actualAttributeKey => $actualAttribute) {
if (isset($actualAttribute[AttributeMetadata::ATTRIBUTE_CODE])
&& $expectedResult[AttributeMetadata::ATTRIBUTE_CODE]
== $actualAttribute[AttributeMetadata::ATTRIBUTE_CODE]
) {
$this->checkValidationRules($expectedResult, $actualAttribute);
unset($actualResultSet[$actualAttributeKey][AttributeMetadata::VALIDATION_RULES]);
}
}
unset($expectedResult[AttributeMetadata::VALIDATION_RULES]);
}
}
return [$expectedResult, $actualResultSet];
}
}