Skip to content

Commit 4bdcb15

Browse files
committed
Merge branch 'MC-21654' into 2.3-develop-com-pr2
2 parents 76ca752 + b02c848 commit 4bdcb15

File tree

4 files changed

+238
-23
lines changed

4 files changed

+238
-23
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Block\Category;
9+
10+
use Magento\Catalog\Api\CategoryRepositoryInterface;
11+
use Magento\Catalog\Model\CategoryFactory;
12+
use Magento\Framework\ObjectManagerInterface;
13+
use Magento\Framework\View\LayoutInterface;
14+
use Magento\TestFramework\Helper\Bootstrap;
15+
use Magento\Theme\Block\Html\Topmenu;
16+
use PHPUnit\Framework\TestCase;
17+
18+
/**
19+
* Class checks top menu link behaviour.
20+
*
21+
* @magentoAppArea frontend
22+
* @magentoDbIsolation enabled
23+
*/
24+
class TopMenuTest extends TestCase
25+
{
26+
/** @var ObjectManagerInterface */
27+
private $objectManager;
28+
29+
/** @var Topmenu */
30+
private $block;
31+
32+
/** @var CategoryFactory */
33+
private $categoryFactory;
34+
35+
/** @var CategoryRepositoryInterface */
36+
private $categoryRepository;
37+
38+
/**
39+
* @inheritdoc
40+
*/
41+
protected function setUp()
42+
{
43+
parent::setUp();
44+
45+
$this->objectManager = Bootstrap::getObjectManager();
46+
$this->categoryFactory = $this->objectManager->create(CategoryFactory::class);
47+
$this->categoryRepository = $this->objectManager->create(CategoryRepositoryInterface::class);
48+
$this->block = $this->objectManager->get(LayoutInterface::class)->createBlock(Topmenu::class);
49+
}
50+
51+
/**
52+
* Checks menu item displaying.
53+
*
54+
* @magentoDataFixture Magento/Catalog/_files/category.php
55+
* @return void
56+
*/
57+
public function testTopMenuItemDisplay(): void
58+
{
59+
$output = $this->block->getHtml('level-top', 'submenu', 0);
60+
$this->assertContains('Category 1', $output);
61+
}
62+
63+
/**
64+
* Checks that menu item is not displayed if the category is disabled or include in menu is disabled.
65+
*
66+
* @dataProvider invisibilityDataProvider
67+
* @param array $data
68+
* @return void
69+
*/
70+
public function testTopMenuItemInvisibility(array $data): void
71+
{
72+
$category = $this->categoryFactory->create();
73+
$category->setData($data);
74+
$this->categoryRepository->save($category);
75+
$output = $this->block->getHtml('level-top', 'submenu', 0);
76+
$this->assertEmpty($output, 'The category is displayed in top menu navigation');
77+
}
78+
79+
/**
80+
* @return array
81+
*/
82+
public function invisibilityDataProvider(): array
83+
{
84+
return [
85+
'include_in_menu_disable' => [
86+
'data' => [
87+
'name' => 'Test Category',
88+
'path' => '1/2/',
89+
'is_active' => '1',
90+
'include_in_menu' => false,
91+
],
92+
],
93+
'category_disable' => [
94+
'data' => [
95+
'name' => 'Test Category 2',
96+
'path' => '1/2/',
97+
'is_active' => false,
98+
'include_in_menu' => true,
99+
],
100+
],
101+
];
102+
}
103+
}

dev/tests/integration/testsuite/Magento/Catalog/Controller/CategoryTest.php

Lines changed: 79 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,65 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Catalog\Controller;
79

10+
use Magento\Catalog\Model\Category;
11+
use Magento\Catalog\Model\Session;
12+
use Magento\Framework\ObjectManagerInterface;
13+
use Magento\Framework\Registry as Registry;
14+
use Magento\Framework\View\LayoutInterface;
15+
use Magento\TestFramework\Helper\Bootstrap;
16+
use Magento\TestFramework\TestCase\AbstractController;
17+
818
/**
9-
* Test class for \Magento\Catalog\Controller\Category.
19+
* Responsible for testing category view action on strorefront.
1020
*
21+
* @see \Magento\Catalog\Controller\Category\View
1122
* @magentoAppArea frontend
1223
*/
13-
class CategoryTest extends \Magento\TestFramework\TestCase\AbstractController
24+
class CategoryTest extends AbstractController
1425
{
26+
/** @var ObjectManagerInterface */
27+
private $objectManager;
28+
29+
/** @var Registry */
30+
private $registry;
31+
32+
/** @var Session */
33+
private $session;
34+
35+
/** @var LayoutInterface */
36+
private $layout;
37+
38+
/**
39+
* @inheritdoc
40+
*/
41+
protected function setUp()
42+
{
43+
parent::setUp();
44+
45+
$this->objectManager = Bootstrap::getObjectManager();
46+
$this->registry = $this->objectManager->get(Registry::class);
47+
$this->layout = $this->objectManager->get(LayoutInterface::class);
48+
$this->session = $this->objectManager->get(Session::class);
49+
}
50+
51+
/**
52+
* @inheritdoc
53+
*/
1554
public function assert404NotFound()
1655
{
1756
parent::assert404NotFound();
18-
/** @var $objectManager \Magento\TestFramework\ObjectManager */
19-
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
20-
$this->assertNull($objectManager->get(\Magento\Framework\Registry::class)->registry('current_category'));
57+
58+
$this->assertNull($this->registry->registry('current_category'));
2159
}
2260

23-
public function getViewActionDataProvider()
61+
/**
62+
* @return array
63+
*/
64+
public function getViewActionDataProvider(): array
2465
{
2566
return [
2667
'category without children' => [
@@ -56,51 +97,66 @@ public function getViewActionDataProvider()
5697
* @dataProvider getViewActionDataProvider
5798
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/categories_with_product_ids.php
5899
* @magentoDbIsolation disabled
100+
* @param int $categoryId
101+
* @param array $expectedHandles
102+
* @param array $expectedContent
103+
* @return void
59104
*/
60-
public function testViewAction($categoryId, array $expectedHandles, array $expectedContent)
105+
public function testViewAction(int $categoryId, array $expectedHandles, array $expectedContent): void
61106
{
62107
$this->dispatch("catalog/category/view/id/{$categoryId}");
63-
64-
/** @var $objectManager \Magento\TestFramework\ObjectManager */
65-
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
66-
67-
/** @var $currentCategory \Magento\Catalog\Model\Category */
68-
$currentCategory = $objectManager->get(\Magento\Framework\Registry::class)->registry('current_category');
69-
$this->assertInstanceOf(\Magento\Catalog\Model\Category::class, $currentCategory);
108+
/** @var $currentCategory Category */
109+
$currentCategory = $this->registry->registry('current_category');
110+
$this->assertInstanceOf(Category::class, $currentCategory);
70111
$this->assertEquals($categoryId, $currentCategory->getId(), 'Category in registry.');
71112

72-
$lastCategoryId = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
73-
\Magento\Catalog\Model\Session::class
74-
)->getLastVisitedCategoryId();
113+
$lastCategoryId = $this->session->getLastVisitedCategoryId();
75114
$this->assertEquals($categoryId, $lastCategoryId, 'Last visited category.');
76115

77116
/* Layout updates */
78-
$handles = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
79-
\Magento\Framework\View\LayoutInterface::class
80-
)->getUpdate()->getHandles();
117+
$handles = $this->layout->getUpdate()->getHandles();
81118
foreach ($expectedHandles as $expectedHandleName) {
82119
$this->assertContains($expectedHandleName, $handles);
83120
}
84121

85122
$responseBody = $this->getResponse()->getBody();
86-
87123
/* Response content */
88124
foreach ($expectedContent as $expectedText) {
89125
$this->assertStringMatchesFormat($expectedText, $responseBody);
90126
}
91127
}
92128

93-
public function testViewActionNoCategoryId()
129+
/**
130+
* @return void
131+
*/
132+
public function testViewActionNoCategoryId(): void
94133
{
95134
$this->dispatch('catalog/category/view/');
96135

97136
$this->assert404NotFound();
98137
}
99138

100-
public function testViewActionInactiveCategory()
139+
/**
140+
* @return void
141+
*/
142+
public function testViewActionNotExistingCategory(): void
101143
{
102144
$this->dispatch('catalog/category/view/id/8');
103145

104146
$this->assert404NotFound();
105147
}
148+
149+
/**
150+
* Checks that disabled category is not available in storefront
151+
*
152+
* @magentoDbIsolation enabled
153+
* @magentoDataFixture Magento/Catalog/_files/inactive_category.php
154+
* @return void
155+
*/
156+
public function testViewActionDisabledCategory(): void
157+
{
158+
$this->dispatch('catalog/category/view/id/111');
159+
160+
$this->assert404NotFound();
161+
}
106162
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
use Magento\Catalog\Model\Category;
8+
use Magento\Catalog\Model\CategoryFactory;
9+
use Magento\Catalog\Model\ResourceModel\Category as CategoryResource;
10+
use Magento\TestFramework\Helper\Bootstrap as Bootstrap;
11+
12+
$objectManager = Bootstrap::getObjectManager();
13+
/** @var CategoryResource $categoryResource */
14+
$categoryResource = $objectManager->create(CategoryResource::class);
15+
/** @var Category $category */
16+
$category = $objectManager->get(CategoryFactory::class)->create();
17+
$category->isObjectNew(true);
18+
$data = [
19+
'entity_id' => 111,
20+
'path' => '1/2/111',
21+
'name' => 'Test Category',
22+
'attribute_set_id' => $category->getDefaultAttributeSetId(),
23+
'parent_id' => 2,
24+
'is_active' => false,
25+
'include_in_menu' => true,
26+
];
27+
$category->setData($data);
28+
$categoryResource->save($category);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
use Magento\Catalog\Api\CategoryRepositoryInterface;
8+
use Magento\Framework\Exception\NoSuchEntityException;
9+
use Magento\Framework\Registry;
10+
use Magento\TestFramework\Helper\Bootstrap;
11+
12+
$objectManager = Bootstrap::getObjectManager();
13+
/** @var Registry $registry */
14+
$registry = $objectManager->get(Registry::class);
15+
$registry->unregister('isSecureArea');
16+
$registry->register('isSecureArea', true);
17+
/** @var CategoryRepositoryInterface $categoryRepository */
18+
$categoryRepository = $objectManager->get(CategoryRepositoryInterface::class);
19+
20+
try {
21+
$category = $categoryRepository->get(111);
22+
$categoryRepository->delete($category);
23+
} catch (NoSuchEntityException $e) {
24+
//category already removed
25+
}
26+
27+
$registry->unregister('isSecureArea');
28+
$registry->register('isSecureArea', false);

0 commit comments

Comments
 (0)