Skip to content

Commit e20027b

Browse files
authored
Merge pull request #4457 from magento-mpi/PR-07-10-2019
[mpi] MAGETWO-99716: Limit list of type ahead suggested terms for Search keyword; currently the list can go over 100
2 parents 6fd1cb4 + 5439632 commit e20027b

File tree

5 files changed

+44
-4
lines changed

5 files changed

+44
-4
lines changed

app/code/Magento/CatalogSearch/Model/Autocomplete/DataProvider.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,16 @@
1010
use Magento\Search\Model\QueryFactory;
1111
use Magento\Search\Model\Autocomplete\DataProviderInterface;
1212
use Magento\Search\Model\Autocomplete\ItemFactory;
13+
use Magento\Framework\App\Config\ScopeConfigInterface as ScopeConfig;
14+
use Magento\Store\Model\ScopeInterface;
1315

1416
class DataProvider implements DataProviderInterface
1517
{
18+
/**
19+
* Autocomplete limit
20+
*/
21+
private static $CONFIG_AUTOCOMPLETE_LIMIT = 'catalog/search/autocomplete_limit';
22+
1623
/**
1724
* Query factory
1825
*
@@ -27,16 +34,25 @@ class DataProvider implements DataProviderInterface
2734
*/
2835
protected $itemFactory;
2936

37+
/**
38+
* Scope Config Object
39+
*
40+
* @var ScopeConfig
41+
*/
42+
private $scopeConfig;
43+
3044
/**
3145
* @param QueryFactory $queryFactory
3246
* @param ItemFactory $itemFactory
3347
*/
3448
public function __construct(
3549
QueryFactory $queryFactory,
36-
ItemFactory $itemFactory
50+
ItemFactory $itemFactory,
51+
ScopeConfig $scopeConfig
3752
) {
3853
$this->queryFactory = $queryFactory;
3954
$this->itemFactory = $itemFactory;
55+
$this->scopeConfig = $scopeConfig;
4056
}
4157

4258
/**
@@ -46,6 +62,10 @@ public function getItems()
4662
{
4763
$collection = $this->getSuggestCollection();
4864
$query = $this->queryFactory->get()->getQueryText();
65+
$limit = (int) $this->scopeConfig->getValue(
66+
static::$CONFIG_AUTOCOMPLETE_LIMIT,
67+
ScopeInterface::SCOPE_STORE
68+
);
4969
$result = [];
5070
foreach ($collection as $item) {
5171
$resultItem = $this->itemFactory->create([
@@ -58,7 +78,7 @@ public function getItems()
5878
$result[] = $resultItem;
5979
}
6080
}
61-
return $result;
81+
return ($limit) ? array_splice($result, 0, $limit) : $result;
6282
}
6383

6484
/**

app/code/Magento/CatalogSearch/Test/Unit/Model/Autocomplete/DataProviderTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ class DataProviderTest extends \PHPUnit\Framework\TestCase
2525
*/
2626
private $itemFactory;
2727

28+
/**
29+
* @var Magento\Framework\App\Config\ScopeConfigInterface |\PHPUnit_Framework_MockObject_MockObject
30+
*/
31+
private $scopeConfig;
32+
2833
/**
2934
* @var \Magento\Search\Model\ResourceModel\Query\Collection |\PHPUnit_Framework_MockObject_MockObject
3035
*/
@@ -60,18 +65,25 @@ protected function setUp()
6065
->setMethods(['create'])
6166
->getMock();
6267

68+
$this->scopeConfig = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
69+
->setMethods(['getValue'])
70+
->disableOriginalConstructor()
71+
->getMockForAbstractClass();
72+
6373
$this->model = $helper->getObject(
6474
\Magento\CatalogSearch\Model\Autocomplete\DataProvider::class,
6575
[
6676
'queryFactory' => $queryFactory,
67-
'itemFactory' => $this->itemFactory
77+
'itemFactory' => $this->itemFactory,
78+
'scopeConfig' => $this->scopeConfig
6879
]
6980
);
7081
}
7182

7283
public function testGetItems()
7384
{
7485
$queryString = 'string';
86+
$limit = 3;
7587
$expected = ['title' => $queryString, 'num_results' => 100500];
7688
$collection = [
7789
['query_text' => 'string1', 'num_results' => 1],
@@ -80,6 +92,8 @@ public function testGetItems()
8092
['query_text' => 'string100', 'num_results' => 100],
8193
['query_text' => $queryString, 'num_results' => 100500]
8294
];
95+
$this->scopeConfig->method('getValue')
96+
->willReturn($limit);
8397
$this->buildCollection($collection);
8498
$this->query->expects($this->once())
8599
->method('getQueryText')
@@ -105,6 +119,7 @@ public function testGetItems()
105119
$this->itemFactory->expects($this->any())->method('create')->willReturn($itemMock);
106120
$result = $this->model->getItems();
107121
$this->assertEquals($expected, $result[0]->toArray());
122+
$this->assertEquals($limit, count($result));
108123
}
109124

110125
/**

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
<comment>Number of popular search terms to be cached for faster response. Use “0” to cache all results after a term is searched for the second time.</comment>
3535
<validate>validate-digits</validate>
3636
</field>
37+
<field id="autocomplete_limit" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
38+
<label>Autocomplete Limit</label>
39+
<validate>validate-digits</validate>
40+
</field>
3741
<field id="enable_eav_indexer" translate="label" type="select" sortOrder="18" showInDefault="1" showInWebsite="0" showInStore="0">
3842
<label>Enable EAV Indexer</label>
3943
<comment>Enable/Disable Product EAV indexer to improve indexation speed. Make sure that indexer is not used by 3rd party extensions.</comment>

app/code/Magento/CatalogSearch/etc/config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<min_query_length>3</min_query_length>
1717
<max_query_length>128</max_query_length>
1818
<max_count_cacheable_search_terms>100</max_count_cacheable_search_terms>
19+
<autocomplete_limit>8</autocomplete_limit>
1920
<enable_eav_indexer>1</enable_eav_indexer>
2021
</search>
2122
</catalog>

app/code/Magento/Payment/view/frontend/web/js/view/payment/iframe.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ define([
144144
* {Function}
145145
*/
146146
setPaymentInformation: function () {
147-
setPaymentInformationAction(
147+
return setPaymentInformationAction(
148148
this.messageContainer,
149149
{
150150
method: this.getCode()

0 commit comments

Comments
 (0)