Skip to content
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

Deleting disabled category forces category flat and catalog search re-indexed fully #32082

Merged
merged 12 commits into from
Mar 7, 2021
Merged
5 changes: 4 additions & 1 deletion app/code/Magento/Catalog/Model/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,10 @@ public function reindex()
*/
public function afterDeleteCommit()
{
$this->reindex();
if ($this->getIsActive() || $this->getDeletedChildrenIds()) {
$this->reindex();
}

return parent::afterDeleteCommit();
}

Expand Down
5 changes: 4 additions & 1 deletion app/code/Magento/Catalog/Model/ResourceModel/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,10 @@ protected function _beforeDelete(\Magento\Framework\DataObject $object)
*/
protected function _afterDelete(DataObject $object)
{
$this->indexerProcessor->markIndexerAsInvalid();
if ($object->getIsActive() || $object->getDeletedChildrenIds()) {
$this->indexerProcessor->markIndexerAsInvalid();
}

return parent::_afterDelete($object);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Magento\Catalog\Model\ResourceModel\Category as Resource;
use Magento\CatalogSearch\Model\Indexer\Fulltext\Processor;
use Magento\Framework\DataObject;

/**
* Perform indexer invalidation after a category delete.
Expand All @@ -33,12 +34,15 @@ public function __construct(Processor $fulltextIndexerProcessor)
*
* @param Resource $subjectCategory
* @param Resource $resultCategory
* @param DataObject $object
* @return Resource
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterDelete(Resource $subjectCategory, Resource $resultCategory) : Resource
public function afterDelete(Resource $subjectCategory, Resource $resultCategory, DataObject $object) : Resource
{
$this->fulltextIndexerProcessor->markIndexerAsInvalid();
if ($object->getIsActive() || $object->getDeletedChildrenIds()) {
$this->fulltextIndexerProcessor->markIndexerAsInvalid();
}

return $resultCategory;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,36 @@ public function testCategoryDelete()
}
}

public function testCategoryCreate()

/**
* Verify that indexer still valid after deleting inactive category
*
* @magentoAppArea adminhtml
* @magentoDataFixture Magento/Catalog/_files/categories_disabled.php
*
* @return void
*/
public function testDeleteInactiveCategory(): void
{
$this->indexer->reindexAll();
$isInvalidIndexer = $this->indexer->isInvalid();

$this->categoryRepository->deleteByIdentifier(8);

$state = $this->indexer->getState();
$state->loadByIndexer($this->indexer->getId());
$status = $state->getStatus();

$this->assertFalse($isInvalidIndexer);
$this->assertEquals(StateInterface::STATUS_VALID, $status);
}

/**
* Create category
*
* @return void
*/
public function testCategoryCreate(): void
{
$this->testReindexAll();
$categories = $this->getCategories(4);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@

use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
use Magento\CatalogSearch\Model\Indexer\Fulltext\Processor;
use Magento\Framework\Indexer\StateInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;

class CategoryTest extends \PHPUnit\Framework\TestCase
/**
* Test for category repository plugin
*/
class CategoryTest extends TestCase
{
/**
* @var Processor
Expand All @@ -24,10 +30,19 @@ class CategoryTest extends \PHPUnit\Framework\TestCase
*/
private $categoryRepository;

/**
* @var CategoryCollectionFactory
*/
private $categoryCollectionFactory;

/**
* @inheritDoc
*/
protected function setUp(): void
{
$this->indexerProcessor = Bootstrap::getObjectManager()->create(Processor::class);
$this->categoryRepository = Bootstrap::getObjectManager()->create(CategoryRepositoryInterface::class);
$this->categoryCollectionFactory = Bootstrap::getObjectManager()->create(CategoryCollectionFactory::class);
}

/**
Expand All @@ -47,22 +62,22 @@ public function testIndexerInvalidatedAfterCategoryDelete()
$status = $state->getStatus();

$this->assertTrue($isIndexerValid);
$this->assertEquals(\Magento\Framework\Indexer\StateInterface::STATUS_INVALID, $status);
$this->assertEquals(StateInterface::STATUS_INVALID, $status);
}

/**
* Returns categories
*
* @param int $count
* @return Category[]
*/
private function getCategories($count)
private function getCategories(int $count): array
{
/** @var Category $category */
$category = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
\Magento\Catalog\Model\Category::class
);

$result = $category->getCollection()->addAttributeToSelect('name')->getItems();
$result = array_slice($result, 2);
$collection = $this->categoryCollectionFactory->create()
->addAttributeToSelect('name')
->addAttributeToSelect('is_active')
->getItems();
$result = array_slice($collection, 2);

return array_slice($result, 0, $count);
}
Expand Down