Skip to content

magento/magento2#28358: URL rewrites are not generated for categorie with 'Include in Menu=No' when creating a store view #30427

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

Open
wants to merge 22 commits into
base: 2.4-develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
2 changes: 2 additions & 0 deletions app/code/Magento/Catalog/Model/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,8 @@ public function getProductCount()
* @param bool $asCollection
* @param bool $toLoad
* @return \Magento\Framework\Data\Tree\Node\Collection|\Magento\Catalog\Model\ResourceModel\Category\Collection
* @deprecated The method doesn't consider onlyActive, onlyIncludeInMenu params
* @see \Magento\Catalog\Model\Category\GetCategoriesCollection
*/
public function getCategories($parent, $recursionLevel = 0, $sorted = false, $asCollection = false, $toLoad = true)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Catalog\Model\Category;

use Magento\Catalog\Model\ResourceModel\Category\Collection;
use Magento\Catalog\Model\ResourceModel\Category\TreeFactory;
use Magento\Framework\Data\Tree\Node\Collection as NodeCollection;

/**
* Returns categories collection
*/
class GetCategoriesCollection
{
/**
* @var TreeFactory
*/
private $categoryTreeFactory;

/**
* @param TreeFactory $categoryTreeFactory
*/
public function __construct(TreeFactory $categoryTreeFactory)
{
$this->categoryTreeFactory = $categoryTreeFactory;
}

/**
* Returns collection
*
* @param int $parent
* @param int|null $recursionLevel
* @param bool|null $sorted
* @param bool|null $asCollection
* @param bool|null $toLoad
* @param bool|null $onlyActive
* @param bool|null $onlyIncludeInMenu
* @return NodeCollection|Collection
*/
public function execute(
int $parent,
?int $recursionLevel = 0,
?bool $sorted = false,
?bool $asCollection = false,
?bool $toLoad = true,
?bool $onlyActive = true,
?bool $onlyIncludeInMenu = true
) {
$tree = $this->categoryTreeFactory->create();
$nodes = $tree->loadNode($parent)
->loadChildren($recursionLevel)
->getChildren();

$tree->addCollectionDataParams(null, $sorted, $parent, $toLoad, $onlyActive, $onlyIncludeInMenu);

return $asCollection ? $tree->getCollection() : $nodes;
}
}
10 changes: 6 additions & 4 deletions app/code/Magento/Catalog/Model/ResourceModel/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public function __construct(
$this->_categoryTreeFactory = $categoryTreeFactory;
$this->_categoryCollectionFactory = $categoryCollectionFactory;
$this->_eventManager = $eventManager;
$this->connectionName = 'catalog';
$this->connectionName = 'catalog';
$this->indexerProcessor = $indexerProcessor;
$this->serializer = $serializer ?: ObjectManager::getInstance()
->get(\Magento\Framework\Serialize\Serializer\Json::class);
Expand Down Expand Up @@ -289,7 +289,7 @@ protected function _beforeSave(\Magento\Framework\DataObject $object)
$object->setPosition($this->_getMaxPosition($object->getPath()) + 1);
}
$path = explode('/', (string)$object->getPath());
$level = count($path) - ($object->getId() ? 1 : 0);
$level = count($path) - ($object->getId() ? 1 : 0);
$toUpdateChild = array_diff($path, [$object->getId()]);

if (!$object->hasPosition()) {
Expand Down Expand Up @@ -696,7 +696,7 @@ public function getProductCount($category)
$bind = ['category_id' => (int)$category->getId()];
$counts = $this->getConnection()->fetchOne($select, $bind);

return (int) $counts;
return (int)$counts;
}

/**
Expand All @@ -708,6 +708,8 @@ public function getProductCount($category)
* @param boolean $asCollection
* @param boolean $toLoad
* @return \Magento\Framework\Data\Tree\Node\Collection|\Magento\Catalog\Model\ResourceModel\Category\Collection
* @deprecated The method doesn't consider onlyActive, onlyIncludeInMenu params
* @see \Magento\Catalog\Model\Category\GetCategoriesCollection
*/
public function getCategories($parent, $recursionLevel = 0, $sorted = false, $asCollection = false, $toLoad = true)
{
Expand Down Expand Up @@ -1112,7 +1114,7 @@ public function delete($object)
/**
* Save entity's attributes into the object's resource
*
* @param \Magento\Framework\Model\AbstractModel $object
* @param \Magento\Framework\Model\AbstractModel $object
* @return $this
* @throws \Exception
*/
Expand Down
77 changes: 77 additions & 0 deletions app/code/Magento/Catalog/Model/ResourceModel/Category/Tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ public function getStoreId()
* @param boolean $toLoad
* @param boolean $onlyActive
* @return $this
* @deprecated This method is not intended for usage in child classes
* @see addCollectionDataParams($collection, $sorted, $exclude, $toLoad, $onlyActive, $onlyIncludeInMenu)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
Expand Down Expand Up @@ -236,6 +238,81 @@ public function addCollectionData(
return $this;
}

/**
* Add data params to collection
*
* @param Collection|null $collection
* @param boolean $sorted
* @param array $exclude
* @param boolean $toLoad
* @param boolean $onlyActive
* @param boolean $onlyIncludeInMenu
* @return $this
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function addCollectionDataParams(
$collection = null,
$sorted = false,
$exclude = [],
$toLoad = true,
$onlyActive = false,
$onlyIncludeInMenu = false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing an @api public method is BIC, please have a look at https://devdocs.magento.com/contributor-guide/backward-compatible-development/

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi @gabrieldagama i have added code as BIC . Please review this

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sodhisarab please, extract logic to the separate class

) {
if ($collection === null) {
$collection = $this->getCollection($sorted);
} else {
$this->setCollection($collection);
}

if (!is_array($exclude)) {
$exclude = [$exclude];
}

$nodeIds = [];
foreach ($this->getNodes() as $node) {
if (!in_array($node->getId(), $exclude)) {
$nodeIds[] = $node->getId();
}
}
$collection->addIdFilter($nodeIds);

if ($onlyActive) {
$disabledIds = $this->_getDisabledIds($collection, $nodeIds);
if ($disabledIds) {
$collection->addFieldToFilter('entity_id', ['nin' => $disabledIds]);
}
$collection->addAttributeToFilter('is_active', 1);
if ($onlyIncludeInMenu) {
$collection->addAttributeToFilter('include_in_menu', 1);
}
}

if ($this->_joinUrlRewriteIntoCollection) {
$collection->joinUrlRewrite();
$this->_joinUrlRewriteIntoCollection = false;
}

if ($toLoad) {
$collection->load();

foreach ($collection as $category) {
$node = $this->getNodeById($category->getId());
if ($node) {
$node->addData($category->getData());
}
}

foreach ($this->getNodes() as $node) {
if ($node->getParent() && !$collection->getItemById($node->getId())) {
$this->removeNode($node);
}
}
}

return $this;
}

/**
* Add inactive categories ids
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,6 @@ public function testAddCollectionData()

$model->addNode($nodeMock);

$this->assertSame($model, $model->addCollectionData(null, false, [], false, true));
$this->assertSame($model, $model->addCollectionDataParams(null, false, [], false, true, false));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Magento\CatalogUrlRewrite\Model\Category\Plugin\Store;

use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\Category\GetCategoriesCollection;
use Magento\Catalog\Model\CategoryFactory;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\ProductFactory;
Expand Down Expand Up @@ -56,25 +57,33 @@ class View
*/
private $origStore;

/**
* @var GetCategoriesCollection
*/
private $getCategoriesCollection;

/**
* @param UrlPersistInterface $urlPersist
* @param CategoryFactory $categoryFactory
* @param ProductFactory $productFactory
* @param CategoryUrlRewriteGenerator $categoryUrlRewriteGenerator
* @param ProductUrlRewriteGenerator $productUrlRewriteGenerator
* @param GetCategoriesCollection $getCategoriesCollection
*/
public function __construct(
UrlPersistInterface $urlPersist,
CategoryFactory $categoryFactory,
ProductFactory $productFactory,
CategoryUrlRewriteGenerator $categoryUrlRewriteGenerator,
ProductUrlRewriteGenerator $productUrlRewriteGenerator
ProductUrlRewriteGenerator $productUrlRewriteGenerator,
GetCategoriesCollection $getCategoriesCollection
) {
$this->categoryUrlRewriteGenerator = $categoryUrlRewriteGenerator;
$this->productUrlRewriteGenerator = $productUrlRewriteGenerator;
$this->urlPersist = $urlPersist;
$this->categoryFactory = $categoryFactory;
$this->productFactory = $productFactory;
$this->getCategoriesCollection = $getCategoriesCollection;
}

/**
Expand Down Expand Up @@ -154,7 +163,8 @@ protected function generateProductUrls(int $storeId): array
protected function generateCategoryUrls(int $rootCategoryId, int $storeId): array
{
$urls = [];
$categories = $this->categoryFactory->create()->getCategories($rootCategoryId, 1, false, true, false);
$categories = $this->getCategoriesCollection
->execute($rootCategoryId, 1, false, true, false, true, false);
$categories->setStoreId($storeId);
foreach ($categories as $category) {
/** @var Category $category */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ class ViewTest extends TestCase
*/
private $productMock;

/**
* @var Category\GetCategoriesCollection|MockObject
*/
private $getCategoriesCollectionMock;

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -131,14 +136,16 @@ protected function setUp(): void
->disableOriginalConstructor()
->setMethods(['getCollection'])
->getMock();
$this->getCategoriesCollectionMock = $this->createMock(Category\GetCategoriesCollection::class);
$this->plugin = $this->objectManager->getObject(
StoreViewPlugin::class,
[
'urlPersist' => $this->urlPersistMock,
'categoryFactory' => $this->categoryFactoryMock,
'productFactory' => $this->productFactoryMock,
'categoryUrlRewriteGenerator' => $this->categoryUrlRewriteGeneratorMock,
'productUrlRewriteGenerator' => $this->productUrlRewriteGeneratorMock
'productUrlRewriteGenerator' => $this->productUrlRewriteGeneratorMock,
'getCategoriesCollection' => $this->getCategoriesCollectionMock
]
);
}
Expand Down Expand Up @@ -171,12 +178,9 @@ public function testAfterSave(): void
$categoryCollection->expects($this->any())
->method('getIterator')
->willReturn(new \ArrayIterator([]));
$this->categoryMock->expects($this->once())
->method('getCategories')
$this->getCategoriesCollectionMock->expects($this->once())
->method('execute')
->willReturn($categoryCollection);
$this->categoryFactoryMock->expects($this->once())
->method('create')
->willReturn($this->categoryMock);
$this->productFactoryMock->expects($this->once())
->method('create')
->willReturn($this->productMock);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogUrlRewrite\Model\Category\Plugin\Store;

use Magento\Framework\ObjectManagerInterface;
use Magento\Store\Model\StoreFactory;
use Magento\Store\Model\StoreManagerInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\UrlRewrite\Model\ResourceModel\UrlRewriteCollectionFactory;
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
use PHPUnit\Framework\TestCase;

/**
* Verify generate url rewrites after creating store view.
*/
class ViewTest extends TestCase
{
/**
* @var ObjectManagerInterface
*/
private $objectManager;

/**
* @var StoreFactory
*/
private $storeFactory;

/**
* @var UrlRewriteCollectionFactory
*/
private $urlRewriteCollectionFactory;

/**
* @inheritDoc
*/
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->storeFactory = $this->objectManager->create(StoreFactory::class);
$this->urlRewriteCollectionFactory = $this->objectManager->get(UrlRewriteCollectionFactory::class);
}

/**
* Verify that url will be generated for category which excluded for menu after creating store view
*
* @magentoAppArea adminhtml
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/category_excluded_from_menu.php
*
* @return void
*/
public function testAfterSaveVerifyCategoryExcludedForMenuUrlRewrite(): void
{
$website = $this->objectManager->get(StoreManagerInterface::class)
->getWebsite();

$store = $this->storeFactory->create();
$store->setCode('custom_store_777')
->setWebsiteId($website->getId())
->setGroupId($website->getDefaultGroupId())
->setName('Custom Test Store')
->setSortOrder(10)
->setIsActive(1)
->save();

$urlRewriteCollection = $this->urlRewriteCollectionFactory->create();
$urlRewriteCollection->addFieldToFilter(UrlRewrite::STORE_ID, $store->getId())
->addFieldToFilter(UrlRewrite::TARGET_PATH, 'catalog/category/view/id/' . 3);

$this->assertCount(1, $urlRewriteCollection);
}
}
Loading