Skip to content

Commit 06a2b7b

Browse files
committed
MC-18165: Quick search with two chars shows all products
1 parent d93d288 commit 06a2b7b

File tree

7 files changed

+70
-26
lines changed

7 files changed

+70
-26
lines changed

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

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,8 @@ public function __construct(
212212
DefaultFilterStrategyApplyCheckerInterface $defaultFilterStrategyApplyChecker = null
213213
) {
214214
$this->queryFactory = $catalogSearchData;
215-
if ($searchResultFactory === null) {
216-
$this->searchResultFactory = \Magento\Framework\App\ObjectManager::getInstance()
215+
$this->searchResultFactory = $searchResultFactory ?? \Magento\Framework\App\ObjectManager::getInstance()
217216
->get(\Magento\Framework\Api\Search\SearchResultFactory::class);
218-
}
219217
parent::__construct(
220218
$entityFactory,
221219
$logger,
@@ -427,25 +425,40 @@ protected function _renderFiltersBefore()
427425
return;
428426
}
429427

430-
$this->prepareSearchTermFilter();
431-
$this->preparePriceAggregation();
432-
433-
$searchCriteria = $this->getSearchCriteriaResolver()->resolve();
434-
try {
435-
$this->searchResult = $this->getSearch()->search($searchCriteria);
436-
$this->_totalRecords = $this->getTotalRecordsResolver($this->searchResult)->resolve();
437-
} catch (EmptyRequestDataException $e) {
438-
/** @var \Magento\Framework\Api\Search\SearchResultInterface $searchResult */
439-
$this->searchResult = $this->searchResultFactory->create()->setItems([]);
440-
} catch (NonExistingRequestNameException $e) {
441-
$this->_logger->error($e->getMessage());
442-
throw new LocalizedException(__('An error occurred. For details, see the error log.'));
428+
if ($this->searchRequestName != 'quick_search_container'
429+
|| strlen(trim($this->queryText))
430+
) {
431+
$this->prepareSearchTermFilter();
432+
$this->preparePriceAggregation();
433+
434+
$searchCriteria = $this->getSearchCriteriaResolver()->resolve();
435+
try {
436+
$this->searchResult = $this->getSearch()->search($searchCriteria);
437+
$this->_totalRecords = $this->getTotalRecordsResolver($this->searchResult)->resolve();
438+
} catch (EmptyRequestDataException $e) {
439+
$this->searchResult = $this->createEmptyResult();
440+
} catch (NonExistingRequestNameException $e) {
441+
$this->_logger->error($e->getMessage());
442+
throw new LocalizedException(__('An error occurred. For details, see the error log.'));
443+
}
444+
} else {
445+
$this->searchResult = $this->createEmptyResult();
443446
}
444447

445448
$this->getSearchResultApplier($this->searchResult)->apply();
446449
parent::_renderFiltersBefore();
447450
}
448451

452+
/**
453+
* Create empty search result
454+
*
455+
* @return SearchResultInterface
456+
*/
457+
private function createEmptyResult()
458+
{
459+
return $this->searchResultFactory->create()->setItems([]);
460+
}
461+
449462
/**
450463
* Set sort order for search query.
451464
*

app/code/Magento/CatalogSearch/Test/Mftf/ActionGroup/StorefrontCatalogSearchActionGroup.xml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,24 @@
4141
<see userInput="Search results for: '{{phrase}}'" selector="{{StorefrontCatalogSearchMainSection.SearchTitle}}" stepKey="assertQuickSearchName"/>
4242
</actionGroup>
4343

44+
<actionGroup name="StorefrontQuickSearchTooShortStringActionGroup" extends="StorefrontCheckQuickSearchStringActionGroup">
45+
<annotations>
46+
<description>Fill the Storefront Search field. Submits the Form. Validates that 'Minimum Search query length' warning appears.</description>
47+
</annotations>
48+
49+
<see selector="{{StorefrontQuickSearchResultsSection.messageSection}}" userInput="Minimum Search query length is 3" stepKey="assertQuickSearchNeedThreeOrMoreChars"/>
50+
</actionGroup>
51+
52+
<actionGroup name="StorefrontQuickSearchRelatedSearchTermsAppearsActionGroup">
53+
<arguments>
54+
<argument name="term" type="string"/>
55+
</arguments>
56+
57+
<waitForElementVisible selector="{{StorefrontCatalogSearchMainSection.relatedSearchTermsTitle}}" stepKey="waitMessageAppears"/>
58+
<see selector="{{StorefrontCatalogSearchMainSection.relatedSearchTermsTitle}}" userInput="Related search terms" stepKey="checkRelatedTermsTitle"/>
59+
<see selector="{{StorefrontCatalogSearchMainSection.relatedSearchTerm}}" userInput="{{term}}" stepKey="checkRelatedTermExists"/>
60+
</actionGroup>
61+
4462
<!-- Opens product from QuickSearch and performs assertions-->
4563
<actionGroup name="StorefrontOpenProductFromQuickSearch">
4664
<annotations>
@@ -101,7 +119,7 @@
101119

102120
<dontSee selector="{{StorefrontQuickSearchResultsSection.allResults}}" userInput="{{productName}}" stepKey="dontSeeProductName"/>
103121
</actionGroup>
104-
122+
105123
<!-- Open advanced search page -->
106124
<actionGroup name="StorefrontOpenAdvancedSearchActionGroup">
107125
<annotations>

app/code/Magento/CatalogSearch/Test/Mftf/Section/StorefrontCatalogSearchMainSection.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@
1616
<element name="productCount" type="text" selector="#toolbar-amount"/>
1717
<element name="message" type="text" selector="div.message div"/>
1818
<element name="searchResults" type="block" selector="#maincontent .column.main"/>
19+
<element name="relatedSearchTermsTitle" type="text" selector="div.message dl.block dt.title"/>
20+
<element name="relatedSearchTerm" type="text" selector="div.message dl.block dd.item a"/>
1921
</section>
2022
</sections>

app/code/Magento/CatalogSearch/Test/Mftf/Test/SearchEntityResultsTest.xml

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
<deleteData stepKey="deleteProduct" createDataKey="createSimpleProduct"/>
9393
<deleteData stepKey="deleteCategory" createDataKey="createCategory"/>
9494
</after>
95+
9596
<amOnPage url="{{StorefrontHomePage.url}}" stepKey="goToFrontPage"/>
9697
<actionGroup ref="StorefrontCheckQuickSearchStringActionGroup" stepKey="searchStorefront">
9798
<argument name="phrase" value="ThisShouldn'tReturnAnything"/>
@@ -100,20 +101,26 @@
100101
</test>
101102
<test name="QuickSearchWithTwoCharsEmptyResults" extends="QuickSearchEmptyResults">
102103
<annotations>
104+
<features value="CatalogSearch"/>
103105
<stories value="Search Product on Storefront"/>
104106
<title value="User should not get search results on query that only contains two characters"/>
105107
<description value="Use of 2 character query to return no products"/>
106108
<severity value="MAJOR"/>
107109
<testCaseId value="MC-14794"/>
108110
<group value="CatalogSearch"/>
109111
<group value="mtf_migrated"/>
110-
<skip>
111-
<issueId value="MC-15827"/>
112-
</skip>
113112
</annotations>
114-
<executeJS function="var s = '$createSimpleProduct.name$'; var ret=s.substring(0,2); return ret;" stepKey="getFirstTwoLetters" before="searchStorefront"/>
115-
<actionGroup ref="StorefrontCheckQuickSearchStringActionGroup" stepKey="searchStorefront">
116-
<argument name="phrase" value="{$getFirstTwoLetters}"/>
113+
<executeJS function="var s = '$createSimpleProduct.name$'; var ret=s.substring(0,2); return ret;" before="searchStorefront" stepKey="getFirstTwoLetters"/>
114+
<executeJS function="var s = '$createSimpleProduct.name$'; var ret=s.substring(0,3); return ret;" before="searchStorefront" stepKey="getFirstThreeLetters"/>
115+
116+
<actionGroup ref="StorefrontCheckQuickSearchStringActionGroup" after="checkEmpty" stepKey="searchStorefrontThreeLetters">
117+
<argument name="phrase" value="$getFirstThreeLetters"/>
118+
</actionGroup>
119+
<actionGroup ref="StorefrontQuickSearchTooShortStringActionGroup" after="searchStorefrontThreeLetters" stepKey="checkCannotSearchWithTooShortString">
120+
<argument name="phrase" value="$getFirstTwoLetters"/>
121+
</actionGroup>
122+
<actionGroup ref="StorefrontQuickSearchRelatedSearchTermsAppearsActionGroup" after="checkCannotSearchWithTooShortString" stepKey="checkRelatedSearchTerm">
123+
<argument name="term" value="$getFirstThreeLetters"/>
117124
</actionGroup>
118125
</test>
119126
<test name="QuickSearchProductByNameWithThreeLetters" extends="QuickSearchProductBySku">

app/code/Magento/Search/view/frontend/templates/form.mini.phtml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ $helper = $this->helper(\Magento\Search\Helper\Data::class);
2424
data-mage-init='{"quickSearch":{
2525
"formSelector":"#search_mini_form",
2626
"url":"<?= $block->escapeUrl($helper->getSuggestUrl())?>",
27-
"destinationSelector":"#search_autocomplete"}
27+
"destinationSelector":"#search_autocomplete",
28+
"minSearchLength":"<?= $block->escapeHtml($helper->getMinQueryLength()) ?>"}
2829
}'
2930
type="text"
3031
name="<?= $block->escapeHtmlAttr($helper->getQueryParamName()) ?>"

app/code/Magento/Search/view/frontend/web/js/form-mini.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ define([
3030
$.widget('mage.quickSearch', {
3131
options: {
3232
autocomplete: 'off',
33-
minSearchLength: 2,
33+
minSearchLength: 3,
3434
responseFieldElements: 'ul li',
3535
selectClass: 'selected',
3636
template:

dev/tests/integration/testsuite/Magento/CatalogSearch/Model/ResourceModel/Fulltext/CollectionTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ public function testLoadWithFilterSearch($request, $filters, $expectedCount)
2626
foreach ($filters as $field => $value) {
2727
$fulltextCollection->addFieldToFilter($field, $value);
2828
}
29+
if ($request == 'quick_search_container' && isset($filters['search_term'])) {
30+
$fulltextCollection->addSearchFilter($filters['search_term']);
31+
}
2932
$fulltextCollection->loadWithFilter();
3033
$items = $fulltextCollection->getItems();
3134
$this->assertCount($expectedCount, $items);
@@ -48,7 +51,7 @@ public function testSearchResultsAreTheSameForSameRequests()
4851
['searchRequestName' => 'quick_search_container']
4952
);
5053

51-
$fulltextCollection->addFieldToFilter('search_term', 'shorts');
54+
$fulltextCollection->addSearchFilter('shorts');
5255
$fulltextCollection->setOrder('relevance');
5356
$fulltextCollection->load();
5457
$items = $fulltextCollection->getItems();

0 commit comments

Comments
 (0)