7
7
8
8
namespace Magento \Catalog \Model \Product \Attribute ;
9
9
10
+ use Magento \Catalog \Api \ProductAttributeRepositoryInterface ;
10
11
use Magento \Catalog \Model \Product ;
11
12
use Magento \Catalog \Model \ResourceModel \Product \Attribute \CollectionFactory ;
12
- use Magento \Eav \Api \AttributeGroupRepositoryInterface ;
13
13
use Magento \Eav \Api \AttributeSetRepositoryInterface ;
14
14
use Magento \Eav \Api \Data \AttributeGroupInterface ;
15
15
use Magento \Eav \Model \Config ;
16
16
use Magento \Eav \Model \ResourceModel \Entity \Attribute \Set as AttributeSetResource ;
17
- use Magento \Framework \Api \SearchCriteriaBuilder ;
17
+ use Magento \Framework \Api \AttributeInterface ;
18
18
use Magento \Framework \ObjectManagerInterface ;
19
19
use Magento \TestFramework \Helper \Bootstrap ;
20
+ use Magento \TestFramework \Eav \Model \GetAttributeGroupByName ;
20
21
21
22
/**
22
23
* Provides tests for attribute set model saving.
@@ -36,20 +37,15 @@ class SetTest extends \PHPUnit\Framework\TestCase
36
37
private $ setRepository ;
37
38
38
39
/**
39
- * @var AttributeGroupRepositoryInterface
40
+ * @var ProductAttributeRepositoryInterface
40
41
*/
41
- private $ groupRepository ;
42
+ private $ attributeRepository ;
42
43
43
44
/**
44
45
* @var Config
45
46
*/
46
47
private $ config ;
47
48
48
- /**
49
- * @var SearchCriteriaBuilder
50
- */
51
- private $ criteriaBuilder ;
52
-
53
49
/**
54
50
* @var AttributeSetResource
55
51
*/
@@ -65,6 +61,11 @@ class SetTest extends \PHPUnit\Framework\TestCase
65
61
*/
66
62
private $ defaultSetId ;
67
63
64
+ /**
65
+ * @var GetAttributeGroupByName
66
+ */
67
+ private $ attributeGroupByName ;
68
+
68
69
/**
69
70
* @inheritdoc
70
71
*/
@@ -73,12 +74,12 @@ protected function setUp()
73
74
parent ::setUp ();
74
75
$ this ->objectManager = Bootstrap::getObjectManager ();
75
76
$ this ->setRepository = $ this ->objectManager ->get (AttributeSetRepositoryInterface::class);
76
- $ this ->groupRepository = $ this ->objectManager ->create (AttributeGroupRepositoryInterface ::class);
77
+ $ this ->attributeRepository = $ this ->objectManager ->create (ProductAttributeRepositoryInterface ::class);
77
78
$ this ->config = $ this ->objectManager ->get (Config::class);
78
79
$ this ->defaultSetId = (int )$ this ->config ->getEntityType (Product::ENTITY )->getDefaultAttributeSetId ();
79
- $ this ->criteriaBuilder = $ this ->objectManager ->create (SearchCriteriaBuilder::class);
80
80
$ this ->attributeSetResource = $ this ->objectManager ->get (AttributeSetResource::class);
81
81
$ this ->attributeCollectionFactory = $ this ->objectManager ->get (CollectionFactory ::class);
82
+ $ this ->attributeGroupByName = $ this ->objectManager ->get (GetAttributeGroupByName::class);
82
83
}
83
84
84
85
/**
@@ -91,10 +92,9 @@ protected function setUp()
91
92
public function testSaveWithGroupsAndAttributes (string $ groupName , string $ attributeCode ): void
92
93
{
93
94
$ set = $ this ->setRepository ->get ($ this ->defaultSetId );
94
- $ groupId = $ this ->getAttributeGroup ($ groupName )
95
- ? $ this ->getAttributeGroup ($ groupName )->getAttributeGroupId ()
96
- : 'ynode-1 ' ;
97
- $ attributeId = (int )$ this ->config ->getAttribute (Product::ENTITY , $ attributeCode )->getAttributeId ();
95
+ $ attributeGroup = $ this ->getAttributeGroup ($ groupName );
96
+ $ groupId = $ attributeGroup ? $ attributeGroup ->getAttributeGroupId () : 'ynode-1 ' ;
97
+ $ attributeId = (int )$ this ->attributeRepository ->get ($ attributeCode )->getAttributeId ();
98
98
$ additional = [
99
99
'attributes ' => [
100
100
[$ attributeId , $ groupId , 1 ],
@@ -177,11 +177,11 @@ public function testSaveWithRemovedGroup(): void
177
177
$ this ->getAttributeGroup ('Design ' ),
178
178
'Group Design wan \'t deleted. '
179
179
);
180
- $ unusedSetAttributes = $ this ->getUnusedSetAttributes ((int )$ set ->getAttributeSetId ());
180
+ $ unusedSetAttributes = $ this ->getSetExcludedAttributes ((int )$ set ->getAttributeSetId ());
181
181
$ designAttributeCodes = ['page_layout ' , 'options_container ' , 'custom_layout_update ' ];
182
182
$ this ->assertNotEmpty (
183
183
array_intersect ($ designAttributeCodes , $ unusedSetAttributes ),
184
- 'Attributes from Design group still assigned to attribute set. '
184
+ 'Attributes from " Design" group still assigned to attribute set. '
185
185
);
186
186
}
187
187
@@ -191,8 +191,7 @@ public function testSaveWithRemovedGroup(): void
191
191
public function testSaveWithRemovedAttribute (): void
192
192
{
193
193
$ set = $ this ->setRepository ->get ($ this ->defaultSetId );
194
- $ attributeId = (int )$ this ->config ->getAttribute (Product::ENTITY , 'meta_description ' )
195
- ->getAttributeId ();
194
+ $ attributeId = (int )$ this ->attributeRepository ->get ('meta_description ' )->getAttributeId ();
196
195
$ additional = [
197
196
'not_attributes ' => [$ this ->getEntityAttributeId ($ this ->defaultSetId , $ attributeId )],
198
197
];
@@ -201,7 +200,7 @@ public function testSaveWithRemovedAttribute(): void
201
200
$ this ->config ->clear ();
202
201
$ setInfo = $ this ->attributeSetResource ->getSetInfo ([$ attributeId ], $ this ->defaultSetId );
203
202
$ this ->assertEmpty ($ setInfo [$ attributeId ]);
204
- $ unusedSetAttributes = $ this ->getUnusedSetAttributes ((int )$ set ->getAttributeSetId ());
203
+ $ unusedSetAttributes = $ this ->getSetExcludedAttributes ((int )$ set ->getAttributeSetId ());
205
204
$ this ->assertNotEmpty (
206
205
array_intersect (['meta_description ' ], $ unusedSetAttributes ),
207
206
'Attribute still assigned to attribute set. '
@@ -235,12 +234,7 @@ private function getAttributeSetData(array $additional): array
235
234
*/
236
235
private function getAttributeGroup (string $ groupName ): ?AttributeGroupInterface
237
236
{
238
- $ searchCriteria = $ this ->criteriaBuilder ->addFilter ('attribute_group_name ' , $ groupName )
239
- ->addFilter ('attribute_set_id ' , $ this ->defaultSetId )
240
- ->create ();
241
- $ result = $ this ->groupRepository ->getList ($ searchCriteria )->getItems ();
242
-
243
- return !empty ($ result ) ? reset ($ result ) : null ;
237
+ return $ this ->attributeGroupByName ->execute ($ this ->defaultSetId , $ groupName );
244
238
}
245
239
246
240
/**
@@ -249,32 +243,26 @@ private function getAttributeGroup(string $groupName): ?AttributeGroupInterface
249
243
* @param int $setId
250
244
* @return array
251
245
*/
252
- private function getUnusedSetAttributes (int $ setId ): array
246
+ private function getSetExcludedAttributes (int $ setId ): array
253
247
{
254
- $ result = [];
255
- $ attributesIds = $ this ->attributeCollectionFactory ->create ()
256
- ->setAttributeSetFilter ($ setId )
257
- ->getAllIds ();
258
248
$ collection = $ this ->attributeCollectionFactory ->create ()
259
- ->setAttributesExcludeFilter ($ attributesIds )
260
- ->addVisibleFilter ();
261
- /** @var AbstractAttribute $attribute */
262
- foreach ($ collection as $ attribute ) {
263
- $ result [] = $ attribute ->getAttributeCode ();
264
- }
249
+ ->setExcludeSetFilter ($ setId );
250
+ $ result = $ collection ->getColumnValues (AttributeInterface::ATTRIBUTE_CODE );
265
251
266
252
return $ result ;
267
253
}
268
254
269
255
/**
270
- * @param int|null $setId
256
+ * Returns entity attribute id.
257
+ *
258
+ * @param int $setId
271
259
* @param int $attributeId
272
260
* @return int
273
261
*/
274
- private function getEntityAttributeId (? int $ setId , int $ attributeId ): int
262
+ private function getEntityAttributeId (int $ setId , int $ attributeId ): int
275
263
{
276
264
$ select = $ this ->attributeSetResource ->getConnection ()->select ()
277
- ->from ('eav_entity_attribute ' , ['entity_attribute_id ' ])
265
+ ->from ($ this -> attributeSetResource -> getTable ( 'eav_entity_attribute ' ) , ['entity_attribute_id ' ])
278
266
->where ('attribute_set_id = ? ' , $ setId )
279
267
->where ('attribute_id = ? ' , $ attributeId );
280
268
0 commit comments