Skip to content

Commit ff096a8

Browse files
committed
MC-37006: Adding/removing disabled products to Magento flushes categories cache
1 parent 40512d4 commit ff096a8

File tree

4 files changed

+100
-36
lines changed

4 files changed

+100
-36
lines changed

dev/tests/integration/testsuite/Magento/Catalog/Controller/Adminhtml/Product/Initialization/HelperTest.php

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,14 @@ protected function setUp(): void
2727
}
2828

2929
/**
30-
* @magentoDataFixture Magento/Catalog/_files/categories.php
31-
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
30+
* @magentoDataFixture Magento/Catalog/_files/products_in_categories.php
3231
* @dataProvider initializeCategoriesFromDataProvider
3332
* @param string $sku
3433
* @param array $categoryIds
3534
*/
3635
public function testInitializeCategoriesFromData(string $sku, array $categoryIds): void
3736
{
38-
$productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class);
37+
$productRepository = Bootstrap::getObjectManager()->create(ProductRepositoryInterface::class);
3938
/** @var \Magento\Catalog\Model\Product $product */
4039
$product = $productRepository->get($sku);
4140
$productData = $product->getData();
@@ -55,34 +54,15 @@ public function testInitializeCategoriesFromData(string $sku, array $categoryIds
5554
public function initializeCategoriesFromDataProvider(): array
5655
{
5756
return [
58-
'assign categories' => [
59-
'simple',
60-
[2, 3, 4, 11, 12, 13],
61-
],
62-
'unassign categories' => [
63-
'simple-4',
64-
[11, 12],
65-
],
66-
'update categories' => [
67-
'simple-3',
68-
[10, 12, 13],
69-
],
70-
'change all categories' => [
71-
'simple-3',
72-
[4, 5]
73-
],
74-
'unassign all categories' => [
75-
'simple-3',
76-
[],
77-
],
78-
'assign new category' => [
79-
'simple2',
80-
[11],
81-
],
82-
'assign new categories' => [
83-
'simple2',
84-
[11, 13],
85-
],
57+
'assign category' => ['simple1', [3, 4]],
58+
'assign categories' => ['simple1', [3, 5, 6]],
59+
'unassign category' => ['simple2', [3, 6]],
60+
'unassign categories' => ['simple2', [3]],
61+
'update categories' => ['simple2', [3, 4, 6]],
62+
'change all categories' => ['simple2', [4]],
63+
'unassign all categories' => ['simple2', []],
64+
'assign new category' => ['simple3', [4]],
65+
'assign new categories' => ['simple3', [4, 5]],
8666
];
8767
}
8868
}

dev/tests/integration/testsuite/Magento/Catalog/_files/categories.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,11 @@
1212
$productRepository = $objectManager->create(
1313
\Magento\Catalog\Api\ProductRepositoryInterface::class
1414
);
15-
$categoryRepository = $objectManager->create(
16-
\Magento\Catalog\Api\CategoryRepositoryInterface::class
17-
);
1815

1916
$categoryLinkRepository = $objectManager->create(
2017
\Magento\Catalog\Api\CategoryLinkRepositoryInterface::class,
2118
[
22-
'productRepository' => $productRepository,
23-
'categoryRepository' => $categoryRepository,
19+
'productRepository' => $productRepository
2420
]
2521
);
2622

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
8+
9+
foreach (range(3, 7) as $categoryId) {
10+
$category = $objectManager->create(\Magento\Catalog\Model\Category::class);
11+
$category->isObjectNew(true);
12+
$category->setId($categoryId)
13+
->setName('Category ' . $categoryId)
14+
->setParentId(2)
15+
->setPath('1/2/' . $categoryId)
16+
->setLevel(2)
17+
->setIsActive(true)
18+
->save();
19+
}
20+
21+
foreach (range(1, 3) as $productId) {
22+
$product = $objectManager->create(\Magento\Catalog\Model\Product::class);
23+
$product->isObjectNew(true);
24+
$product->setId($productId)
25+
->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
26+
->setAttributeSetId(4)
27+
->setStoreId(1)
28+
->setWebsiteIds([1])
29+
->setName('Simple Product ' . $productId)
30+
->setSku('simple' . $productId)
31+
->setPrice(10)
32+
->setWeight(1)
33+
->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
34+
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
35+
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
36+
->save();
37+
}
38+
39+
$categoryRepository = $objectManager->create(\Magento\Catalog\Api\CategoryRepositoryInterface::class);
40+
$categoryLinkRepository = $objectManager->create(
41+
\Magento\Catalog\Api\CategoryLinkRepositoryInterface::class,
42+
[
43+
'categoryRepository' => $categoryRepository,
44+
]
45+
);
46+
$categoryLinkManagement = $objectManager->create(
47+
\Magento\Catalog\Api\CategoryLinkManagementInterface::class,
48+
[
49+
'categoryRepository' => $categoryRepository,
50+
]
51+
);
52+
$reflectionClass = new \ReflectionClass(get_class($categoryLinkManagement));
53+
$reflectionProperty = $reflectionClass->getProperty('categoryLinkRepository');
54+
$reflectionProperty->setAccessible(true);
55+
$reflectionProperty->setValue($categoryLinkManagement, $categoryLinkRepository);
56+
$categoryLinkManagement->assignProductToCategories('simple1', [3]);
57+
$categoryLinkManagement->assignProductToCategories('simple2', [3, 5, 6]);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
8+
9+
$registry = $objectManager->get(\Magento\Framework\Registry::class);
10+
$registry->unregister('isSecureArea');
11+
$registry->register('isSecureArea', true);
12+
13+
$productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
14+
$productsToDelete = ['simple1', 'simple2', 'simple3'];
15+
foreach ($productsToDelete as $sku) {
16+
try {
17+
$product = $productRepository->get($sku, false, null, true);
18+
$productRepository->delete($product);
19+
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
20+
//Product already removed
21+
}
22+
}
23+
24+
$collection = $objectManager->create(\Magento\Catalog\Model\ResourceModel\Category\Collection::class);
25+
foreach ($collection->addAttributeToFilter('level', 2) as $category) {
26+
/** @var \Magento\Catalog\Model\Category $category */
27+
$category->delete();
28+
}
29+
30+
$registry->unregister('isSecureArea');
31+
$registry->register('isSecureArea', false);

0 commit comments

Comments
 (0)