-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MAGETWO-84808: 12110: Missing cascade into attribute set deletion. #1…
- Loading branch information
Showing
8 changed files
with
258 additions
and
1 deletion.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
app/code/Magento/Catalog/Plugin/Model/AttributeSetRepository/RemoveProducts.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\Catalog\Plugin\Model\AttributeSetRepository; | ||
|
||
use Magento\Catalog\Model\ResourceModel\Product\Collection; | ||
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; | ||
use Magento\Eav\Api\AttributeSetRepositoryInterface; | ||
use Magento\Eav\Api\Data\AttributeSetInterface; | ||
|
||
/** | ||
* Delete related products after attribute set successfully removed. | ||
*/ | ||
class RemoveProducts | ||
{ | ||
/** | ||
* Retrieve products related to specific attribute set. | ||
* | ||
* @var CollectionFactory | ||
*/ | ||
private $collectionFactory; | ||
|
||
/** | ||
* RemoveProducts constructor. | ||
* | ||
* @param CollectionFactory $collectionFactory | ||
*/ | ||
public function __construct(CollectionFactory $collectionFactory) | ||
{ | ||
$this->collectionFactory = $collectionFactory; | ||
} | ||
|
||
/** | ||
* Delete related to specific attribute set products, if attribute set was removed successfully. | ||
* | ||
* @param AttributeSetRepositoryInterface $subject | ||
* @param bool $result | ||
* @param AttributeSetInterface $attributeSet | ||
* @return bool | ||
* | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function afterDelete( | ||
AttributeSetRepositoryInterface $subject, | ||
bool $result, | ||
AttributeSetInterface $attributeSet | ||
) { | ||
/** @var Collection $productCollection */ | ||
$productCollection = $this->collectionFactory->create(); | ||
$productCollection->addFieldToFilter('attribute_set_id', ['eq' => $attributeSet->getId()]); | ||
$productCollection->delete(); | ||
|
||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...code/Magento/Catalog/Test/Unit/Plugin/Model/AttributeSetRepository/RemoveProductsTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\Catalog\Test\Unit\Plugin\Model\AttributeSetRepository; | ||
|
||
use Magento\Catalog\Model\ResourceModel\Product\Collection; | ||
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; | ||
use Magento\Catalog\Plugin\Model\AttributeSetRepository\RemoveProducts; | ||
use Magento\Eav\Api\AttributeSetRepositoryInterface; | ||
use Magento\Eav\Api\Data\AttributeSetInterface; | ||
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* Provide tests for RemoveProducts plugin. | ||
*/ | ||
class RemoveProductsTest extends TestCase | ||
{ | ||
/** | ||
* @var RemoveProducts | ||
*/ | ||
private $testSubject; | ||
|
||
/** | ||
* @var CollectionFactory|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private $collectionFactory; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function setUp() | ||
{ | ||
$objectManager = new ObjectManager($this); | ||
$this->collectionFactory = $this->getMockBuilder(CollectionFactory::class) | ||
->disableOriginalConstructor() | ||
->setMethods(['create']) | ||
->getMock(); | ||
$this->testSubject = $objectManager->getObject( | ||
RemoveProducts::class, | ||
[ | ||
'collectionFactory' => $this->collectionFactory, | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* Test plugin will delete all related products for given attribute set. | ||
*/ | ||
public function testAfterDelete() | ||
{ | ||
$attributeSetId = '1'; | ||
|
||
/** @var Collection|\PHPUnit_Framework_MockObject_MockObject $collection */ | ||
$collection = $this->getMockBuilder(Collection::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$collection->expects(self::once()) | ||
->method('addFieldToFilter') | ||
->with(self::identicalTo('attribute_set_id'), self::identicalTo(['eq' => $attributeSetId])); | ||
$collection->expects(self::once()) | ||
->method('delete'); | ||
|
||
$this->collectionFactory->expects(self::once()) | ||
->method('create') | ||
->willReturn($collection); | ||
|
||
/** @var AttributeSetRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject $attributeSetRepository */ | ||
$attributeSetRepository = $this->getMockBuilder(AttributeSetRepositoryInterface::class) | ||
->disableOriginalConstructor() | ||
->getMockForAbstractClass(); | ||
|
||
/** @var AttributeSetInterface|\PHPUnit_Framework_MockObject_MockObject $attributeSet */ | ||
$attributeSet = $this->getMockBuilder(AttributeSetInterface::class) | ||
->setMethods(['getId']) | ||
->disableOriginalConstructor() | ||
->getMockForAbstractClass(); | ||
$attributeSet->expects(self::once()) | ||
->method('getId') | ||
->willReturn($attributeSetId); | ||
|
||
self::assertTrue($this->testSubject->afterDelete($attributeSetRepository, true, $attributeSet)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...tion/testsuite/Magento/Catalog/Plugin/Model/AttributeSetRepository/RemoveProductsTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\Catalog\Plugin\Model\AttributeSetRepository; | ||
|
||
use Magento\Catalog\Api\ProductRepositoryInterface; | ||
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; | ||
use Magento\Eav\Api\AttributeSetRepositoryInterface; | ||
use Magento\Eav\Model\Entity\Attribute\Set; | ||
use Magento\TestFramework\Helper\Bootstrap; | ||
use Magento\TestFramework\Interception\PluginList; | ||
use Magento\UrlRewrite\Model\ResourceModel\UrlRewriteCollectionFactory; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* Provide tests for RemoveProducts plugin. | ||
* @magentoAppArea adminhtml | ||
*/ | ||
class RemoveProductsTest extends TestCase | ||
{ | ||
/** | ||
* @return void | ||
*/ | ||
public function testRemoveProductsIsRegistered() | ||
{ | ||
$pluginInfo = Bootstrap::getObjectManager()->get(PluginList::class) | ||
->get(AttributeSetRepositoryInterface::class, []); | ||
self::assertSame(RemoveProducts::class, $pluginInfo['remove_products']['instance']); | ||
} | ||
|
||
/** | ||
* Test related to given attribute set products will be removed, if attribute set will be deleted. | ||
* | ||
* @magentoDataFixture Magento/Catalog/_files/attribute_set_with_product.php | ||
*/ | ||
public function testAfterDelete() | ||
{ | ||
$attributeSet = Bootstrap::getObjectManager()->get(Set::class); | ||
$attributeSet->load('empty_attribute_set', 'attribute_set_name'); | ||
|
||
$productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class); | ||
$product = $productRepository->get('simple'); | ||
|
||
$productCollection = Bootstrap::getObjectManager()->get(CollectionFactory::class)->create(); | ||
$productCollection->addIdFilter($product->getId()); | ||
$urlRewriteCollection = Bootstrap::getObjectManager()->get(UrlRewriteCollectionFactory::class)->create(); | ||
$urlRewriteCollection->addFieldToFilter('entity_type', 'product'); | ||
$urlRewriteCollection->addFieldToFilter('entity_id', $product->getId()); | ||
|
||
self::assertSame(1, $urlRewriteCollection->getSize()); | ||
self::assertSame(1, $productCollection->getSize()); | ||
|
||
$attributeSetRepository = Bootstrap::getObjectManager()->get(AttributeSetRepositoryInterface::class); | ||
$attributeSetRepository->deleteById($attributeSet->getAttributeSetId()); | ||
|
||
$productCollection = Bootstrap::getObjectManager()->get(CollectionFactory::class)->create(); | ||
$productCollection->addIdFilter($product->getId()); | ||
$urlRewriteCollection = Bootstrap::getObjectManager()->get(UrlRewriteCollectionFactory::class)->create(); | ||
$urlRewriteCollection->addFieldToFilter('entity_type', 'product'); | ||
$urlRewriteCollection->addFieldToFilter('entity_id', $product->getId()); | ||
|
||
self::assertSame(0, $urlRewriteCollection->getSize()); | ||
self::assertSame(0, $productCollection->getSize()); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
dev/tests/integration/testsuite/Magento/Catalog/_files/attribute_set_with_product.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
require __DIR__ . '/../../Eav/_files/empty_attribute_set.php'; | ||
require __DIR__ . '/../../Catalog/_files/product_simple.php'; | ||
|
||
$product->setAttributeSetId($attributeSet->getId()); | ||
$product->save(); |
8 changes: 8 additions & 0 deletions
8
...ests/integration/testsuite/Magento/Catalog/_files/attribute_set_with_product_rollback.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
require __DIR__ . '/../../Catalog/_files/product_simple_rollback.php'; | ||
require __DIR__ . '/../../Eav/_files/empty_attribute_set_rollback.php'; |