Skip to content

Commit 21eebde

Browse files
committed
Merge remote-tracking branch 'origin/imported-magento-magento2-31060' into 2.4-develop-pr121
2 parents f512f33 + 917dccc commit 21eebde

File tree

6 files changed

+187
-5
lines changed

6 files changed

+187
-5
lines changed

app/code/Magento/AdvancedPricingImportExport/Controller/Adminhtml/Export/GetFilter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public function execute()
3434
/** @var $export \Magento\ImportExport\Model\Export */
3535
$export = $this->_objectManager->create(\Magento\ImportExport\Model\Export::class);
3636
$export->setData($data);
37-
$export->filterAttributeCollection(
38-
$attrFilterBlock->prepareCollection($export->getEntityAttributeCollection())
37+
$attrFilterBlock->prepareCollection(
38+
$export->filterAttributeCollection($export->getEntityAttributeCollection())
3939
);
4040
return $resultLayout;
4141
} catch (\Exception $e) {

app/code/Magento/ImportExport/Block/Adminhtml/Export/Filter.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
use Magento\Eav\Model\Entity\Attribute;
99
use Magento\Catalog\Api\Data\ProductAttributeInterface;
10+
use Magento\Framework\App\ObjectManager;
11+
use Magento\ImportExport\Model\ResourceModel\Export\AttributeGridCollectionFactory;
1012

1113
/**
1214
* Export filter block
@@ -40,20 +42,29 @@ class Filter extends \Magento\Backend\Block\Widget\Grid\Extended
4042
'updated_at' => \Magento\ImportExport\Model\Export::FILTER_TYPE_DATE,
4143
];
4244

45+
/**
46+
* @var AttributeGridCollectionFactory
47+
*/
48+
private $attributeGridCollectionFactory;
49+
4350
/**
4451
* @param \Magento\Backend\Block\Template\Context $context
4552
* @param \Magento\Backend\Helper\Data $backendHelper
4653
* @param \Magento\ImportExport\Helper\Data $importExportData
4754
* @param array $data
55+
* @param AttributeGridCollectionFactory|null $attributeGridCollectionFactory
4856
*/
4957
public function __construct(
5058
\Magento\Backend\Block\Template\Context $context,
5159
\Magento\Backend\Helper\Data $backendHelper,
5260
\Magento\ImportExport\Helper\Data $importExportData,
53-
array $data = []
61+
array $data = [],
62+
?AttributeGridCollectionFactory $attributeGridCollectionFactory = null
5463
) {
5564
$this->_importExportData = $importExportData;
5665
parent::__construct($context, $backendHelper, $data);
66+
$this->attributeGridCollectionFactory = $attributeGridCollectionFactory
67+
?: ObjectManager::getInstance()->get(AttributeGridCollectionFactory::class);
5768
}
5869

5970
/**
@@ -426,7 +437,10 @@ public function getRowUrl($row)
426437
*/
427438
public function prepareCollection(\Magento\Framework\Data\Collection $collection)
428439
{
429-
$this->setCollection($collection);
430-
return $this->getCollection();
440+
$attributeGridCollection = $this->attributeGridCollectionFactory->create();
441+
$gridCollection = $attributeGridCollection->setItems($collection->getItems());
442+
$this->setCollection($gridCollection);
443+
444+
return $collection;
431445
}
432446
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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\ImportExport\Model\ResourceModel\Export;
9+
10+
use Magento\Framework\Data\Collection;
11+
use Magento\Framework\Phrase;
12+
13+
/**
14+
* Association of attributes for grid
15+
*/
16+
class AttributeGridCollection extends Collection
17+
{
18+
private const FILTERED_FLAG_NAME = 'agc_filtered';
19+
20+
/**
21+
* Adding item to collection
22+
*
23+
* @param array $items
24+
* @return $this
25+
*/
26+
public function setItems(array $items): self
27+
{
28+
foreach ($items as $item) {
29+
$this->addItem($item);
30+
}
31+
32+
return $this;
33+
}
34+
35+
/**
36+
* @inheritDoc
37+
*/
38+
public function getSize(): int
39+
{
40+
return count($this->getItems());
41+
}
42+
43+
/**
44+
* @inheritDoc
45+
*/
46+
public function addFieldToFilter($field, $condition)
47+
{
48+
if (isset($condition['like'])) {
49+
$value = $this->unescapeLikeValue((string)$condition['like']);
50+
$this->addFilter($field, $value);
51+
}
52+
53+
return $this;
54+
}
55+
56+
/**
57+
* @inheritDoc
58+
*/
59+
public function load($printQuery = false, $logQuery = false)
60+
{
61+
$this->filterCollection();
62+
$this->sortCollectionByAttributeCode();
63+
64+
return $this;
65+
}
66+
67+
/**
68+
* Add filters to collection
69+
*
70+
* @return $this
71+
*/
72+
private function filterCollection()
73+
{
74+
if (!$this->getFlag(self::FILTERED_FLAG_NAME) && !empty($this->_filters)) {
75+
foreach ($this->_filters as $filter) {
76+
foreach ($this->_items as $item) {
77+
$field = $item->getData($filter->getData('field')) ?? '';
78+
if ($field instanceof Phrase) {
79+
$field = (string)$field;
80+
}
81+
if (stripos($field, $filter->getData('value')) === false) {
82+
$this->removeItemByKey($item->getId());
83+
}
84+
}
85+
}
86+
$this->setFlag(self::FILTERED_FLAG_NAME, true);
87+
}
88+
89+
return $this;
90+
}
91+
92+
/**
93+
* Sort collection by attribute code
94+
*
95+
* @return $this
96+
*/
97+
private function sortCollectionByAttributeCode()
98+
{
99+
$sortOrder = $this->_orders['attribute_code'];
100+
uasort($this->_items, function ($a, $b) use ($sortOrder) {
101+
$cmp = strnatcmp($a->getData('attribute_code'), $b->getData('attribute_code'));
102+
103+
return $sortOrder === self::SORT_ORDER_ASC ? $cmp : -$cmp;
104+
});
105+
106+
return $this;
107+
}
108+
109+
/**
110+
* Unescape 'like' value from condition
111+
*
112+
* @param string $likeValue
113+
* @return string
114+
*/
115+
private function unescapeLikeValue(string $likeValue): string
116+
{
117+
$replaceFrom = ['\\\\', '\_', '\%'];
118+
$replaceTo = ['\\', '_', '%'];
119+
$value = trim($likeValue, "'%");
120+
$value = str_replace($replaceFrom, $replaceTo, $value);
121+
122+
return $value;
123+
}
124+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
10+
<actionGroup name="AdminExportAttributeGridFillColumnFilterFieldsAndApplyActionGroup">
11+
<arguments>
12+
<argument name="attributeLabel" type="string" defaultValue=""/>
13+
<argument name="attributeCode" type="string" defaultValue=""/>
14+
</arguments>
15+
16+
<waitForElementVisible selector="{{AdminExportAttributeSection.filterByFrontLabel}}" stepKey="seeFilterByFrontLabel"/>
17+
<fillField selector="{{AdminExportAttributeSection.filterByFrontLabel}}" userInput="{{attributeLabel}}" stepKey="fillAttributeLabelField"/>
18+
<fillField selector="{{AdminExportAttributeSection.filterByAttributeCode}}" userInput="{{attributeCode}}" stepKey="fillAttributeCodeField"/>
19+
<click selector="{{AdminExportAttributeSection.search}}" stepKey="clickSearch"/>
20+
</actionGroup>
21+
</actionGroups>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
10+
<actionGroup name="AssertAdminExportFilterGridFirstRowContainsAttributeCodeActionGroup">
11+
<arguments>
12+
<argument name="attributeCode" type="string" defaultValue="allow_message"/>
13+
</arguments>
14+
15+
<grabTextFrom selector="{{AdminExportAttributeSection.rowAttributeCodeCell('1')}}" stepKey="getTextFirstRowContainsAttributeCode"/>
16+
<assertStringContainsString stepKey="checkTextInFirstRowContainsAttributeCode">
17+
<actualResult type="variable">getTextFirstRowContainsAttributeCode</actualResult>
18+
<expectedResult type="string">{{attributeCode}}</expectedResult>
19+
</assertStringContainsString>
20+
</actionGroup>
21+
</actionGroups>

app/code/Magento/ImportExport/Test/Mftf/Section/AdminExportAttributeSection.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Page/etc/SectionObject.xsd">
1010
<section name="AdminExportAttributeSection">
1111
<element name="filterByAttributeCode" type="input" selector="#export_filter_grid_filter_attribute_code"/>
12+
<element name="filterByFrontLabel" type="input" selector="#export_filter_grid_filter_frontend_label"/>
1213
<element name="resetFilter" type="button" selector="button[data-action='grid-filter-reset']" timeout="30"/>
1314
<element name="search" type="button" selector="button[data-action=grid-filter-apply]" timeout="30"/>
15+
<element name="rowAttributeCodeCell" type="block" selector="//table[@id='export_filter_grid_table']/tbody/tr[{{row}}]/td[contains(@class,'col-attribute_code')]" parameterized="true"/>
1416
<element name="chooseAttribute" type="checkbox" selector="//*[@name='export_filter[{{var}}]']/ancestor::tr//input[@type='checkbox']" parameterized="true"/>
1517
<element name="fillFilter" type="input" selector="//*[@name='export_filter[{{var}}]']/ancestor::tr//input[@type='text']" parameterized="true"/>
1618
<element name="continueBtn" type="button" selector="//*[@id='export_filter_container']/button" timeout="30"/>

0 commit comments

Comments
 (0)