Skip to content

Commit dfb79dc

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

File tree

4 files changed

+44
-9
lines changed

4 files changed

+44
-9
lines changed

dev/tests/integration/testsuite/Magento/Catalog/Controller/Adminhtml/Category/EditTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
namespace Magento\Catalog\Controller\Adminhtml\Category;
99

1010
use Magento\Catalog\Api\Data\CategoryInterface;
11-
use Magento\Catalog\Model\ResourceModel\Category\Collection;
1211
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
1312
use Magento\Framework\App\Request\Http as HttpRequest;
1413
use Magento\Store\Model\StoreManagerInterface;
@@ -23,7 +22,7 @@
2322
*/
2423
class EditTest extends AbstractBackendController
2524
{
26-
/** @var Collection */
25+
/** @var CollectionFactory */
2726
private $categoryCollectionFactory;
2827

2928
/** @var StoreManagerInterface */

dev/tests/integration/testsuite/Magento/Catalog/Controller/Adminhtml/Category/Save/SaveCategoryTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Magento\Framework\Exception\NoSuchEntityException;
1616
use Magento\Framework\Message\MessageInterface;
1717
use Magento\Store\Model\StoreManagerInterface;
18+
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
1819

1920
/**
2021
* Test cases for save category controller.
@@ -36,6 +37,9 @@ class SaveCategoryTest extends AbstractSaveCategoryTest
3637
/** @var StoreManagerInterface */
3738
private $storeManager;
3839

40+
/** @var CollectionFactory */
41+
private $categoryCollectionFactory;
42+
3943
/**
4044
* @inheritdoc
4145
*/
@@ -46,6 +50,7 @@ protected function setUp(): void
4650
$this->categoryRepository = $this->_objectManager->get(CategoryRepositoryInterface::class);
4751
$this->getBlockByIdentifier = $this->_objectManager->get(GetBlockByIdentifierInterface::class);
4852
$this->storeManager = $this->_objectManager->get(StoreManagerInterface::class);
53+
$this->categoryCollectionFactory = $this->_objectManager->get(CollectionFactory::class);
4954
}
5055

5156
/**
@@ -109,4 +114,37 @@ public function testTryToCreateCategoryWithEmptyValues(): void
109114
);
110115
$this->assertSessionMessages($this->containsEqual($message), MessageInterface::TYPE_ERROR);
111116
}
117+
118+
/**
119+
* @magentoDataFixture Magento/Catalog/_files/second_root_category.php
120+
*
121+
* @return void
122+
*/
123+
public function testSwitchingStoreViewsCategory(): void
124+
{
125+
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
126+
$id = (int)$this->getCategoryIdByName('Second Root Category');
127+
$storeId = (int)$this->storeManager->getStore('default')->getId();
128+
$this->getRequest()->setParams(['store' => $storeId, 'id' => $id]);
129+
$this->dispatch('backend/catalog/category/save');
130+
$this->assertRedirect($this->stringContains('backend/catalog/category/index'));
131+
$this->assertStringNotContainsString('/id/', $this->getResponse()->getHeader('Location')->getFieldValue());
132+
}
133+
134+
/**
135+
* Get category id by name
136+
*
137+
* @param string $name
138+
* @return string|null
139+
*/
140+
private function getCategoryIdByName(string $name): ?string
141+
{
142+
$categoryCollection = $this->categoryCollectionFactory->create();
143+
$category = $categoryCollection
144+
->addAttributeToFilter(CategoryInterface::KEY_NAME, $name)
145+
->setPageSize(1)
146+
->getFirstItem();
147+
148+
return $category->getId();
149+
}
112150
}

dev/tests/integration/testsuite/Magento/Catalog/_files/second_root_category.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
$objectManager = Bootstrap::getObjectManager();
1414
/** @var CategoryFactory $categoryFactory */
1515
$categoryFactory = $objectManager->get(CategoryFactory::class);
16+
/** @var CategoryRepositoryInterface $categoryRepository */
1617
$categoryRepository = $objectManager->get(CategoryRepositoryInterface::class);
1718
$rootCategory = $categoryFactory->create();
1819
$rootCategory->setName('Second Root Category')
1920
->setParentId(Category::TREE_ROOT_ID)
2021
->setIsActive(true);
21-
$rootCategory = $categoryRepository->save($rootCategory);
22+
$categoryRepository->save($rootCategory);

dev/tests/integration/testsuite/Magento/Catalog/_files/second_root_category_rollback.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,21 @@
77

88
use Magento\Catalog\Api\CategoryRepositoryInterface;
99
use Magento\Catalog\Api\Data\CategoryInterface;
10-
use Magento\Catalog\Model\CategoryRepository;
11-
use Magento\Catalog\Model\ResourceModel\Category\Collection;
1210
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
1311
use Magento\Framework\Registry;
1412
use Magento\TestFramework\Helper\Bootstrap;
1513

1614
$objectManager = Bootstrap::getObjectManager();
17-
/** @var CategoryRepository $categoryRepository */
18-
$categoryRepository = $objectManager->create(CategoryRepositoryInterface::class);
15+
/** @var CategoryRepositoryInterface $categoryRepository */
16+
$categoryRepository = $objectManager->get(CategoryRepositoryInterface::class);
1917
/** @var CollectionFactory $categoryCollectionFactory */
2018
$categoryCollectionFactory = $objectManager->get(CollectionFactory::class);
2119

2220
/** @var Registry $registry */
2321
$registry = $objectManager->get(Registry::class);
2422
$registry->unregister('isSecureArea');
2523
$registry->register('isSecureArea', true);
26-
/** @var Collection $categoryCollection */
27-
$categoryCollection = $categoryCollectionFactory->get();
24+
$categoryCollection = $categoryCollectionFactory->create();
2825
$category = $categoryCollection
2926
->addAttributeToFilter(CategoryInterface::KEY_NAME, 'Second Root Category')
3027
->setPageSize(1)

0 commit comments

Comments
 (0)