Skip to content

Commit d96b7c9

Browse files
authored
ENGCOM-7513: fixed negative children_count after deleting categories #28044
2 parents 02b87f6 + 1bb0f9f commit d96b7c9

File tree

3 files changed

+108
-4
lines changed

3 files changed

+108
-4
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@
88
use Magento\Catalog\Model\Category;
99

1010
/**
11+
* Aggregate count for parent category after deleting child category
12+
*
1113
* Class AggregateCount
1214
*/
1315
class AggregateCount
1416
{
1517
/**
18+
* Reduces children count for parent categories
19+
*
1620
* @param Category $category
1721
* @return void
1822
*/
@@ -25,9 +29,7 @@ public function processDelete(Category $category)
2529
*/
2630
$parentIds = $category->getParentIds();
2731
if ($parentIds) {
28-
$childDecrease = $category->getChildrenCount() + 1;
29-
// +1 is itself
30-
$data = ['children_count' => new \Zend_Db_Expr('children_count - ' . $childDecrease)];
32+
$data = ['children_count' => new \Zend_Db_Expr('children_count - 1')];
3133
$where = ['entity_id IN(?)' => $parentIds];
3234
$resourceModel->getConnection()->update($resourceModel->getEntityTable(), $data, $where);
3335
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Test\Unit\Model\ResourceModel\Category;
9+
10+
use Magento\Catalog\Model\Category;
11+
use Magento\Catalog\Model\ResourceModel\Category\AggregateCount;
12+
use Magento\Catalog\Model\ResourceModel\Category as ResourceCategory;
13+
use Magento\Framework\DB\Adapter\AdapterInterface;
14+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
15+
use PHPUnit\Framework\MockObject\MockObject;
16+
use PHPUnit\Framework\TestCase;
17+
18+
/**
19+
* Aggregate count model test
20+
*/
21+
class AggregateCountTest extends TestCase
22+
{
23+
24+
/**
25+
* @var AggregateCount
26+
*/
27+
protected $aggregateCount;
28+
29+
/**
30+
* @var ObjectManagerHelper
31+
*/
32+
protected $objectManagerHelper;
33+
34+
/**
35+
* @var Category|MockObject
36+
*/
37+
protected $categoryMock;
38+
39+
/**
40+
* @var ResourceCategory|MockObject
41+
*/
42+
protected $resourceCategoryMock;
43+
44+
/**
45+
* @var AdapterInterface|MockObject
46+
*/
47+
protected $connectionMock;
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
public function setUp(): void
53+
{
54+
$this->categoryMock = $this->createMock(Category::class);
55+
$this->resourceCategoryMock = $this->createMock(ResourceCategory::class);
56+
$this->connectionMock = $this->getMockBuilder(AdapterInterface::class)
57+
->getMockForAbstractClass();
58+
$this->objectManagerHelper = new ObjectManagerHelper($this);
59+
$this->aggregateCount = $this->objectManagerHelper->getObject(AggregateCount::class);
60+
}
61+
62+
/**
63+
* @return void
64+
*/
65+
public function testProcessDelete(): void
66+
{
67+
$parentIds = 3;
68+
$table = 'catalog_category_entity';
69+
70+
$this->categoryMock->expects($this->once())
71+
->method('getResource')
72+
->willReturn($this->resourceCategoryMock);
73+
$this->categoryMock->expects($this->once())
74+
->method('getParentIds')
75+
->willReturn($parentIds);
76+
$this->resourceCategoryMock->expects($this->any())
77+
->method('getEntityTable')
78+
->willReturn($table);
79+
$this->resourceCategoryMock->expects($this->once())
80+
->method('getConnection')
81+
->willReturn($this->connectionMock);
82+
$this->connectionMock->expects($this->once())
83+
->method('update')
84+
->with(
85+
$table,
86+
['children_count' => new \Zend_Db_Expr('children_count - 1')],
87+
['entity_id IN(?)' => $parentIds]
88+
);
89+
$this->aggregateCount->processDelete($this->categoryMock);
90+
}
91+
}

dev/tests/integration/testsuite/Magento/Catalog/Model/CategoryTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class CategoryTest extends TestCase
4949
*/
5050
protected $objectManager;
5151

52-
/** @var CategoryRepository */
52+
/** @var CategoryResource */
5353
private $categoryResource;
5454

5555
/** @var CategoryRepositoryInterface */
@@ -355,6 +355,17 @@ public function testDeleteChildren(): void
355355
$this->assertEquals($this->_model->getId(), null);
356356
}
357357

358+
/**
359+
* @magentoDbIsolation enabled
360+
* @magentoAppArea adminhtml
361+
* @magentoDataFixture Magento/Catalog/_files/categories_no_products.php
362+
*/
363+
public function testChildrenCountAfterDeleteParentCategory(): void
364+
{
365+
$this->categoryRepository->deleteByIdentifier(3);
366+
$this->assertEquals(8, $this->categoryResource->getChildrenCount(1));
367+
}
368+
358369
/**
359370
* @magentoDataFixture Magento/Catalog/_files/category.php
360371
*/

0 commit comments

Comments
 (0)