Skip to content

Added a method to remove an attribute from a group in EavSetup #39618

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: 2.4-develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions app/code/Magento/Eav/Setup/EavSetup.php
Original file line number Diff line number Diff line change
Expand Up @@ -1347,6 +1347,43 @@ public function addAttributeToGroup($entityType, $setId, $groupId, $attributeId,
return $this;
}

/**
* Remove an attribute from a group and an attribute set.
*
* @return $this
*/
public function removeAttributeFromGroup(int|string $entityTypeId, int|string $setId, int|string $groupId, int|string $attributeId): static
{
$entityTypeId = $this->getEntityTypeId($entityTypeId);
$setId = $this->getAttributeSetId($entityTypeId, $setId);
$groupId = $this->getAttributeGroupId($entityTypeId, $setId, $groupId);
$attributeId = $this->getAttributeId($entityTypeId, $attributeId);

$table = $this->setup->getTable('eav_entity_attribute');
$select = $this->setup->getConnection()->select()->from($table)
->where('entity_type_id = :entity_type_id')
->where('attribute_set_id = :attribute_set_id')
->where('attribute_group_id = :attribute_group_id')
->where('attribute_id = :attribute_id');

$row = $this->setup->getConnection()->fetchRow($select, [
'entity_type_id' => $entityTypeId,
'attribute_set_id' => $setId,
'attribute_group_id' => $groupId,
'attribute_id' => $attributeId,
]);

if (false === $row) {
return $this;
}

$this->setup->getConnection()->delete($table, [
'entity_attribute_id = ?' => $row['entity_attribute_id'],
]);

return $this;
}

/******************* BULK INSTALL *****************/

/**
Expand Down
99 changes: 93 additions & 6 deletions dev/tests/integration/testsuite/Magento/Eav/Setup/EavSetupTest.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2025 Adobe
* All Rights Reserved.
*/

namespace Magento\Eav\Setup;

use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\TestFramework\Fixture\AppIsolation;

/**
* Test class for Magento\Eav\Setup\EavSetup.
* @magentoDbIsolation enabled
*/
class EavSetupTest extends \PHPUnit\Framework\TestCase
{
/**
* Eav setup.
*
* @var \Magento\Eav\Setup\EavSetup
*/
private $eavSetup;
Expand Down Expand Up @@ -66,6 +66,7 @@ public static function addAttributeDataProvider()
*
* @dataProvider addAttributeThrowExceptionDataProvider
*/
#[AppIsolation(true)]
public function testAddAttributeThrowException($attributeCode)
{
$this->expectException(\Magento\Framework\Exception\LocalizedException::class);
Expand Down Expand Up @@ -98,10 +99,12 @@ public static function addAttributeThrowExceptionDataProvider()
*
* @dataProvider addInvalidAttributeThrowExceptionDataProvider
*/
#[AppIsolation(true)]
public function testAddInvalidAttributeThrowException($attributeCode)
{
$this->expectException(\Magento\Framework\Exception\LocalizedException::class);
$this->expectExceptionMessage('Please use only letters (a-z or A-Z), numbers (0-9) or underscore (_) in this field,');
$this->expectExceptionMessage('Please use only letters (a-z or A-Z), ' .
'numbers (0-9) or underscore (_) in this field,');

$attributeData = $this->getAttributeData();
$this->eavSetup->addAttribute(\Magento\Catalog\Model\Product::ENTITY, $attributeCode, $attributeData);
Expand All @@ -119,6 +122,90 @@ public static function addInvalidAttributeThrowExceptionDataProvider()
];
}

/**
* Verify that addAttributeToGroup adds the attribute to the specified group and its attribute set.
*/
#[AppIsolation(true)]
public function testAddAttributeToGroup(): void
{
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
/** @var ModuleDataSetupInterface $setup */
$setup = $objectManager->create(ModuleDataSetupInterface::class);

$attributeData = $this->getAttributeData();
$uniqueAttributeName = 'db24abf125674bceabbbd9977bfc4ada';
$uniqueGroupName = '64a9ed905ca74cbd86533600a3604d43';
$this->eavSetup->addAttribute(\Magento\Catalog\Model\Product::ENTITY, $uniqueAttributeName, $attributeData);
$this->eavSetup->addAttributeGroup(\Magento\Catalog\Model\Product::ENTITY, 'Default', $uniqueGroupName);

$entityTypeId = $this->eavSetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
$setId = $this->eavSetup->getAttributeSetId(\Magento\Catalog\Model\Product::ENTITY, 'Default');
$groupId = $this->eavSetup->getAttributeGroupId(\Magento\Catalog\Model\Product::ENTITY, $setId, $uniqueGroupName);
$attributeId = $this->eavSetup->getAttributeId(\Magento\Catalog\Model\Product::ENTITY, $uniqueAttributeName);
$select = $setup->getConnection()->select()
->from($setup->getTable('eav_entity_attribute'))
->where('entity_type_id = ?', $entityTypeId)
->where('attribute_set_id = ?', $setId)
->where('attribute_group_id = ?', $groupId)
->where('attribute_id = ?', $attributeId);

// Make sure that the attribute is not assigned to the group already.
$row = $select->query()->fetch();
$this->assertFalse($row);

// The actual action
$this->eavSetup->addAttributeToGroup($entityTypeId, $setId, $groupId, $attributeId);

$row = $select->query()->fetch();
$this->assertIsArray($row);
$this->assertSame($entityTypeId, $row['entity_type_id']);
$this->assertSame($setId, $row['attribute_set_id']);
$this->assertSame($groupId, $row['attribute_group_id']);
$this->assertSame($attributeId, $row['attribute_id']);
}

/**
* Verify that testRemoveAttributeFromGroup removes the attribute from the specified group and attribute set.
*/
#[AppIsolation(true)]
public function testRemoveAttributeFromGroup(): void
{
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
/** @var ModuleDataSetupInterface $setup */
$setup = $objectManager->create(ModuleDataSetupInterface::class);

$attributeData = $this->getAttributeData();
$uniqueAttributeName = 'e0db51820df24b6fb6a181571aab8823';
$uniqueGroupName = 'c6771ccb1ab549378a87ecd6cb1d1352';
$this->eavSetup->addAttribute(\Magento\Catalog\Model\Product::ENTITY, $uniqueAttributeName, $attributeData);
$this->eavSetup->addAttributeGroup(\Magento\Catalog\Model\Product::ENTITY, 'Default', $uniqueGroupName);

$entityTypeId = $this->eavSetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
$setId = $this->eavSetup->getAttributeSetId(\Magento\Catalog\Model\Product::ENTITY, 'Default');
$groupId = $this->eavSetup->getAttributeGroupId(\Magento\Catalog\Model\Product::ENTITY, $setId, $uniqueGroupName);
$attributeId = $this->eavSetup->getAttributeId(\Magento\Catalog\Model\Product::ENTITY, $uniqueAttributeName);
$this->eavSetup->addAttributeToGroup($entityTypeId, $setId, $groupId, $attributeId);

$select = $setup->getConnection()->select()
->from($setup->getTable('eav_entity_attribute'))
->where('entity_type_id = ?', $entityTypeId)
->where('attribute_set_id = ?', $setId)
->where('attribute_group_id = ?', $groupId)
->where('attribute_id = ?', $attributeId);

// Make sure that the attribute is assigned to the group.
$row = $select->query()->fetch();
$this->assertIsArray($row);
$this->assertNotEmpty($row['entity_attribute_id']);

// The actual action
$this->eavSetup->removeAttributeFromGroup($entityTypeId, $setId, $groupId, $attributeId);

// Make sure that the attribute was removed from the group
$row = $select->query()->fetch();
$this->assertFalse($row);
}

/**
* Get simple attribute data.
*/
Expand Down