Skip to content

Commit

Permalink
Correct child node load when multiple calls to CategoryManagement::ge…
Browse files Browse the repository at this point in the history
…tTree

Tree::getNode currently loads nodes using a resource singleton. The tree
resource's parent class contains the property _loaded and only loads child
nodes when _loaded is false. This behavior means only the first call to
CategoryManagement::getTree in a request returns child nodes.

Fixes #17297
  • Loading branch information
pmclain committed Oct 19, 2018
1 parent f710f9b commit 0db0e84
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
14 changes: 12 additions & 2 deletions app/code/Magento/Catalog/Model/Category/Tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,31 @@ class Tree
*/
protected $treeFactory;

/**
* @var \Magento\Catalog\Model\ResourceModel\Category\TreeFactory
*/
private $treeResourceFactory;

/**
* @param \Magento\Catalog\Model\ResourceModel\Category\Tree $categoryTree
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Catalog\Model\ResourceModel\Category\Collection $categoryCollection
* @param \Magento\Catalog\Api\Data\CategoryTreeInterfaceFactory $treeFactory
* @param \Magento\Catalog\Model\ResourceModel\Category\TreeFactory|null $treeResourceFactory
*/
public function __construct(
\Magento\Catalog\Model\ResourceModel\Category\Tree $categoryTree,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Catalog\Model\ResourceModel\Category\Collection $categoryCollection,
\Magento\Catalog\Api\Data\CategoryTreeInterfaceFactory $treeFactory
\Magento\Catalog\Api\Data\CategoryTreeInterfaceFactory $treeFactory,
\Magento\Catalog\Model\ResourceModel\Category\TreeFactory $treeResourceFactory = null
) {
$this->categoryTree = $categoryTree;
$this->storeManager = $storeManager;
$this->categoryCollection = $categoryCollection;
$this->treeFactory = $treeFactory;
$this->treeResourceFactory = $treeResourceFactory ?? \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Catalog\Model\ResourceModel\Category\TreeFactory::class);
}

/**
Expand Down Expand Up @@ -77,7 +86,8 @@ public function getRootNode($category = null)
protected function getNode(\Magento\Catalog\Model\Category $category)
{
$nodeId = $category->getId();
$node = $this->categoryTree->loadNode($nodeId);
$categoryTree = $this->treeResourceFactory->create();
$node = $categoryTree->loadNode($nodeId);
$node->loadChildren();
$this->prepareCollection();
$this->categoryTree->addCollectionData($this->categoryCollection);
Expand Down
12 changes: 11 additions & 1 deletion app/code/Magento/Catalog/Test/Unit/Model/Category/TreeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ class TreeTest extends \PHPUnit\Framework\TestCase
*/
protected $node;

/**
* @var \Magento\Catalog\Model\ResourceModel\Category\TreeFactory
*/
private $treeResourceFactoryMock;

protected function setUp()
{
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
Expand All @@ -59,6 +64,10 @@ protected function setUp()
\Magento\Store\Model\StoreManagerInterface::class
)->disableOriginalConstructor()->getMock();

$this->treeResourceFactoryMock = $this->createMock(\Magento\Catalog\Model\ResourceModel\Category\TreeFactory::class);
$this->treeResourceFactoryMock->method('create')
->willReturn($this->categoryTreeMock);

$methods = ['create'];
$this->treeFactoryMock =
$this->createPartialMock(\Magento\Catalog\Api\Data\CategoryTreeInterfaceFactory::class, $methods);
Expand All @@ -70,7 +79,8 @@ protected function setUp()
'categoryCollection' => $this->categoryCollection,
'categoryTree' => $this->categoryTreeMock,
'storeManager' => $this->storeManagerMock,
'treeFactory' => $this->treeFactoryMock
'treeFactory' => $this->treeFactoryMock,
'treeResourceFactory' => $this->treeResourceFactoryMock,
]
);
}
Expand Down

0 comments on commit 0db0e84

Please sign in to comment.