Skip to content

Commit

Permalink
MC-20694: Admin: Delete attribute set
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanPletnyov committed Oct 22, 2019
1 parent 5a82cc9 commit 94277ef
Show file tree
Hide file tree
Showing 6 changed files with 325 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\TestFramework\Eav\Model;

use Magento\Eav\Api\AttributeSetRepositoryInterface;
use Magento\Eav\Api\Data\AttributeSetInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;

/**
* Attribute set additional functions.
*/
class GetAttributeSetByName
{
/**
* @var SearchCriteriaBuilder
*/
private $searchCriteriaBuilder;

/**
* @var AttributeSetRepositoryInterface
*/
private $attributeSetRepository;

/**
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param AttributeSetRepositoryInterface $attributeSetRepository
*/
public function __construct(
SearchCriteriaBuilder $searchCriteriaBuilder,
AttributeSetRepositoryInterface $attributeSetRepository
) {
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->attributeSetRepository = $attributeSetRepository;
}

/**
* Search and return attribute set by name.
*
* @param string $attributeSetName
* @return AttributeSetInterface|null
*/
public function execute(string $attributeSetName): ?AttributeSetInterface
{
$this->searchCriteriaBuilder->addFilter('attribute_set_name', $attributeSetName);
$searchCriteria = $this->searchCriteriaBuilder->create();
$result = $this->attributeSetRepository->getList($searchCriteria);
$items = $result->getItems();

return array_pop($items);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,146 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Controller\Adminhtml\Product\Set;

use Magento\Framework\Message\MessageInterface;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product;
use Magento\Eav\Api\AttributeSetRepositoryInterface;
use Magento\Framework\App\Request\Http as HttpRequest;
use Magento\Framework\Escaper;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Message\MessageInterface;
use Magento\TestFramework\Eav\Model\GetAttributeSetByName;
use Magento\TestFramework\TestCase\AbstractBackendController;

class DeleteTest extends \Magento\TestFramework\TestCase\AbstractBackendController
/**
* Test for attribute set deleting.
*
* @magentoAppArea adminhtml
* @magentoDbIsolation enabled
*/
class DeleteTest extends AbstractBackendController
{
/**
* @var GetAttributeSetByName
*/
private $getAttributeSetByName;

/**
* @var ProductInterface|Product
*/
private $product;

/**
* @var AttributeSetRepositoryInterface
*/
private $attributeSetRepository;

/**
* @var Escaper
*/
private $escaper;

/**
* @var ProductRepositoryInterface
*/
private $productRepository;

/**
* @inheritdoc
*/
protected function setUp()
{
parent::setUp();
$this->getAttributeSetByName = $this->_objectManager->get(GetAttributeSetByName::class);
$this->product = $this->_objectManager->get(ProductInterface::class);
$this->attributeSetRepository = $this->_objectManager->get(AttributeSetRepositoryInterface::class);
$this->escaper = $this->_objectManager->get(Escaper::class);
$this->productRepository = $this->_objectManager->get(ProductRepositoryInterface::class);
}

/**
* Assert that default attribute set is not deleted.
*
* @return void
*/
public function testDefaultAttributeSetIsNotDeleted(): void
{
$productDefaultAttrSetId = (int)$this->product->getDefaultAttributeSetId();
$this->performDeleteAttributeSetRequest($productDefaultAttrSetId);
$expectedSessionMessage = $this->escaper->escapeHtml((string)__('We can\'t delete this set right now.'));
$this->assertSessionMessages(
$this->equalTo([$expectedSessionMessage]),
MessageInterface::TYPE_ERROR
);
try {
$this->attributeSetRepository->get($productDefaultAttrSetId);
} catch (NoSuchEntityException $e) {
$this->fail(sprintf('Default attribute set was deleted. Message: %s', $e->getMessage()));
}
}

/**
* Assert that custom attribute set deleting properly.
*
* @magentoDataFixture Magento/Eav/_files/empty_attribute_set.php
*
* @return void
*/
public function testDeleteById()
public function testDeleteCustomAttributeSetById(): void
{
$attributeSet = $this->getAttributeSetByName('empty_attribute_set');
$this->getRequest()->setParam('id', $attributeSet->getId())->setMethod(HttpRequest::METHOD_POST);
$this->deleteAttributeSetByNameAndAssert('empty_attribute_set');
}

$this->dispatch('backend/catalog/product_set/delete/');
/**
* Assert that product will be deleted if delete attribute set which the product is attached.
*
* @magentoDataFixture Magento/Catalog/_files/product_with_test_attribute_set.php
*
* @return void
*/
public function testProductIsDeletedAfterDeleteItsAttributeSet(): void
{
$this->deleteAttributeSetByNameAndAssert('new_attribute_set');
$this->expectExceptionObject(
new NoSuchEntityException(
__('The product that was requested doesn\'t exist. Verify the product and try again.')
)
);
$this->productRepository->get('simple');
}

$this->assertNull($this->getAttributeSetByName('empty_attribute_set'));
/**
* Perform request to delete attribute set and assert that attribute set is deleted.
*
* @param string $attributeSetName
* @return void
*/
private function deleteAttributeSetByNameAndAssert(string $attributeSetName): void
{
$attributeSet = $this->getAttributeSetByName->execute($attributeSetName);
$this->performDeleteAttributeSetRequest((int)$attributeSet->getAttributeSetId());
$this->assertSessionMessages(
$this->equalTo(['The attribute set has been removed.']),
$this->equalTo([(string)__('The attribute set has been removed.')]),
MessageInterface::TYPE_SUCCESS
);
$this->assertRedirect($this->stringContains('catalog/product_set/index/'));
$this->assertNull($this->getAttributeSetByName->execute($attributeSetName));
}

/**
* Retrieve attribute set based on given name.
* Perform "catalog/product_set/delete" controller dispatch.
*
* @param string $attributeSetName
* @return \Magento\Eav\Model\Entity\Attribute\Set|null
* @param int $attributeSetId
* @return void
*/
protected function getAttributeSetByName($attributeSetName)
private function performDeleteAttributeSetRequest(int $attributeSetId): void
{
$attributeSet = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
\Magento\Eav\Model\Entity\Attribute\Set::class
)->load($attributeSetName, 'attribute_set_name');
return $attributeSet->getId() === null ? null : $attributeSet;
$this->getRequest()
->setParam('id', $attributeSetId)
->setMethod(HttpRequest::METHOD_POST);
$this->dispatch('backend/catalog/product_set/delete/');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Eav\Api\AttributeSetRepositoryInterface;
use Magento\Eav\Api\Data\AttributeGroupInterface;
use Magento\Eav\Api\Data\AttributeSetInterfaceFactory;
use Magento\Eav\Model\Entity\Attribute\GroupFactory;
use Magento\Eav\Model\Entity\Type;
use Magento\TestFramework\Helper\Bootstrap;

$objectManager = Bootstrap::getObjectManager();
/** @var AttributeSetRepositoryInterface $attributeSetRepository */
$attributeSetRepository = $objectManager->get(AttributeSetRepositoryInterface::class);
/** @var AttributeSetInterfaceFactory $attributeSetFactory */
$attributeSetFactory = $objectManager->get(AttributeSetInterfaceFactory::class);
/** @var Type $entityType */
$entityType = $objectManager->create(Type::class)->loadByCode(ProductAttributeInterface::ENTITY_TYPE_CODE);
/** @var ProductInterface $product */
$product = $objectManager->create(ProductInterface::class);
$attributeSet = $attributeSetFactory->create(
[
'data' => [
'id' => null,
'attribute_set_name' => 'new_attribute_set',
'entity_type_id' => $entityType->getId(),
'sort_order' => 300,
],
]
);
$attributeSet->isObjectNew(true);
$attributeSet->setHasDataChanges(true);
$attributeSet->validate();
$attributeSetRepository->save($attributeSet);
$attributeSet->initFromSkeleton($product->getDefaultAttributeSetid());
/** @var AttributeGroupInterface $newGroup */
$newGroup = $objectManager->get(GroupFactory::class)->create();
$newGroup->setId(null)
->setAttributeGroupName('Test attribute group name')
->setAttributeSetId($attributeSet->getAttributeSetId())
->setSortOrder(11)
->setAttributes([]);
/** @var AttributeGroupInterface[] $groups */
$groups = $attributeSet->getGroups();
array_push($groups, $newGroup);
$attributeSet->setGroups($groups);
$attributeSetRepository->save($attributeSet);
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

use Magento\Eav\Api\AttributeSetRepositoryInterface;
use Magento\TestFramework\Eav\Model\GetAttributeSetByName;
use Magento\TestFramework\Helper\Bootstrap;

$objectManager = Bootstrap::getObjectManager();
/** @var GetAttributeSetByName $getAttributeSetByName */
$getAttributeSetByName = $objectManager->get(GetAttributeSetByName::class);
/** @var AttributeSetRepositoryInterface $attributeSetRepository */
$attributeSetRepository = $objectManager->get(AttributeSetRepositoryInterface::class);
$attributeSet = $getAttributeSetByName->execute('new_attribute_set');

if ($attributeSet) {
$attributeSetRepository->delete($attributeSet);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

require __DIR__ . '/attribute_set_based_on_default_with_custom_group.php';

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Catalog\Model\Product\Visibility;
use Magento\Catalog\Model\ProductFactory;
use Magento\Store\Model\Store;
use Magento\TestFramework\Eav\Model\GetAttributeSetByName;
use Magento\TestFramework\Helper\Bootstrap;

$objectManager = Bootstrap::getObjectManager();
/** @var ProductRepositoryInterface $productRepository */
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
/** @var ProductFactory $productFactory */
$productFactory = $objectManager->get(ProductFactory::class);
/** @var GetAttributeSetByName $attributeSet */
$attributeSet = $objectManager->get(GetAttributeSetByName::class);
$customAttributeSet = $attributeSet->execute('new_attribute_set');
$product = $productFactory->create();
$product
->setTypeId('simple')
->setAttributeSetId($customAttributeSet->getAttributeSetId())
->setWebsiteIds([1])
->setStoreId(Store::DEFAULT_STORE_ID)
->setName('Simple Product')
->setSku('simple')
->setPrice(10)
->setMetaTitle('meta title')
->setMetaKeyword('meta keyword')
->setMetaDescription('meta description')
->setVisibility(Visibility::VISIBILITY_BOTH)
->setStatus(Status::STATUS_ENABLED)
->setStockData(['use_config_manage_stock' => 1, 'qty' => 22, 'is_in_stock' => 1])
->setQty(22);
$productRepository->save($product);
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\CatalogInventory\Model\StockRegistryStorage;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Registry;
use Magento\TestFramework\Helper\Bootstrap;

$objectManager = Bootstrap::getObjectManager();
/** @var Registry $registry */
$registry = $objectManager->get(Registry::class);
$registry->unregister('isSecureArea');
$registry->register('isSecureArea', true);
/** @var ProductRepositoryInterface $productRepository */
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
/** @var StockRegistryStorage $stockRegistryStorage */
$stockRegistryStorage = $objectManager->get(StockRegistryStorage::class);
try {
$product = $productRepository->get('simple');
$productRepository->delete($product);
} catch (NoSuchEntityException $e) {
//Product already deleted.
}
$stockRegistryStorage->clean();
$registry->unregister('isSecureArea');
$registry->register('isSecureArea', false);

require __DIR__ . '/attribute_set_based_on_default_with_custom_group_rollback.php';

0 comments on commit 94277ef

Please sign in to comment.