|
5 | 5 | */
|
6 | 6 | namespace Magento\Eav\Setup;
|
7 | 7 |
|
| 8 | +use Magento\Framework\Setup\ModuleDataSetupInterface; |
8 | 9 | use Magento\TestFramework\Fixture\AppIsolation;
|
9 | 10 |
|
10 | 11 | /**
|
@@ -121,6 +122,90 @@ public static function addInvalidAttributeThrowExceptionDataProvider()
|
121 | 122 | ];
|
122 | 123 | }
|
123 | 124 |
|
| 125 | + /** |
| 126 | + * Verify that addAttributeToGroup adds the attribute to the specified group and its attribute set. |
| 127 | + */ |
| 128 | + #[AppIsolation(true)] |
| 129 | + public function testAddAttributeToGroup(): void |
| 130 | + { |
| 131 | + $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); |
| 132 | + /** @var ModuleDataSetupInterface $setup */ |
| 133 | + $setup = $objectManager->create(ModuleDataSetupInterface::class); |
| 134 | + |
| 135 | + $attributeData = $this->getAttributeData(); |
| 136 | + $uniqueAttributeName = 'db24abf125674bceabbbd9977bfc4ada'; |
| 137 | + $uniqueGroupName = '64a9ed905ca74cbd86533600a3604d43'; |
| 138 | + $this->eavSetup->addAttribute(\Magento\Catalog\Model\Product::ENTITY, $uniqueAttributeName, $attributeData); |
| 139 | + $this->eavSetup->addAttributeGroup(\Magento\Catalog\Model\Product::ENTITY, 'Default', $uniqueGroupName); |
| 140 | + |
| 141 | + $entityTypeId = $this->eavSetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY); |
| 142 | + $setId = $this->eavSetup->getAttributeSetId(\Magento\Catalog\Model\Product::ENTITY, 'Default'); |
| 143 | + $groupId = $this->eavSetup->getAttributeGroupId(\Magento\Catalog\Model\Product::ENTITY, $setId, $uniqueGroupName); |
| 144 | + $attributeId = $this->eavSetup->getAttributeId(\Magento\Catalog\Model\Product::ENTITY, $uniqueAttributeName); |
| 145 | + $select = $setup->getConnection()->select() |
| 146 | + ->from($setup->getTable('eav_entity_attribute')) |
| 147 | + ->where('entity_type_id = ?', $entityTypeId) |
| 148 | + ->where('attribute_set_id = ?', $setId) |
| 149 | + ->where('attribute_group_id = ?', $groupId) |
| 150 | + ->where('attribute_id = ?', $attributeId); |
| 151 | + |
| 152 | + // Make sure that the attribute is not assigned to the group already. |
| 153 | + $row = $select->query()->fetch(); |
| 154 | + $this->assertFalse($row); |
| 155 | + |
| 156 | + // The actual action |
| 157 | + $this->eavSetup->addAttributeToGroup($entityTypeId, $setId, $groupId, $attributeId); |
| 158 | + |
| 159 | + $row = $select->query()->fetch(); |
| 160 | + $this->assertIsArray($row); |
| 161 | + $this->assertSame($entityTypeId, $row['entity_type_id']); |
| 162 | + $this->assertSame($setId, $row['attribute_set_id']); |
| 163 | + $this->assertSame($groupId, $row['attribute_group_id']); |
| 164 | + $this->assertSame($attributeId, $row['attribute_id']); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Verify that testRemoveAttributeFromGroup removes the attribute from the specified group and attribute set. |
| 169 | + */ |
| 170 | + #[AppIsolation(true)] |
| 171 | + public function testRemoveAttributeFromGroup(): void |
| 172 | + { |
| 173 | + $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); |
| 174 | + /** @var ModuleDataSetupInterface $setup */ |
| 175 | + $setup = $objectManager->create(ModuleDataSetupInterface::class); |
| 176 | + |
| 177 | + $attributeData = $this->getAttributeData(); |
| 178 | + $uniqueAttributeName = 'e0db51820df24b6fb6a181571aab8823'; |
| 179 | + $uniqueGroupName = 'c6771ccb1ab549378a87ecd6cb1d1352'; |
| 180 | + $this->eavSetup->addAttribute(\Magento\Catalog\Model\Product::ENTITY, $uniqueAttributeName, $attributeData); |
| 181 | + $this->eavSetup->addAttributeGroup(\Magento\Catalog\Model\Product::ENTITY, 'Default', $uniqueGroupName); |
| 182 | + |
| 183 | + $entityTypeId = $this->eavSetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY); |
| 184 | + $setId = $this->eavSetup->getAttributeSetId(\Magento\Catalog\Model\Product::ENTITY, 'Default'); |
| 185 | + $groupId = $this->eavSetup->getAttributeGroupId(\Magento\Catalog\Model\Product::ENTITY, $setId, $uniqueGroupName); |
| 186 | + $attributeId = $this->eavSetup->getAttributeId(\Magento\Catalog\Model\Product::ENTITY, $uniqueAttributeName); |
| 187 | + $this->eavSetup->addAttributeToGroup($entityTypeId, $setId, $groupId, $attributeId); |
| 188 | + |
| 189 | + $select = $setup->getConnection()->select() |
| 190 | + ->from($setup->getTable('eav_entity_attribute')) |
| 191 | + ->where('entity_type_id = ?', $entityTypeId) |
| 192 | + ->where('attribute_set_id = ?', $setId) |
| 193 | + ->where('attribute_group_id = ?', $groupId) |
| 194 | + ->where('attribute_id = ?', $attributeId); |
| 195 | + |
| 196 | + // Make sure that the attribute is assigned to the group. |
| 197 | + $row = $select->query()->fetch(); |
| 198 | + $this->assertIsArray($row); |
| 199 | + $this->assertNotEmpty($row['entity_attribute_id']); |
| 200 | + |
| 201 | + // The actual action |
| 202 | + $this->eavSetup->removeAttributeFromGroup($entityTypeId, $setId, $groupId, $attributeId); |
| 203 | + |
| 204 | + // Make sure that the attribute was removed from the group |
| 205 | + $row = $select->query()->fetch(); |
| 206 | + $this->assertFalse($row); |
| 207 | + } |
| 208 | + |
124 | 209 | /**
|
125 | 210 | * Get simple attribute data.
|
126 | 211 | */
|
|
0 commit comments