|
| 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\Delete; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\CategoryRepositoryInterface; |
| 11 | +use Magento\Catalog\Model\Indexer\Category\Flat\State; |
| 12 | +use Magento\Catalog\Model\ResourceModel\Category\Flat as CategoryFlatResource; |
| 13 | +use Magento\Catalog\Model\ResourceModel\Category\Flat\CollectionFactory; |
| 14 | +use Magento\Framework\App\Request\Http as HttpRequest; |
| 15 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 16 | +use Magento\Framework\Indexer\IndexerRegistry; |
| 17 | +use Magento\TestFramework\TestCase\AbstractBackendController; |
| 18 | + |
| 19 | +/** |
| 20 | + * Test cases related to delete category with enabled category flat. |
| 21 | + * |
| 22 | + * @magentoAppArea adminhtml |
| 23 | + * @magentoDbIsolation disabled |
| 24 | + */ |
| 25 | +class DeleteCategoryWithEnabledFlatTest extends AbstractBackendController |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @var IndexerRegistry |
| 29 | + */ |
| 30 | + private $indexerRegistry; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var CategoryRepositoryInterface |
| 34 | + */ |
| 35 | + private $categoryRepository; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var CategoryFlatResource |
| 39 | + */ |
| 40 | + private $categoryFlatResource; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var CollectionFactory |
| 44 | + */ |
| 45 | + private $categoryFlatCollectionFactory; |
| 46 | + |
| 47 | + /** |
| 48 | + * @inheritdoc |
| 49 | + */ |
| 50 | + protected function setUp() |
| 51 | + { |
| 52 | + parent::setUp(); |
| 53 | + $this->indexerRegistry = $this->_objectManager->get(IndexerRegistry::class); |
| 54 | + $this->categoryRepository = $this->_objectManager->get(CategoryRepositoryInterface::class); |
| 55 | + $this->categoryFlatResource = $this->_objectManager->get(CategoryFlatResource::class); |
| 56 | + $this->categoryFlatCollectionFactory = $this->_objectManager->get(CollectionFactory::class); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @inheritdoc |
| 61 | + */ |
| 62 | + protected function tearDown() |
| 63 | + { |
| 64 | + parent::tearDown(); |
| 65 | + $categoryFlatIndexer = $this->indexerRegistry->get(State::INDEXER_ID); |
| 66 | + $categoryFlatIndexer->invalidate(); |
| 67 | + $this->categoryFlatResource->getConnection()->dropTable($this->categoryFlatResource->getMainTable()); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Check that product is deleted from flat table. |
| 72 | + * |
| 73 | + * @magentoConfigFixture current_store catalog/frontend/flat_catalog_category true |
| 74 | + * |
| 75 | + * @magentoDataFixture Magento/Catalog/_files/category.php |
| 76 | + * @magentoDataFixture Magento/Catalog/_files/reindex_catalog_category_flat.php |
| 77 | + * |
| 78 | + * @return void |
| 79 | + */ |
| 80 | + public function testDeleteCategory(): void |
| 81 | + { |
| 82 | + $this->assertEquals(1, $this->getFlatCategoryCollectionSizeByCategoryId(333)); |
| 83 | + $this->getRequest()->setMethod(HttpRequest::METHOD_POST); |
| 84 | + $this->getRequest()->setPostValue(['id' => 333]); |
| 85 | + $this->dispatch('backend/catalog/category/delete'); |
| 86 | + $this->assertSessionMessages($this->equalTo([(string)__('You deleted the category.')])); |
| 87 | + $this->assertEquals(0, $this->getFlatCategoryCollectionSizeByCategoryId(333)); |
| 88 | + $this->checkCategoryIsDeleted(333); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Return collection size from category flat collection by category ID. |
| 93 | + * |
| 94 | + * @param int $categoryId |
| 95 | + * @return int |
| 96 | + */ |
| 97 | + private function getFlatCategoryCollectionSizeByCategoryId(int $categoryId): int |
| 98 | + { |
| 99 | + $categoryFlatCollection = $this->categoryFlatCollectionFactory->create(); |
| 100 | + $categoryFlatCollection->addIdFilter($categoryId); |
| 101 | + |
| 102 | + return $categoryFlatCollection->getSize(); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Assert that category is deleted. |
| 107 | + * |
| 108 | + * @param int $categoryId |
| 109 | + */ |
| 110 | + private function checkCategoryIsDeleted(int $categoryId): void |
| 111 | + { |
| 112 | + $this->expectExceptionObject(new NoSuchEntityException(__("No such entity with id = {$categoryId}"))); |
| 113 | + $this->categoryRepository->get($categoryId); |
| 114 | + } |
| 115 | +} |
0 commit comments