Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

33589 fix array values in catalogsearch parameters #33682

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions app/code/Magento/CatalogSearch/Model/Advanced.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,12 @@ public function addFilters($values)
$this->addSearchCriteria($attribute, $preparedSearchValue);

if ($attribute->getAttributeCode() == 'price') {
foreach ($value as $key => $element) {
if (is_array($element)) {
$value[$key] = 0;
}
}

$rate = 1;
$store = $this->_storeManager->getStore();
$currency = $store->getCurrentCurrencyCode();
Expand Down Expand Up @@ -351,23 +357,31 @@ protected function addSearchCriteria($attribute, $value)
/**
* Add data about search criteria to object state
*
* @todo: Move this code to block
*
* @param EntityAttribute $attribute
* @param mixed $value
*
* @return string|bool
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @throws LocalizedException
* @todo: Move this code to block
*
*/
protected function getPreparedSearchCriteria($attribute, $value)
{
$from = null;
$to = null;
if (is_array($value)) {
foreach ($value as $key => $element) {
if (is_array($element)) {
$value[$key] = '';
}
}
if (isset($value['from']) && isset($value['to'])) {
if (!empty($value['from']) || !empty($value['to'])) {
$from = '';
$to = '';

if (isset($value['currency'])) {
/** @var $currencyModel Currency */
$currencyModel = $this->_currencyFactory->create()->load($value['currency']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
* See COPYING.txt for license details.
*/

/** This changes need to valid applying filters and configuration before search process is started. */
use Magento\CatalogSearch\Block\Result;

/** These changes need to valid applying filters and configuration before search process is started. */

/** @var $block Result*/
$productList = $block->getProductListHtml();
?>
<?php if ($block->getResultCount()) : ?>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,95 @@ public function testExecuteWithUnderscore(): void
$this->assertStringContainsString('name_simple_product', $responseBody);
}

/**
* Advanced search with array in price parameters
*
* @magentoAppArea frontend
* @magentoDataFixture Magento/CatalogSearch/_files/product_for_search.php
* @magentoDataFixture Magento/CatalogSearch/_files/full_reindex.php
* @dataProvider searchParamsInArrayDataProvider
*
* @param array $searchParams
* @return void
*/
public function testExecuteWithArrayInParam(array $searchParams): void
{
$this->getRequest()->setQuery(
$this->_objectManager->create(
Parameters::class,
[
'values' => $searchParams
]
)
);
$this->dispatch('catalogsearch/advanced/result');
$this->assertEquals(200, $this->getResponse()->getStatusCode());
$responseBody = $this->getResponse()->getBody();
$this->assertStringContainsString(
'We can&#039;t find any items matching these search criteria.',
$responseBody
);
}

/**
* Data provider with array in params values
*
* @return array
*/
public function searchParamsInArrayDataProvider(): array
{
return [
'search_with_from_param_is_array' => [
[
'name' => '',
'sku' => '',
'description' => '',
'short_description' => '',
'price' => [
'from' => [],
'to' => 1,
]
]
],
'search_with_to_param_is_array' => [
[
'name' => '',
'sku' => '',
'description' => '',
'short_description' => '',
'price' => [
'from' => 0,
'to' => [],
]
]
],
'search_with_params_in_array' => [
[
'name' => '',
'sku' => '',
'description' => '',
'short_description' => '',
'price' => [
'from' => ['0' => 1],
'to' => [1],
]
]
],
'search_with_params_in_array_in_array' => [
[
'name' => '',
'sku' => '',
'description' => '',
'short_description' => '',
'price' => [
'from' => ['0' => ['0' => 1]],
'to' => 1,
]
]
]
];
}

/**
* Data provider with strings for quick search.
*
Expand Down