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 19 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
35 changes: 35 additions & 0 deletions app/code/Magento/Catalog/Model/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -980,13 +980,48 @@ 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 getCategoriesCollection
*/
public function getCategories($parent, $recursionLevel = 0, $sorted = false, $asCollection = false, $toLoad = true)
{
$categories = $this->getResource()->getCategories($parent, $recursionLevel, $sorted, $asCollection, $toLoad);
return $categories;
}

/**
* Retrieve categories collection by parent
*
* @param int $parent
* @param int $recursionLevel
* @param bool $sorted
* @param bool $asCollection
* @param bool $toLoad
* @param bool $onlyActive
* @param bool $onlyIncludeInMenu
* @return \Magento\Framework\Data\Tree\Node\Collection|\Magento\Catalog\Model\ResourceModel\Category\Collection
*/
public function getCategoriesCollection(
$parent,
$recursionLevel = 0,
$sorted = false,
$asCollection = false,
$toLoad = true,
$onlyActive = true,
$onlyIncludeInMenu = true
) {
return $this->getResource()
->getCategoriesCollection(
$parent,
$recursionLevel,
$sorted,
$asCollection,
$toLoad,
$onlyActive,
$onlyIncludeInMenu
);
}

/**
* Return parent categories of current category
*
Expand Down
41 changes: 37 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 getCategoriesCollection
*/
public function getCategories($parent, $recursionLevel = 0, $sorted = false, $asCollection = false, $toLoad = true)
{
Expand All @@ -723,6 +725,37 @@ public function getCategories($parent, $recursionLevel = 0, $sorted = false, $as
return $nodes;
}

/**
* Retrieve categories
*
* @param integer $parent
* @param integer $recursionLevel
* @param boolean|string $sorted
* @param boolean $asCollection
* @param boolean $toLoad
* @param boolean $onlyActive
* @param boolean $onlyIncludeInMenu
* @return \Magento\Framework\Data\Tree\Node\Collection|\Magento\Catalog\Model\ResourceModel\Category\Collection
*/
public function getCategoriesCollection(
$parent,
$recursionLevel = 0,
$sorted = false,
$asCollection = false,
$toLoad = true,
$onlyActive = true,
$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;
}

/**
* Return parent categories of category
*
Expand Down Expand Up @@ -1112,7 +1145,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 @@ -154,7 +154,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->categoryFactory->create()
->getCategoriesCollection($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 @@ -104,10 +104,7 @@ protected function setUp(): void
$this->urlPersistMock = $this->getMockBuilder(UrlPersistInterface::class)
->setMethods(['deleteByData'])
->getMockForAbstractClass();
$this->categoryMock = $this->getMockBuilder(Category::class)
->disableOriginalConstructor()
->setMethods(['getCategories'])
->getMock();
$this->categoryMock = $this->createMock(Category::class);
$this->categoryFactoryMock = $this->getMockBuilder(CategoryFactory::class)
->disableOriginalConstructor()
->setMethods(['create'])
Expand Down Expand Up @@ -172,7 +169,7 @@ public function testAfterSave(): void
->method('getIterator')
->willReturn(new \ArrayIterator([]));
$this->categoryMock->expects($this->once())
->method('getCategories')
->method('getCategoriesCollection')
->willReturn($categoryCollection);
$this->categoryFactoryMock->expects($this->once())
->method('create')
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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

use Magento\Catalog\Api\Data\CategoryInterfaceFactory;
use Magento\Catalog\Model\Category;
use Magento\TestFramework\Helper\Bootstrap;

$objectManager = Bootstrap::getObjectManager();
$categoryFactory = $objectManager->get(CategoryInterfaceFactory::class);

/** @var $category Category */
$category = $categoryFactory->create();
$category->isObjectNew(true);
$category->setId(3)
->setName('Category 123')
->setParentId(2)
->setPath('1/2/3')
->setLevel(2)
->setAvailableSortBy('name')
->setDefaultSortBy('name')
->setIncludeInMenu(false)
->setIsActive(true)
->setPosition(1)
->save();
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

use Magento\TestFramework\Workaround\Override\Fixture\Resolver;

Resolver::getInstance()->requireDataFixture('Magento/Catalog/_files/categories_rollback.php');