diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Category/Collection.php b/app/code/Magento/Catalog/Model/ResourceModel/Category/Collection.php index 9aa073ceacb85..d15116bd50fc9 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Category/Collection.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Category/Collection.php @@ -412,7 +412,6 @@ private function getCountFromCategoryTableBulk( [] ) ->where('ce.entity_id IN (?)', $categoryIds); - $connection->query( $connection->insertFromSelect( $selectDescendants, @@ -420,6 +419,15 @@ private function getCountFromCategoryTableBulk( ['category_id', 'descendant_id'] ) ); + foreach ($categoryIds as $catId) { + $connection->insert( + $tempTableName, + [ + 'category_id' => $catId, + 'descendant_id' => $catId + ] + ); + } $select = $connection->select() ->from( ['t' => $tempTableName], diff --git a/app/code/Magento/Catalog/Test/Unit/Helper/CategoryTestHelper.php b/app/code/Magento/Catalog/Test/Unit/Helper/CategoryTestHelper.php new file mode 100644 index 0000000000000..08743b3959893 --- /dev/null +++ b/app/code/Magento/Catalog/Test/Unit/Helper/CategoryTestHelper.php @@ -0,0 +1,30 @@ +getData('is_anchor'); + } +} diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Category/CollectionTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Category/CollectionTest.php index f29bfedc48511..d70704871e469 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Category/CollectionTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Category/CollectionTest.php @@ -8,6 +8,7 @@ namespace Magento\Catalog\Test\Unit\Model\ResourceModel\Category; use Magento\Catalog\Model\Category; +use Magento\Catalog\Test\Unit\Helper\CategoryTestHelper; use Magento\Framework\Data\Collection\EntityFactory; use Magento\Store\Model\Store; use Psr\Log\LoggerInterface; @@ -229,11 +230,7 @@ public function testLoadProductCountCallsBulkMethodForLargeCategoryCount() $items = []; $categoryIds = []; for ($i = 1; $i <= $categoryCount; $i++) { - $category = $this->getMockBuilder(Category::class) - ->addMethods(['getIsAnchor']) - ->onlyMethods(['getId', 'setProductCount']) - ->disableOriginalConstructor() - ->getMock(); + $category = $this->createMock(CategoryTestHelper::class); $category->method('getId')->willReturn($i); $category->method('getIsAnchor')->willReturn(true); $category->expects($this->once())->method('setProductCount')->with(5); @@ -265,6 +262,26 @@ public function testLoadProductCountCallsBulkMethodForLargeCategoryCount() $this->connection->method('select')->willReturn($this->select); $this->connection->method('insertFromSelect')->willReturn('INSERT QUERY'); $this->connection->method('query')->with('INSERT QUERY')->willReturnSelf(); + $withs = []; + foreach ($categoryIds as $categoryId) { + $withs[] = [ + 'category_id' => $categoryId, + 'descendant_id' => $categoryId + ]; + } + $callIndex = 0; + $this->connection + ->expects($this->exactly(count($categoryIds))) + ->method('insert') + ->with( + $this->stringContains('temp_category_descendants_'), + $this->callback(function($args) use (&$callIndex, $withs) { + $expected = $withs[$callIndex]; + $valid = $args === $expected; + $callIndex++; + return $valid; + }) + ); $this->select->method('from')->willReturnSelf(); $this->select->method('joinLeft')->willReturnSelf(); $this->select->method('join')->willReturnSelf();