Skip to content

Commit 5a92ebf

Browse files
author
Serhii Bohomaz
committed
MC-39584: Create automated test for: "Switching store views of category"
1 parent 1e846a4 commit 5a92ebf

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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\Controller\Adminhtml\Category;
9+
10+
use Magento\Catalog\Api\Data\CategoryInterface;
11+
use Magento\Catalog\Model\ResourceModel\Category\Collection;
12+
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
13+
use Magento\Framework\App\Request\Http as HttpRequest;
14+
use Magento\Store\Model\StoreManagerInterface;
15+
use Magento\TestFramework\TestCase\AbstractBackendController;
16+
17+
/**
18+
* Test cases related to edit category.
19+
*
20+
* @see \Magento\Catalog\Controller\Adminhtml\Category\Edit
21+
* @magentoAppArea adminhtml
22+
* @magentoDbIsolation enabled
23+
*/
24+
class EditTest extends AbstractBackendController
25+
{
26+
/** @var Collection */
27+
private $categoryCollectionFactory;
28+
29+
/** @var StoreManagerInterface */
30+
private $storeManager;
31+
32+
/**
33+
* @inheritDoc
34+
*/
35+
protected function setUp(): void
36+
{
37+
parent::setUp();
38+
39+
$this->categoryCollectionFactory = $this->_objectManager->get(CollectionFactory::class);
40+
$this->storeManager = $this->_objectManager->get(StoreManagerInterface::class);
41+
}
42+
43+
/**
44+
* @magentoDataFixture Magento/Catalog/_files/second_root_category.php
45+
*
46+
* @return void
47+
*/
48+
public function testSwitchingStoreViewsCategory(): void
49+
{
50+
$this->getRequest()->setMethod(HttpRequest::METHOD_GET);
51+
$id = (int)$this->getCategoryIdByName('Second Root Category');
52+
$storeId = (int)$this->storeManager->getStore('default')->getId();
53+
$this->getRequest()->setParams(['store' => $storeId, 'id' => $id]);
54+
$this->dispatch('backend/catalog/category/edit');
55+
$this->assertRedirect($this->stringContains('backend/catalog/category/index'));
56+
$this->assertStringNotContainsString('/id/', $this->getResponse()->getHeader('Location')->getFieldValue());
57+
}
58+
59+
/**
60+
* Get category id by name
61+
*
62+
* @param string $name
63+
* @return string|null
64+
*/
65+
private function getCategoryIdByName(string $name): ?string
66+
{
67+
$categoryCollection = $this->categoryCollectionFactory->create();
68+
$category = $categoryCollection
69+
->addAttributeToFilter(CategoryInterface::KEY_NAME, $name)
70+
->setPageSize(1)
71+
->getFirstItem();
72+
73+
return $category->getId();
74+
}
75+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
use Magento\Catalog\Api\CategoryRepositoryInterface;
9+
use Magento\Catalog\Model\Category;
10+
use Magento\Catalog\Model\CategoryFactory;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
13+
$objectManager = Bootstrap::getObjectManager();
14+
/** @var CategoryFactory $categoryFactory */
15+
$categoryFactory = $objectManager->get(CategoryFactory::class);
16+
$categoryRepository = $objectManager->get(CategoryRepositoryInterface::class);
17+
$rootCategory = $categoryFactory->create();
18+
$rootCategory->setName('Second Root Category')
19+
->setParentId(Category::TREE_ROOT_ID)
20+
->setIsActive(true);
21+
$rootCategory = $categoryRepository->save($rootCategory);
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
use Magento\Catalog\Api\CategoryRepositoryInterface;
9+
use Magento\Catalog\Api\Data\CategoryInterface;
10+
use Magento\Catalog\Model\CategoryRepository;
11+
use Magento\Catalog\Model\ResourceModel\Category\Collection;
12+
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
13+
use Magento\Framework\Registry;
14+
use Magento\TestFramework\Helper\Bootstrap;
15+
16+
$objectManager = Bootstrap::getObjectManager();
17+
/** @var CategoryRepository $categoryRepository */
18+
$categoryRepository = $objectManager->create(CategoryRepositoryInterface::class);
19+
/** @var CollectionFactory $categoryCollectionFactory */
20+
$categoryCollectionFactory = $objectManager->get(CollectionFactory::class);
21+
22+
/** @var Registry $registry */
23+
$registry = $objectManager->get(Registry::class);
24+
$registry->unregister('isSecureArea');
25+
$registry->register('isSecureArea', true);
26+
/** @var Collection $categoryCollection */
27+
$categoryCollection = $categoryCollectionFactory->get();
28+
$category = $categoryCollection
29+
->addAttributeToFilter(CategoryInterface::KEY_NAME, 'Second Root Category')
30+
->setPageSize(1)
31+
->getFirstItem();
32+
if ($category->getId()) {
33+
$categoryRepository->delete($category);
34+
}
35+
36+
$registry->unregister('isSecureArea');
37+
$registry->register('isSecureArea', false);

0 commit comments

Comments
 (0)