6
6
7
7
namespace Magento \Customer \Api ;
8
8
9
+ use Magento \Config \Model \ResourceModel \Config ;
9
10
use Magento \Customer \Api \Data \AddressInterface as Address ;
10
11
use Magento \Customer \Model \Data \AttributeMetadata ;
12
+ use Magento \Framework \App \Config \ReinitableConfigInterface ;
13
+ use Magento \TestFramework \ObjectManager ;
11
14
use Magento \TestFramework \TestCase \WebapiAbstract ;
12
15
13
16
/**
@@ -19,15 +22,40 @@ class AddressMetadataTest extends WebapiAbstract
19
22
const SERVICE_VERSION = "V1 " ;
20
23
const RESOURCE_PATH = "/V1/attributeMetadata/customerAddress " ;
21
24
25
+ /**
26
+ * @var Config $config
27
+ */
28
+ private $ resourceConfig ;
29
+
30
+ /**
31
+ * @var ReinitableConfigInterface
32
+ */
33
+ private $ reinitConfig ;
34
+
35
+ /**
36
+ * @inheritdoc
37
+ */
38
+ protected function setUp ()
39
+ {
40
+ parent ::setUp ();
41
+
42
+ $ objectManager = ObjectManager::getInstance ();
43
+ $ this ->resourceConfig = $ objectManager ->get (Config::class);
44
+ $ this ->reinitConfig = $ objectManager ->get (ReinitableConfigInterface::class);
45
+ }
46
+
22
47
/**
23
48
* Test retrieval of attribute metadata for the address entity type.
24
49
*
25
50
* @param string $attributeCode The attribute code of the requested metadata.
26
51
* @param array $expectedMetadata Expected entity metadata for the attribute code.
27
52
* @dataProvider getAttributeMetadataDataProvider
53
+ * @magentoDbIsolation disabled
28
54
*/
29
- public function testGetAttributeMetadata ($ attributeCode , $ expectedMetadata )
55
+ public function testGetAttributeMetadata ($ attributeCode , $ configOptions , $ expectedMetadata )
30
56
{
57
+ $ this ->initConfig ($ configOptions );
58
+
31
59
$ serviceInfo = [
32
60
'rest ' => [
33
61
'resourcePath ' => self ::RESOURCE_PATH . "/attribute/ $ attributeCode " ,
@@ -54,12 +82,14 @@ public function testGetAttributeMetadata($attributeCode, $expectedMetadata)
54
82
* Data provider for testGetAttributeMetadata.
55
83
*
56
84
* @return array
85
+ * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
57
86
*/
58
87
public function getAttributeMetadataDataProvider ()
59
88
{
60
89
return [
61
90
Address::POSTCODE => [
62
91
Address::POSTCODE ,
92
+ [],
63
93
[
64
94
AttributeMetadata::FRONTEND_INPUT => 'text ' ,
65
95
AttributeMetadata::INPUT_FILTER => '' ,
@@ -83,7 +113,85 @@ public function getAttributeMetadataDataProvider()
83
113
AttributeMetadata::IS_SEARCHABLE_IN_GRID => true ,
84
114
AttributeMetadata::ATTRIBUTE_CODE => 'postcode ' ,
85
115
],
86
- ]
116
+ ],
117
+ 'prefix ' => [
118
+ 'prefix ' ,
119
+ [
120
+ ['path ' => 'customer/address/prefix_show ' , 'value ' => 'opt ' ],
121
+ ['path ' => 'customer/address/prefix_options ' , 'value ' => 'prefA;prefB ' ]
122
+ ],
123
+ [
124
+ AttributeMetadata::FRONTEND_INPUT => 'text ' ,
125
+ AttributeMetadata::INPUT_FILTER => '' ,
126
+ AttributeMetadata::STORE_LABEL => 'Name Prefix ' ,
127
+ AttributeMetadata::MULTILINE_COUNT => 0 ,
128
+ AttributeMetadata::VALIDATION_RULES => [],
129
+ AttributeMetadata::VISIBLE => false ,
130
+ AttributeMetadata::REQUIRED => false ,
131
+ AttributeMetadata::DATA_MODEL => '' ,
132
+ AttributeMetadata::OPTIONS => [
133
+ [
134
+ 'label ' => 'prefA ' ,
135
+ 'value ' => 'prefA ' ,
136
+ ],
137
+ [
138
+ 'label ' => 'prefB ' ,
139
+ 'value ' => 'prefB ' ,
140
+ ],
141
+ ],
142
+ AttributeMetadata::FRONTEND_CLASS => '' ,
143
+ AttributeMetadata::USER_DEFINED => false ,
144
+ AttributeMetadata::SORT_ORDER => 10 ,
145
+ AttributeMetadata::FRONTEND_LABEL => 'Name Prefix ' ,
146
+ AttributeMetadata::NOTE => '' ,
147
+ AttributeMetadata::SYSTEM => false ,
148
+ AttributeMetadata::BACKEND_TYPE => 'static ' ,
149
+ AttributeMetadata::IS_USED_IN_GRID => false ,
150
+ AttributeMetadata::IS_VISIBLE_IN_GRID => false ,
151
+ AttributeMetadata::IS_FILTERABLE_IN_GRID => false ,
152
+ AttributeMetadata::IS_SEARCHABLE_IN_GRID => false ,
153
+ AttributeMetadata::ATTRIBUTE_CODE => 'prefix ' ,
154
+ ],
155
+ ],
156
+ 'suffix ' => [
157
+ 'suffix ' ,
158
+ [
159
+ ['path ' => 'customer/address/suffix_show ' , 'value ' => 'opt ' ],
160
+ ['path ' => 'customer/address/suffix_options ' , 'value ' => 'suffA;suffB ' ]
161
+ ],
162
+ [
163
+ AttributeMetadata::FRONTEND_INPUT => 'text ' ,
164
+ AttributeMetadata::INPUT_FILTER => '' ,
165
+ AttributeMetadata::STORE_LABEL => 'Name Suffix ' ,
166
+ AttributeMetadata::MULTILINE_COUNT => 0 ,
167
+ AttributeMetadata::VALIDATION_RULES => [],
168
+ AttributeMetadata::VISIBLE => false ,
169
+ AttributeMetadata::REQUIRED => false ,
170
+ AttributeMetadata::DATA_MODEL => '' ,
171
+ AttributeMetadata::OPTIONS => [
172
+ [
173
+ 'label ' => 'suffA ' ,
174
+ 'value ' => 'suffA ' ,
175
+ ],
176
+ [
177
+ 'label ' => 'suffB ' ,
178
+ 'value ' => 'suffB ' ,
179
+ ],
180
+ ],
181
+ AttributeMetadata::FRONTEND_CLASS => '' ,
182
+ AttributeMetadata::USER_DEFINED => false ,
183
+ AttributeMetadata::SORT_ORDER => 50 ,
184
+ AttributeMetadata::FRONTEND_LABEL => 'Name Suffix ' ,
185
+ AttributeMetadata::NOTE => '' ,
186
+ AttributeMetadata::SYSTEM => false ,
187
+ AttributeMetadata::BACKEND_TYPE => 'static ' ,
188
+ AttributeMetadata::IS_USED_IN_GRID => false ,
189
+ AttributeMetadata::IS_VISIBLE_IN_GRID => false ,
190
+ AttributeMetadata::IS_FILTERABLE_IN_GRID => false ,
191
+ AttributeMetadata::IS_SEARCHABLE_IN_GRID => false ,
192
+ AttributeMetadata::ATTRIBUTE_CODE => 'suffix ' ,
193
+ ],
194
+ ],
87
195
];
88
196
}
89
197
@@ -106,7 +214,7 @@ public function testGetAllAttributesMetadata()
106
214
107
215
$ attributeMetadata = $ this ->_webApiCall ($ serviceInfo );
108
216
$ this ->assertCount (19 , $ attributeMetadata );
109
- $ postcode = $ this ->getAttributeMetadataDataProvider ()[Address::POSTCODE ][1 ];
217
+ $ postcode = $ this ->getAttributeMetadataDataProvider ()[Address::POSTCODE ][2 ];
110
218
$ validationResult = $ this ->checkMultipleAttributesValidationRules ($ postcode , $ attributeMetadata );
111
219
list ($ postcode , $ attributeMetadata ) = $ validationResult ;
112
220
$ this ->assertContains ($ postcode , $ attributeMetadata );
@@ -187,7 +295,7 @@ public function getAttributesDataProvider()
187
295
return [
188
296
[
189
297
'customer_address_edit ' ,
190
- $ attributeMetadata [Address::POSTCODE ][1 ],
298
+ $ attributeMetadata [Address::POSTCODE ][2 ],
191
299
]
192
300
];
193
301
}
@@ -200,6 +308,7 @@ public function getAttributesDataProvider()
200
308
* @param array $actualResult
201
309
* @return array
202
310
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
311
+ * phpcs:disable Generic.Metrics.NestingLevel
203
312
*/
204
313
public function checkValidationRules ($ expectedResult , $ actualResult )
205
314
{
@@ -235,6 +344,7 @@ public function checkValidationRules($expectedResult, $actualResult)
235
344
}
236
345
return [$ expectedResult , $ actualResult ];
237
346
}
347
+ //phpcs:enable
238
348
239
349
/**
240
350
* Check specific attribute validation rules in set of multiple attributes
@@ -277,4 +387,19 @@ public static function tearDownAfterClass()
277
387
$ attribute ->delete ();
278
388
}
279
389
}
390
+
391
+ /**
392
+ * Set core config data.
393
+ *
394
+ * @param $configOptions
395
+ */
396
+ private function initConfig (array $ configOptions ): void
397
+ {
398
+ if ($ configOptions ) {
399
+ foreach ($ configOptions as $ option ) {
400
+ $ this ->resourceConfig ->saveConfig ($ option ['path ' ], $ option ['value ' ]);
401
+ }
402
+ }
403
+ $ this ->reinitConfig ->reinit ();
404
+ }
280
405
}
0 commit comments