Skip to content
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
24 changes: 22 additions & 2 deletions app/code/Magento/CatalogSearch/Model/Autocomplete/DataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@
use Magento\Search\Model\QueryFactory;
use Magento\Search\Model\Autocomplete\DataProviderInterface;
use Magento\Search\Model\Autocomplete\ItemFactory;
use Magento\Framework\App\Config\ScopeConfigInterface as ScopeConfig;
use Magento\Store\Model\ScopeInterface;

class DataProvider implements DataProviderInterface
{
/**
* Autocomplete limit
*/
const CONFIG_AUTOCOMPLETE_LIMIT = 'catalog/search/autocomplete_limit';

/**
* Query factory
*
Expand All @@ -27,16 +34,29 @@ class DataProvider implements DataProviderInterface
*/
protected $itemFactory;

/**
* Limit
*
* @var int
*/
protected $limit;

/**
* @param QueryFactory $queryFactory
* @param ItemFactory $itemFactory
*/
public function __construct(
QueryFactory $queryFactory,
ItemFactory $itemFactory
ItemFactory $itemFactory,
ScopeConfig $scopeConfig
) {
$this->queryFactory = $queryFactory;
$this->itemFactory = $itemFactory;

$this->limit = (int) $scopeConfig->getValue(
self::CONFIG_AUTOCOMPLETE_LIMIT,
ScopeInterface::SCOPE_STORE
);
}

/**
Expand All @@ -58,7 +78,7 @@ public function getItems()
$result[] = $resultItem;
}
}
return $result;
return ($this->limit) ? array_splice($result, 0, $this->limit) : $result;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ class DataProviderTest extends \PHPUnit\Framework\TestCase
*/
private $suggestCollection;

/**
* @var integer
*/
private $limit = 3;

protected function setUp()
{
$helper = new ObjectManager($this);
Expand Down Expand Up @@ -60,11 +65,20 @@ protected function setUp()
->setMethods(['create'])
->getMock();

$scopeConfig = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
->setMethods(['getValue'])
->disableOriginalConstructor()
->getMockForAbstractClass();
$scopeConfig->expects($this->any())
->method('getValue')
->willReturn($this->limit);

$this->model = $helper->getObject(
\Magento\CatalogSearch\Model\Autocomplete\DataProvider::class,
[
'queryFactory' => $queryFactory,
'itemFactory' => $this->itemFactory
'itemFactory' => $this->itemFactory,
'scopeConfig' => $scopeConfig
]
);
}
Expand Down Expand Up @@ -103,8 +117,10 @@ public function testGetItems()
->will($this->returnValue($expected));

$this->itemFactory->expects($this->any())->method('create')->willReturn($itemMock);

$result = $this->model->getItems();
$this->assertEquals($expected, $result[0]->toArray());
$this->assertEquals($this->limit, count($result));
}

private function buildCollection(array $data)
Expand Down
4 changes: 4 additions & 0 deletions app/code/Magento/CatalogSearch/etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
<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>
<validate>validate-digits</validate>
</field>
<field id="autocomplete_limit" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Autocomplete Limit</label>
<validate>validate-digits</validate>
</field>
</group>
</section>
</system>
Expand Down
1 change: 1 addition & 0 deletions app/code/Magento/CatalogSearch/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<min_query_length>1</min_query_length>
<max_query_length>128</max_query_length>
<max_count_cacheable_search_terms>100</max_count_cacheable_search_terms>
<autocomplete_limit>8</autocomplete_limit>
</search>
</catalog>
</default>
Expand Down