|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\Catalog\Model\ResourceModel; |
| 10 | + |
| 11 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 12 | +use Magento\Eav\Model\Entity\Attribute as EavAttribute; |
| 13 | +use Magento\Framework\EntityManager\MetadataPool; |
| 14 | + |
| 15 | +/** |
| 16 | + * Test class for Catalog attribute resource model. |
| 17 | + * |
| 18 | + * @see Magento\Catalog\Model\ResourceModel\Attribute |
| 19 | + */ |
| 20 | +class AttributeTest extends \PHPUnit\Framework\TestCase |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var \Magento\Catalog\Model\ResourceModel\Attribute |
| 24 | + */ |
| 25 | + private $model; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var \Magento\Catalog\Model\ResourceModel\Product |
| 29 | + */ |
| 30 | + protected $productResource; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var ProductRepositoryInterface |
| 34 | + */ |
| 35 | + private $productRepository; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var MetadataPool |
| 39 | + */ |
| 40 | + private $metadataPool; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var \Magento\Framework\ObjectManagerInterface |
| 44 | + */ |
| 45 | + protected $objectManager; |
| 46 | + |
| 47 | + protected function setUp() |
| 48 | + { |
| 49 | + $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); |
| 50 | + $this->model = $this->objectManager->get( |
| 51 | + \Magento\Catalog\Model\ResourceModel\Attribute::class |
| 52 | + ); |
| 53 | + $this->productResource = $this->objectManager->get( |
| 54 | + \Magento\Catalog\Model\ResourceModel\Product::class |
| 55 | + ); |
| 56 | + $this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class); |
| 57 | + $this->metadataPool = $this->objectManager->get(MetadataPool::class); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Retrieve eav attribute row. |
| 62 | + * |
| 63 | + * @param int $entityTypeId |
| 64 | + * @param int $attributeSetId |
| 65 | + * @param int $attributeId |
| 66 | + * @return array|false |
| 67 | + */ |
| 68 | + private function getEavEntityAttributeRow($entityTypeId, $attributeSetId, $attributeId) |
| 69 | + { |
| 70 | + $connection = $this->productResource->getConnection(); |
| 71 | + $select = $connection->select() |
| 72 | + ->from($this->productResource->getTable('eav_entity_attribute')) |
| 73 | + ->where('attribute_set_id=?', $attributeSetId) |
| 74 | + ->where('attribute_id=?', $attributeId) |
| 75 | + ->where('entity_type_id=?', $entityTypeId); |
| 76 | + |
| 77 | + return $connection->fetchRow($select); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Test to delete entity attribute with type "Fixed Product Tax". |
| 82 | + * |
| 83 | + * @magentoDataFixture Magento/Weee/_files/fixed_product_attribute.php |
| 84 | + * @return void |
| 85 | + */ |
| 86 | + public function testDeleteEntityFixedTax() |
| 87 | + { |
| 88 | + /* @var EavAttribute $attribute */ |
| 89 | + $attribute = $this->objectManager->get(EavAttribute::class); |
| 90 | + $attribute->loadByCode(\Magento\Catalog\Model\Product::ENTITY, 'fixed_product_attribute'); |
| 91 | + |
| 92 | + $entityEavAttributeRow = $this->getEavEntityAttributeRow( |
| 93 | + $attribute->getEntityTypeId(), |
| 94 | + 4, |
| 95 | + $attribute->getId() |
| 96 | + ); |
| 97 | + $this->assertNotEmpty( |
| 98 | + $entityEavAttributeRow['entity_attribute_id'], |
| 99 | + 'The record is absent in table `eav_entity_attribute` for `fixed_product_attribute`' |
| 100 | + ); |
| 101 | + |
| 102 | + $attribute->setData('entity_attribute_id', $entityEavAttributeRow['entity_attribute_id']); |
| 103 | + $this->model->deleteEntity($attribute); |
| 104 | + |
| 105 | + $entityEavAttributeRow = $this->getEavEntityAttributeRow( |
| 106 | + $attribute->getEntityTypeId(), |
| 107 | + 4, |
| 108 | + $attribute->getId() |
| 109 | + ); |
| 110 | + $this->assertEmpty( |
| 111 | + $entityEavAttributeRow, |
| 112 | + 'The record is not remove from table `eav_entity_attribute` for `fixed_product_attribute`' |
| 113 | + ); |
| 114 | + } |
| 115 | +} |
0 commit comments