Skip to content

Commit 5c499bc

Browse files
authored
ENGCOM-3252: Correct child node load when multiple calls to CategoryManagement::ge… #18705
2 parents 9fe587c + db4eff9 commit 5c499bc

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,40 @@ class Tree
3232
*/
3333
protected $treeFactory;
3434

35+
/**
36+
* @var \Magento\Catalog\Model\ResourceModel\Category\TreeFactory
37+
*/
38+
private $treeResourceFactory;
39+
3540
/**
3641
* @param \Magento\Catalog\Model\ResourceModel\Category\Tree $categoryTree
3742
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
3843
* @param \Magento\Catalog\Model\ResourceModel\Category\Collection $categoryCollection
3944
* @param \Magento\Catalog\Api\Data\CategoryTreeInterfaceFactory $treeFactory
45+
* @param \Magento\Catalog\Model\ResourceModel\Category\TreeFactory|null $treeResourceFactory
4046
*/
4147
public function __construct(
4248
\Magento\Catalog\Model\ResourceModel\Category\Tree $categoryTree,
4349
\Magento\Store\Model\StoreManagerInterface $storeManager,
4450
\Magento\Catalog\Model\ResourceModel\Category\Collection $categoryCollection,
45-
\Magento\Catalog\Api\Data\CategoryTreeInterfaceFactory $treeFactory
51+
\Magento\Catalog\Api\Data\CategoryTreeInterfaceFactory $treeFactory,
52+
\Magento\Catalog\Model\ResourceModel\Category\TreeFactory $treeResourceFactory = null
4653
) {
4754
$this->categoryTree = $categoryTree;
4855
$this->storeManager = $storeManager;
4956
$this->categoryCollection = $categoryCollection;
5057
$this->treeFactory = $treeFactory;
58+
$this->treeResourceFactory = $treeResourceFactory ?? \Magento\Framework\App\ObjectManager::getInstance()
59+
->get(\Magento\Catalog\Model\ResourceModel\Category\TreeFactory::class);
5160
}
5261

5362
/**
63+
* Get root node by category.
64+
*
5465
* @param \Magento\Catalog\Model\Category|null $category
5566
* @return Node|null
67+
* @throws \Magento\Framework\Exception\LocalizedException
68+
* @throws \Magento\Framework\Exception\NoSuchEntityException
5669
*/
5770
public function getRootNode($category = null)
5871
{
@@ -71,21 +84,30 @@ public function getRootNode($category = null)
7184
}
7285

7386
/**
87+
* Get node by category.
88+
*
7489
* @param \Magento\Catalog\Model\Category $category
7590
* @return Node
91+
* @throws \Magento\Framework\Exception\LocalizedException
92+
* @throws \Magento\Framework\Exception\NoSuchEntityException
7693
*/
7794
protected function getNode(\Magento\Catalog\Model\Category $category)
7895
{
7996
$nodeId = $category->getId();
80-
$node = $this->categoryTree->loadNode($nodeId);
97+
$categoryTree = $this->treeResourceFactory->create();
98+
$node = $categoryTree->loadNode($nodeId);
8199
$node->loadChildren();
82100
$this->prepareCollection();
83101
$this->categoryTree->addCollectionData($this->categoryCollection);
84102
return $node;
85103
}
86104

87105
/**
106+
* Prepare category collection.
107+
*
88108
* @return void
109+
* @throws \Magento\Framework\Exception\LocalizedException
110+
* @throws \Magento\Framework\Exception\NoSuchEntityException
89111
*/
90112
protected function prepareCollection()
91113
{
@@ -104,6 +126,8 @@ protected function prepareCollection()
104126
}
105127

106128
/**
129+
* Get tree by node.
130+
*
107131
* @param \Magento\Framework\Data\Tree\Node $node
108132
* @param int $depth
109133
* @param int $currentLevel
@@ -127,6 +151,8 @@ public function getTree($node, $depth = null, $currentLevel = 0)
127151
}
128152

129153
/**
154+
* Get node children.
155+
*
130156
* @param \Magento\Framework\Data\Tree\Node $node
131157
* @param int $depth
132158
* @param int $currentLevel

app/code/Magento/Catalog/Test/Unit/Model/Category/TreeTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ class TreeTest extends \PHPUnit\Framework\TestCase
4343
*/
4444
protected $node;
4545

46+
/**
47+
* @var \Magento\Catalog\Model\ResourceModel\Category\TreeFactory
48+
*/
49+
private $treeResourceFactoryMock;
50+
4651
protected function setUp()
4752
{
4853
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
@@ -59,6 +64,12 @@ protected function setUp()
5964
\Magento\Store\Model\StoreManagerInterface::class
6065
)->disableOriginalConstructor()->getMock();
6166

67+
$this->treeResourceFactoryMock = $this->createMock(
68+
\Magento\Catalog\Model\ResourceModel\Category\TreeFactory::class
69+
);
70+
$this->treeResourceFactoryMock->method('create')
71+
->willReturn($this->categoryTreeMock);
72+
6273
$methods = ['create'];
6374
$this->treeFactoryMock =
6475
$this->createPartialMock(\Magento\Catalog\Api\Data\CategoryTreeInterfaceFactory::class, $methods);
@@ -70,7 +81,8 @@ protected function setUp()
7081
'categoryCollection' => $this->categoryCollection,
7182
'categoryTree' => $this->categoryTreeMock,
7283
'storeManager' => $this->storeManagerMock,
73-
'treeFactory' => $this->treeFactoryMock
84+
'treeFactory' => $this->treeFactoryMock,
85+
'treeResourceFactory' => $this->treeResourceFactoryMock,
7486
]
7587
);
7688
}

0 commit comments

Comments
 (0)