From 6dfb610d611f6b22770f0f738951bdb89ca73734 Mon Sep 17 00:00:00 2001 From: Denis Ristic Date: Tue, 10 Oct 2017 15:48:26 +0200 Subject: [PATCH 1/5] ADDED $sortByPostion flag to getChildren() --- .../Magento/Catalog/Model/ResourceModel/Category/Flat.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Category/Flat.php b/app/code/Magento/Catalog/Model/ResourceModel/Category/Flat.php index d7f8bd9d789a2..01e4b072b0367 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Category/Flat.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Category/Flat.php @@ -602,9 +602,10 @@ public function isInRootCategoryList($category) * @param \Magento\Catalog\Model\Category $category * @param bool $recursive * @param bool $isActive + * @param bool $sortByPosition * @return array */ - public function getChildren($category, $recursive = true, $isActive = true) + public function getChildren($category, $recursive = true, $isActive = true, $sortByPosition = false) { $select = $this->getConnection()->select()->from( $this->getMainStoreTable($category->getStoreId()), @@ -619,6 +620,9 @@ public function getChildren($category, $recursive = true, $isActive = true) if ($isActive) { $select->where('is_active = ?', '1'); } + if ($sortByPosition) { + $select->order('position ASC'); + } $_categories = $this->getConnection()->fetchAll($select); $categoriesIds = []; foreach ($_categories as $_category) { From f77dadbd3ad92610515b7fecd22f6217bb708df5 Mon Sep 17 00:00:00 2001 From: Denis Ristic Date: Wed, 11 Oct 2017 10:38:56 +0200 Subject: [PATCH 2/5] ADDED $sortByPosition flag to Magento\Catalog\Model\Category ADDED test testGetChildrenSroted() to integration testsuite Magento\Catalog\Model\Category --- app/code/Magento/Catalog/Model/Category.php | 5 +++-- .../testsuite/Magento/Catalog/Model/CategoryTreeTest.php | 6 ++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Catalog/Model/Category.php b/app/code/Magento/Catalog/Model/Category.php index c4d6742a02dd3..28d55830a1342 100644 --- a/app/code/Magento/Catalog/Model/Category.php +++ b/app/code/Magento/Catalog/Model/Category.php @@ -780,11 +780,12 @@ public function getAllChildren($asArray = false) /** * Retrieve children ids comma separated * + * @param boolean $sortByPosition * @return string */ - public function getChildren() + public function getChildren($sortByPosition = false) { - return implode(',', $this->getResource()->getChildren($this, false)); + return implode(',', $this->getResource()->getChildren($this, false, true, $sortByPosition)); } /** diff --git a/dev/tests/integration/testsuite/Magento/Catalog/Model/CategoryTreeTest.php b/dev/tests/integration/testsuite/Magento/Catalog/Model/CategoryTreeTest.php index 5e9244367bb13..84ccb4f91d97a 100644 --- a/dev/tests/integration/testsuite/Magento/Catalog/Model/CategoryTreeTest.php +++ b/dev/tests/integration/testsuite/Magento/Catalog/Model/CategoryTreeTest.php @@ -128,6 +128,12 @@ public function testGetChildren() $this->assertEquals(array_diff([4, 13], explode(',', $this->_model->getChildren())), []); } + public function testGetChildrenSorted() + { + $this->_model->load(2); + $this->assertEquals(array_diff([3, 4, 5], explode(',', $this->_model->getChildren(true))), []); + } + public function testGetPathInStore() { $this->_model->load(5); From fc4723441c7652917201cac2b4411639bf2acd76 Mon Sep 17 00:00:00 2001 From: Denis Ristic Date: Wed, 11 Oct 2017 15:50:52 +0200 Subject: [PATCH 3/5] FIXED integration test ADDED $recursive and $isActive, flags to getChildren() --- app/code/Magento/Catalog/Model/Category.php | 6 ++++-- .../testsuite/Magento/Catalog/Model/CategoryTreeTest.php | 4 +++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/Catalog/Model/Category.php b/app/code/Magento/Catalog/Model/Category.php index 28d55830a1342..7fc15928b784d 100644 --- a/app/code/Magento/Catalog/Model/Category.php +++ b/app/code/Magento/Catalog/Model/Category.php @@ -780,12 +780,14 @@ public function getAllChildren($asArray = false) /** * Retrieve children ids comma separated * + * @param boolean $recursive + * @param boolean $isActive * @param boolean $sortByPosition * @return string */ - public function getChildren($sortByPosition = false) + public function getChildren($recursive = true, $isActive = true, $sortByPosition = false) { - return implode(',', $this->getResource()->getChildren($this, false, true, $sortByPosition)); + return implode(',', $this->getResource()->getChildren($this, $recursive, $isActive, $sortByPosition)); } /** diff --git a/dev/tests/integration/testsuite/Magento/Catalog/Model/CategoryTreeTest.php b/dev/tests/integration/testsuite/Magento/Catalog/Model/CategoryTreeTest.php index 84ccb4f91d97a..8707c54865996 100644 --- a/dev/tests/integration/testsuite/Magento/Catalog/Model/CategoryTreeTest.php +++ b/dev/tests/integration/testsuite/Magento/Catalog/Model/CategoryTreeTest.php @@ -131,7 +131,9 @@ public function testGetChildren() public function testGetChildrenSorted() { $this->_model->load(2); - $this->assertEquals(array_diff([3, 4, 5], explode(',', $this->_model->getChildren(true))), []); + $unsorted = explode(',', $this->_model->getChildren()); + usort($unsorted); + $this->assertEquals(array_diff($unsorted, explode(',', $this->_model->getChildren(true, true, true))), []); } public function testGetPathInStore() From 9d593a4dbd55d6bd0343c3587c1b9bc3e1470d5e Mon Sep 17 00:00:00 2001 From: Denis Ristic Date: Thu, 12 Oct 2017 10:18:30 +0200 Subject: [PATCH 4/5] FIXED sort --- .../testsuite/Magento/Catalog/Model/CategoryTreeTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dev/tests/integration/testsuite/Magento/Catalog/Model/CategoryTreeTest.php b/dev/tests/integration/testsuite/Magento/Catalog/Model/CategoryTreeTest.php index 8707c54865996..8eeba1230f8b1 100644 --- a/dev/tests/integration/testsuite/Magento/Catalog/Model/CategoryTreeTest.php +++ b/dev/tests/integration/testsuite/Magento/Catalog/Model/CategoryTreeTest.php @@ -132,7 +132,7 @@ public function testGetChildrenSorted() { $this->_model->load(2); $unsorted = explode(',', $this->_model->getChildren()); - usort($unsorted); + sort($unsorted); $this->assertEquals(array_diff($unsorted, explode(',', $this->_model->getChildren(true, true, true))), []); } From 79693c8800f61780bcd44cf3fbbee92615f70514 Mon Sep 17 00:00:00 2001 From: Denis Ristic Date: Thu, 12 Oct 2017 13:57:53 +0200 Subject: [PATCH 5/5] FIXED default getChildren() $recursive value --- app/code/Magento/Catalog/Model/Category.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Catalog/Model/Category.php b/app/code/Magento/Catalog/Model/Category.php index 7fc15928b784d..ff840635e4c70 100644 --- a/app/code/Magento/Catalog/Model/Category.php +++ b/app/code/Magento/Catalog/Model/Category.php @@ -785,7 +785,7 @@ public function getAllChildren($asArray = false) * @param boolean $sortByPosition * @return string */ - public function getChildren($recursive = true, $isActive = true, $sortByPosition = false) + public function getChildren($recursive = false, $isActive = true, $sortByPosition = false) { return implode(',', $this->getResource()->getChildren($this, $recursive, $isActive, $sortByPosition)); }