From 44d1ebf6cb95df49e31a81b58be94d1103cb4e82 Mon Sep 17 00:00:00 2001 From: Sarab Sodhi Date: Sat, 10 Oct 2020 16:15:18 +0530 Subject: [PATCH 01/18] magento/magento2#28358: URL rewrites are not generated for categories with 'Include in Menu=No' when creating a store view --- .../Catalog/Model/ResourceModel/Category.php | 20 +++++++++++-------- .../Model/ResourceModel/Category/Tree.php | 9 +++++++-- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Category.php b/app/code/Magento/Catalog/Model/ResourceModel/Category.php index 917aafb643b47..377841cce9eed 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Category.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Category.php @@ -20,6 +20,7 @@ use Magento\Framework\DataObject; use Magento\Framework\EntityManager\EntityManager; use Magento\Framework\EntityManager\MetadataPool; +use Magento\Tests\NamingConvention\true\false; /** * Resource model for category entity @@ -127,7 +128,8 @@ public function __construct( $data = [], \Magento\Framework\Serialize\Serializer\Json $serializer = null, MetadataPool $metadataPool = null - ) { + ) + { parent::__construct( $context, $storeManager, @@ -137,7 +139,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); @@ -289,7 +291,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()) { @@ -696,7 +698,7 @@ public function getProductCount($category) $bind = ['category_id' => (int)$category->getId()]; $counts = $this->getConnection()->fetchOne($select, $bind); - return (int) $counts; + return (int)$counts; } /** @@ -709,13 +711,14 @@ public function getProductCount($category) * @param boolean $toLoad * @return \Magento\Framework\Data\Tree\Node\Collection|\Magento\Catalog\Model\ResourceModel\Category\Collection */ - public function getCategories($parent, $recursionLevel = 0, $sorted = false, $asCollection = false, $toLoad = true) + public function getCategories($parent, $recursionLevel = 0, $sorted = false, $asCollection = false, $toLoad = + true) { $tree = $this->_categoryTreeFactory->create(); /* @var $tree \Magento\Catalog\Model\ResourceModel\Category\Tree */ $nodes = $tree->loadNode($parent)->loadChildren($recursionLevel)->getChildren(); - $tree->addCollectionData(null, $sorted, $parent, $toLoad, true); + $tree->addCollectionData(null, $sorted, $parent, $toLoad, true, false); if ($asCollection) { return $tree->getCollection(); @@ -949,7 +952,8 @@ public function changeParent( \Magento\Catalog\Model\Category $category, \Magento\Catalog\Model\Category $newParent, $afterCategoryId = null - ) { + ) + { $childrenCount = $this->getChildrenCount($category->getId()) + 1; $table = $this->getEntityTable(); $connection = $this->getConnection(); @@ -1112,7 +1116,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 */ diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Category/Tree.php b/app/code/Magento/Catalog/Model/ResourceModel/Category/Tree.php index 972f11db7aae3..e0544f0aeee20 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Category/Tree.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Category/Tree.php @@ -170,6 +170,7 @@ public function getStoreId() * @param array $exclude * @param boolean $toLoad * @param boolean $onlyActive + * @param boolean $onlyIncludeInMenu * @return $this * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) @@ -179,7 +180,8 @@ public function addCollectionData( $sorted = false, $exclude = [], $toLoad = true, - $onlyActive = false + $onlyActive = false, + $onlyIncludeInMenu = false ) { if ($collection === null) { $collection = $this->getCollection($sorted); @@ -204,7 +206,10 @@ public function addCollectionData( $collection->addFieldToFilter('entity_id', ['nin' => $disabledIds]); } $collection->addAttributeToFilter('is_active', 1); - $collection->addAttributeToFilter('include_in_menu', 1); + if($onlyIncludeInMenu){ + $collection->addAttributeToFilter('include_in_menu', 1); + } + } if ($this->_joinUrlRewriteIntoCollection) { From 3403f39db0aefc8c79c10cfd52e436d9f030cc0f Mon Sep 17 00:00:00 2001 From: sodhisarab <49445299+sodhisarab@users.noreply.github.com> Date: Sat, 10 Oct 2020 16:55:38 +0530 Subject: [PATCH 02/18] Update Category.php --- app/code/Magento/Catalog/Model/ResourceModel/Category.php | 1 - 1 file changed, 1 deletion(-) diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Category.php b/app/code/Magento/Catalog/Model/ResourceModel/Category.php index 377841cce9eed..5a5f79b713fe6 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Category.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Category.php @@ -20,7 +20,6 @@ use Magento\Framework\DataObject; use Magento\Framework\EntityManager\EntityManager; use Magento\Framework\EntityManager\MetadataPool; -use Magento\Tests\NamingConvention\true\false; /** * Resource model for category entity From 660b0fb1aa97880eb6ddda4b8a7c06b463009ebc Mon Sep 17 00:00:00 2001 From: sodhisarab <49445299+sodhisarab@users.noreply.github.com> Date: Mon, 12 Oct 2020 10:17:05 +0530 Subject: [PATCH 03/18] Update Category.php Fix Magento Console logs erros --- .../Magento/Catalog/Model/ResourceModel/Category.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Category.php b/app/code/Magento/Catalog/Model/ResourceModel/Category.php index 5a5f79b713fe6..2bc4dffd91cfa 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Category.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Category.php @@ -127,8 +127,7 @@ public function __construct( $data = [], \Magento\Framework\Serialize\Serializer\Json $serializer = null, MetadataPool $metadataPool = null - ) - { + ){ parent::__construct( $context, $storeManager, @@ -710,8 +709,7 @@ public function getProductCount($category) * @param boolean $toLoad * @return \Magento\Framework\Data\Tree\Node\Collection|\Magento\Catalog\Model\ResourceModel\Category\Collection */ - public function getCategories($parent, $recursionLevel = 0, $sorted = false, $asCollection = false, $toLoad = - true) + public function getCategories($parent, $recursionLevel = 0, $sorted = false, $asCollection = false, $toLoad = true) { $tree = $this->_categoryTreeFactory->create(); /* @var $tree \Magento\Catalog\Model\ResourceModel\Category\Tree */ @@ -951,8 +949,7 @@ public function changeParent( \Magento\Catalog\Model\Category $category, \Magento\Catalog\Model\Category $newParent, $afterCategoryId = null - ) - { + ){ $childrenCount = $this->getChildrenCount($category->getId()) + 1; $table = $this->getEntityTable(); $connection = $this->getConnection(); From 1f6f3ffb1b6ab52f9307c916a6e0fea152913daf Mon Sep 17 00:00:00 2001 From: sodhisarab <49445299+sodhisarab@users.noreply.github.com> Date: Mon, 12 Oct 2020 10:18:14 +0530 Subject: [PATCH 04/18] Update Category.php --- app/code/Magento/Catalog/Model/ResourceModel/Category.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Category.php b/app/code/Magento/Catalog/Model/ResourceModel/Category.php index 2bc4dffd91cfa..9cd5706be91e9 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Category.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Category.php @@ -127,7 +127,7 @@ public function __construct( $data = [], \Magento\Framework\Serialize\Serializer\Json $serializer = null, MetadataPool $metadataPool = null - ){ + ) { parent::__construct( $context, $storeManager, @@ -949,7 +949,7 @@ public function changeParent( \Magento\Catalog\Model\Category $category, \Magento\Catalog\Model\Category $newParent, $afterCategoryId = null - ){ + ) { $childrenCount = $this->getChildrenCount($category->getId()) + 1; $table = $this->getEntityTable(); $connection = $this->getConnection(); From bc28211ad9e3622d3c63e52967f0a7c484f19bc2 Mon Sep 17 00:00:00 2001 From: sodhisarab <49445299+sodhisarab@users.noreply.github.com> Date: Mon, 12 Oct 2020 10:32:25 +0530 Subject: [PATCH 05/18] Update Category.php --- app/code/Magento/Catalog/Model/ResourceModel/Category.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Category.php b/app/code/Magento/Catalog/Model/ResourceModel/Category.php index 9cd5706be91e9..2bc4dffd91cfa 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Category.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Category.php @@ -127,7 +127,7 @@ public function __construct( $data = [], \Magento\Framework\Serialize\Serializer\Json $serializer = null, MetadataPool $metadataPool = null - ) { + ){ parent::__construct( $context, $storeManager, @@ -949,7 +949,7 @@ public function changeParent( \Magento\Catalog\Model\Category $category, \Magento\Catalog\Model\Category $newParent, $afterCategoryId = null - ) { + ){ $childrenCount = $this->getChildrenCount($category->getId()) + 1; $table = $this->getEntityTable(); $connection = $this->getConnection(); From 88c8aaa22638cc812210cbd669683d5ac0e713e3 Mon Sep 17 00:00:00 2001 From: sodhisarab <49445299+sodhisarab@users.noreply.github.com> Date: Fri, 30 Oct 2020 16:21:24 +0530 Subject: [PATCH 06/18] Update Tree.php --- .../Model/ResourceModel/Category/Tree.php | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Category/Tree.php b/app/code/Magento/Catalog/Model/ResourceModel/Category/Tree.php index e0544f0aeee20..f560389dd4c59 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Category/Tree.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Category/Tree.php @@ -161,6 +161,26 @@ public function getStoreId() } return $this->_storeId; } + /** + * Add data to collection + * @param Collection $collection + * @param boolean $sorted + * @param array $exclude + * @param boolean $toLoad + * @param boolean $onlyActive + * @return $this + - @deprecated This method is not intended for usage in child classes + - @see addCollectionDataWithIncludeMenu($collection, $sorted, $exclude, $toLoad, $onlyActive, $onlyIncludeInMenu) + */ + public function addCollectionData( + $collection = null, + $sorted = false, + $exclude = [], + $toLoad = true, + $onlyActive = false + ) { + $this->addCollectionDataWithIncludeMenu($collection, $sorted, $exclude, $toLoad, $onlyActive, true); + } /** * Add data to collection @@ -175,7 +195,7 @@ public function getStoreId() * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ - public function addCollectionData( + private function addCollectionDataWithIncludeMenu( $collection = null, $sorted = false, $exclude = [], From 5202a5b2b04e75d9c5d0213f1b613732cc3d660e Mon Sep 17 00:00:00 2001 From: sodhisarab <49445299+sodhisarab@users.noreply.github.com> Date: Fri, 30 Oct 2020 16:23:36 +0530 Subject: [PATCH 07/18] Update Category.php --- app/code/Magento/Catalog/Model/ResourceModel/Category.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Category.php b/app/code/Magento/Catalog/Model/ResourceModel/Category.php index 2bc4dffd91cfa..dc33c91774532 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Category.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Category.php @@ -127,7 +127,7 @@ public function __construct( $data = [], \Magento\Framework\Serialize\Serializer\Json $serializer = null, MetadataPool $metadataPool = null - ){ + ) { parent::__construct( $context, $storeManager, @@ -715,7 +715,7 @@ public function getCategories($parent, $recursionLevel = 0, $sorted = false, $as /* @var $tree \Magento\Catalog\Model\ResourceModel\Category\Tree */ $nodes = $tree->loadNode($parent)->loadChildren($recursionLevel)->getChildren(); - $tree->addCollectionData(null, $sorted, $parent, $toLoad, true, false); + $tree->addCollectionData(null, $sorted, $parent, $toLoad, true); if ($asCollection) { return $tree->getCollection(); From c974fe51e94e081111d0cacd8750f3035a318267 Mon Sep 17 00:00:00 2001 From: sodhisarab <49445299+sodhisarab@users.noreply.github.com> Date: Fri, 30 Oct 2020 18:27:18 +0530 Subject: [PATCH 08/18] Update Tree.php --- app/code/Magento/Catalog/Model/ResourceModel/Category/Tree.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Category/Tree.php b/app/code/Magento/Catalog/Model/ResourceModel/Category/Tree.php index f560389dd4c59..5ac576e27362d 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Category/Tree.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Category/Tree.php @@ -195,7 +195,7 @@ public function addCollectionData( * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ - private function addCollectionDataWithIncludeMenu( + public function addCollectionDataWithIncludeMenu( $collection = null, $sorted = false, $exclude = [], From 37a60cb1212b5038bbecc2dc4016fadd6c26af3c Mon Sep 17 00:00:00 2001 From: sodhisarab <49445299+sodhisarab@users.noreply.github.com> Date: Fri, 30 Oct 2020 18:29:49 +0530 Subject: [PATCH 09/18] Update TreeTest.php --- .../Catalog/Test/Unit/Model/ResourceModel/Category/TreeTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Category/TreeTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Category/TreeTest.php index 6593f28641ec6..7f48bfd5f7404 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Category/TreeTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Category/TreeTest.php @@ -222,6 +222,6 @@ public function testAddCollectionData() $model->addNode($nodeMock); - $this->assertSame($model, $model->addCollectionData(null, false, [], false, true)); + $this->assertSame($model, $model->addCollectionDataWithIncludeMenu(null, false, [], false, true, false)); } } From be1b08c5b933dfcb144de24c08eddc44f12e762d Mon Sep 17 00:00:00 2001 From: sodhisarab <49445299+sodhisarab@users.noreply.github.com> Date: Sat, 31 Oct 2020 03:24:11 +0530 Subject: [PATCH 10/18] Update Tree.php --- app/code/Magento/Catalog/Model/ResourceModel/Category/Tree.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Category/Tree.php b/app/code/Magento/Catalog/Model/ResourceModel/Category/Tree.php index 5ac576e27362d..96b33ce9841ea 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Category/Tree.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Category/Tree.php @@ -179,7 +179,7 @@ public function addCollectionData( $toLoad = true, $onlyActive = false ) { - $this->addCollectionDataWithIncludeMenu($collection, $sorted, $exclude, $toLoad, $onlyActive, true); + return $this->addCollectionDataWithIncludeMenu($collection, $sorted, $exclude, $toLoad, $onlyActive, true); } /** From fa6a9203b23ed3da9fe9c69651af07031c320fcb Mon Sep 17 00:00:00 2001 From: sodhisarab <49445299+sodhisarab@users.noreply.github.com> Date: Sun, 1 Nov 2020 12:22:14 +0530 Subject: [PATCH 11/18] Update Category.php --- app/code/Magento/Catalog/Model/ResourceModel/Category.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Category.php b/app/code/Magento/Catalog/Model/ResourceModel/Category.php index dc33c91774532..116eb64e87ae2 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Category.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Category.php @@ -949,7 +949,7 @@ public function changeParent( \Magento\Catalog\Model\Category $category, \Magento\Catalog\Model\Category $newParent, $afterCategoryId = null - ){ + ) { $childrenCount = $this->getChildrenCount($category->getId()) + 1; $table = $this->getEntityTable(); $connection = $this->getConnection(); From b939410bbb45e70f1bbbf1aaa02f1a6025c77582 Mon Sep 17 00:00:00 2001 From: sodhisarab <49445299+sodhisarab@users.noreply.github.com> Date: Sun, 1 Nov 2020 12:27:57 +0530 Subject: [PATCH 12/18] Update Tree.php --- .../Model/ResourceModel/Category/Tree.php | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Category/Tree.php b/app/code/Magento/Catalog/Model/ResourceModel/Category/Tree.php index 96b33ce9841ea..57f37fe2b55b8 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Category/Tree.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Category/Tree.php @@ -162,16 +162,17 @@ public function getStoreId() return $this->_storeId; } /** - * Add data to collection - * @param Collection $collection - * @param boolean $sorted - * @param array $exclude - * @param boolean $toLoad - * @param boolean $onlyActive - * @return $this - - @deprecated This method is not intended for usage in child classes - - @see addCollectionDataWithIncludeMenu($collection, $sorted, $exclude, $toLoad, $onlyActive, $onlyIncludeInMenu) - */ + * Add data to collection + * + * @param Collection $collection + * @param boolean $sorted + * @param array $exclude + * @param boolean $toLoad + * @param boolean $onlyActive + * @return $this + - @deprecated This method is not intended for usage in child classes + - @see addCollectionDataWithIncludeMenu($collection, $sorted, $exclude, $toLoad, $onlyActive, $onlyIncludeInMenu) + */ public function addCollectionData( $collection = null, $sorted = false, @@ -179,7 +180,7 @@ public function addCollectionData( $toLoad = true, $onlyActive = false ) { - return $this->addCollectionDataWithIncludeMenu($collection, $sorted, $exclude, $toLoad, $onlyActive, true); + return $this->addCollectionDataWithIncludeMenu($collection, $sorted, $exclude, $toLoad, $onlyActive, true); } /** @@ -226,7 +227,7 @@ public function addCollectionDataWithIncludeMenu( $collection->addFieldToFilter('entity_id', ['nin' => $disabledIds]); } $collection->addAttributeToFilter('is_active', 1); - if($onlyIncludeInMenu){ + if ($onlyIncludeInMenu) { $collection->addAttributeToFilter('include_in_menu', 1); } From 5f9c440c4d5df6862c06abbc8f9e2dde5faaa0d8 Mon Sep 17 00:00:00 2001 From: "vadim.malesh" Date: Mon, 9 Nov 2020 11:18:57 +0200 Subject: [PATCH 13/18] revert old method behavior, refactor --- .../Model/ResourceModel/Category/Tree.php | 87 +++++++++++++++---- 1 file changed, 69 insertions(+), 18 deletions(-) diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Category/Tree.php b/app/code/Magento/Catalog/Model/ResourceModel/Category/Tree.php index 902ddc4970ce6..f3d3ff65e25ec 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Category/Tree.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Category/Tree.php @@ -166,18 +166,21 @@ public function getStoreId() } return $this->_storeId; } - /** - * Add data to collection - * - * @param Collection $collection - * @param boolean $sorted - * @param array $exclude - * @param boolean $toLoad - * @param boolean $onlyActive - * @return $this - - @deprecated This method is not intended for usage in child classes - - @see addCollectionDataWithIncludeMenu($collection, $sorted, $exclude, $toLoad, $onlyActive, $onlyIncludeInMenu) - */ + + /** + * Add data to collection + * + * @param Collection $collection + * @param boolean $sorted + * @param array $exclude + * @param boolean $toLoad + * @param boolean $onlyActive + * @return $this + * @deprecated This method is not intended for usage in child classes + * @see addCollectionDataWithIncludeMenu($collection, $sorted, $exclude, $toLoad, $onlyActive, $onlyIncludeInMenu) + * @SuppressWarnings(PHPMD.CyclomaticComplexity) + * @SuppressWarnings(PHPMD.NPathComplexity) + */ public function addCollectionData( $collection = null, $sorted = false, @@ -185,11 +188,58 @@ public function addCollectionData( $toLoad = true, $onlyActive = false ) { - return $this->addCollectionDataWithIncludeMenu($collection, $sorted, $exclude, $toLoad, $onlyActive, true); + 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); + $collection->addAttributeToFilter('include_in_menu', 1); + } + + if ($this->_joinUrlRewriteIntoCollection) { + $collection->joinUrlRewrite(); + $this->_joinUrlRewriteIntoCollection = false; + } + + if ($toLoad) { + $collection->load(); + + foreach ($collection as $category) { + if ($this->getNodeById($category->getId())) { + $this->getNodeById($category->getId())->addData($category->getData()); + } + } + + foreach ($this->getNodes() as $node) { + if (!$collection->getItemById($node->getId()) && $node->getParent()) { + $this->removeNode($node); + } + } + } + + return $this; } /** - * Add data to collection + * Add data to collection with include in menu * * @param Collection $collection * @param boolean $sorted @@ -226,6 +276,7 @@ public function addCollectionDataWithIncludeMenu( } } $collection->addIdFilter($nodeIds); + if ($onlyActive) { $disabledIds = $this->_getDisabledIds($collection, $nodeIds); if ($disabledIds) { @@ -235,7 +286,6 @@ public function addCollectionDataWithIncludeMenu( if ($onlyIncludeInMenu) { $collection->addAttributeToFilter('include_in_menu', 1); } - } if ($this->_joinUrlRewriteIntoCollection) { @@ -247,13 +297,14 @@ public function addCollectionDataWithIncludeMenu( $collection->load(); foreach ($collection as $category) { - if ($this->getNodeById($category->getId())) { - $this->getNodeById($category->getId())->addData($category->getData()); + $node = $this->getNodeById($category->getId()); + if ($node) { + $node->addData($category->getData()); } } foreach ($this->getNodes() as $node) { - if (!$collection->getItemById($node->getId()) && $node->getParent()) { + if ($node->getParent() && !$collection->getItemById($node->getId())) { $this->removeNode($node); } } From 18e46f2d72d0f3dc78a74b1560b9d8500b857f77 Mon Sep 17 00:00:00 2001 From: "vadim.malesh" Date: Tue, 10 Nov 2020 16:08:07 +0200 Subject: [PATCH 14/18] fix generate URL rewrites for category with 'Include in Menu=No' after creating a store view --- app/code/Magento/Catalog/Model/Category.php | 35 +++++++++++++++++++ .../Catalog/Model/ResourceModel/Category.php | 33 +++++++++++++++++ .../Model/ResourceModel/Category/Tree.php | 8 ++--- .../Model/Category/Plugin/Store/View.php | 3 +- 4 files changed, 74 insertions(+), 5 deletions(-) diff --git a/app/code/Magento/Catalog/Model/Category.php b/app/code/Magento/Catalog/Model/Category.php index 538a721d356d7..4d4ffd00ddef5 100644 --- a/app/code/Magento/Catalog/Model/Category.php +++ b/app/code/Magento/Catalog/Model/Category.php @@ -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 getCategoriesCollection */ public function getCategories($parent, $recursionLevel = 0, $sorted = false, $asCollection = false, $toLoad = true) { @@ -987,6 +989,39 @@ public function getCategories($parent, $recursionLevel = 0, $sorted = false, $as 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 * diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Category.php b/app/code/Magento/Catalog/Model/ResourceModel/Category.php index 274869c0e773f..31ab8f259c4f8 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Category.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Category.php @@ -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) { @@ -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 * diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Category/Tree.php b/app/code/Magento/Catalog/Model/ResourceModel/Category/Tree.php index f3d3ff65e25ec..67b24ab979bbb 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Category/Tree.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Category/Tree.php @@ -177,7 +177,7 @@ public function getStoreId() * @param boolean $onlyActive * @return $this * @deprecated This method is not intended for usage in child classes - * @see addCollectionDataWithIncludeMenu($collection, $sorted, $exclude, $toLoad, $onlyActive, $onlyIncludeInMenu) + * @see addCollectionDataParams($collection, $sorted, $exclude, $toLoad, $onlyActive, $onlyIncludeInMenu) * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ @@ -239,9 +239,9 @@ public function addCollectionData( } /** - * Add data to collection with include in menu + * Add data params to collection * - * @param Collection $collection + * @param Collection|null $collection * @param boolean $sorted * @param array $exclude * @param boolean $toLoad @@ -251,7 +251,7 @@ public function addCollectionData( * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ - public function addCollectionDataWithIncludeMenu( + public function addCollectionDataParams( $collection = null, $sorted = false, $exclude = [], diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Store/View.php b/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Store/View.php index 31bb630718e55..ba124e8756ca8 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Store/View.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Store/View.php @@ -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 */ From 43d26e7cdd2826aa828968960d2bf90bbf1832aa Mon Sep 17 00:00:00 2001 From: "vadim.malesh" Date: Tue, 10 Nov 2020 16:20:07 +0200 Subject: [PATCH 15/18] add integration test --- .../Model/Category/Plugin/Store/ViewTest.php | 76 +++++++++++++++++++ .../_files/category_excluded_from_menu.php | 28 +++++++ .../category_excluded_from_menu_rollback.php | 10 +++ 3 files changed, 114 insertions(+) create mode 100644 dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/Model/Category/Plugin/Store/ViewTest.php create mode 100644 dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/_files/category_excluded_from_menu.php create mode 100644 dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/_files/category_excluded_from_menu_rollback.php diff --git a/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/Model/Category/Plugin/Store/ViewTest.php b/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/Model/Category/Plugin/Store/ViewTest.php new file mode 100644 index 0000000000000..9c937f00cbaca --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/Model/Category/Plugin/Store/ViewTest.php @@ -0,0 +1,76 @@ +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); + } +} diff --git a/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/_files/category_excluded_from_menu.php b/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/_files/category_excluded_from_menu.php new file mode 100644 index 0000000000000..9f1c1bf082c4e --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/_files/category_excluded_from_menu.php @@ -0,0 +1,28 @@ +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(); diff --git a/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/_files/category_excluded_from_menu_rollback.php b/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/_files/category_excluded_from_menu_rollback.php new file mode 100644 index 0000000000000..1e69bad0e86a4 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/_files/category_excluded_from_menu_rollback.php @@ -0,0 +1,10 @@ +requireDataFixture('Magento/Catalog/_files/categories_rollback.php'); From ffb4cee0d101883dc608761ca66328639e89c3bd Mon Sep 17 00:00:00 2001 From: "vadim.malesh" Date: Wed, 11 Nov 2020 10:41:01 +0200 Subject: [PATCH 16/18] fix unit tests --- .../Test/Unit/Model/ResourceModel/Category/TreeTest.php | 2 +- .../Test/Unit/Model/Category/Plugin/Store/ViewTest.php | 7 ++----- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Category/TreeTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Category/TreeTest.php index 7f48bfd5f7404..f92f5421fc1dc 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Category/TreeTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Category/TreeTest.php @@ -222,6 +222,6 @@ public function testAddCollectionData() $model->addNode($nodeMock); - $this->assertSame($model, $model->addCollectionDataWithIncludeMenu(null, false, [], false, true, false)); + $this->assertSame($model, $model->addCollectionDataParams(null, false, [], false, true, false)); } } diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/Store/ViewTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/Store/ViewTest.php index 61557db883aa1..7349168e3a894 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/Store/ViewTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/Store/ViewTest.php @@ -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']) @@ -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') From 9f9470da066a60416843480ebe0482bbe9666f0c Mon Sep 17 00:00:00 2001 From: "vadim.malesh" Date: Tue, 17 Nov 2020 12:59:02 +0200 Subject: [PATCH 17/18] move new method to separate class --- app/code/Magento/Catalog/Model/Category.php | 35 +---------- .../Category/GetCategoriesCollection.php | 63 +++++++++++++++++++ .../Catalog/Model/ResourceModel/Category.php | 33 +--------- .../Model/Category/Plugin/Store/View.php | 15 ++++- 4 files changed, 77 insertions(+), 69 deletions(-) create mode 100644 app/code/Magento/Catalog/Model/Category/GetCategoriesCollection.php diff --git a/app/code/Magento/Catalog/Model/Category.php b/app/code/Magento/Catalog/Model/Category.php index 4d4ffd00ddef5..4592d7d9199f4 100644 --- a/app/code/Magento/Catalog/Model/Category.php +++ b/app/code/Magento/Catalog/Model/Category.php @@ -981,7 +981,7 @@ public function getProductCount() * @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 + * @see \Magento\Catalog\Model\Category\GetCategoriesCollection */ public function getCategories($parent, $recursionLevel = 0, $sorted = false, $asCollection = false, $toLoad = true) { @@ -989,39 +989,6 @@ public function getCategories($parent, $recursionLevel = 0, $sorted = false, $as 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 * diff --git a/app/code/Magento/Catalog/Model/Category/GetCategoriesCollection.php b/app/code/Magento/Catalog/Model/Category/GetCategoriesCollection.php new file mode 100644 index 0000000000000..9b20468925d05 --- /dev/null +++ b/app/code/Magento/Catalog/Model/Category/GetCategoriesCollection.php @@ -0,0 +1,63 @@ +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; + } +} diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Category.php b/app/code/Magento/Catalog/Model/ResourceModel/Category.php index 31ab8f259c4f8..9615e368011a5 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Category.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Category.php @@ -709,7 +709,7 @@ public function getProductCount($category) * @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 + * @see \Magento\Catalog\Model\Category\GetCategoriesCollection */ public function getCategories($parent, $recursionLevel = 0, $sorted = false, $asCollection = false, $toLoad = true) { @@ -725,37 +725,6 @@ 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 * diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Store/View.php b/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Store/View.php index ba124e8756ca8..26c0b9d67787a 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Store/View.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Store/View.php @@ -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; @@ -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; } /** @@ -154,8 +163,8 @@ protected function generateProductUrls(int $storeId): array protected function generateCategoryUrls(int $rootCategoryId, int $storeId): array { $urls = []; - $categories = $this->categoryFactory->create() - ->getCategoriesCollection($rootCategoryId, 1, false, true, false, true, false); + $categories = $this->getCategoriesCollection + ->execute($rootCategoryId, 1, false, true, false, true, false); $categories->setStoreId($storeId); foreach ($categories as $category) { /** @var Category $category */ From 5800b498ec2adc78f7f74b1d876ed31c8cc0267a Mon Sep 17 00:00:00 2001 From: "vadim.malesh" Date: Tue, 17 Nov 2020 13:09:37 +0200 Subject: [PATCH 18/18] fix unit --- .../Model/Category/Plugin/Store/ViewTest.php | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/Store/ViewTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/Store/ViewTest.php index 7349168e3a894..e891c9afe120e 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/Store/ViewTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/Store/ViewTest.php @@ -88,6 +88,11 @@ class ViewTest extends TestCase */ private $productMock; + /** + * @var Category\GetCategoriesCollection|MockObject + */ + private $getCategoriesCollectionMock; + /** * @inheritdoc */ @@ -104,7 +109,10 @@ protected function setUp(): void $this->urlPersistMock = $this->getMockBuilder(UrlPersistInterface::class) ->setMethods(['deleteByData']) ->getMockForAbstractClass(); - $this->categoryMock = $this->createMock(Category::class); + $this->categoryMock = $this->getMockBuilder(Category::class) + ->disableOriginalConstructor() + ->setMethods(['getCategories']) + ->getMock(); $this->categoryFactoryMock = $this->getMockBuilder(CategoryFactory::class) ->disableOriginalConstructor() ->setMethods(['create']) @@ -128,6 +136,7 @@ protected function setUp(): void ->disableOriginalConstructor() ->setMethods(['getCollection']) ->getMock(); + $this->getCategoriesCollectionMock = $this->createMock(Category\GetCategoriesCollection::class); $this->plugin = $this->objectManager->getObject( StoreViewPlugin::class, [ @@ -135,7 +144,8 @@ protected function setUp(): void 'categoryFactory' => $this->categoryFactoryMock, 'productFactory' => $this->productFactoryMock, 'categoryUrlRewriteGenerator' => $this->categoryUrlRewriteGeneratorMock, - 'productUrlRewriteGenerator' => $this->productUrlRewriteGeneratorMock + 'productUrlRewriteGenerator' => $this->productUrlRewriteGeneratorMock, + 'getCategoriesCollection' => $this->getCategoriesCollectionMock ] ); } @@ -168,12 +178,9 @@ public function testAfterSave(): void $categoryCollection->expects($this->any()) ->method('getIterator') ->willReturn(new \ArrayIterator([])); - $this->categoryMock->expects($this->once()) - ->method('getCategoriesCollection') + $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);