|
9 | 9 |
|
10 | 10 | use Magento\Catalog\Api\CategoryRepositoryInterface;
|
11 | 11 | use Magento\Catalog\Api\Data\CategoryInterface;
|
| 12 | +use Magento\Catalog\Api\Data\ProductInterface; |
12 | 13 | use Magento\Catalog\Model\ResourceModel\Collection\AbstractCollection;
|
| 14 | +use Magento\Catalog\Test\Fixture\AssignProducts as AssignProductsFixture; |
| 15 | +use Magento\Catalog\Test\Fixture\Category as CategoryFixture; |
| 16 | +use Magento\Catalog\Test\Fixture\Product as ProductFixture; |
| 17 | +use Magento\Framework\ObjectManagerInterface; |
13 | 18 | use Magento\Framework\Registry;
|
14 | 19 | use Magento\Framework\View\LayoutInterface;
|
| 20 | +use Magento\Store\Test\Fixture\Store as StoreFixture; |
| 21 | +use Magento\TestFramework\Fixture\DataFixture; |
| 22 | +use Magento\TestFramework\Fixture\DataFixtureStorageManager; |
15 | 23 | use Magento\TestFramework\Helper\Bootstrap;
|
16 | 24 | use PHPUnit\Framework\TestCase;
|
17 |
| -use Magento\Framework\ObjectManagerInterface; |
18 |
| -use Magento\Catalog\Api\Data\ProductInterface; |
19 | 25 |
|
20 | 26 | /**
|
21 | 27 | * Checks grid data on the tab 'Products in Category' category view page.
|
@@ -141,6 +147,102 @@ public function optionsFilterProvider(): array
|
141 | 147 | ];
|
142 | 148 | }
|
143 | 149 |
|
| 150 | + /** |
| 151 | + * @dataProvider sortingOptionsProvider |
| 152 | + * @param string $sortField |
| 153 | + * @param string $sortDirection |
| 154 | + * @param string $store |
| 155 | + * @param array $items |
| 156 | + * @return void |
| 157 | + */ |
| 158 | + #[ |
| 159 | + DataFixture(CategoryFixture::class, ['name' => 'CategoryA'], as: 'category'), |
| 160 | + DataFixture( |
| 161 | + ProductFixture::class, |
| 162 | + ['name' => 'ProductA','sku' => 'ProductA'], |
| 163 | + as: 'productA' |
| 164 | + ), |
| 165 | + DataFixture( |
| 166 | + ProductFixture::class, |
| 167 | + ['name' => 'ProductB','sku' => 'ProductB'], |
| 168 | + as: 'productB' |
| 169 | + ), |
| 170 | + DataFixture( |
| 171 | + AssignProductsFixture::class, |
| 172 | + ['products' => ['$productA$', '$productB$'], 'category' => '$category$'], |
| 173 | + as: 'assignProducts' |
| 174 | + ), |
| 175 | + DataFixture(StoreFixture::class, ['code' => 'second_store'], as: 'store2'), |
| 176 | + ] |
| 177 | + public function testSortProductsInCategory( |
| 178 | + string $sortField, |
| 179 | + string $sortDirection, |
| 180 | + string $store, |
| 181 | + array $items |
| 182 | + ): void { |
| 183 | + $fixtures = DataFixtureStorageManager::getStorage(); |
| 184 | + $fixtures->get('productA')->addAttributeUpdate('name', 'SimpleProductA', $fixtures->get('store2')->getId()); |
| 185 | + $fixtures->get('productB')->addAttributeUpdate('name', 'SimpleProductB', $fixtures->get('store2')->getId()); |
| 186 | + $collection = $this->sortProductsInGrid( |
| 187 | + $sortField, |
| 188 | + $sortDirection, |
| 189 | + (int)$fixtures->get('category')->getId(), |
| 190 | + $store === 'default' ? 1 : (int)$fixtures->get($store)->getId(), |
| 191 | + ); |
| 192 | + $productNames = []; |
| 193 | + foreach ($collection as $product) { |
| 194 | + $productNames[] = $product->getName(); |
| 195 | + } |
| 196 | + $this->assertEquals($productNames, $items); |
| 197 | + } |
| 198 | + |
| 199 | + /** |
| 200 | + * Different variations for sorting test. |
| 201 | + * |
| 202 | + * @return array |
| 203 | + */ |
| 204 | + public function sortingOptionsProvider(): array |
| 205 | + { |
| 206 | + return [ |
| 207 | + 'default_store_sort_name_asc' => [ |
| 208 | + 'sort_field' => 'name', |
| 209 | + 'sort_direction' => 'asc', |
| 210 | + 'store' => 'default', |
| 211 | + 'sortItems' => [ |
| 212 | + 'ProductA', |
| 213 | + 'ProductB', |
| 214 | + ], |
| 215 | + ], |
| 216 | + 'default_store_sort_name_desc' => [ |
| 217 | + 'sort_field' => 'name', |
| 218 | + 'sort_direction' => 'desc', |
| 219 | + 'store' => 'default', |
| 220 | + 'items' => [ |
| 221 | + 'ProductB', |
| 222 | + 'ProductA', |
| 223 | + ], |
| 224 | + ], |
| 225 | + 'second_store_sort_name_asc' => [ |
| 226 | + 'sort_field' => 'name', |
| 227 | + 'sort_direction' => 'asc', |
| 228 | + 'store' => 'store2', |
| 229 | + 'sortItems' => [ |
| 230 | + 'SimpleProductA', |
| 231 | + 'SimpleProductB', |
| 232 | + ], |
| 233 | + ], |
| 234 | + 'second_store_sort_name_desc' => [ |
| 235 | + 'sort_field' => 'name', |
| 236 | + 'sort_direction' => 'desc', |
| 237 | + 'store' => 'store2', |
| 238 | + 'sortItems' => [ |
| 239 | + 'SimpleProductB', |
| 240 | + 'SimpleProductA', |
| 241 | + ], |
| 242 | + ], |
| 243 | + ]; |
| 244 | + } |
| 245 | + |
144 | 246 | /**
|
145 | 247 | * Filter product in grid
|
146 | 248 | *
|
@@ -174,4 +276,33 @@ private function registerCategory(CategoryInterface $category): void
|
174 | 276 | $this->registry->unregister('category');
|
175 | 277 | $this->registry->register('category', $category);
|
176 | 278 | }
|
| 279 | + |
| 280 | + /** |
| 281 | + * Sort products in grid |
| 282 | + * |
| 283 | + * @param string $sortField |
| 284 | + * @param string $sortDirection |
| 285 | + * @param int $categoryId |
| 286 | + * @param int $storeId |
| 287 | + * @return AbstractCollection |
| 288 | + * @throws \Magento\Framework\Exception\NoSuchEntityException |
| 289 | + */ |
| 290 | + private function sortProductsInGrid( |
| 291 | + string $sortField, |
| 292 | + string $sortDirection, |
| 293 | + int $categoryId, |
| 294 | + int $storeId |
| 295 | + ): AbstractCollection { |
| 296 | + $this->registerCategory($this->categoryRepository->get($categoryId)); |
| 297 | + $block = $this->layout->createBlock(Product::class); |
| 298 | + $block->getRequest()->setParams([ |
| 299 | + 'id' => $categoryId, |
| 300 | + 'sort' => $sortField, |
| 301 | + 'dir' => $sortDirection, |
| 302 | + 'store' => $storeId, |
| 303 | + ]); |
| 304 | + $block->toHtml(); |
| 305 | + |
| 306 | + return $block->getCollection(); |
| 307 | + } |
177 | 308 | }
|
0 commit comments