Skip to content

Commit b660062

Browse files
authored
ENGCOM-7554: Invalidate product index after change product position in the category #28150
2 parents 2d09485 + f069bb6 commit b660062

File tree

2 files changed

+117
-64
lines changed
  • app/code/Magento/Catalog/Model/ResourceModel
  • dev/tests/integration/testsuite/Magento/Catalog/Model/Indexer/Category

2 files changed

+117
-64
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -469,10 +469,6 @@ protected function _saveCategoryProducts($category)
469469

470470
if (!empty($insert) || !empty($delete)) {
471471
$productIds = array_unique(array_merge(array_keys($insert), array_keys($delete)));
472-
$this->_eventManager->dispatch(
473-
'catalog_category_change_products',
474-
['category' => $category, 'product_ids' => $productIds]
475-
);
476472

477473
$category->setChangedProductIds($productIds);
478474
}
@@ -484,6 +480,10 @@ protected function _saveCategoryProducts($category)
484480
* Setting affected products to category for third party engine index refresh
485481
*/
486482
$productIds = array_keys($insert + $delete + $update);
483+
$this->_eventManager->dispatch(
484+
'catalog_category_change_products',
485+
['category' => $category, 'product_ids' => $productIds]
486+
);
487487
$category->setAffectedProductIds($productIds);
488488
}
489489
return $this;
@@ -1078,7 +1078,6 @@ public function countVisible()
10781078
*/
10791079
public function load($object, $entityId, $attributes = [])
10801080
{
1081-
$this->_attributes = [];
10821081
$select = $this->_getLoadRowSelect($object, $entityId);
10831082
$row = $this->getConnection()->fetchRow($select);
10841083

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

Lines changed: 113 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -5,49 +5,70 @@
55
*/
66
namespace Magento\Catalog\Model\Indexer\Category;
77

8+
use Magento\Catalog\Api\CategoryRepositoryInterface;
89
use Magento\Catalog\Model\Category;
10+
use Magento\Catalog\Model\Indexer\Category\Product as CategoryProductIndexer;
11+
use Magento\Catalog\Model\Product as ProductModel;
12+
use Magento\Catalog\Model\ResourceModel\Category as CategoryResource;
13+
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
14+
use Magento\Framework\Indexer\IndexerInterface;
15+
use Magento\Framework\Indexer\StateInterface;
16+
use Magento\Indexer\Model\Indexer;
17+
use Magento\TestFramework\Catalog\Model\GetCategoryByName;
18+
use Magento\TestFramework\Helper\Bootstrap;
19+
use PHPUnit\Framework\TestCase;
920

1021
/**
22+
* Test for catalog_category_product indexer.
23+
*
1124
* @magentoDataFixture Magento/Catalog/_files/indexer_catalog_category.php
1225
* @magentoDataFixture Magento/Catalog/_files/indexer_catalog_products.php
1326
* @magentoDbIsolation disabled
1427
* @magentoAppIsolation enabled
28+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1529
*/
16-
class ProductTest extends \PHPUnit\Framework\TestCase
30+
class ProductTest extends TestCase
1731
{
18-
const DEFAULT_ROOT_CATEGORY = 2;
32+
private const DEFAULT_ROOT_CATEGORY = 2;
1933

2034
/**
21-
* @var \Magento\Framework\Indexer\IndexerInterface
35+
* @var IndexerInterface
2236
*/
23-
protected $indexer;
37+
private $indexer;
2438

2539
/**
26-
* @var \Magento\Catalog\Model\ResourceModel\Product
40+
* @var ProductResource
2741
*/
28-
protected $productResource;
42+
private $productResource;
2943

3044
/**
31-
* @var \Magento\Catalog\Api\CategoryRepositoryInterface
45+
* @var CategoryRepositoryInterface
3246
*/
3347
private $categoryRepository;
3448

35-
protected function setUp(): void
36-
{
37-
/** @var \Magento\Framework\Indexer\IndexerInterface indexer */
38-
$this->indexer = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
39-
\Magento\Indexer\Model\Indexer::class
40-
);
41-
$this->indexer->load('catalog_category_product');
49+
/**
50+
* @var GetCategoryByName
51+
*/
52+
private $getCategoryByName;
4253

43-
/** @var \Magento\Catalog\Model\ResourceModel\Product $productResource */
44-
$this->productResource = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
45-
\Magento\Catalog\Model\ResourceModel\Product::class
46-
);
54+
/**
55+
* @var CategoryResource
56+
*/
57+
private $categoryResource;
4758

48-
$this->categoryRepository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
49-
\Magento\Catalog\Api\CategoryRepositoryInterface::class
50-
);
59+
/**
60+
* @inheritdoc
61+
*/
62+
protected function setUp(): void
63+
{
64+
/** @var IndexerInterface indexer */
65+
$this->indexer = Bootstrap::getObjectManager()->create(Indexer::class);
66+
$this->indexer->load(CategoryProductIndexer::INDEXER_ID);
67+
68+
$this->productResource = Bootstrap::getObjectManager()->get(ProductResource::class);
69+
$this->categoryRepository = Bootstrap::getObjectManager()->create(CategoryRepositoryInterface::class);
70+
$this->categoryResource = Bootstrap::getObjectManager()->create(CategoryResource::class);
71+
$this->getCategoryByName = Bootstrap::getObjectManager()->create(GetCategoryByName::class);
5172
}
5273

5374
/**
@@ -61,7 +82,7 @@ public function testReindexAll()
6182
/** @var Category $categoryFourth */
6283
$categoryFourth = end($categories);
6384
foreach ($products as $product) {
64-
/** @var \Magento\Catalog\Model\Product $product */
85+
/** @var ProductModel $product */
6586
$product->setCategoryIds([$categoryFourth->getId()]);
6687
$product->save();
6788
}
@@ -77,7 +98,7 @@ public function testReindexAll()
7798
$this->indexer->reindexAll();
7899

79100
foreach ($products as $product) {
80-
/** @var \Magento\Catalog\Model\Product $product */
101+
/** @var ProductModel $product */
81102
foreach ($categories as $categoryId) {
82103
$this->assertTrue((bool)$this->productResource->canBeShowInCategory($product, $categoryId));
83104
}
@@ -99,7 +120,7 @@ public function testCategoryMove()
99120
/** @var Category $categoryFourth */
100121
$categoryFourth = end($categories);
101122
foreach ($products as $product) {
102-
/** @var \Magento\Catalog\Model\Product $product */
123+
/** @var ProductModel $product */
103124
$product->setCategoryIds([$categoryFourth->getId()]);
104125
$product->save();
105126
}
@@ -125,7 +146,7 @@ public function testCategoryMove()
125146
$categories = [self::DEFAULT_ROOT_CATEGORY, $categorySecond->getId(), $categoryFourth->getId()];
126147

127148
foreach ($products as $product) {
128-
/** @var \Magento\Catalog\Model\Product $product */
149+
/** @var ProductModel $product */
129150
foreach ($categories as $categoryId) {
130151
$this->assertTrue((bool)$this->productResource->canBeShowInCategory($product, $categoryId));
131152
}
@@ -153,7 +174,7 @@ public function testCategoryDelete()
153174
$categories = [$categorySecond->getId(), $categoryFourth->getId()];
154175

155176
foreach ($products as $product) {
156-
/** @var \Magento\Catalog\Model\Product $product */
177+
/** @var ProductModel $product */
157178
foreach ($categories as $categoryId) {
158179
$this->assertFalse((bool)$this->productResource->canBeShowInCategory($product, $categoryId));
159180
}
@@ -178,22 +199,15 @@ public function testCategoryCreate()
178199
$categoryFourth = end($categories);
179200

180201
/** @var Category $categorySixth */
181-
$categorySixth = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
182-
\Magento\Catalog\Model\Category::class
183-
);
184-
$categorySixth->setName(
185-
'Category 6'
186-
)->setPath(
187-
$categoryFourth->getPath()
188-
)->setAvailableSortBy(
189-
'name'
190-
)->setDefaultSortBy(
191-
'name'
192-
)->setIsActive(
193-
true
194-
)->save();
195-
196-
/** @var \Magento\Catalog\Model\Product $productThird */
202+
$categorySixth = Bootstrap::getObjectManager()->create(Category::class);
203+
$categorySixth->setName('Category 6')
204+
->setPath($categoryFourth->getPath())
205+
->setAvailableSortBy('name')
206+
->setDefaultSortBy('name')
207+
->setIsActive(true)
208+
->save();
209+
210+
/** @var ProductModel $productThird */
197211
$productThird = end($products);
198212
$productThird->setCategoryIds([$categorySixth->getId()]);
199213
$productThird->save();
@@ -211,33 +225,64 @@ public function testCategoryCreate()
211225

212226
/**
213227
* @magentoAppArea adminhtml
214-
* @depends testReindexAll
228+
*
229+
* @magentoDataFixture Magento/Catalog/_files/category_tree.php
230+
*
231+
* @return void
215232
*/
216-
public function testCatalogCategoryProductIndexInvalidateAfterDelete()
233+
public function testCatalogCategoryProductIndexInvalidateAfterDelete(): void
217234
{
218-
$indexerShouldBeValid = (bool)$this->indexer->isInvalid();
235+
$this->indexer->reindexAll();
236+
$indexerShouldBeValid = $this->indexer->isInvalid();
219237

220-
$categories = $this->getCategories(1);
221-
$this->categoryRepository->delete(array_pop($categories));
238+
$this->categoryRepository->deleteByIdentifier(400);
222239

223240
$state = $this->indexer->getState();
224241
$state->loadByIndexer($this->indexer->getId());
225242
$status = $state->getStatus();
226243

227244
$this->assertFalse($indexerShouldBeValid);
228-
$this->assertEquals(\Magento\Framework\Indexer\StateInterface::STATUS_INVALID, $status);
245+
$this->assertEquals(StateInterface::STATUS_INVALID, $status);
246+
}
247+
248+
/**
249+
* Test invalidate reindex after change product position on category
250+
*
251+
* @magentoAppArea adminhtml
252+
* @magentoDataFixture Magento/Catalog/_files/category_with_different_price_products.php
253+
*
254+
* @return void
255+
*/
256+
public function testCatalogCategoryProductIndexInvalidateAfterChangeProductPosition(): void
257+
{
258+
$this->indexer->setScheduled(true);
259+
$indexerShouldBeValid = $this->indexer->isValid();
260+
261+
$category = $this->getCategoryByName->execute('Category 999');
262+
263+
$category->setPostedProducts([
264+
$this->productResource->getIdBySku('simple1000') => 1,
265+
$this->productResource->getIdBySku('simple1001') => 2
266+
]);
267+
268+
$this->categoryResource->save($category);
269+
270+
$state = $this->indexer->getState();
271+
$state->loadByIndexer($this->indexer->getId());
272+
$status = $state->getStatus();
273+
274+
$this->assertTrue($indexerShouldBeValid);
275+
$this->assertEquals(StateInterface::STATUS_INVALID, $status);
229276
}
230277

231278
/**
232279
* @param int $count
233280
* @return Category[]
234281
*/
235-
protected function getCategories($count)
282+
private function getCategories(int $count): array
236283
{
237284
/** @var Category $category */
238-
$category = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
239-
\Magento\Catalog\Model\Category::class
240-
);
285+
$category = Bootstrap::getObjectManager()->create(Category::class);
241286

242287
$result = $category->getCollection()->addAttributeToSelect('name')->getItems();
243288
$result = array_slice($result, 2);
@@ -247,14 +292,12 @@ protected function getCategories($count)
247292

248293
/**
249294
* @param int $count
250-
* @return \Magento\Catalog\Model\Product[]
295+
* @return ProductModel[]
251296
*/
252-
protected function getProducts($count)
297+
private function getProducts(int $count): array
253298
{
254-
/** @var \Magento\Catalog\Model\Product $product */
255-
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
256-
\Magento\Catalog\Model\Product::class
257-
);
299+
/** @var ProductModel $product */
300+
$product = Bootstrap::getObjectManager()->create(ProductModel::class);
258301

259302
$result[] = $product->load(1);
260303
$result[] = $product->load(2);
@@ -266,7 +309,7 @@ protected function getProducts($count)
266309
/**
267310
* Clear index data
268311
*/
269-
protected function clearIndex()
312+
private function clearIndex()
270313
{
271314
$this->productResource->getConnection()->delete(
272315
$this->productResource->getTable('catalog_category_product_index')
@@ -280,4 +323,15 @@ protected function clearIndex()
280323
);
281324
$this->assertFalse($actualResult);
282325
}
326+
327+
/**
328+
* @inheritdoc
329+
*/
330+
protected function tearDown(): void
331+
{
332+
parent::tearDown();
333+
334+
$this->indexer->reindexAll();
335+
$this->indexer->setScheduled(false);
336+
}
283337
}

0 commit comments

Comments
 (0)