Skip to content

Commit 0bd9d67

Browse files
ENGCOM-8801: Deleting disabled category forces category flat and catalog search re-indexed fully #32082
2 parents 5470d97 + c51447c commit 0bd9d67

File tree

5 files changed

+69
-15
lines changed

5 files changed

+69
-15
lines changed

app/code/Magento/Catalog/Model/Category.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1159,7 +1159,10 @@ public function reindex()
11591159
*/
11601160
public function afterDeleteCommit()
11611161
{
1162-
$this->reindex();
1162+
if ($this->getIsActive() || $this->getDeletedChildrenIds()) {
1163+
$this->reindex();
1164+
}
1165+
11631166
return parent::afterDeleteCommit();
11641167
}
11651168

app/code/Magento/Catalog/Model/ResourceModel/Category.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,10 @@ protected function _beforeDelete(\Magento\Framework\DataObject $object)
232232
*/
233233
protected function _afterDelete(DataObject $object)
234234
{
235-
$this->indexerProcessor->markIndexerAsInvalid();
235+
if ($object->getIsActive() || $object->getDeletedChildrenIds()) {
236+
$this->indexerProcessor->markIndexerAsInvalid();
237+
}
238+
236239
return parent::_afterDelete($object);
237240
}
238241

app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Model/Plugin/Category.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Magento\Catalog\Model\ResourceModel\Category as Resource;
1111
use Magento\CatalogSearch\Model\Indexer\Fulltext\Processor;
12+
use Magento\Framework\DataObject;
1213

1314
/**
1415
* Perform indexer invalidation after a category delete.
@@ -33,12 +34,15 @@ public function __construct(Processor $fulltextIndexerProcessor)
3334
*
3435
* @param Resource $subjectCategory
3536
* @param Resource $resultCategory
37+
* @param DataObject $object
3638
* @return Resource
3739
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
3840
*/
39-
public function afterDelete(Resource $subjectCategory, Resource $resultCategory) : Resource
41+
public function afterDelete(Resource $subjectCategory, Resource $resultCategory, DataObject $object) : Resource
4042
{
41-
$this->fulltextIndexerProcessor->markIndexerAsInvalid();
43+
if ($object->getIsActive() || $object->getDeletedChildrenIds()) {
44+
$this->fulltextIndexerProcessor->markIndexerAsInvalid();
45+
}
4246

4347
return $resultCategory;
4448
}

dev/tests/integration/testsuite/Magento/Catalog/Model/Indexer/Category/ProductTest.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,36 @@ public function testCategoryDelete()
184184
}
185185
}
186186

187-
public function testCategoryCreate()
187+
188+
/**
189+
* Verify that indexer still valid after deleting inactive category
190+
*
191+
* @magentoAppArea adminhtml
192+
* @magentoDataFixture Magento/Catalog/_files/categories_disabled.php
193+
*
194+
* @return void
195+
*/
196+
public function testDeleteInactiveCategory(): void
197+
{
198+
$this->indexer->reindexAll();
199+
$isInvalidIndexer = $this->indexer->isInvalid();
200+
201+
$this->categoryRepository->deleteByIdentifier(8);
202+
203+
$state = $this->indexer->getState();
204+
$state->loadByIndexer($this->indexer->getId());
205+
$status = $state->getStatus();
206+
207+
$this->assertFalse($isInvalidIndexer);
208+
$this->assertEquals(StateInterface::STATUS_VALID, $status);
209+
}
210+
211+
/**
212+
* Create category
213+
*
214+
* @return void
215+
*/
216+
public function testCategoryCreate(): void
188217
{
189218
$this->testReindexAll();
190219
$categories = $this->getCategories(4);

dev/tests/integration/testsuite/Magento/CatalogSearch/Model/Indexer/Fulltext/Model/Plugin/CategoryTest.php

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,16 @@
99

1010
use Magento\Catalog\Api\CategoryRepositoryInterface;
1111
use Magento\Catalog\Model\Category;
12+
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
1213
use Magento\CatalogSearch\Model\Indexer\Fulltext\Processor;
14+
use Magento\Framework\Indexer\StateInterface;
1315
use Magento\TestFramework\Helper\Bootstrap;
16+
use PHPUnit\Framework\TestCase;
1417

15-
class CategoryTest extends \PHPUnit\Framework\TestCase
18+
/**
19+
* Test for category repository plugin
20+
*/
21+
class CategoryTest extends TestCase
1622
{
1723
/**
1824
* @var Processor
@@ -24,10 +30,19 @@ class CategoryTest extends \PHPUnit\Framework\TestCase
2430
*/
2531
private $categoryRepository;
2632

33+
/**
34+
* @var CategoryCollectionFactory
35+
*/
36+
private $categoryCollectionFactory;
37+
38+
/**
39+
* @inheritDoc
40+
*/
2741
protected function setUp(): void
2842
{
2943
$this->indexerProcessor = Bootstrap::getObjectManager()->create(Processor::class);
3044
$this->categoryRepository = Bootstrap::getObjectManager()->create(CategoryRepositoryInterface::class);
45+
$this->categoryCollectionFactory = Bootstrap::getObjectManager()->create(CategoryCollectionFactory::class);
3146
}
3247

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

4964
$this->assertTrue($isIndexerValid);
50-
$this->assertEquals(\Magento\Framework\Indexer\StateInterface::STATUS_INVALID, $status);
65+
$this->assertEquals(StateInterface::STATUS_INVALID, $status);
5166
}
5267

5368
/**
69+
* Returns categories
70+
*
5471
* @param int $count
5572
* @return Category[]
5673
*/
57-
private function getCategories($count)
74+
private function getCategories(int $count): array
5875
{
59-
/** @var Category $category */
60-
$category = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
61-
\Magento\Catalog\Model\Category::class
62-
);
63-
64-
$result = $category->getCollection()->addAttributeToSelect('name')->getItems();
65-
$result = array_slice($result, 2);
76+
$collection = $this->categoryCollectionFactory->create()
77+
->addAttributeToSelect('name')
78+
->addAttributeToSelect('is_active')
79+
->getItems();
80+
$result = array_slice($collection, 2);
6681

6782
return array_slice($result, 0, $count);
6883
}

0 commit comments

Comments
 (0)