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 \Framework \Api \DataObjectHelper ;
12
19
use Magento \Framework \Api \SearchCriteriaBuilder ;
13
- use Magento \TestFramework \Helper \Bootstrap ;
14
20
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 ;
21
+ use Magento \Framework \Logger \Handler \System ;
21
22
use Magento \Framework \Logger \Monolog ;
22
- use Magento \Catalog \Model \Product \Attribute \Repository ;
23
+ use Magento \Framework \Message \MessageInterface ;
24
+ use Magento \TestFramework \Helper \Bootstrap ;
25
+ use Magento \TestFramework \TestCase \AbstractBackendController ;
23
26
24
27
/**
25
- * Test save attribute set
28
+ * Testing for saving an existing or creating a new attribute set.
26
29
*
27
30
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
28
31
*/
29
- class SaveTest extends \ Magento \ TestFramework \ TestCase \ AbstractBackendController
32
+ class SaveTest extends AbstractBackendController
30
33
{
31
34
/**
32
35
* @var string
@@ -63,6 +66,11 @@ class SaveTest extends \Magento\TestFramework\TestCase\AbstractBackendController
63
66
*/
64
67
private $ attributeRepository ;
65
68
69
+ /**
70
+ * @var AttributeSetRepositoryInterface
71
+ */
72
+ private $ attributeSetRepository ;
73
+
66
74
/**
67
75
* @inheritDoc
68
76
*/
@@ -80,11 +88,11 @@ public function setUp()
80
88
$ this ->productRepository = $ this ->_objectManager ->get (ProductRepositoryInterface::class);
81
89
$ this ->attributeRepository = $ this ->_objectManager ->get (Repository::class);
82
90
$ this ->dataObjectHelper = $ this ->_objectManager ->get (DataObjectHelper::class);
91
+ $ this ->attributeSetRepository = $ this ->_objectManager ->get (AttributeSetRepositoryInterface::class);
83
92
}
84
93
85
94
/**
86
95
* @inheritdoc
87
- * @throws \Magento\Framework\Exception\FileSystemException
88
96
*/
89
97
public function tearDown ()
90
98
{
@@ -93,9 +101,65 @@ public function tearDown()
93
101
}
94
102
95
103
/**
104
+ * Test that new attribute set based on default attribute set will be successfully created.
105
+ *
106
+ * @magentoDbIsolation enabled
107
+ *
108
+ * @return void
109
+ */
110
+ public function testCreateNewAttributeSetBasedOnDefaultAttributeSet (): void
111
+ {
112
+ $ this ->createAttributeSetBySkeletonAndAssert ('Attribute set name for test ' , 4 );
113
+ }
114
+
115
+ /**
116
+ * Test that new attribute set based on custom attribute set will be successfully created.
117
+ *
118
+ * @magentoDataFixture Magento/Catalog/_files/attribute_set_with_renamed_group.php
119
+ *
120
+ * @magentoDbIsolation enabled
121
+ *
122
+ * @return void
123
+ */
124
+ public function testCreateNewAttributeSetBasedOnCustomAttributeSet (): void
125
+ {
126
+ $ existCustomAttributeSet = $ this ->getAttributeSetByName ('attribute_set_test ' );
127
+ $ this ->createAttributeSetBySkeletonAndAssert (
128
+ 'Attribute set name for test ' ,
129
+ (int )$ existCustomAttributeSet ->getAttributeSetId ()
130
+ );
131
+ }
132
+
133
+ /**
134
+ * Test that new attribute set based on custom attribute set will be successfully created.
135
+ *
136
+ * @magentoDbIsolation enabled
137
+ *
138
+ * @return void
139
+ */
140
+ public function testGotErrorDuringCreateAttributeSetWithoutName (): void
141
+ {
142
+ $ this ->getRequest ()->setMethod (HttpRequest::METHOD_POST );
143
+ $ this ->getRequest ()->setPostValue (
144
+ [
145
+ 'gotoEdit ' => '1 ' ,
146
+ 'skeleton_set ' => 4 ,
147
+ ]
148
+ );
149
+ $ this ->dispatch ('backend/catalog/product_set/save/ ' );
150
+ $ this ->assertSessionMessages (
151
+ $ this ->contains ('The attribute set name is empty. Enter the name and try again. ' ),
152
+ MessageInterface::TYPE_ERROR
153
+ );
154
+ }
155
+
156
+ /**
157
+ * Test that exception throws during save attribute set name process if name of attribute set already exists.
158
+ *
96
159
* @magentoDataFixture Magento/Catalog/_files/attribute_set_with_renamed_group.php
160
+ * @return void
97
161
*/
98
- public function testAlreadyExistsExceptionProcessingWhenGroupCodeIsDuplicated ()
162
+ public function testAlreadyExistsExceptionProcessingWhenGroupCodeIsDuplicated (): void
99
163
{
100
164
$ attributeSet = $ this ->getAttributeSetByName ('attribute_set_test ' );
101
165
$ this ->assertNotEmpty ($ attributeSet , 'Attribute set with name "attribute_set_test" is missed ' );
@@ -128,35 +192,16 @@ public function testAlreadyExistsExceptionProcessingWhenGroupCodeIsDuplicated()
128
192
);
129
193
}
130
194
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
195
/**
152
196
* Test behavior when attribute set was changed to a new set
153
- * with deleted attribute from the previous set
197
+ * with deleted attribute from the previous set.
154
198
*
155
199
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
156
200
* @magentoDataFixture Magento/Catalog/_files/attribute_set_based_on_default.php
157
201
* @magentoDbIsolation disabled
202
+ * @return void
158
203
*/
159
- public function testRemoveAttributeFromAttributeSet ()
204
+ public function testRemoveAttributeFromAttributeSet (): void
160
205
{
161
206
$ message = 'Attempt to load value of nonexistent EAV attribute ' ;
162
207
$ this ->removeSyslog ();
@@ -178,15 +223,15 @@ public function testRemoveAttributeFromAttributeSet()
178
223
}
179
224
180
225
/**
181
- * Retrieve system.log file path
226
+ * Retrieve system.log file path.
182
227
*
183
228
* @return string
184
229
*/
185
230
private function getSyslogPath (): string
186
231
{
187
232
if (!$ this ->systemLogPath ) {
188
233
foreach ($ this ->logger ->getHandlers () as $ handler ) {
189
- if ($ handler instanceof \ Magento \ Framework \ Logger \ Handler \ System) {
234
+ if ($ handler instanceof System) {
190
235
$ this ->systemLogPath = $ handler ->getUrl ();
191
236
}
192
237
}
@@ -200,11 +245,89 @@ private function getSyslogPath(): string
200
245
*
201
246
* @return void
202
247
*/
203
- private function removeSyslog ()
248
+ private function removeSyslog (): void
204
249
{
205
250
$ this ->syslogHandler ->close ();
206
251
if (file_exists ($ this ->getSyslogPath ())) {
207
252
unlink ($ this ->getSyslogPath ());
208
253
}
209
254
}
255
+
256
+ /**
257
+ * Search and return attribute set by name.
258
+ *
259
+ * @param string $attributeSetName
260
+ * @return AttributeSetInterface|null
261
+ */
262
+ private function getAttributeSetByName (string $ attributeSetName ): ?AttributeSetInterface
263
+ {
264
+ /** @var SearchCriteriaBuilder $searchCriteriaBuilder */
265
+ $ searchCriteriaBuilder = $ this ->_objectManager ->get (SearchCriteriaBuilder::class);
266
+ $ searchCriteriaBuilder ->addFilter ('attribute_set_name ' , $ attributeSetName );
267
+ $ result = $ this ->attributeSetRepository ->getList ($ searchCriteriaBuilder ->create ());
268
+ $ items = $ result ->getItems ();
269
+
270
+ return $ result ->getTotalCount () ? array_pop ($ items ) : null ;
271
+ }
272
+
273
+ /**
274
+ * Create attribute set by skeleton attribute set id and assert that attribute set
275
+ * created successfully and attributes from skeleton attribute set and created attribute set are equals.
276
+ *
277
+ * @param string $attributeSetName
278
+ * @param int $skeletonAttributeSetId
279
+ */
280
+ private function createAttributeSetBySkeletonAndAssert (
281
+ string $ attributeSetName ,
282
+ int $ skeletonAttributeSetId
283
+ ): void {
284
+ $ this ->getRequest ()->setMethod (HttpRequest::METHOD_POST );
285
+ $ this ->getRequest ()->setPostValue (
286
+ [
287
+ 'attribute_set_name ' => $ attributeSetName ,
288
+ 'gotoEdit ' => '1 ' ,
289
+ 'skeleton_set ' => $ skeletonAttributeSetId ,
290
+ ]
291
+ );
292
+ $ this ->dispatch ('backend/catalog/product_set/save/ ' );
293
+ $ this ->assertSessionMessages (
294
+ $ this ->contains ('You saved the attribute set. ' ),
295
+ MessageInterface::TYPE_SUCCESS
296
+ );
297
+ $ createdAttributeSet = $ this ->getAttributeSetByName ($ attributeSetName );
298
+ $ existAttributeSet = $ this ->attributeSetRepository ->get ($ skeletonAttributeSetId );
299
+
300
+ $ this ->assertNotNull ($ createdAttributeSet );
301
+ $ this ->assertEquals ($ attributeSetName , $ createdAttributeSet ->getAttributeSetName ());
302
+
303
+ $ this ->assertAttributeSetsAttributesAreEquals ($ createdAttributeSet , $ existAttributeSet );
304
+ }
305
+
306
+ /**
307
+ * Assert that both attribute sets contains identical attributes by attribute ids.
308
+ *
309
+ * @param AttributeSetInterface $createdAttributeSet
310
+ * @param AttributeSetInterface $existAttributeSet
311
+ */
312
+ private function assertAttributeSetsAttributesAreEquals (
313
+ AttributeSetInterface $ createdAttributeSet ,
314
+ AttributeSetInterface $ existAttributeSet
315
+ ): void {
316
+ $ expectedAttributeIds = array_keys (
317
+ $ this ->attributeManagement ->getAttributes (
318
+ ProductAttributeInterface::ENTITY_TYPE_CODE ,
319
+ $ existAttributeSet ->getAttributeSetId ()
320
+ )
321
+ );
322
+ $ actualAttributeIds = array_keys (
323
+ $ this ->attributeManagement ->getAttributes (
324
+ ProductAttributeInterface::ENTITY_TYPE_CODE ,
325
+ $ createdAttributeSet ->getAttributeSetId ()
326
+ )
327
+ );
328
+ $ this ->assertEquals (count ($ expectedAttributeIds ), count ($ actualAttributeIds ));
329
+ foreach ($ actualAttributeIds as $ attributeId ) {
330
+ $ this ->assertTrue (in_array ($ attributeId , $ expectedAttributeIds , true ));
331
+ }
332
+ }
210
333
}
0 commit comments