From 61b3564a09bfe41aa9633014b3b626935a2b239c Mon Sep 17 00:00:00 2001 From: Max Chadwick Date: Thu, 19 Oct 2017 21:07:55 -0400 Subject: [PATCH 1/4] Add autocomplete limit field --- app/code/Magento/CatalogSearch/etc/adminhtml/system.xml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/code/Magento/CatalogSearch/etc/adminhtml/system.xml b/app/code/Magento/CatalogSearch/etc/adminhtml/system.xml index 0eeb6ab33871e..9cd4479d0aac4 100644 --- a/app/code/Magento/CatalogSearch/etc/adminhtml/system.xml +++ b/app/code/Magento/CatalogSearch/etc/adminhtml/system.xml @@ -27,6 +27,10 @@ validate-digits + + + validate-digits + From 6f077d17695b0c649330204e007b009fae4290b6 Mon Sep 17 00:00:00 2001 From: Max Chadwick Date: Thu, 19 Oct 2017 21:08:10 -0400 Subject: [PATCH 2/4] Add default autocomplete value --- app/code/Magento/CatalogSearch/etc/config.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/app/code/Magento/CatalogSearch/etc/config.xml b/app/code/Magento/CatalogSearch/etc/config.xml index d5ff194813b9c..64c8b6363ed53 100644 --- a/app/code/Magento/CatalogSearch/etc/config.xml +++ b/app/code/Magento/CatalogSearch/etc/config.xml @@ -15,6 +15,7 @@ mysql 1 128 + 8 From 86b5442e3ea9c4bf2f7123a8c07f31fa1708066e Mon Sep 17 00:00:00 2001 From: Max Chadwick Date: Thu, 19 Oct 2017 21:08:41 -0400 Subject: [PATCH 3/4] Handle autocomplete limit --- .../Model/Autocomplete/DataProvider.php | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/CatalogSearch/Model/Autocomplete/DataProvider.php b/app/code/Magento/CatalogSearch/Model/Autocomplete/DataProvider.php index c4413d002e19c..c1c9997bc83ea 100644 --- a/app/code/Magento/CatalogSearch/Model/Autocomplete/DataProvider.php +++ b/app/code/Magento/CatalogSearch/Model/Autocomplete/DataProvider.php @@ -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 * @@ -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 + ); } /** @@ -58,7 +78,7 @@ public function getItems() $result[] = $resultItem; } } - return $result; + return ($this->limit) ? array_splice($result, 0, $this->limit) : $result; } /** From 99e81ca22b5ba84401668ccfc08a1038987e702d Mon Sep 17 00:00:00 2001 From: Max Chadwick Date: Thu, 19 Oct 2017 21:39:14 -0400 Subject: [PATCH 4/4] Add test for autocomplete limit --- .../Model/Autocomplete/DataProviderTest.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/CatalogSearch/Test/Unit/Model/Autocomplete/DataProviderTest.php b/app/code/Magento/CatalogSearch/Test/Unit/Model/Autocomplete/DataProviderTest.php index 75daf438f7bf2..bb8fc848bb2b7 100644 --- a/app/code/Magento/CatalogSearch/Test/Unit/Model/Autocomplete/DataProviderTest.php +++ b/app/code/Magento/CatalogSearch/Test/Unit/Model/Autocomplete/DataProviderTest.php @@ -30,6 +30,11 @@ class DataProviderTest extends \PHPUnit\Framework\TestCase */ private $suggestCollection; + /** + * @var integer + */ + private $limit = 3; + protected function setUp() { $helper = new ObjectManager($this); @@ -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 ] ); } @@ -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)