Skip to content

Commit d4c8287

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-51538' into PR
2 parents d719480 + 17d58de commit d4c8287

File tree

19 files changed

+364
-131
lines changed

19 files changed

+364
-131
lines changed

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
use Magento\Catalog\Model\Product;
99
use Magento\Framework\Api\FilterBuilder;
1010
use Magento\Framework\Api\Search\SearchCriteriaBuilder;
11+
use Magento\Framework\Api\Search\SearchResultFactory;
12+
use Magento\Framework\Exception\LocalizedException;
1113
use Magento\Framework\Search\Adapter\Mysql\TemporaryStorage;
14+
use Magento\Framework\Search\Request\EmptyRequestDataException;
15+
use Magento\Framework\Search\Request\NonExistingRequestNameException;
1216

1317
/**
1418
* Collection Advanced
@@ -39,6 +43,11 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection
3943
*/
4044
private $searchCriteriaBuilder;
4145

46+
/**
47+
* @var SearchResultFactory
48+
*/
49+
private $searchResultFactory;
50+
4251
/**
4352
* @var FilterBuilder
4453
*/
@@ -69,6 +78,7 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection
6978
* @param \Magento\Search\Model\SearchEngine $searchEngine
7079
* @param \Magento\Framework\Search\Adapter\Mysql\TemporaryStorageFactory $temporaryStorageFactory
7180
* @param \Zend_Db_Adapter_Abstract $connection
81+
* @param SearchResultFactory $searchResultFactory
7282
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
7383
*/
7484
public function __construct(
@@ -94,11 +104,16 @@ public function __construct(
94104
\Magento\CatalogSearch\Model\Advanced\Request\Builder $requestBuilder,
95105
\Magento\Search\Model\SearchEngine $searchEngine,
96106
\Magento\Framework\Search\Adapter\Mysql\TemporaryStorageFactory $temporaryStorageFactory,
97-
$connection = null
107+
$connection = null,
108+
SearchResultFactory $searchResultFactory = null
98109
) {
99110
$this->requestBuilder = $requestBuilder;
100111
$this->searchEngine = $searchEngine;
101112
$this->temporaryStorageFactory = $temporaryStorageFactory;
113+
if ($searchResultFactory === null) {
114+
$this->searchResultFactory = \Magento\Framework\App\ObjectManager::getInstance()
115+
->get('Magento\Framework\Api\Search\SearchResultFactory');
116+
}
102117
parent::__construct(
103118
$entityFactory,
104119
$logger,
@@ -152,8 +167,15 @@ protected function _renderFiltersBefore()
152167
}
153168
$searchCriteria = $this->getSearchCriteriaBuilder()->create();
154169
$searchCriteria->setRequestName('advanced_search_container');
155-
$searchResult = $this->getSearch()->search($searchCriteria);
156-
170+
try {
171+
$searchResult = $this->getSearch()->search($searchCriteria);
172+
} catch (EmptyRequestDataException $e) {
173+
/** @var \Magento\Framework\Api\Search\SearchResultInterface $searchResult */
174+
$searchResult = $this->searchResultFactory->create()->setItems([]);
175+
} catch (NonExistingRequestNameException $e) {
176+
$this->_logger->error($e->getMessage());
177+
throw new LocalizedException(__('Sorry, something went wrong. You can find out more in the error log.'));
178+
}
157179
$temporaryStorage = $this->temporaryStorageFactory->create();
158180
$table = $temporaryStorage->storeApiDocuments($searchResult->getItems());
159181

app/code/Magento/CatalogSearch/Model/ResourceModel/Fulltext/Collection.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
use Magento\Framework\Exception\StateException;
1010
use Magento\Framework\Search\Adapter\Mysql\TemporaryStorage;
1111
use Magento\Framework\Search\Response\QueryResponse;
12+
use Magento\Framework\Search\Request\EmptyRequestDataException;
13+
use Magento\Framework\Search\Request\NonExistingRequestNameException;
14+
use Magento\Framework\Api\Search\SearchResultFactory;
15+
use Magento\Framework\Exception\LocalizedException;
1216
use Magento\Framework\App\ObjectManager;
1317

1418
/**
@@ -78,6 +82,11 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection
7882
*/
7983
private $searchResult;
8084

85+
/**
86+
* @var SearchResultFactory
87+
*/
88+
private $searchResultFactory;
89+
8190
/**
8291
* @var \Magento\Framework\Api\FilterBuilder
8392
*/
@@ -109,6 +118,7 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection
109118
* @param \Magento\Framework\Search\Adapter\Mysql\TemporaryStorageFactory $temporaryStorageFactory
110119
* @param \Magento\Framework\DB\Adapter\AdapterInterface $connection
111120
* @param string $searchRequestName
121+
* @param SearchResultFactory $searchResultFactory
112122
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
113123
*/
114124
public function __construct(
@@ -136,9 +146,14 @@ public function __construct(
136146
\Magento\Search\Model\SearchEngine $searchEngine,
137147
\Magento\Framework\Search\Adapter\Mysql\TemporaryStorageFactory $temporaryStorageFactory,
138148
\Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
139-
$searchRequestName = 'catalog_view_container'
149+
$searchRequestName = 'catalog_view_container',
150+
SearchResultFactory $searchResultFactory = null
140151
) {
141152
$this->queryFactory = $catalogSearchData;
153+
if ($searchResultFactory === null) {
154+
$this->searchResultFactory = \Magento\Framework\App\ObjectManager::getInstance()
155+
->get('Magento\Framework\Api\Search\SearchResultFactory');
156+
}
142157
parent::__construct(
143158
$entityFactory,
144159
$logger,
@@ -307,7 +322,15 @@ protected function _renderFiltersBefore()
307322

308323
$searchCriteria = $this->searchCriteriaBuilder->create();
309324
$searchCriteria->setRequestName($this->searchRequestName);
310-
$this->searchResult = $this->search->search($searchCriteria);
325+
try {
326+
$this->searchResult = $this->getSearch()->search($searchCriteria);
327+
} catch (EmptyRequestDataException $e) {
328+
/** @var \Magento\Framework\Api\Search\SearchResultInterface $searchResult */
329+
$this->searchResult = $this->searchResultFactory->create()->setItems([]);
330+
} catch (NonExistingRequestNameException $e) {
331+
$this->_logger->error($e->getMessage());
332+
throw new LocalizedException(__('Sorry, something went wrong. You can find out more in the error log.'));
333+
}
311334

312335
$temporaryStorage = $this->temporaryStorageFactory->create();
313336
$table = $temporaryStorage->storeApiDocuments($this->searchResult->getItems());

app/code/Magento/Indexer/Model/Source/ServiceSource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ private function getRequestedFields(SearchResults $list, array $fields)
6969
foreach ($list->getItems() as $key => $item) {
7070
foreach (array_keys($fields) as $fieldName) {
7171
if (!isset($item[$fieldName])) {
72-
throw new NotFoundException(__("Field {$fieldName} not found"));
72+
throw new NotFoundException(__("Field '%1' not found", $fieldName));
7373
}
7474

7575
$requestedData[$key][$fieldName] = $item[$fieldName];

app/code/Magento/Search/etc/adminhtml/system.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@
1313
<label>Search Engine</label>
1414
<source_model>Magento\Search\Model\Adminhtml\System\Config\Source\Engine</source_model>
1515
</field>
16-
<field id="search_type">
17-
<depends>
18-
<field id="engine">mysql</field>
19-
</depends>
20-
</field>
2116
</group>
2217
</section>
2318
</system>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\CatalogSearch\Model\ResourceModel\Advanced;
7+
8+
/**
9+
* Test class for \Magento\CatalogSearch\Model\ResourceModel\Advanced\Collection.
10+
* @magentoDbIsolation disabled
11+
*/
12+
class CollectionTest extends \PHPUnit_Framework_TestCase
13+
{
14+
/**
15+
* @var \Magento\CatalogSearch\Model\ResourceModel\Advanced\Collection
16+
*/
17+
private $advancedCollection;
18+
19+
protected function setUp()
20+
{
21+
$this->advancedCollection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
22+
->create('\Magento\CatalogSearch\Model\ResourceModel\Advanced\Collection');
23+
}
24+
25+
/**
26+
* @dataProvider filtersDataProvider
27+
* @magentoDataFixture Magento/Framework/Search/_files/products.php
28+
*/
29+
public function testLoadWithFilterNoFilters($filters, $expectedCount)
30+
{
31+
// addFieldsToFilter will load filters,
32+
// then loadWithFilter will trigger _renderFiltersBefore code in Advanced/Collection
33+
$this->advancedCollection->addFieldsToFilter([$filters])->loadWithFilter();
34+
$items = $this->advancedCollection->getItems();
35+
$this->assertCount($expectedCount, $items);
36+
}
37+
38+
public function filtersDataProvider()
39+
{
40+
return [
41+
[['name' => ['like' => 'shorts'], 'description' => ['like' => 'green']], 1],
42+
[['name' => 'white', 'description' => ' '], 1],
43+
[['name' => ' ', 'description' => 'green'], 2],
44+
[['name' => ' ', 'description' => ' '], 0],
45+
];
46+
}
47+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\CatalogSearch\Model\ResourceModel\Fulltext;
7+
8+
/**
9+
* Test class for \Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection.
10+
* @magentoDbIsolation disabled
11+
*/
12+
class CollectionTest extends \PHPUnit_Framework_TestCase
13+
{
14+
/**
15+
* @dataProvider filtersDataProviderSearch
16+
* @magentoDataFixture Magento/Framework/Search/_files/products.php
17+
*/
18+
public function testLoadWithFilterSearch($request, $filters, $expectedCount)
19+
{
20+
$objManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
21+
/** @var \Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection $fulltextCollection */
22+
$fulltextCollection = $objManager->create(
23+
'\Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection',
24+
['searchRequestName' => $request]
25+
);
26+
foreach ($filters as $field => $value) {
27+
$fulltextCollection->addFieldToFilter($field, $value);
28+
}
29+
$fulltextCollection->loadWithFilter();
30+
$items = $fulltextCollection->getItems();
31+
$this->assertCount($expectedCount, $items);
32+
}
33+
34+
public function filtersDataProviderSearch()
35+
{
36+
return [
37+
['quick_search_container', ['search_term' => ' shorts'], 2],
38+
['quick_search_container', ['search_term' => ' '], 0],
39+
['catalog_view_container', ['category_ids' => 2], 5],
40+
['catalog_view_container', ['category_ids' => 100001], 0],
41+
['catalog_view_container', ['category_ids' => []], 0],
42+
['catalog_view_container', [], 0],
43+
];
44+
}
45+
}

0 commit comments

Comments
 (0)