Skip to content

Commit 6dd36b1

Browse files
ENGCOM-9391: #33586: fix php warning message at search advansed result and index pages #34995
- Merge Pull Request #34995 from korovitskyi/magento2:33586-fix-php-warning-search-advanced-pages - Merged commits: 1. 7a15c40 2. 9b130c2
2 parents 1d1614b + 9b130c2 commit 6dd36b1

File tree

4 files changed

+138
-33
lines changed

4 files changed

+138
-33
lines changed

app/code/Magento/CatalogSearch/Block/Advanced/Form.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,11 @@
2626
class Form extends Template
2727
{
2828
/**
29-
* Currency factory
30-
*
3129
* @var CurrencyFactory
3230
*/
3331
protected $_currencyFactory;
3432

3533
/**
36-
* Catalog search advanced
37-
*
3834
* @var Advanced
3935
*/
4036
protected $_catalogSearchAdvanced;
@@ -125,15 +121,12 @@ public function getAttributeValidationClass($attribute)
125121
public function getAttributeValue($attribute, $part = null)
126122
{
127123
$value = $this->getRequest()->getQuery($attribute->getAttributeCode());
124+
128125
if ($part && $value) {
129-
if (isset($value[$part])) {
130-
$value = $value[$part];
131-
} else {
132-
$value = '';
133-
}
126+
$value = $value[$part] ?? '';
134127
}
135128

136-
return $value;
129+
return is_array($value) ? '' : $value;
137130
}
138131

139132
/**

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

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,65 +51,47 @@
5151
class Advanced extends \Magento\Framework\Model\AbstractModel
5252
{
5353
/**
54-
* User friendly search criteria list
55-
*
5654
* @var array
5755
*/
5856
protected $_searchCriterias = [];
5957

6058
/**
61-
* Product collection
62-
*
6359
* @var ProductCollection
6460
*/
6561
protected $_productCollection;
6662

6763
/**
68-
* Initialize dependencies
69-
*
7064
* @deprecated 101.0.2
7165
* @var Config
7266
*/
7367
protected $_catalogConfig;
7468

7569
/**
76-
* Catalog product visibility
77-
*
7870
* @var Visibility
7971
*/
8072
protected $_catalogProductVisibility;
8173

8274
/**
83-
* Attribute collection factory
84-
*
8575
* @var AttributeCollectionFactory
8676
*/
8777
protected $_attributeCollectionFactory;
8878

8979
/**
90-
* Store manager
91-
*
9280
* @var \Magento\Store\Model\StoreManagerInterface
9381
*/
9482
protected $_storeManager;
9583

9684
/**
97-
* Product factory
98-
*
9985
* @var ProductFactory
10086
*/
10187
protected $_productFactory;
10288

10389
/**
104-
* Currency factory
105-
*
10690
* @var CurrencyFactory
10791
*/
10892
protected $_currencyFactory;
10993

11094
/**
111-
* Advanced Collection Factory
112-
*
11395
* @deprecated
11496
* @see $collectionProvider
11597
* @var ProductCollectionFactory
@@ -197,12 +179,15 @@ public function addFilters($values)
197179
if (!isset($values[$attribute->getAttributeCode()])) {
198180
continue;
199181
}
200-
if ($attribute->getFrontendInput() == 'text' || $attribute->getFrontendInput() == 'textarea') {
201-
if (!trim($values[$attribute->getAttributeCode()])) {
202-
continue;
203-
}
204-
}
182+
205183
$value = $values[$attribute->getAttributeCode()];
184+
185+
if (($attribute->getFrontendInput() == 'text' || $attribute->getFrontendInput() == 'textarea')
186+
&& (!is_string($value) || !trim($value))
187+
) {
188+
continue;
189+
}
190+
206191
$preparedSearchValue = $this->getPreparedSearchCriteria($attribute, $value);
207192
if (false === $preparedSearchValue) {
208193
continue;
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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\CatalogSearch\Controller\Advanced;
9+
10+
use Magento\TestFramework\TestCase\AbstractController;
11+
use Laminas\Stdlib\Parameters;
12+
13+
/**
14+
* Test cases for catalog advanced index using params.
15+
*
16+
* @magentoDbIsolation disabled
17+
* @magentoAppIsolation enabled
18+
*/
19+
class IndexTest extends AbstractController
20+
{
21+
/**
22+
* Advanced index test by params with the array in params.
23+
*
24+
* @magentoAppArea frontend
25+
* @dataProvider fromParamsInArrayDataProvider
26+
*
27+
* @param array $searchParams
28+
* @return void
29+
*/
30+
public function testExecuteWithArrayInParams(array $searchParams): void
31+
{
32+
$this->getRequest()->setQuery(
33+
$this->_objectManager->create(
34+
Parameters::class,
35+
[
36+
'values' => $searchParams
37+
]
38+
)
39+
);
40+
$this->dispatch('catalogsearch/advanced/index');
41+
$this->assertEquals(200, $this->getResponse()->getStatusCode());
42+
$this->getResponse()->getBody();
43+
}
44+
45+
/**
46+
* Data provider with array in param values.
47+
*
48+
* @return array
49+
*/
50+
public function fromParamsInArrayDataProvider(): array
51+
{
52+
return [
53+
'from_data_with_from_param_is_array' => [
54+
[
55+
'name' => '',
56+
'sku' => '',
57+
'description' => '',
58+
'short_description' => '',
59+
'price' => [
60+
'from' => [],
61+
'to' => 1,
62+
]
63+
]
64+
],
65+
'from_data_with_to_param_is_array' => [
66+
[
67+
'name' => '',
68+
'sku' => '',
69+
'description' => '',
70+
'short_description' => '',
71+
'price' => [
72+
'from' => 0,
73+
'to' => [],
74+
]
75+
]
76+
],
77+
'from_data_with_params_in_array' => [
78+
[
79+
'name' => '',
80+
'sku' => '',
81+
'description' => '',
82+
'short_description' => '',
83+
'price' => [
84+
'from' => ['0' => 1],
85+
'to' => [1],
86+
]
87+
]
88+
],
89+
'from_data_with_params_in_array_in_array' => [
90+
[
91+
'name' => '',
92+
'sku' => '',
93+
'description' => '',
94+
'short_description' => '',
95+
'price' => [
96+
'from' => ['0' => ['0' => 1]],
97+
'to' => 1,
98+
]
99+
]
100+
],
101+
'from_data_with_name_param_is_array' => [
102+
[
103+
'name' => [],
104+
'sku' => '',
105+
'description' => '',
106+
'short_description' => '',
107+
'price' => [
108+
'from' => 0,
109+
'to' => 20,
110+
]
111+
]
112+
]
113+
];
114+
}
115+
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,18 @@ public function searchParamsInArrayDataProvider(): array
219219
'to' => 1,
220220
]
221221
]
222+
],
223+
'search_with_name_param_is_array' => [
224+
[
225+
'name' => [],
226+
'sku' => '',
227+
'description' => '',
228+
'short_description' => '',
229+
'price' => [
230+
'from' => 0,
231+
'to' => 20,
232+
]
233+
]
222234
]
223235
];
224236
}

0 commit comments

Comments
 (0)