Skip to content

Commit f886410

Browse files
committed
Merge remote-tracking branch 'origin/AC-6761' into spartans_pr_13062023
2 parents b8ea3a1 + 403e9ba commit f886410

File tree

2 files changed

+163
-15
lines changed
  • app/code/Magento/Catalog/Block/Adminhtml/Category/Tab
  • dev/tests/integration/testsuite/Magento/Catalog/Block/Adminhtml/Category/Tab

2 files changed

+163
-15
lines changed

app/code/Magento/Catalog/Block/Adminhtml/Category/Tab/Product.php

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,26 @@
1111
*/
1212
namespace Magento\Catalog\Block\Adminhtml\Category\Tab;
1313

14+
use Magento\Backend\Block\Template\Context;
1415
use Magento\Backend\Block\Widget\Grid;
1516
use Magento\Backend\Block\Widget\Grid\Column;
1617
use Magento\Backend\Block\Widget\Grid\Extended;
18+
use Magento\Backend\Helper\Data;
1719
use Magento\Catalog\Model\Product\Attribute\Source\Status;
1820
use Magento\Catalog\Model\Product\Visibility;
21+
use Magento\Catalog\Model\ProductFactory;
1922
use Magento\Framework\App\ObjectManager;
23+
use Magento\Framework\Registry;
2024

21-
class Product extends \Magento\Backend\Block\Widget\Grid\Extended
25+
class Product extends Extended
2226
{
2327
/**
24-
* Core registry
25-
*
26-
* @var \Magento\Framework\Registry
28+
* @var Registry
2729
*/
2830
protected $_coreRegistry = null;
2931

3032
/**
31-
* @var \Magento\Catalog\Model\ProductFactory
33+
* @var ProductFactory
3234
*/
3335
protected $_productFactory;
3436

@@ -43,19 +45,19 @@ class Product extends \Magento\Backend\Block\Widget\Grid\Extended
4345
private $visibility;
4446

4547
/**
46-
* @param \Magento\Backend\Block\Template\Context $context
47-
* @param \Magento\Backend\Helper\Data $backendHelper
48-
* @param \Magento\Catalog\Model\ProductFactory $productFactory
49-
* @param \Magento\Framework\Registry $coreRegistry
48+
* @param Context $context
49+
* @param Data $backendHelper
50+
* @param ProductFactory $productFactory
51+
* @param Registry $coreRegistry
5052
* @param array $data
5153
* @param Visibility|null $visibility
5254
* @param Status|null $status
5355
*/
5456
public function __construct(
55-
\Magento\Backend\Block\Template\Context $context,
56-
\Magento\Backend\Helper\Data $backendHelper,
57-
\Magento\Catalog\Model\ProductFactory $productFactory,
58-
\Magento\Framework\Registry $coreRegistry,
57+
Context $context,
58+
Data $backendHelper,
59+
ProductFactory $productFactory,
60+
Registry $coreRegistry,
5961
array $data = [],
6062
Visibility $visibility = null,
6163
Status $status = null
@@ -68,6 +70,8 @@ public function __construct(
6870
}
6971

7072
/**
73+
* Initialize object
74+
*
7175
* @return void
7276
*/
7377
protected function _construct()
@@ -79,6 +83,8 @@ protected function _construct()
7983
}
8084

8185
/**
86+
* Get current category
87+
*
8288
* @return array|null
8389
*/
8490
public function getCategory()
@@ -87,6 +93,8 @@ public function getCategory()
8793
}
8894

8995
/**
96+
* Add column filter to collection
97+
*
9098
* @param Column $column
9199
* @return $this
92100
*/
@@ -110,6 +118,8 @@ protected function _addColumnFilterToCollection($column)
110118
}
111119

112120
/**
121+
* Prepare collection.
122+
*
113123
* @return Grid
114124
*/
115125
protected function _prepareCollection()
@@ -136,6 +146,7 @@ protected function _prepareCollection()
136146
'left'
137147
);
138148
$storeId = (int)$this->getRequest()->getParam('store', 0);
149+
$collection->setStoreId($storeId);
139150
if ($storeId > 0) {
140151
$collection->addStoreFilter($storeId);
141152
}
@@ -153,6 +164,8 @@ protected function _prepareCollection()
153164
}
154165

155166
/**
167+
* Prepare columns.
168+
*
156169
* @return Extended
157170
*/
158171
protected function _prepareColumns()
@@ -230,6 +243,8 @@ protected function _prepareColumns()
230243
}
231244

232245
/**
246+
* Retrieve grid reload url
247+
*
233248
* @return string
234249
*/
235250
public function getGridUrl()
@@ -238,6 +253,8 @@ public function getGridUrl()
238253
}
239254

240255
/**
256+
* Get selected products
257+
*
241258
* @return array
242259
*/
243260
protected function _getSelectedProducts()

dev/tests/integration/testsuite/Magento/Catalog/Block/Adminhtml/Category/Tab/ProductTest.php

Lines changed: 133 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,19 @@
99

1010
use Magento\Catalog\Api\CategoryRepositoryInterface;
1111
use Magento\Catalog\Api\Data\CategoryInterface;
12+
use Magento\Catalog\Api\Data\ProductInterface;
1213
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;
1318
use Magento\Framework\Registry;
1419
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;
1523
use Magento\TestFramework\Helper\Bootstrap;
1624
use PHPUnit\Framework\TestCase;
17-
use Magento\Framework\ObjectManagerInterface;
18-
use Magento\Catalog\Api\Data\ProductInterface;
1925

2026
/**
2127
* Checks grid data on the tab 'Products in Category' category view page.
@@ -141,6 +147,102 @@ public function optionsFilterProvider(): array
141147
];
142148
}
143149

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+
144246
/**
145247
* Filter product in grid
146248
*
@@ -174,4 +276,33 @@ private function registerCategory(CategoryInterface $category): void
174276
$this->registry->unregister('category');
175277
$this->registry->register('category', $category);
176278
}
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+
}
177308
}

0 commit comments

Comments
 (0)