|
| 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\Adminhtml\Category\Tab; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\CategoryRepositoryInterface; |
| 11 | +use Magento\Catalog\Api\Data\CategoryInterface; |
| 12 | +use Magento\Catalog\Model\ResourceModel\Collection\AbstractCollection; |
| 13 | +use Magento\Framework\Registry; |
| 14 | +use Magento\Framework\View\LayoutInterface; |
| 15 | +use Magento\TestFramework\Helper\Bootstrap; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | +use Magento\Framework\ObjectManagerInterface; |
| 18 | +use Magento\Catalog\Api\Data\ProductInterface; |
| 19 | + |
| 20 | +/** |
| 21 | + * Checks grid data on the tab 'Products in Category' category view page. |
| 22 | + * |
| 23 | + * @see \Magento\Catalog\Block\Adminhtml\Category\Tab\Product |
| 24 | + * @magentoAppArea adminhtml |
| 25 | + * @magentoDbIsolation enabled |
| 26 | + * @magentoAppIsolation enabled |
| 27 | + */ |
| 28 | +class ProductTest extends TestCase |
| 29 | +{ |
| 30 | + /** @var ObjectManagerInterface */ |
| 31 | + private $objectManager; |
| 32 | + |
| 33 | + /** @var LayoutInterface */ |
| 34 | + private $layout; |
| 35 | + |
| 36 | + /** @var CategoryRepositoryInterface */ |
| 37 | + private $categoryRepository; |
| 38 | + |
| 39 | + /** @var Registry */ |
| 40 | + private $registry; |
| 41 | + |
| 42 | + /** |
| 43 | + * @inheritdoc |
| 44 | + */ |
| 45 | + protected function setUp(): void |
| 46 | + { |
| 47 | + parent::setUp(); |
| 48 | + |
| 49 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 50 | + $this->layout = $this->objectManager->get(LayoutInterface::class); |
| 51 | + $this->categoryRepository = $this->objectManager->get(CategoryRepositoryInterface::class); |
| 52 | + $this->registry = $this->objectManager->get(Registry::class); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @inheritdoc |
| 57 | + */ |
| 58 | + protected function tearDown(): void |
| 59 | + { |
| 60 | + $this->registry->unregister('category'); |
| 61 | + |
| 62 | + parent::tearDown(); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @magentoDataFixture Magento/Catalog/_files/category_with_two_products.php |
| 67 | + * @magentoDataFixture Magento/Catalog/_files/product_associated.php |
| 68 | + * @magentoDataFixture Magento/Catalog/_files/simple_product_disabled.php |
| 69 | + * @dataProvider optionsFilterProvider |
| 70 | + * @param string $filterColumn |
| 71 | + * @param int $categoryId |
| 72 | + * @param int $storeId |
| 73 | + * @param array $items |
| 74 | + * @return void |
| 75 | + */ |
| 76 | + public function testFilterProductInCategory(string $filterColumn, int $categoryId, int $storeId, array $items): void |
| 77 | + { |
| 78 | + $collection = $this->filterProductInGrid($filterColumn, $categoryId, $storeId); |
| 79 | + $this->assertCount(count($items), $collection->getItems()); |
| 80 | + foreach ($items as $item) { |
| 81 | + $this->assertNotNull($collection->getItemByColumnValue(ProductInterface::SKU, $item)); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @magentoDataFixture Magento/Catalog/_files/category.php |
| 87 | + * @return void |
| 88 | + */ |
| 89 | + public function testEmptyProductIdColumnFilter(): void |
| 90 | + { |
| 91 | + $this->assertCount(0, $this->filterProductInGrid('in_category=1', 333, 1)->getItems()); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Different variations for filter test. |
| 96 | + * |
| 97 | + * @return array |
| 98 | + */ |
| 99 | + public function optionsFilterProvider(): array |
| 100 | + { |
| 101 | + return [ |
| 102 | + 'filter_yes' => [ |
| 103 | + 'filter_column' => 'in_category=1', |
| 104 | + 'id_category' => 333, |
| 105 | + 'store_id' => 1, |
| 106 | + 'items' => [ |
| 107 | + 'simple333', |
| 108 | + 'simple2', |
| 109 | + ], |
| 110 | + ], |
| 111 | + 'filter_no' => [ |
| 112 | + 'filter_column' => 'in_category=0', |
| 113 | + 'id_category' => 333, |
| 114 | + 'store_id' => 1, |
| 115 | + 'items' => [ |
| 116 | + 'product_disabled', |
| 117 | + 'simple', |
| 118 | + ], |
| 119 | + ], |
| 120 | + 'filter_any' => [ |
| 121 | + 'filter_column' => "", |
| 122 | + 'id_category' => 333, |
| 123 | + 'store_id' => 1, |
| 124 | + 'items' => [ |
| 125 | + 'product_disabled', |
| 126 | + 'simple333', |
| 127 | + 'simple2', |
| 128 | + 'simple', |
| 129 | + ], |
| 130 | + ], |
| 131 | + 'flag_status' => [ |
| 132 | + 'filter_column' => 'status=1', |
| 133 | + 'id_category' => 333, |
| 134 | + 'store_id' => 1, |
| 135 | + 'items' => [ |
| 136 | + 'simple333', |
| 137 | + 'simple2', |
| 138 | + 'simple', |
| 139 | + ], |
| 140 | + ], |
| 141 | + ]; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Filter product in grid |
| 146 | + * |
| 147 | + * @param string $filterOption |
| 148 | + * @param int $categoryId |
| 149 | + * @param int $storeId |
| 150 | + * @return AbstractCollection |
| 151 | + */ |
| 152 | + private function filterProductInGrid(string $filterOption, int $categoryId, int $storeId): AbstractCollection |
| 153 | + { |
| 154 | + $this->registerCategory($this->categoryRepository->get($categoryId)); |
| 155 | + $block = $this->layout->createBlock(Product::class); |
| 156 | + $block->getRequest()->setParams([ |
| 157 | + 'id' => $categoryId, |
| 158 | + 'filter' => base64_encode($filterOption), |
| 159 | + 'store' => $storeId, |
| 160 | + ]); |
| 161 | + $block->toHtml(); |
| 162 | + |
| 163 | + return $block->getCollection(); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Register category in registry |
| 168 | + * |
| 169 | + * @param CategoryInterface $category |
| 170 | + * @return void |
| 171 | + */ |
| 172 | + private function registerCategory(CategoryInterface $category): void |
| 173 | + { |
| 174 | + $this->registry->unregister('category'); |
| 175 | + $this->registry->register('category', $category); |
| 176 | + } |
| 177 | +} |
0 commit comments