|
| 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\Widget\Block\Adminhtml\Widget; |
| 9 | + |
| 10 | +use Magento\Framework\App\RequestInterface; |
| 11 | +use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection; |
| 12 | +use Magento\Framework\ObjectManagerInterface; |
| 13 | +use Magento\Framework\View\LayoutInterface; |
| 14 | +use Magento\Framework\View\Result\PageFactory; |
| 15 | +use Magento\TestFramework\Helper\Bootstrap; |
| 16 | +use Magento\Theme\Model\ResourceModel\Theme as ThemeResource; |
| 17 | +use Magento\Theme\Model\ThemeFactory; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | + |
| 20 | +/** |
| 21 | + * Checks widget grid filtering and sorting |
| 22 | + * |
| 23 | + * @magentoAppArea adminhtml |
| 24 | + * @magentoAppIsolation enabled |
| 25 | + * @magentoDbIsolation enabled |
| 26 | + */ |
| 27 | +class InstanceTest extends TestCase |
| 28 | +{ |
| 29 | + /** @var ObjectManagerInterface */ |
| 30 | + private $objectManager; |
| 31 | + |
| 32 | + /** @var PageFactory */ |
| 33 | + private $pageFactory; |
| 34 | + |
| 35 | + /** @var RequestInterface */ |
| 36 | + private $request; |
| 37 | + |
| 38 | + /** |
| 39 | + * @inheritdoc |
| 40 | + */ |
| 41 | + protected function setUp(): void |
| 42 | + { |
| 43 | + parent::setUp(); |
| 44 | + |
| 45 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 46 | + $this->request = $this->objectManager->get(RequestInterface::class); |
| 47 | + $this->pageFactory = $this->objectManager->get(PageFactory::class); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @dataProvider gridFiltersDataProvider |
| 52 | + * |
| 53 | + * @magentoDataFixture Magento/Widget/_files/widgets.php |
| 54 | + * |
| 55 | + * @param array $filter |
| 56 | + * @param array $expectedWidgets |
| 57 | + * @return void |
| 58 | + */ |
| 59 | + public function testGridFiltering(array $filter, array $expectedWidgets): void |
| 60 | + { |
| 61 | + $this->request->setParams($filter); |
| 62 | + $collection = $this->getGridCollection(); |
| 63 | + |
| 64 | + $this->assertWidgets($expectedWidgets, $collection); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @return array |
| 69 | + */ |
| 70 | + public function gridFiltersDataProvider(): array |
| 71 | + { |
| 72 | + return [ |
| 73 | + 'first_page' => [ |
| 74 | + 'filter' => [ |
| 75 | + 'limit' => 2, |
| 76 | + 'page' => 1, |
| 77 | + ], |
| 78 | + 'expected_widgets' => [ |
| 79 | + 'cms page widget title', |
| 80 | + 'product link widget title', |
| 81 | + ], |
| 82 | + ], |
| 83 | + 'second_page' => [ |
| 84 | + 'filter' => [ |
| 85 | + 'limit' => 2, |
| 86 | + 'page' => 2, |
| 87 | + ], |
| 88 | + 'expected_widgets' => [ |
| 89 | + 'recently compared products', |
| 90 | + ], |
| 91 | + ], |
| 92 | + 'filter_by_title' => [ |
| 93 | + 'filter' => [ |
| 94 | + 'filter' => base64_encode('title=product link widget title'), |
| 95 | + ], |
| 96 | + 'expected_widgets' => [ |
| 97 | + 'product link widget title', |
| 98 | + ], |
| 99 | + ], |
| 100 | + 'filter_by_type' => [ |
| 101 | + 'filter' => [ |
| 102 | + 'filter' => base64_encode('type=Magento%5CCms%5CBlock%5CWidget%5CPage%5CLink'), |
| 103 | + ], |
| 104 | + 'expected_widgets' => [ |
| 105 | + 'cms page widget title', |
| 106 | + ], |
| 107 | + ], |
| 108 | + 'filter_by_theme' => [ |
| 109 | + 'filter' => [ |
| 110 | + 'filter' => base64_encode('theme_id=' . $this->loadThemeIdByCode('Magento/blank')), |
| 111 | + ], |
| 112 | + 'expected_widgets' => [ |
| 113 | + 'recently compared products', |
| 114 | + ], |
| 115 | + ], |
| 116 | + 'filter_by_sort_order' => [ |
| 117 | + 'filter' => [ |
| 118 | + 'filter' => base64_encode('sort_order=1'), |
| 119 | + ], |
| 120 | + 'expected_widgets' => [ |
| 121 | + 'recently compared products' |
| 122 | + ], |
| 123 | + ], |
| 124 | + 'filter_by_multiple_filters' => [ |
| 125 | + 'filter' => [ |
| 126 | + 'filter' => base64_encode( |
| 127 | + 'type=Magento%5CCatalog%5CBlock%5CWidget%5CRecentlyCompared&sort_order=1' |
| 128 | + ), |
| 129 | + ], |
| 130 | + 'expected_widgets' => [ |
| 131 | + 'recently compared products', |
| 132 | + ], |
| 133 | + ], |
| 134 | + ]; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * @dataProvider gridSortDataProvider |
| 139 | + * |
| 140 | + * @magentoDataFixture Magento/Widget/_files/widgets.php |
| 141 | + * |
| 142 | + * @param array $filter |
| 143 | + * @param array $expectedWidgets |
| 144 | + * @return void |
| 145 | + */ |
| 146 | + public function testGridSorting(array $filter, array $expectedWidgets): void |
| 147 | + { |
| 148 | + $this->request->setParams($filter); |
| 149 | + $collection = $this->getGridCollection(); |
| 150 | + $this->assertEquals($expectedWidgets, $collection->getColumnValues('title')); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * @return array |
| 155 | + */ |
| 156 | + public function gridSortDataProvider(): array |
| 157 | + { |
| 158 | + return [ |
| 159 | + 'sort_by_id_asc' => [ |
| 160 | + 'filter' => ['sort' => 'instance_id', 'dir' => 'asc'], |
| 161 | + 'expected_widgets' => [ |
| 162 | + 'cms page widget title', |
| 163 | + 'product link widget title', |
| 164 | + 'recently compared products', |
| 165 | + ], |
| 166 | + ], |
| 167 | + 'sort_by_id_desc' => [ |
| 168 | + 'filter' => ['sort' => 'instance_id', 'dir' => 'desc'], |
| 169 | + 'expected_widgets' => [ |
| 170 | + 'recently compared products', |
| 171 | + 'product link widget title', |
| 172 | + 'cms page widget title', |
| 173 | + ], |
| 174 | + ], |
| 175 | + 'sort_by_title_asc' => [ |
| 176 | + 'filter' => ['sort' => 'title', 'dir' => 'asc'], |
| 177 | + 'expected_widgets' => [ |
| 178 | + 'cms page widget title', |
| 179 | + 'product link widget title', |
| 180 | + 'recently compared products', |
| 181 | + ], |
| 182 | + ], |
| 183 | + 'sort_by_title_desc' => [ |
| 184 | + 'filter' => ['sort' => 'title', 'dir' => 'desc'], |
| 185 | + 'expected_widgets' => [ |
| 186 | + 'recently compared products', |
| 187 | + 'product link widget title', |
| 188 | + 'cms page widget title', |
| 189 | + ], |
| 190 | + ], |
| 191 | + 'sort_by_type_asc' => [ |
| 192 | + 'filter' => ['sort' => 'type', 'dir' => 'asc'], |
| 193 | + 'expected_widgets' => [ |
| 194 | + 'product link widget title', |
| 195 | + 'recently compared products', |
| 196 | + 'cms page widget title', |
| 197 | + ], |
| 198 | + ], |
| 199 | + 'sort_by_type_desc' => [ |
| 200 | + 'filter' => ['sort' => 'type', 'dir' => 'desc'], |
| 201 | + 'expected_widgets' => [ |
| 202 | + 'cms page widget title', |
| 203 | + 'recently compared products', |
| 204 | + 'product link widget title', |
| 205 | + ], |
| 206 | + ], |
| 207 | + 'sort_by_sort_order_asc' => [ |
| 208 | + 'filter' => ['sort' => 'sort_order', 'dir' => 'asc'], |
| 209 | + 'expected_widgets' => [ |
| 210 | + 'recently compared products', |
| 211 | + 'product link widget title', |
| 212 | + 'cms page widget title', |
| 213 | + ], |
| 214 | + ], |
| 215 | + 'sort_by_sort_order_desc' => [ |
| 216 | + 'filter' => ['sort' => 'sort_order', 'dir' => 'desc'], |
| 217 | + 'expected_widgets' => [ |
| 218 | + 'cms page widget title', |
| 219 | + 'product link widget title', |
| 220 | + 'recently compared products', |
| 221 | + ], |
| 222 | + ], |
| 223 | + 'sort_by_theme_asc' => [ |
| 224 | + 'filter' => ['sort' => 'theme_id', 'dir' => 'asc'], |
| 225 | + 'expected_widgets' => [ |
| 226 | + 'recently compared products', |
| 227 | + 'cms page widget title', |
| 228 | + 'product link widget title', |
| 229 | + ], |
| 230 | + ], |
| 231 | + 'sort_by_theme_desc' => [ |
| 232 | + 'filter' => ['sort' => 'theme_id', 'dir' => 'asc'], |
| 233 | + 'expected_widgets' => [ |
| 234 | + 'recently compared products', |
| 235 | + 'cms page widget title', |
| 236 | + 'product link widget title', |
| 237 | + ], |
| 238 | + ], |
| 239 | + ]; |
| 240 | + } |
| 241 | + |
| 242 | + /** |
| 243 | + * Load theme by theme id |
| 244 | + * |
| 245 | + * @param string $code |
| 246 | + * @return int |
| 247 | + */ |
| 248 | + private function loadThemeIdByCode(string $code): int |
| 249 | + { |
| 250 | + $objectManager = Bootstrap::getObjectManager(); |
| 251 | + /** @var ThemeFactory $themeFactory */ |
| 252 | + $themeFactory = $objectManager->get(ThemeFactory::class); |
| 253 | + /** @var ThemeResource $themeResource */ |
| 254 | + $themeResource = $objectManager->get(ThemeResource::class); |
| 255 | + $theme = $themeFactory->create(); |
| 256 | + $themeResource->load($theme, $code, 'code'); |
| 257 | + |
| 258 | + return (int)$theme->getId(); |
| 259 | + } |
| 260 | + |
| 261 | + /** |
| 262 | + * Assert widget instances |
| 263 | + * |
| 264 | + * @param $expectedWidgets |
| 265 | + * @param AbstractCollection $collection |
| 266 | + * @return void |
| 267 | + */ |
| 268 | + private function assertWidgets($expectedWidgets, AbstractCollection $collection): void |
| 269 | + { |
| 270 | + $this->assertCount(count($expectedWidgets), $collection); |
| 271 | + foreach ($expectedWidgets as $widgetTitle) { |
| 272 | + $item = $collection->getItemByColumnValue('title', $widgetTitle); |
| 273 | + $this->assertNotNull($item); |
| 274 | + } |
| 275 | + } |
| 276 | + |
| 277 | + /** |
| 278 | + * Prepare page layout |
| 279 | + * |
| 280 | + * @return LayoutInterface |
| 281 | + */ |
| 282 | + private function preparePageLayout(): LayoutInterface |
| 283 | + { |
| 284 | + $page = $this->pageFactory->create(); |
| 285 | + $page->addHandle([ |
| 286 | + 'default', |
| 287 | + 'adminhtml_widget_instance_index', |
| 288 | + ]); |
| 289 | + |
| 290 | + return $page->getLayout()->generateXml(); |
| 291 | + } |
| 292 | + |
| 293 | + /** |
| 294 | + * Get prepared grid collection |
| 295 | + * |
| 296 | + * @return AbstractCollection |
| 297 | + */ |
| 298 | + private function getGridCollection(): AbstractCollection |
| 299 | + { |
| 300 | + $layout = $this->preparePageLayout(); |
| 301 | + $containerBlock = $layout->getBlock('adminhtml.widget.instance.grid.container'); |
| 302 | + $grid = $containerBlock->getChildBlock('grid'); |
| 303 | + $this->assertNotFalse($grid); |
| 304 | + |
| 305 | + return $grid->getPreparedCollection(); |
| 306 | + } |
| 307 | +} |
0 commit comments