Skip to content

Commit 1aa10cf

Browse files
Merge remote-tracking branch 'jackalopes/MAGETWO-72112-topmenu' into BundledPR-Sep8
2 parents d3772dc + 56ffff7 commit 1aa10cf

File tree

7 files changed

+349
-3
lines changed

7 files changed

+349
-3
lines changed

app/code/Magento/Catalog/Plugin/Block/Topmenu.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,11 @@ public function beforeGetHtml(
9393
$parentCategoryNode = $mapping[$categoryParentId];
9494

9595
$categoryNode = new Node(
96-
$this->getCategoryAsArray($category, $currentCategory),
96+
$this->getCategoryAsArray(
97+
$category,
98+
$currentCategory,
99+
$category->getParentId() == $categoryParentId
100+
),
97101
'id',
98102
$parentCategoryNode->getTree(),
99103
$parentCategoryNode
@@ -147,16 +151,18 @@ private function getCurrentCategory()
147151
*
148152
* @param \Magento\Catalog\Model\Category $category
149153
* @param \Magento\Catalog\Model\Category $currentCategory
154+
* @param bool $isParentActive
150155
* @return array
151156
*/
152-
private function getCategoryAsArray($category, $currentCategory)
157+
private function getCategoryAsArray($category, $currentCategory, $isParentActive)
153158
{
154159
return [
155160
'name' => $category->getName(),
156161
'id' => 'category-node-' . $category->getId(),
157162
'url' => $this->catalogCategory->getCategoryUrl($category),
158163
'has_active' => in_array((string)$category->getId(), explode('/', $currentCategory->getPath()), true),
159-
'is_active' => $category->getId() == $currentCategory->getId()
164+
'is_active' => $category->getId() == $currentCategory->getId(),
165+
'is_parent_active' => $isParentActive
160166
];
161167
}
162168

app/code/Magento/Theme/Block/Html/Topmenu.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,11 @@ protected function _getHtml(
220220
$parentPositionClass = $menuTree->getPositionClass();
221221
$itemPositionClassPrefix = $parentPositionClass ? $parentPositionClass . '-' : 'nav-';
222222

223+
/** @var \Magento\Framework\Data\Tree\Node $child */
223224
foreach ($children as $child) {
225+
if ($childLevel === 0 && $child->getData('is_parent_active') === false) {
226+
continue;
227+
}
224228
$child->setLevel($childLevel);
225229
$child->setIsFirst($counter == 1);
226230
$child->setIsLast($counter == $childrenCount);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Test\Constraint;
8+
9+
use Magento\Catalog\Test\Fixture\Category;
10+
use Magento\Catalog\Test\Page\Category\CatalogCategoryView;
11+
use Magento\Cms\Test\Page\CmsIndex;
12+
use Magento\Mtf\Constraint\AbstractConstraint;
13+
14+
/**
15+
* Check category is visible in navigation menu.
16+
*/
17+
class AssertCategoryIncludeInNavigationMenu extends AbstractConstraint
18+
{
19+
/**
20+
* Assert subcategory is not visible in navigation menu.
21+
*
22+
* @param Category $subcategory
23+
* @param CatalogCategoryView $catalogCategoryView
24+
* @param CmsIndex $cmsIndex
25+
* @return void
26+
*/
27+
public function processAssert(
28+
Category $category,
29+
CatalogCategoryView $catalogCategoryView,
30+
CmsIndex $cmsIndex
31+
) {
32+
$cmsIndex->open();
33+
\PHPUnit_Framework_Assert::assertTrue(
34+
$catalogCategoryView->getTopmenu()->isCategoryVisible($category->getName()),
35+
'Expected that ' . $category->getName() . ' is visible in navigation menu.'
36+
);
37+
}
38+
39+
/**
40+
* Returns a string representation of the object.
41+
*
42+
* @return string
43+
*/
44+
public function toString()
45+
{
46+
return "Category is visible in navigation menu";
47+
}
48+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Test\Constraint;
8+
9+
use Magento\Catalog\Test\Fixture\Category;
10+
use Magento\Catalog\Test\Page\Category\CatalogCategoryView;
11+
use Magento\Cms\Test\Page\CmsIndex;
12+
use Magento\Mtf\Constraint\AbstractConstraint;
13+
14+
/**
15+
* Check category is not visible in navigation menu.
16+
*/
17+
class AssertCategoryNotInNavigationMenu extends AbstractConstraint
18+
{
19+
/**
20+
* Assert subcategory is not visible in navigation menu.
21+
*
22+
* @param Category $subcategory
23+
* @param CatalogCategoryView $catalogCategoryView
24+
* @param CmsIndex $cmsIndex
25+
* @return void
26+
*/
27+
public function processAssert(
28+
Category $category,
29+
CatalogCategoryView $catalogCategoryView,
30+
CmsIndex $cmsIndex
31+
) {
32+
$cmsIndex->open();
33+
\PHPUnit_Framework_Assert::assertFalse(
34+
$catalogCategoryView->getTopmenu()->isCategoryVisible($category->getName()),
35+
'Expected that ' . $category->getName() . ' is not visible in navigation menu.'
36+
);
37+
}
38+
39+
/**
40+
* Returns a string representation of the object.
41+
*
42+
* @return string
43+
*/
44+
public function toString()
45+
{
46+
return "Category is not visible in navigation menu";
47+
}
48+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Test\Constraint;
8+
9+
use Magento\Catalog\Test\Fixture\Category;
10+
use Magento\Catalog\Test\Page\Category\CatalogCategoryView;
11+
use Magento\Cms\Test\Page\CmsIndex;
12+
use Magento\Mtf\Constraint\AbstractConstraint;
13+
14+
/**
15+
* Check subCategory is not visible in navigation menu.
16+
*/
17+
class AssertSubCategoryNotInNavigationMenu extends AbstractConstraint
18+
{
19+
/**
20+
* Assert subcategory is not visible in navigation menu.
21+
*
22+
* @param Category $subcategory
23+
* @param CatalogCategoryView $catalogCategoryView
24+
* @param CmsIndex $cmsIndex
25+
* @return void
26+
*/
27+
public function processAssert(
28+
Category $subcategory,
29+
CatalogCategoryView $catalogCategoryView,
30+
CmsIndex $cmsIndex
31+
) {
32+
$cmsIndex->open();
33+
\PHPUnit_Framework_Assert::assertFalse(
34+
$catalogCategoryView->getTopmenu()->isCategoryVisible($subcategory->getName()),
35+
'Expected that ' . $subcategory->getName() . ' is not visible in navigation menu.'
36+
);
37+
}
38+
39+
/**
40+
* Returns a string representation of the object.
41+
*
42+
* @return string
43+
*/
44+
public function toString()
45+
{
46+
return "Subcategory is not visible in navigation menu";
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Test\TestCase\Category;
8+
9+
use Magento\Catalog\Test\Fixture\Category;
10+
use Magento\Catalog\Test\Page\Adminhtml\CatalogCategoryEdit;
11+
use Magento\Catalog\Test\Page\Adminhtml\CatalogCategoryIndex;
12+
use Magento\Mtf\TestCase\Injectable;
13+
use Magento\Mtf\Fixture\FixtureFactory;
14+
15+
/**
16+
* Preconditions:
17+
* 1. Create category.
18+
*
19+
* Steps:
20+
* 1. Navigate Products->Categories.
21+
* 2. Open category created in preconditions.
22+
* 3. Update data according to data set.
23+
* 4. Save category.
24+
* 5. Perform assertions.
25+
*
26+
* @group Category_Management
27+
* @ZephyrId MAGETWO-72238
28+
*/
29+
class SubcategoryNotIncludeInNavigationMenuTest extends Injectable
30+
{
31+
/* tags */
32+
const MVP = 'yes';
33+
/* end tags */
34+
35+
/**
36+
* Catalog category index page.
37+
*
38+
* @var CatalogCategoryIndex
39+
*/
40+
private $catalogCategoryIndex;
41+
42+
/**
43+
* Catalog category edit page.
44+
*
45+
* @var CatalogCategoryEdit
46+
*/
47+
private $catalogCategoryEdit;
48+
49+
/**
50+
* Fixture Factory.
51+
*
52+
* @var FixtureFactory
53+
*/
54+
private $fixtureFactory;
55+
56+
/**
57+
* Inject pages.
58+
*
59+
* @param CatalogCategoryIndex $catalogCategoryIndex
60+
* @param CatalogCategoryEdit $catalogCategoryEdit
61+
* @param FixtureFactory $fixtureFactory
62+
* @return void
63+
*/
64+
public function __inject(
65+
CatalogCategoryIndex $catalogCategoryIndex,
66+
CatalogCategoryEdit $catalogCategoryEdit,
67+
FixtureFactory $fixtureFactory
68+
) {
69+
$this->fixtureFactory = $fixtureFactory;
70+
$this->catalogCategoryIndex = $catalogCategoryIndex;
71+
$this->catalogCategoryEdit = $catalogCategoryEdit;
72+
}
73+
74+
/**
75+
* Top parent category update test.
76+
*
77+
* @param Category $category
78+
* @param Category $initialCategory
79+
* @param int $nestingLevel
80+
* @return array
81+
*/
82+
public function test(
83+
Category $category,
84+
Category $initialCategory,
85+
$nestingLevel
86+
) {
87+
$initialCategory->persist();
88+
$topCategory = $this->getParentCategoryByNestingLevel($initialCategory, $nestingLevel);
89+
$this->catalogCategoryIndex->open();
90+
$this->catalogCategoryIndex->getTreeCategories()->selectCategory($topCategory);
91+
$this->catalogCategoryEdit->getEditForm()->fill($category);
92+
$this->catalogCategoryEdit->getFormPageActions()->save();
93+
94+
$categories = [];
95+
$this->getCategoryFixture($categories, $initialCategory, $category->getData(), $nestingLevel);
96+
return [
97+
'category' => $categories[1],
98+
'subcategory' => $categories[2],
99+
];
100+
}
101+
102+
/**
103+
* Get category fixture after saving in the admin panel.
104+
*
105+
* @param array $categories
106+
* @param Category $currentCategory
107+
* @param array $data
108+
* @param int $nestingLevel
109+
* @return Category
110+
*/
111+
private function getCategoryFixture(array &$categories, Category $currentCategory, array $data, int $nestingLevel)
112+
{
113+
if (--$nestingLevel) {
114+
$parentCategory = $this->getCategoryFixture(
115+
$categories,
116+
$currentCategory->getDataFieldConfig('parent_id')['source']->getParentCategory(),
117+
$data,
118+
$nestingLevel
119+
);
120+
$category = $this->fixtureFactory->createByCode(
121+
'category',
122+
['data' => array_merge($currentCategory->getData(), ['parent_id' => ['source' => $parentCategory]])]
123+
);
124+
} else {
125+
$category = $this->fixtureFactory->createByCode(
126+
'category',
127+
['data' => array_merge($currentCategory->getData(), $data)]
128+
);
129+
}
130+
$categories[$nestingLevel + 1] = $category;
131+
return $category;
132+
}
133+
134+
/**
135+
* Get parent category by category nesting level.
136+
*
137+
* @param Category $category
138+
* @param int $nestingLevel
139+
* @return Category
140+
*/
141+
private function getParentCategoryByNestingLevel(Category $category, $nestingLevel)
142+
{
143+
for ($nestingIterator = 1; $nestingIterator < $nestingLevel; $nestingIterator++) {
144+
$category = $category->getDataFieldConfig('parent_id')['source']->getParentCategory();
145+
}
146+
147+
return $category;
148+
}
149+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../vendor/magento/mtf/etc/variations.xsd">
9+
<testCase name="Magento\Catalog\Test\TestCase\Category\MyTest" summary="Test child categories should not include in menu" ticketId="MAGETWO-72238">
10+
<variation name="CategoryIncludeInNavigationMenuAndSubcategoryNotIncludeInNavigationMenu" summary="Active category and check that category is visible on navigation menu and subcategory is not visible on navigation menu">
11+
<data name="initialCategory/dataset" xsi:type="string">two_nested_categories</data>
12+
<data name="nestingLevel" xsi:type="number">2</data>
13+
<data name="category/data/is_active" xsi:type="string">Yes</data>
14+
<data name="category/data/include_in_menu" xsi:type="string">Yes</data>
15+
<constraint name="Magento\Catalog\Test\Constraint\AssertCategoryIncludeInNavigationMenu" />
16+
<constraint name="Magento\Catalog\Test\Constraint\AssertSubCategoryNotInNavigationMenu" />
17+
</variation>
18+
<variation name="CategoryAndSubcategotyNotIncludeInNavigationMenu1" summary="Turn off include_in_menu category and check that category and subcategory are not visible on navigation menu">
19+
<data name="initialCategory/dataset" xsi:type="string">two_nested_categories</data>
20+
<data name="nestingLevel" xsi:type="number">2</data>
21+
<data name="category/data/is_active" xsi:type="string">Yes</data>
22+
<data name="category/data/include_in_menu" xsi:type="string">No</data>
23+
<constraint name="Magento\Catalog\Test\Constraint\AssertCategoryNotInNavigationMenu" />
24+
<constraint name="Magento\Catalog\Test\Constraint\AssertSubCategoryNotInNavigationMenu" />
25+
</variation>
26+
<variation name="InactiveCategoryAndSubcategotyNotIncludeInNavigationMenu" summary="Inactive category and check that category and subcategory are not visible on navigation menu">
27+
<data name="initialCategory/dataset" xsi:type="string">two_nested_categories</data>
28+
<data name="nestingLevel" xsi:type="number">2</data>
29+
<data name="category/data/is_active" xsi:type="string">No</data>
30+
<data name="category/data/include_in_menu" xsi:type="string">Yes</data>
31+
<constraint name="Magento\Catalog\Test\Constraint\AssertCategoryNotInNavigationMenu" />
32+
<constraint name="Magento\Catalog\Test\Constraint\AssertSubCategoryNotInNavigationMenu" />
33+
</variation>
34+
<variation name="CategoryAndSubcategotyNotIncludeInNavigationMenu2" summary="Turn off include_in_menu category, inactive category and check that category and subcategory are not visible on navigation menu">
35+
<data name="initialCategory/dataset" xsi:type="string">two_nested_categories</data>
36+
<data name="nestingLevel" xsi:type="number">2</data>
37+
<data name="category/data/is_active" xsi:type="string">No</data>
38+
<data name="category/data/include_in_menu" xsi:type="string">No</data>
39+
<constraint name="Magento\Catalog\Test\Constraint\AssertCategoryNotInNavigationMenu" />
40+
<constraint name="Magento\Catalog\Test\Constraint\AssertSubCategoryNotInNavigationMenu" />
41+
</variation>
42+
</testCase>
43+
</config>

0 commit comments

Comments
 (0)