Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit 14e095b

Browse files
author
Stanislav Idolov
authored
ENGCOM-1060: [Backport 2.3] Added Visibility and Status filter to category product grid #12577
2 parents 9633761 + 5c4c20d commit 14e095b

File tree

4 files changed

+159
-1
lines changed

4 files changed

+159
-1
lines changed

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

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
use Magento\Backend\Block\Widget\Grid;
1515
use Magento\Backend\Block\Widget\Grid\Column;
1616
use Magento\Backend\Block\Widget\Grid\Extended;
17+
use Magento\Catalog\Model\Product\Attribute\Source\Status;
18+
use Magento\Catalog\Model\Product\Visibility;
19+
use Magento\Framework\App\ObjectManager;
1720

1821
class Product extends \Magento\Backend\Block\Widget\Grid\Extended
1922
{
@@ -29,22 +32,38 @@ class Product extends \Magento\Backend\Block\Widget\Grid\Extended
2932
*/
3033
protected $_productFactory;
3134

35+
/**
36+
* @var Status
37+
*/
38+
private $status;
39+
40+
/**
41+
* @var Visibility
42+
*/
43+
private $visibility;
44+
3245
/**
3346
* @param \Magento\Backend\Block\Template\Context $context
3447
* @param \Magento\Backend\Helper\Data $backendHelper
3548
* @param \Magento\Catalog\Model\ProductFactory $productFactory
3649
* @param \Magento\Framework\Registry $coreRegistry
3750
* @param array $data
51+
* @param Visibility|null $visibility
52+
* @param Status|null $status
3853
*/
3954
public function __construct(
4055
\Magento\Backend\Block\Template\Context $context,
4156
\Magento\Backend\Helper\Data $backendHelper,
4257
\Magento\Catalog\Model\ProductFactory $productFactory,
4358
\Magento\Framework\Registry $coreRegistry,
44-
array $data = []
59+
array $data = [],
60+
Visibility $visibility = null,
61+
Status $status = null
4562
) {
4663
$this->_productFactory = $productFactory;
4764
$this->_coreRegistry = $coreRegistry;
65+
$this->visibility = $visibility ?: ObjectManager::getInstance()->get(Visibility::class);
66+
$this->status = $status ?: ObjectManager::getInstance()->get(Status::class);
4867
parent::__construct($context, $backendHelper, $data);
4968
}
5069

@@ -102,6 +121,10 @@ protected function _prepareCollection()
102121
'name'
103122
)->addAttributeToSelect(
104123
'sku'
124+
)->addAttributeToSelect(
125+
'visibility'
126+
)->addAttributeToSelect(
127+
'status'
105128
)->addAttributeToSelect(
106129
'price'
107130
)->joinField(
@@ -159,6 +182,28 @@ protected function _prepareColumns()
159182
);
160183
$this->addColumn('name', ['header' => __('Name'), 'index' => 'name']);
161184
$this->addColumn('sku', ['header' => __('SKU'), 'index' => 'sku']);
185+
$this->addColumn(
186+
'visibility',
187+
[
188+
'header' => __('Visibility'),
189+
'index' => 'visibility',
190+
'type' => 'options',
191+
'options' => $this->visibility->getOptionArray(),
192+
'header_css_class' => 'col-visibility',
193+
'column_css_class' => 'col-visibility'
194+
]
195+
);
196+
197+
$this->addColumn(
198+
'status',
199+
[
200+
'header' => __('Status'),
201+
'index' => 'status',
202+
'type' => 'options',
203+
'options' => $this->status->getOptionArray()
204+
]
205+
);
206+
162207
$this->addColumn(
163208
'price',
164209
[

dev/tests/functional/tests/app/Magento/Catalog/Test/Block/Adminhtml/Category/Edit/Section/ProductGrid.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ class ProductGrid extends Grid
2929
'name' => [
3030
'selector' => '#catalog_category_products_filter_name',
3131
],
32+
'visibility' => [
33+
'selector' => '#catalog_category_products_filter_visibility',
34+
'input' => 'select',
35+
],
36+
'status' => [
37+
'selector' => '#catalog_category_products_filter_status',
38+
'input' => 'select',
39+
],
3240
];
3341

3442
/**
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Catalog\Test\Constraint;
10+
11+
use Magento\Catalog\Test\Fixture\Category;
12+
use Magento\Catalog\Test\Page\Adminhtml\CatalogCategoryEdit;
13+
use Magento\Catalog\Test\Page\Adminhtml\CatalogCategoryIndex;
14+
use Magento\Mtf\Constraint\AbstractConstraint;
15+
16+
/**
17+
* Assert that category products grid filter works correctly.
18+
*/
19+
class AssertCategoryProductsGridFilter extends AbstractConstraint
20+
{
21+
/**
22+
* Grid columns for tests
23+
*
24+
* @var array
25+
*/
26+
private $testFilterColumns = [
27+
'visibility',
28+
];
29+
30+
/**
31+
* Assert that category products grid filter works correctly.
32+
*
33+
* @param CatalogCategoryIndex $catalogCategoryIndex
34+
* @param CatalogCategoryEdit $catalogCategoryEdit
35+
* @param Category $category
36+
* @return void
37+
*/
38+
public function processAssert(
39+
CatalogCategoryIndex $catalogCategoryIndex,
40+
CatalogCategoryEdit $catalogCategoryEdit,
41+
Category $category
42+
) {
43+
$catalogCategoryIndex->getTreeCategories()->selectCategory($category, true);
44+
$categoryProducts = $category->getDataFieldConfig('category_products')['source']->getProducts();
45+
$catalogCategoryEdit->getEditForm()->openSection('category_products');
46+
47+
foreach ($this->testFilterColumns as $field) {
48+
$this->testGridFilter($categoryProducts, $catalogCategoryEdit, $field);
49+
}
50+
}
51+
52+
/**
53+
* @param array $categoryProducts
54+
* @param CatalogCategoryEdit $catalogCategoryEdit
55+
* @param string $filterField
56+
* @return void
57+
*/
58+
private function testGridFilter(array $categoryProducts, CatalogCategoryEdit $catalogCategoryEdit, $filterField)
59+
{
60+
$expectedProducts = [];
61+
foreach ($categoryProducts as $product) {
62+
$expectedProducts[$product->getData('name')] = [
63+
'filter' => $filterField,
64+
'value' => $product->getData($filterField)
65+
];
66+
}
67+
68+
$actualProducts = [];
69+
/** @var \Magento\Catalog\Test\Block\Adminhtml\Category\Edit\CategoryForm $productsFieldset */
70+
$productsFieldset = $catalogCategoryEdit->getEditForm()->getSection('category_products');
71+
$gridRows = $productsFieldset->getProductGrid()->getRowsData(['name', $filterField]);
72+
foreach ($gridRows as $row) {
73+
$actualProducts[$row['name']] = [
74+
'filter' => $filterField,
75+
'value' => $row[$filterField]
76+
];
77+
}
78+
79+
\PHPUnit\Framework\Assert::assertEquals(
80+
$expectedProducts,
81+
$actualProducts,
82+
"Category products grid filter '$filterField' does not work correctly"
83+
);
84+
}
85+
86+
/**
87+
* Returns a string representation of the object.
88+
*
89+
* @return string
90+
*/
91+
public function toString() : string
92+
{
93+
return 'Category products grid filter works correctly';
94+
}
95+
}

dev/tests/functional/tests/app/Magento/Catalog/Test/TestCase/Category/CreateCategoryEntityTest.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,5 +175,15 @@
175175
<constraint name="Magento\Catalog\Test\Constraint\AssertCategorySaveMessage" />
176176
<constraint name="Magento\Catalog\Test\Constraint\AssertCategoryOnCustomWebsite" />
177177
</variation>
178+
<variation name="CreateCategoryEntityTestVariation11_ProductsGridFilter" summary="Apply category products grid filter">
179+
<data name="addCategory" xsi:type="string">addSubcategory</data>
180+
<data name="category/data/parent_id/dataset" xsi:type="string">default_category</data>
181+
<data name="category/data/is_active" xsi:type="string">Yes</data>
182+
<data name="category/data/include_in_menu" xsi:type="string">Yes</data>
183+
<data name="category/data/name" xsi:type="string">Subcategory%isolation%</data>
184+
<data name="category/data/category_products/dataset" xsi:type="string">catalogProductSimple::default, catalogProductSimple::not_visible_individually</data>
185+
<constraint name="Magento\Catalog\Test\Constraint\AssertCategorySaveMessage" />
186+
<constraint name="Magento\Catalog\Test\Constraint\AssertCategoryProductsGridFilter" />
187+
</variation>
178188
</testCase>
179189
</config>

0 commit comments

Comments
 (0)