Skip to content

fixed negative children_count after deleting categories #28044

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

Merged
merged 14 commits into from
Jun 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
use Magento\Catalog\Model\Category;

/**
* Aggregate count for parent category after deleting child category
*
* Class AggregateCount
*/
class AggregateCount
{
/**
* Reduces children count for parent categories
*
* @param Category $category
* @return void
*/
Expand All @@ -25,9 +29,7 @@ public function processDelete(Category $category)
*/
$parentIds = $category->getParentIds();
if ($parentIds) {
$childDecrease = $category->getChildrenCount() + 1;
// +1 is itself
$data = ['children_count' => new \Zend_Db_Expr('children_count - ' . $childDecrease)];
$data = ['children_count' => new \Zend_Db_Expr('children_count - 1')];
$where = ['entity_id IN(?)' => $parentIds];
$resourceModel->getConnection()->update($resourceModel->getEntityTable(), $data, $where);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Test\Unit\Model\ResourceModel\Category;

use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\ResourceModel\Category\AggregateCount;
use Magento\Catalog\Model\ResourceModel\Category as ResourceCategory;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* Aggregate count model test
*/
class AggregateCountTest extends TestCase
{

/**
* @var AggregateCount
*/
protected $aggregateCount;

/**
* @var ObjectManagerHelper
*/
protected $objectManagerHelper;

/**
* @var Category|MockObject
*/
protected $categoryMock;

/**
* @var ResourceCategory|MockObject
*/
protected $resourceCategoryMock;

/**
* @var AdapterInterface|MockObject
*/
protected $connectionMock;

/**
* {@inheritdoc}
*/
public function setUp(): void
{
$this->categoryMock = $this->createMock(Category::class);
$this->resourceCategoryMock = $this->createMock(ResourceCategory::class);
$this->connectionMock = $this->getMockBuilder(AdapterInterface::class)
->getMockForAbstractClass();
$this->objectManagerHelper = new ObjectManagerHelper($this);
$this->aggregateCount = $this->objectManagerHelper->getObject(AggregateCount::class);
}

/**
* @return void
*/
public function testProcessDelete(): void
{
$parentIds = 3;
$table = 'catalog_category_entity';

$this->categoryMock->expects($this->once())
->method('getResource')
->willReturn($this->resourceCategoryMock);
$this->categoryMock->expects($this->once())
->method('getParentIds')
->willReturn($parentIds);
$this->resourceCategoryMock->expects($this->any())
->method('getEntityTable')
->willReturn($table);
$this->resourceCategoryMock->expects($this->once())
->method('getConnection')
->willReturn($this->connectionMock);
$this->connectionMock->expects($this->once())
->method('update')
->with(
$table,
['children_count' => new \Zend_Db_Expr('children_count - 1')],
['entity_id IN(?)' => $parentIds]
);
$this->aggregateCount->processDelete($this->categoryMock);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class CategoryTest extends TestCase
*/
protected $objectManager;

/** @var CategoryRepository */
/** @var CategoryResource */
private $categoryResource;

/** @var CategoryRepositoryInterface */
Expand Down Expand Up @@ -355,6 +355,17 @@ public function testDeleteChildren(): void
$this->assertEquals($this->_model->getId(), null);
}

/**
* @magentoDbIsolation enabled
* @magentoAppArea adminhtml
* @magentoDataFixture Magento/Catalog/_files/categories_no_products.php
*/
public function testChildrenCountAfterDeleteParentCategory(): void
{
$this->categoryRepository->deleteByIdentifier(3);
$this->assertEquals(8, $this->categoryResource->getChildrenCount(1));
}

/**
* @magentoDataFixture Magento/Catalog/_files/category.php
*/
Expand Down