Skip to content

Commit d25aba7

Browse files
author
silinmykola
committed
33589 change advanced search model and add test
1 parent 58adfbf commit d25aba7

File tree

2 files changed

+95
-19
lines changed
  • app/code/Magento/CatalogSearch/Model
  • dev/tests/integration/testsuite/Magento/CatalogSearch/Controller/Advanced

2 files changed

+95
-19
lines changed

app/code/Magento/CatalogSearch/Model/Advanced.php

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,12 @@ public function addFilters($values)
210210
$this->addSearchCriteria($attribute, $preparedSearchValue);
211211

212212
if ($attribute->getAttributeCode() == 'price') {
213+
foreach ($value as $key => $element) {
214+
if (is_array($element)) {
215+
$value[$key] = null;
216+
}
217+
}
218+
213219
$rate = 1;
214220
$store = $this->_storeManager->getStore();
215221
$currency = $store->getCurrentCurrencyCode();
@@ -351,13 +357,15 @@ protected function addSearchCriteria($attribute, $value)
351357
/**
352358
* Add data about search criteria to object state
353359
*
354-
* @todo: Move this code to block
355-
*
356360
* @param EntityAttribute $attribute
357361
* @param mixed $value
362+
*
358363
* @return string|bool
359364
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
360365
* @SuppressWarnings(PHPMD.NPathComplexity)
366+
* @throws LocalizedException
367+
* @todo: Move this code to block
368+
*
361369
*/
362370
protected function getPreparedSearchCriteria($attribute, $value)
363371
{
@@ -368,13 +376,14 @@ protected function getPreparedSearchCriteria($attribute, $value)
368376
if (!empty($value['from']) || !empty($value['to'])) {
369377
$from = '';
370378
$to = '';
371-
if (is_array($value['from'])) {
372-
$value['from'] = $this->getFirstArrayElement($value['from']);
373-
}
374-
if (is_array($value['to'])) {
375-
$value['to'] = $this->getFirstArrayElement($value['to']);
379+
380+
foreach ($value as $key => $element) {
381+
if (is_array($element)) {
382+
$value[$key] = null;
383+
}
376384
}
377385

386+
378387
if (isset($value['currency'])) {
379388
/** @var $currencyModel Currency */
380389
$currencyModel = $this->_currencyFactory->create()->load($value['currency']);
@@ -440,16 +449,4 @@ public function getSearchCriterias()
440449
{
441450
return $this->_searchCriterias;
442451
}
443-
444-
/**
445-
* Return first value in array
446-
*
447-
* @param $value
448-
*
449-
* @return mixed
450-
*/
451-
private function getFirstArrayElement($value)
452-
{
453-
return is_array($value) ? $this->getFirstArrayElement(array_shift($value)) : $value;
454-
}
455452
}

dev/tests/integration/testsuite/Magento/CatalogSearch/Controller/Advanced/ResultTest.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,85 @@ public function testExecuteWithUnderscore(): void
134134
$this->assertStringContainsString('name_simple_product', $responseBody);
135135
}
136136

137+
/**
138+
* Advanced search with array in price parameters
139+
*
140+
* @magentoAppArea frontend
141+
* @magentoDataFixture Magento/CatalogSearch/_files/product_for_search.php
142+
* @magentoDataFixture Magento/CatalogSearch/_files/full_reindex.php
143+
* @dataProvider searchParamsWithArrayDataProvider
144+
*
145+
* @param array $searchParams
146+
* @return void
147+
*/
148+
public function testExecuteWithArrayInParams(array $searchParams): void
149+
{
150+
$this->getRequest()->setQuery(
151+
$this->_objectManager->create(
152+
Parameters::class,
153+
[
154+
'values' => $searchParams
155+
]
156+
)
157+
);
158+
$this->dispatch('catalogsearch/advanced/result');
159+
$this->assertEquals(200, $this->getResponse()->getStatusCode());
160+
$responseBody = $this->getResponse()->getBody();
161+
$this->assertStringContainsString(
162+
'We can\'t find any items matching these search criteria',
163+
$responseBody
164+
);
165+
}
166+
167+
/**
168+
* Data provider with arrays in param values
169+
*
170+
* @return array
171+
*/
172+
public function searchParamsWithArrayDataProvider(): array
173+
{
174+
return [
175+
'search_with_empty_arrays' => [
176+
[
177+
'name' => '',
178+
'sku' => '',
179+
'description' => '',
180+
'short_description' => '',
181+
'price' => [
182+
'from' => [
183+
1
184+
],
185+
'to' => 1,
186+
]
187+
]
188+
],
189+
'search_with_params_in_array' => [
190+
[
191+
'name' => '',
192+
'sku' => '',
193+
'description' => '',
194+
'short_description' => '',
195+
'price' => [
196+
'from' => ['0' => 1],
197+
'to' => [1],
198+
]
199+
]
200+
],
201+
'search_with_params_in_array_in_array' => [
202+
[
203+
'name' => '',
204+
'sku' => '',
205+
'description' => '',
206+
'short_description' => '',
207+
'price' => [
208+
['from' => ['0' => 1]],
209+
['to' => 1],
210+
]
211+
]
212+
]
213+
];
214+
}
215+
137216
/**
138217
* Data provider with strings for quick search.
139218
*

0 commit comments

Comments
 (0)