|
| 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 | +} |
0 commit comments