7
7
8
8
namespace Magento \Catalog \Controller \Adminhtml \Product \Set ;
9
9
10
+ use Magento \Catalog \Api \Data \ProductAttributeInterface ;
11
+ use Magento \Catalog \Api \Data \ProductInterface ;
12
+ use Magento \Catalog \Api \ProductRepositoryInterface ;
13
+ use Magento \Catalog \Model \Product \Attribute \Repository ;
14
+ use Magento \Developer \Model \Logger \Handler \Syslog ;
15
+ use Magento \Eav \Api \AttributeManagementInterface ;
10
16
use Magento \Eav \Api \AttributeSetRepositoryInterface ;
11
17
use Magento \Eav \Api \Data \AttributeSetInterface ;
18
+ use Magento \Eav \Model \Config ;
19
+ use Magento \Framework \Api \DataObjectHelper ;
12
20
use Magento \Framework \Api \SearchCriteriaBuilder ;
13
- use Magento \TestFramework \Helper \Bootstrap ;
14
21
use Magento \Framework \App \Request \Http as HttpRequest ;
15
- use Magento \Eav \Api \AttributeManagementInterface ;
16
- use Magento \Catalog \Api \Data \ProductInterfaceFactory ;
17
- use Magento \Framework \Api \DataObjectHelper ;
18
- use Magento \Catalog \Api \ProductRepositoryInterface ;
19
- use Magento \Catalog \Api \Data \ProductInterface ;
20
- use Magento \Developer \Model \Logger \Handler \Syslog ;
22
+ use Magento \Framework \Logger \Handler \System ;
21
23
use Magento \Framework \Logger \Monolog ;
22
- use Magento \Catalog \Model \Product \Attribute \Repository ;
24
+ use Magento \Framework \Message \MessageInterface ;
25
+ use Magento \Framework \Serialize \Serializer \Json ;
26
+ use Magento \TestFramework \Helper \Bootstrap ;
27
+ use Magento \TestFramework \TestCase \AbstractBackendController ;
23
28
24
29
/**
25
- * Test save attribute set
30
+ * Testing for saving an existing or creating a new attribute set.
26
31
*
27
32
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
28
33
*/
29
- class SaveTest extends \ Magento \ TestFramework \ TestCase \ AbstractBackendController
34
+ class SaveTest extends AbstractBackendController
30
35
{
31
36
/**
32
37
* @var string
@@ -63,6 +68,21 @@ class SaveTest extends \Magento\TestFramework\TestCase\AbstractBackendController
63
68
*/
64
69
private $ attributeRepository ;
65
70
71
+ /**
72
+ * @var AttributeSetRepositoryInterface
73
+ */
74
+ private $ attributeSetRepository ;
75
+
76
+ /**
77
+ * @var Config
78
+ */
79
+ private $ eavConfig ;
80
+
81
+ /**
82
+ * @var Json
83
+ */
84
+ private $ json ;
85
+
66
86
/**
67
87
* @inheritDoc
68
88
*/
@@ -80,11 +100,13 @@ public function setUp()
80
100
$ this ->productRepository = $ this ->_objectManager ->get (ProductRepositoryInterface::class);
81
101
$ this ->attributeRepository = $ this ->_objectManager ->get (Repository::class);
82
102
$ this ->dataObjectHelper = $ this ->_objectManager ->get (DataObjectHelper::class);
103
+ $ this ->attributeSetRepository = $ this ->_objectManager ->get (AttributeSetRepositoryInterface::class);
104
+ $ this ->eavConfig = $ this ->_objectManager ->get (Config::class);
105
+ $ this ->json = $ this ->_objectManager ->get (Json::class);
83
106
}
84
107
85
108
/**
86
109
* @inheritdoc
87
- * @throws \Magento\Framework\Exception\FileSystemException
88
110
*/
89
111
public function tearDown ()
90
112
{
@@ -93,17 +115,76 @@ public function tearDown()
93
115
}
94
116
95
117
/**
118
+ * Test that new attribute set based on default attribute set will be successfully created.
119
+ *
120
+ * @magentoDbIsolation enabled
121
+ *
122
+ * @return void
123
+ */
124
+ public function testCreateNewAttributeSetBasedOnDefaultAttributeSet (): void
125
+ {
126
+ $ this ->createAttributeSetBySkeletonAndAssert (
127
+ 'Attribute set name for test ' ,
128
+ $ this ->getCatalogProductDefaultAttributeSetId ()
129
+ );
130
+ }
131
+
132
+ /**
133
+ * Test that new attribute set based on custom attribute set will be successfully created.
134
+ *
96
135
* @magentoDataFixture Magento/Catalog/_files/attribute_set_with_renamed_group.php
136
+ *
137
+ * @magentoDbIsolation enabled
138
+ *
139
+ * @return void
140
+ */
141
+ public function testCreateNewAttributeSetBasedOnCustomAttributeSet (): void
142
+ {
143
+ $ existCustomAttributeSet = $ this ->getAttributeSetByName ('attribute_set_test ' );
144
+ $ this ->createAttributeSetBySkeletonAndAssert (
145
+ 'Attribute set name for test ' ,
146
+ (int )$ existCustomAttributeSet ->getAttributeSetId ()
147
+ );
148
+ }
149
+
150
+ /**
151
+ * Test that new attribute set based on custom attribute set will be successfully created.
152
+ *
153
+ * @magentoDbIsolation enabled
154
+ *
155
+ * @return void
156
+ */
157
+ public function testGotErrorDuringCreateAttributeSetWithoutName (): void
158
+ {
159
+ $ this ->getRequest ()->setMethod (HttpRequest::METHOD_POST );
160
+ $ this ->getRequest ()->setPostValue (
161
+ [
162
+ 'gotoEdit ' => '1 ' ,
163
+ 'skeleton_set ' => $ this ->getCatalogProductDefaultAttributeSetId (),
164
+ ]
165
+ );
166
+ $ this ->dispatch ('backend/catalog/product_set/save/ ' );
167
+ $ this ->assertSessionMessages (
168
+ $ this ->equalTo ([(string )__ ('The attribute set name is empty. Enter the name and try again. ' )]),
169
+ MessageInterface::TYPE_ERROR
170
+ );
171
+ }
172
+
173
+ /**
174
+ * Test that exception throws during save attribute set name process if name of attribute set already exists.
175
+ *
176
+ * @magentoDataFixture Magento/Catalog/_files/attribute_set_with_renamed_group.php
177
+ * @return void
97
178
*/
98
- public function testAlreadyExistsExceptionProcessingWhenGroupCodeIsDuplicated ()
179
+ public function testAlreadyExistsExceptionProcessingWhenGroupCodeIsDuplicated (): void
99
180
{
100
181
$ attributeSet = $ this ->getAttributeSetByName ('attribute_set_test ' );
101
182
$ this ->assertNotEmpty ($ attributeSet , 'Attribute set with name "attribute_set_test" is missed ' );
102
183
103
184
$ this ->getRequest ()->setMethod (HttpRequest::METHOD_POST );
104
185
$ this ->getRequest ()->setPostValue (
105
186
'data ' ,
106
- json_encode (
187
+ $ this -> json -> serialize (
107
188
[
108
189
'attribute_set_name ' => 'attribute_set_test ' ,
109
190
'groups ' => [
@@ -119,44 +200,25 @@ public function testAlreadyExistsExceptionProcessingWhenGroupCodeIsDuplicated()
119
200
);
120
201
$ this ->dispatch ('backend/catalog/product_set/save/id/ ' . $ attributeSet ->getAttributeSetId ());
121
202
122
- $ jsonResponse = json_decode ($ this ->getResponse ()->getBody ());
203
+ $ jsonResponse = $ this -> json -> unserialize ($ this ->getResponse ()->getBody ());
123
204
$ this ->assertNotNull ($ jsonResponse );
124
- $ this ->assertEquals (1 , $ jsonResponse-> error );
205
+ $ this ->assertEquals (1 , $ jsonResponse[ ' error ' ] );
125
206
$ this ->assertContains (
126
- 'Attribute group with same code already exist. Please rename "attribute-group-name" group ' ,
127
- $ jsonResponse-> message
207
+ ( string ) __ ( 'Attribute group with same code already exist. Please rename "attribute-group-name" group ' ) ,
208
+ $ jsonResponse[ ' message ' ]
128
209
);
129
210
}
130
211
131
- /**
132
- * @param string $attributeSetName
133
- * @return AttributeSetInterface|null
134
- */
135
- protected function getAttributeSetByName ($ attributeSetName )
136
- {
137
- $ objectManager = Bootstrap::getObjectManager ();
138
-
139
- /** @var SearchCriteriaBuilder $searchCriteriaBuilder */
140
- $ searchCriteriaBuilder = $ objectManager ->get (SearchCriteriaBuilder::class);
141
- $ searchCriteriaBuilder ->addFilter ('attribute_set_name ' , $ attributeSetName );
142
-
143
- /** @var AttributeSetRepositoryInterface $attributeSetRepository */
144
- $ attributeSetRepository = $ objectManager ->get (AttributeSetRepositoryInterface::class);
145
- $ result = $ attributeSetRepository ->getList ($ searchCriteriaBuilder ->create ());
146
-
147
- $ items = $ result ->getItems ();
148
- return $ result ->getTotalCount () ? array_pop ($ items ) : null ;
149
- }
150
-
151
212
/**
152
213
* Test behavior when attribute set was changed to a new set
153
- * with deleted attribute from the previous set
214
+ * with deleted attribute from the previous set.
154
215
*
155
216
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
156
217
* @magentoDataFixture Magento/Catalog/_files/attribute_set_based_on_default.php
157
218
* @magentoDbIsolation disabled
219
+ * @return void
158
220
*/
159
- public function testRemoveAttributeFromAttributeSet ()
221
+ public function testRemoveAttributeFromAttributeSet (): void
160
222
{
161
223
$ message = 'Attempt to load value of nonexistent EAV attribute ' ;
162
224
$ this ->removeSyslog ();
@@ -178,15 +240,15 @@ public function testRemoveAttributeFromAttributeSet()
178
240
}
179
241
180
242
/**
181
- * Retrieve system.log file path
243
+ * Retrieve system.log file path.
182
244
*
183
245
* @return string
184
246
*/
185
247
private function getSyslogPath (): string
186
248
{
187
249
if (!$ this ->systemLogPath ) {
188
250
foreach ($ this ->logger ->getHandlers () as $ handler ) {
189
- if ($ handler instanceof \ Magento \ Framework \ Logger \ Handler \ System) {
251
+ if ($ handler instanceof System) {
190
252
$ this ->systemLogPath = $ handler ->getUrl ();
191
253
}
192
254
}
@@ -200,11 +262,103 @@ private function getSyslogPath(): string
200
262
*
201
263
* @return void
202
264
*/
203
- private function removeSyslog ()
265
+ private function removeSyslog (): void
204
266
{
205
267
$ this ->syslogHandler ->close ();
206
268
if (file_exists ($ this ->getSyslogPath ())) {
207
269
unlink ($ this ->getSyslogPath ());
208
270
}
209
271
}
272
+
273
+ /**
274
+ * Search and return attribute set by name.
275
+ *
276
+ * @param string $attributeSetName
277
+ * @return AttributeSetInterface|null
278
+ */
279
+ private function getAttributeSetByName (string $ attributeSetName ): ?AttributeSetInterface
280
+ {
281
+ /** @var SearchCriteriaBuilder $searchCriteriaBuilder */
282
+ $ searchCriteriaBuilder = $ this ->_objectManager ->get (SearchCriteriaBuilder::class);
283
+ $ searchCriteriaBuilder ->addFilter ('attribute_set_name ' , $ attributeSetName );
284
+ $ result = $ this ->attributeSetRepository ->getList ($ searchCriteriaBuilder ->create ());
285
+
286
+ $ items = $ result ->getItems ();
287
+
288
+ return array_pop ($ items );
289
+ }
290
+
291
+ /**
292
+ * Create attribute set by skeleton attribute set id and assert that attribute set
293
+ * created successfully and attributes from skeleton attribute set and created attribute set are equals.
294
+ *
295
+ * @param string $attributeSetName
296
+ * @param int $skeletonAttributeSetId
297
+ * @return void
298
+ */
299
+ private function createAttributeSetBySkeletonAndAssert (
300
+ string $ attributeSetName ,
301
+ int $ skeletonAttributeSetId
302
+ ): void {
303
+ $ this ->getRequest ()->setMethod (HttpRequest::METHOD_POST );
304
+ $ this ->getRequest ()->setPostValue (
305
+ [
306
+ 'attribute_set_name ' => $ attributeSetName ,
307
+ 'gotoEdit ' => '1 ' ,
308
+ 'skeleton_set ' => $ skeletonAttributeSetId ,
309
+ ]
310
+ );
311
+ $ this ->dispatch ('backend/catalog/product_set/save/ ' );
312
+ $ this ->assertSessionMessages (
313
+ $ this ->equalTo ([(string )__ ('You saved the attribute set. ' )]),
314
+ MessageInterface::TYPE_SUCCESS
315
+ );
316
+ $ createdAttributeSet = $ this ->getAttributeSetByName ($ attributeSetName );
317
+ $ existAttributeSet = $ this ->attributeSetRepository ->get ($ skeletonAttributeSetId );
318
+
319
+ $ this ->assertNotNull ($ createdAttributeSet );
320
+ $ this ->assertEquals ($ attributeSetName , $ createdAttributeSet ->getAttributeSetName ());
321
+
322
+ $ this ->assertAttributeSetsAttributesAreEquals ($ createdAttributeSet , $ existAttributeSet );
323
+ }
324
+
325
+ /**
326
+ * Assert that both attribute sets contains identical attributes by attribute ids.
327
+ *
328
+ * @param AttributeSetInterface $createdAttributeSet
329
+ * @param AttributeSetInterface $existAttributeSet
330
+ * @return void
331
+ */
332
+ private function assertAttributeSetsAttributesAreEquals (
333
+ AttributeSetInterface $ createdAttributeSet ,
334
+ AttributeSetInterface $ existAttributeSet
335
+ ): void {
336
+ $ expectedAttributeIds = array_keys (
337
+ $ this ->attributeManagement ->getAttributes (
338
+ ProductAttributeInterface::ENTITY_TYPE_CODE ,
339
+ $ existAttributeSet ->getAttributeSetId ()
340
+ )
341
+ );
342
+ sort ($ expectedAttributeIds );
343
+ $ actualAttributeIds = array_keys (
344
+ $ this ->attributeManagement ->getAttributes (
345
+ ProductAttributeInterface::ENTITY_TYPE_CODE ,
346
+ $ createdAttributeSet ->getAttributeSetId ()
347
+ )
348
+ );
349
+ sort ($ actualAttributeIds );
350
+ $ this ->assertSame ($ expectedAttributeIds , $ actualAttributeIds );
351
+ }
352
+
353
+ /**
354
+ * Retrieve default catalog product attribute set ID.
355
+ *
356
+ * @return int
357
+ */
358
+ private function getCatalogProductDefaultAttributeSetId (): int
359
+ {
360
+ return (int )$ this ->eavConfig
361
+ ->getEntityType (ProductAttributeInterface::ENTITY_TYPE_CODE )
362
+ ->getDefaultAttributeSetId ();
363
+ }
210
364
}
0 commit comments