Skip to content

Commit 27bd7c3

Browse files
committed
ACP2E-464: Remove wildcard and LIKE operator and replace with equal operator in customer grid filter search
1 parent 136bf3e commit 27bd7c3

File tree

7 files changed

+76
-20
lines changed

7 files changed

+76
-20
lines changed

app/code/Magento/Customer/Model/Config/Source/FilterConditionType.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
class FilterConditionType implements OptionSourceInterface
1313
{
1414
public const PARTIAL_MATCH = 0;
15-
1615
public const FULL_MATCH = 1;
16+
public const PREFIX_MATCH = 2;
1717

1818
/**
1919
* @inheritdoc
@@ -22,7 +22,8 @@ public function toOptionArray()
2222
{
2323
return [
2424
['value' => self::PARTIAL_MATCH, 'label' => __('Partial Match')],
25-
['value' => self::FULL_MATCH, 'label' => __('Full Match')]
25+
['value' => self::PREFIX_MATCH, 'label' => __('Prefix Match')],
26+
['value' => self::FULL_MATCH, 'label' => __('Full Match')],
2627
];
2728
}
2829
}

app/code/Magento/Customer/Model/Indexer/AttributeProvider.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,18 +95,14 @@ protected function convert(array $attributes, array $fieldset)
9595
'dataType' => $attribute->getBackendType(),
9696
'filters' => [],
9797
'entity' => static::ENTITY,
98-
'bind' => isset($fieldset['references']['customer']['to'])
99-
? $fieldset['references']['customer']['to']
100-
: null,
101-
'index' => $attribute->canBeFilterableInGrid()
102-
&& (int) $attribute->getGridFilterConditionType() === FilterConditionType::FULL_MATCH
98+
'bind' => $fieldset['references']['customer']['to'] ?? null,
99+
'index' => $this->getIndex($attribute)
103100
];
104101
}
105102
} else {
106103
$fields[$attribute->getName()] = [
107104
'type' => $this->getType($attribute),
108-
'index' => $attribute->canBeFilterableInGrid()
109-
&& (int) $attribute->getGridFilterConditionType() === FilterConditionType::FULL_MATCH
105+
'index' => $this->getIndex($attribute)
110106
];
111107
}
112108
}
@@ -156,4 +152,20 @@ protected function merge(array $dataFields, array $searchableFields)
156152

157153
return $dataFields;
158154
}
155+
156+
/**
157+
* Checks whether the attribute should be indexed
158+
*
159+
* @param Attribute $attribute
160+
* @return bool
161+
*/
162+
private function getIndex(Attribute $attribute): bool
163+
{
164+
return $attribute->canBeFilterableInGrid()
165+
&& in_array(
166+
(int) $attribute->getGridFilterConditionType(),
167+
[FilterConditionType::FULL_MATCH, FilterConditionType::PREFIX_MATCH],
168+
true
169+
);
170+
}
159171
}

app/code/Magento/Customer/Test/Unit/Ui/Component/Listing/Filter/TextFilterConfigProviderTest.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,35 @@ public function getConfigDataProvider(): array
3030
[
3131
[],
3232
[
33-
'conditionType' => 'like'
33+
'conditionType' => 'like',
34+
'valueExpression' => '%%%s%%'
3435
]
3536
],
3637
[
3738
[
3839
'grid_filter_condition_type' => 0
3940
],
4041
[
41-
'conditionType' => 'like'
42+
'conditionType' => 'like',
43+
'valueExpression' => '%%%s%%'
4244
]
4345
],
4446
[
4547
[
4648
'grid_filter_condition_type' => 1
4749
],
4850
[
49-
'conditionType' => 'eq'
51+
'conditionType' => 'eq',
52+
'valueExpression' => null
53+
]
54+
],
55+
[
56+
[
57+
'grid_filter_condition_type' => 2
58+
],
59+
[
60+
'conditionType' => 'like',
61+
'valueExpression' => '%s%%'
5062
]
5163
]
5264
];

app/code/Magento/Customer/Ui/Component/Listing/Filter/TextFilterConfigProvider.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,25 @@ class TextFilterConfigProvider implements FilterConfigProviderInterface
1616
private const FILTER_CONDITION_TYPE_MAP = [
1717
FilterConditionType::FULL_MATCH => 'eq',
1818
FilterConditionType::PARTIAL_MATCH => 'like',
19+
FilterConditionType::PREFIX_MATCH => 'like',
20+
];
21+
22+
private const FILTER_CONDITION_TYPE_VALUE_EXPRESSION_MAP = [
23+
FilterConditionType::PARTIAL_MATCH => '%%%s%%',
24+
FilterConditionType::PREFIX_MATCH => '%s%%',
1925
];
2026

2127
/**
2228
* @inheritdoc
2329
*/
2430
public function getConfig(array $attributeData): array
2531
{
26-
$value = $attributeData[self::FILTER_CONDITION_TYPE] ?? null;
32+
$value = $attributeData[self::FILTER_CONDITION_TYPE] ?? FilterConditionType::PARTIAL_MATCH;
2733

2834
return [
2935
'conditionType' => self::FILTER_CONDITION_TYPE_MAP[$value]
30-
?? self::FILTER_CONDITION_TYPE_MAP[FilterConditionType::PARTIAL_MATCH]
36+
?? self::FILTER_CONDITION_TYPE_MAP[FilterConditionType::PARTIAL_MATCH],
37+
'valueExpression' => self::FILTER_CONDITION_TYPE_VALUE_EXPRESSION_MAP[$value] ?? null,
3138
];
3239
}
3340
}

app/code/Magento/Customer/i18n/en_US.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,3 +545,4 @@ Addresses,Addresses
545545
"The specified customer group id does not exist.","The specified customer group id does not exist."
546546
"Partial Match","Partial Match"
547547
"Full Match","Full Match"
548+
"Prefix Match","Prefix Match"

app/code/Magento/Ui/Component/Filters/Type/Input.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Input extends AbstractFilter
1717

1818
public const COMPONENT = 'input';
1919

20-
private const DEFAULT_CONDITION_TYPE = 'like';
20+
private const CONDITION_LIKE = 'like';
2121

2222
/**
2323
* @var ElementInput
@@ -66,13 +66,19 @@ protected function applyFilter(): void
6666
{
6767
$value = $this->filterData[$this->getName()] ?? '';
6868
if (strlen($value) > 0) {
69-
$conditionType = self::DEFAULT_CONDITION_TYPE;
69+
$conditionType = self::CONDITION_LIKE;
70+
$valueExpression = null;
7071
$filterConfig = $this->getData('config/filter');
71-
if (is_array($filterConfig) && isset($filterConfig['conditionType'])) {
72-
$conditionType = $filterConfig['conditionType'];
72+
if (is_array($filterConfig)) {
73+
$conditionType = $filterConfig['conditionType'] ?? null;
74+
$valueExpression = $filterConfig['valueExpression'] ?? null;
7375
}
74-
if ($conditionType === self::DEFAULT_CONDITION_TYPE) {
75-
$value = sprintf('%%%s%%', str_replace(['%', '_'], ['\%', '\_'], $value));
76+
if ($conditionType === self::CONDITION_LIKE) {
77+
$value = str_replace(['%', '_'], ['\%', '\_'], $value);
78+
$valueExpression = $valueExpression ?? '%%%s%%';
79+
}
80+
if ($valueExpression) {
81+
$value = sprintf($valueExpression, $value);
7682
}
7783

7884
$filter = $this->filterBuilder->setConditionType($conditionType)

app/code/Magento/Ui/Test/Unit/Component/Filters/Type/InputTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,23 @@ public function getPrepareDataProvider(): array
253253
'setValue' => '%something%',
254254
],
255255
],
256+
[
257+
[
258+
'name' => 'text_attr',
259+
'config' => [
260+
'filter' => [
261+
'filterType' => 'text',
262+
'conditionType' => 'like',
263+
'valueExpression' => '%s%%'
264+
]
265+
]
266+
],
267+
['text_attr' => 'something'],
268+
[
269+
'setConditionType' => 'like',
270+
'setValue' => 'something%',
271+
],
272+
],
256273
];
257274
}
258275
}

0 commit comments

Comments
 (0)