Skip to content

Commit 6edfb10

Browse files
author
OlgaVasyltsun
committed
MC-21966: Category path changes
1 parent 8df459c commit 6edfb10

File tree

6 files changed

+222
-0
lines changed

6 files changed

+222
-0
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,9 @@ protected function _beforeSave(\Magento\Framework\DataObject $object)
269269
$object->setAttributeSetId(
270270
$object->getAttributeSetId() ?: $this->getEntityType()->getDefaultAttributeSetId()
271271
);
272+
273+
$this->castPathIdsToInt($object);
274+
272275
if ($object->isObjectNew()) {
273276
if ($object->getPosition() === null) {
274277
$object->setPosition($this->_getMaxPosition($object->getPath()) + 1);
@@ -1125,4 +1128,23 @@ private function getAggregateCount()
11251128
}
11261129
return $this->aggregateCount;
11271130
}
1131+
1132+
/**
1133+
* Cast category path ids to int.
1134+
*
1135+
* @param DataObject $object
1136+
* @return void
1137+
*/
1138+
private function castPathIdsToInt(DataObject $object)
1139+
{
1140+
$pathIds = explode('/', (string)$object->getPath());
1141+
1142+
array_walk(
1143+
$pathIds,
1144+
function (&$pathId) {
1145+
$pathId = (int)$pathId;
1146+
}
1147+
);
1148+
$object->setPath(implode('/', $pathIds));
1149+
}
11281150
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,14 @@ public function loadByIds($ids, $addCollectionData = true, $updateAnchorProductC
483483

484484
foreach ($this->_conn->fetchAll($select) as $item) {
485485
$pathIds = explode('/', $item['path']);
486+
487+
array_walk(
488+
$pathIds,
489+
function (&$pathId) {
490+
$pathId = (int)$pathId;
491+
}
492+
);
493+
486494
$level = (int)$item['level'];
487495
while ($level > 0) {
488496
$pathIds[count($pathIds) - 1] = '%';

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
*/
66
namespace Magento\Catalog\Model;
77

8+
use Magento\Catalog\Api\CategoryRepositoryInterface;
9+
use Magento\Catalog\Api\Data\CategoryInterface;
10+
use Magento\Catalog\Api\Data\CategoryInterfaceFactory;
11+
812
/**
913
* Test class for \Magento\Catalog\Model\Category.
1014
* - general behaviour is tested
@@ -365,4 +369,35 @@ protected function getCategoryByName($categoryName)
365369
$collection->addNameToResult()->load();
366370
return $collection->getItemByColumnValue('name', $categoryName);
367371
}
372+
373+
/**
374+
* @return void
375+
*/
376+
public function testSaveCategoryWithWrongPath()
377+
{
378+
/** @var CategoryRepositoryInterface $categoryRepository */
379+
$categoryRepository = $this->objectManager->get(CategoryRepositoryInterface::class);
380+
$categoryFactory = $this->objectManager->get(CategoryInterfaceFactory::class);
381+
382+
/** @var CategoryInterface $category */
383+
$category = $categoryFactory->create(
384+
[
385+
'data' => [
386+
'name' => 'Category With Wrong Path',
387+
'parent_id' => 2,
388+
'path' => 'wrong/path',
389+
'level' => 2,
390+
'available_sort_by' =>['position', 'name'],
391+
'default_sort_by' => 'name',
392+
'is_active' => true,
393+
'position' => 1,
394+
],
395+
]
396+
);
397+
$category->isObjectNew(true);
398+
$category->save();
399+
400+
$createdCategory = $categoryRepository->get($category->getId());
401+
$this->assertEquals('0/0/'. $createdCategory->getId(), $createdCategory->getPath());
402+
}
368403
}
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\Model\ResourceModel\Category;
9+
10+
use Magento\Catalog\Model\Category;
11+
use Magento\Catalog\Model\CategoryList;
12+
use Magento\Framework\Api\SearchCriteriaBuilder;
13+
use Magento\Framework\Data\Tree\Node;
14+
15+
/**
16+
* Test for \Magento\Catalog\Model\ResourceModel\Category\Tree.
17+
*
18+
* @magentoDbIsolation enabled
19+
*/
20+
class TreeTest extends \PHPUnit\Framework\TestCase
21+
{
22+
/**
23+
* @var Tree
24+
*/
25+
private $treeModel;
26+
27+
/**
28+
* @var CategoryList
29+
*/
30+
private $categoryList;
31+
32+
/**
33+
* @var SearchCriteriaBuilder
34+
*/
35+
private $searchCriteriaBuilder;
36+
37+
/**
38+
* @inheritDoc
39+
*/
40+
protected function setUp()
41+
{
42+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
43+
$this->treeModel = $objectManager->create(Tree::class);
44+
$this->categoryList = $objectManager->get(CategoryList::class);
45+
$this->searchCriteriaBuilder = $objectManager->get(SearchCriteriaBuilder::class);
46+
}
47+
48+
/**
49+
* @magentoDataFixture Magento/Catalog/_files/category.php
50+
* @return void
51+
*/
52+
public function testLoadByIds()
53+
{
54+
$categoryId = $this->getCategoryIdByName('Category 1');
55+
// Load category nodes by created category
56+
$this->treeModel->loadByIds([$categoryId]);
57+
$this->assertCount(3, $this->treeModel->getNodes());
58+
$this->assertInstanceOf(Node::class, $this->treeModel->getNodeById($categoryId));
59+
}
60+
61+
/**
62+
* @magentoDataFixture Magento/Catalog/_files/category_with_wrong_path.php
63+
* @return void
64+
*/
65+
public function testLoadByIdsWithWrongCategoryPath()
66+
{
67+
$categoryId = $this->getCategoryIdByName('Category With Wrong Path');
68+
// Load category nodes by created category
69+
$this->treeModel->loadByIds([$categoryId]);
70+
$this->assertCount(1, $this->treeModel->getNodes());
71+
$this->assertNull($this->treeModel->getNodeById($categoryId));
72+
}
73+
74+
/**
75+
* Return category id by category name.
76+
*
77+
* @param string $categoryName
78+
* @return int
79+
*/
80+
private function getCategoryIdByName(string $categoryName): int
81+
{
82+
$searchCriteria = $this->searchCriteriaBuilder
83+
->addFilter(Category::KEY_NAME, $categoryName)
84+
->create();
85+
$categories = $this->categoryList->getList($searchCriteria)->getItems();
86+
$category = reset($categories);
87+
$categoryId = $category->getId();
88+
89+
return (int)$categoryId;
90+
}
91+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
9+
/** @var \Magento\Catalog\Api\Data\CategoryInterfaceFactory $categoryFactory */
10+
$categoryFactory = $objectManager->get(\Magento\Catalog\Api\Data\CategoryInterfaceFactory::class);
11+
12+
/** @var \Magento\Catalog\Api\Data\CategoryInterface $category */
13+
$category = $categoryFactory->create(
14+
[
15+
'data' => [
16+
'name' => 'Category With Wrong Path',
17+
'parent_id' => 2,
18+
'path' => 'wrong/path',
19+
'level' => 2,
20+
'available_sort_by' =>['position', 'name'],
21+
'default_sort_by' => 'name',
22+
'is_active' => true,
23+
'position' => 1,
24+
],
25+
]
26+
);
27+
28+
$category->isObjectNew(true);
29+
$category->save();
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
use Magento\Catalog\Model\Category;
9+
use Magento\Catalog\Model\CategoryList;
10+
use Magento\Catalog\Model\CategoryRepository;
11+
use Magento\Framework\Api\SearchCriteriaBuilder;
12+
use Magento\Framework\Registry;
13+
use Magento\TestFramework\Helper\Bootstrap;
14+
15+
$objectManager = Bootstrap::getObjectManager();
16+
/** @var SearchCriteriaBuilder $searchCriteriaBuilder */
17+
$searchCriteriaBuilder = $objectManager->get(SearchCriteriaBuilder::class);
18+
/** @var CategoryRepository $categoryRepository */
19+
$categoryRepository = $objectManager->get(CategoryRepository::class);
20+
/** @var CategoryList $categoryList */
21+
$categoryList = $objectManager->get(CategoryList::class);
22+
/** @var Registry $registry */
23+
$registry = $objectManager->get(Registry::class);
24+
$registry->unregister('isSecureArea');
25+
$registry->register('isSecureArea', true);
26+
27+
$searchCriteria = $searchCriteriaBuilder
28+
->addFilter(Category::KEY_NAME, 'Category With Wrong Path')
29+
->create();
30+
$categories = $categoryList->getList($searchCriteria)->getItems();
31+
32+
foreach ($categories as $category) {
33+
$categoryRepository->delete($category);
34+
}
35+
36+
$registry->unregister('isSecureArea');
37+
$registry->register('isSecureArea', false);

0 commit comments

Comments
 (0)