15
15
use Magento \Eav \Api \AttributeManagementInterface ;
16
16
use Magento \Eav \Api \AttributeSetRepositoryInterface ;
17
17
use Magento \Eav \Api \Data \AttributeSetInterface ;
18
+ use Magento \Eav \Model \Config ;
18
19
use Magento \Framework \Api \DataObjectHelper ;
19
20
use Magento \Framework \Api \SearchCriteriaBuilder ;
20
21
use Magento \Framework \App \Request \Http as HttpRequest ;
21
22
use Magento \Framework \Logger \Handler \System ;
22
23
use Magento \Framework \Logger \Monolog ;
23
24
use Magento \Framework \Message \MessageInterface ;
25
+ use Magento \Framework \Serialize \Serializer \Json ;
24
26
use Magento \TestFramework \Helper \Bootstrap ;
25
27
use Magento \TestFramework \TestCase \AbstractBackendController ;
26
28
@@ -71,6 +73,16 @@ class SaveTest extends AbstractBackendController
71
73
*/
72
74
private $ attributeSetRepository ;
73
75
76
+ /**
77
+ * @var Config
78
+ */
79
+ private $ eavConfig ;
80
+
81
+ /**
82
+ * @var Json
83
+ */
84
+ private $ json ;
85
+
74
86
/**
75
87
* @inheritDoc
76
88
*/
@@ -89,6 +101,8 @@ public function setUp()
89
101
$ this ->attributeRepository = $ this ->_objectManager ->get (Repository::class);
90
102
$ this ->dataObjectHelper = $ this ->_objectManager ->get (DataObjectHelper::class);
91
103
$ this ->attributeSetRepository = $ this ->_objectManager ->get (AttributeSetRepositoryInterface::class);
104
+ $ this ->eavConfig = $ this ->_objectManager ->get (Config::class);
105
+ $ this ->json = $ this ->_objectManager ->get (Json::class);
92
106
}
93
107
94
108
/**
@@ -109,7 +123,10 @@ public function tearDown()
109
123
*/
110
124
public function testCreateNewAttributeSetBasedOnDefaultAttributeSet (): void
111
125
{
112
- $ this ->createAttributeSetBySkeletonAndAssert ('Attribute set name for test ' , 4 );
126
+ $ this ->createAttributeSetBySkeletonAndAssert (
127
+ 'Attribute set name for test ' ,
128
+ $ this ->getCatalogProductDefaultAttributeSetId ()
129
+ );
113
130
}
114
131
115
132
/**
@@ -143,12 +160,12 @@ public function testGotErrorDuringCreateAttributeSetWithoutName(): void
143
160
$ this ->getRequest ()->setPostValue (
144
161
[
145
162
'gotoEdit ' => '1 ' ,
146
- 'skeleton_set ' => 4 ,
163
+ 'skeleton_set ' => $ this -> getCatalogProductDefaultAttributeSetId () ,
147
164
]
148
165
);
149
166
$ this ->dispatch ('backend/catalog/product_set/save/ ' );
150
167
$ this ->assertSessionMessages (
151
- $ this ->contains ( 'The attribute set name is empty. Enter the name and try again. ' ),
168
+ $ this ->equalTo ([( string ) __ ( 'The attribute set name is empty. Enter the name and try again. ' )] ),
152
169
MessageInterface::TYPE_ERROR
153
170
);
154
171
}
@@ -167,7 +184,7 @@ public function testAlreadyExistsExceptionProcessingWhenGroupCodeIsDuplicated():
167
184
$ this ->getRequest ()->setMethod (HttpRequest::METHOD_POST );
168
185
$ this ->getRequest ()->setPostValue (
169
186
'data ' ,
170
- json_encode (
187
+ $ this -> json -> serialize (
171
188
[
172
189
'attribute_set_name ' => 'attribute_set_test ' ,
173
190
'groups ' => [
@@ -183,12 +200,12 @@ public function testAlreadyExistsExceptionProcessingWhenGroupCodeIsDuplicated():
183
200
);
184
201
$ this ->dispatch ('backend/catalog/product_set/save/id/ ' . $ attributeSet ->getAttributeSetId ());
185
202
186
- $ jsonResponse = json_decode ($ this ->getResponse ()->getBody ());
203
+ $ jsonResponse = $ this -> json -> unserialize ($ this ->getResponse ()->getBody ());
187
204
$ this ->assertNotNull ($ jsonResponse );
188
- $ this ->assertEquals (1 , $ jsonResponse-> error );
205
+ $ this ->assertEquals (1 , $ jsonResponse[ ' error ' ] );
189
206
$ this ->assertContains (
190
- 'Attribute group with same code already exist. Please rename "attribute-group-name" group ' ,
191
- $ jsonResponse-> message
207
+ ( string ) __ ( 'Attribute group with same code already exist. Please rename "attribute-group-name" group ' ) ,
208
+ $ jsonResponse[ ' message ' ]
192
209
);
193
210
}
194
211
@@ -265,9 +282,10 @@ private function getAttributeSetByName(string $attributeSetName): ?AttributeSetI
265
282
$ searchCriteriaBuilder = $ this ->_objectManager ->get (SearchCriteriaBuilder::class);
266
283
$ searchCriteriaBuilder ->addFilter ('attribute_set_name ' , $ attributeSetName );
267
284
$ result = $ this ->attributeSetRepository ->getList ($ searchCriteriaBuilder ->create ());
285
+
268
286
$ items = $ result ->getItems ();
269
287
270
- return $ result -> getTotalCount () ? array_pop ($ items ) : null ;
288
+ return array_pop ($ items );
271
289
}
272
290
273
291
/**
@@ -276,6 +294,7 @@ private function getAttributeSetByName(string $attributeSetName): ?AttributeSetI
276
294
*
277
295
* @param string $attributeSetName
278
296
* @param int $skeletonAttributeSetId
297
+ * @return void
279
298
*/
280
299
private function createAttributeSetBySkeletonAndAssert (
281
300
string $ attributeSetName ,
@@ -308,6 +327,7 @@ private function createAttributeSetBySkeletonAndAssert(
308
327
*
309
328
* @param AttributeSetInterface $createdAttributeSet
310
329
* @param AttributeSetInterface $existAttributeSet
330
+ * @return void
311
331
*/
312
332
private function assertAttributeSetsAttributesAreEquals (
313
333
AttributeSetInterface $ createdAttributeSet ,
@@ -327,7 +347,19 @@ private function assertAttributeSetsAttributesAreEquals(
327
347
);
328
348
$ this ->assertEquals (count ($ expectedAttributeIds ), count ($ actualAttributeIds ));
329
349
foreach ($ actualAttributeIds as $ attributeId ) {
330
- $ this ->assertTrue ( in_array ( $ attributeId , $ expectedAttributeIds, true ) );
350
+ $ this ->assertContains ( $ attributeId , $ expectedAttributeIds );
331
351
}
332
352
}
353
+
354
+ /**
355
+ * Retrieve default catalog product attribute set ID.
356
+ *
357
+ * @return int
358
+ */
359
+ private function getCatalogProductDefaultAttributeSetId (): int
360
+ {
361
+ return (int )$ this ->eavConfig
362
+ ->getEntityType (ProductAttributeInterface::ENTITY_TYPE_CODE )
363
+ ->getDefaultAttributeSetId ();
364
+ }
333
365
}
0 commit comments