/** * Js configuration array for autocomplete * * @return array */ public function getJsConfig() { $config = ["*" => ['Magento_Ui/js/core/app' => ['components' => ['autocompleteInjection' => ['component' => 'Mirasvit_SearchAutocomplete/js/injection', 'config' => []], 'autocomplete' => ['component' => 'Mirasvit_SearchAutocomplete/js/autocomplete', 'provider' => 'autocompleteProvider', 'config' => ['query' => $this->searchHelper->getEscapedQueryText(), 'priceFormat' => $this->localeFormat->getPriceFormat()]], 'autocompleteProvider' => ['component' => 'Mirasvit_SearchAutocomplete/js/provider', 'config' => ['url' => $this->getUrl('searchautocomplete/ajax/suggest', ['_secure' => $this->getRequest()->isSecure()]), 'delay' => $this->config->getDelay(), 'minSearchLength' => $this->config->getMinChars()]], 'autocompleteNavigation' => ['component' => 'Mirasvit_SearchAutocomplete/js/navigation', 'autocomplete' => 'autocomplete']]]]]; if ($this->config->isShowPopularSearches()) { $config['*']['Magento_Ui/js/core/app']['components']['autocompletePopular'] = ['component' => 'Mirasvit_SearchAutocomplete/js/popular', 'autocomplete' => 'autocomplete', 'provider' => 'autocompleteProvider', 'config' => ['enabled' => $this->config->isShowPopularSearches(), 'queries' => $this->config->getPopularSearches()]]; } else { $config['*']['Magento_Ui/js/core/app']['components']['autocompleteRecent'] = ['component' => 'Mirasvit_SearchAutocomplete/js/recent', 'autocomplete' => 'autocomplete', 'provider' => 'autocompleteProvider', 'config' => ['limit' => 5]]; } return $config; }
/** * @dataProvider queryTextDataProvider */ public function testGetEscapedQueryText($queryText, $maxQueryLength, $expected) { $this->requestMock->expects($this->once())->method('getParam')->willReturn($queryText); $this->stringMock->expects($this->any())->method('cleanString')->willReturnArgument(0); $this->scopeConfigMock->expects($this->any())->method('getValue')->willReturn($maxQueryLength); $this->stringMock->expects($this->any())->method('strlen')->will($this->returnCallback(function ($queryText) { return strlen($queryText); })); $this->stringMock->expects($this->any())->method('substr')->with($queryText, 0, $maxQueryLength)->willReturn($expected); $this->escaperMock->expects($this->any())->method('escapeHtml')->willReturnArgument(0); $this->assertEquals($expected, $this->model->getEscapedQueryText()); }
/** * {@inheritdoc} */ public function get() { if (!$this->query) { $maxQueryLength = $this->queryHelper->getMaxQueryLength(); $minQueryLength = $this->queryHelper->getMinQueryLength(); $rawQueryText = $this->getRawQueryText(); $preparedQueryText = $this->getPreparedQueryText($rawQueryText, $maxQueryLength); $query = $this->create()->loadByQueryText($preparedQueryText); if (!$query->getId()) { $query->setQueryText($preparedQueryText); } $query->setIsQueryTextExceeded($this->isQueryTooLong($rawQueryText, $maxQueryLength)); $query->setIsQueryTextShort($this->isQueryTooShort($rawQueryText, $minQueryLength)); $this->query = $query; } return $this->query; }
/** * Convert all results to array * * @return array * @SuppressWarnings(PHPMD.NPathComplexity) */ public function toArray() { $result = ['totalItems' => 0, 'query' => $this->query->getQueryText(), 'indexes' => [], 'noResults' => false, 'urlAll' => $this->searchHelper->getResultUrl($this->query->getQueryText())]; /** @var \Mirasvit\Search\Model\Index $index */ foreach ($this->getIndexes() as $index) { $indexCode = $index->getCode(); if ($indexCode == 'catalogsearch_fulltext') { $indexCode = 'magento_catalog_product'; } $collection = $this->getCollection($index); $localIndex = $this->indexPool->get($indexCode); $localIndex->setCollection($collection); $items = ['code' => $indexCode, 'title' => $index->getTitle(), 'totalItems' => $collection->getSize(), 'isShowTotals' => $indexCode == 'magento_search_query' ? false : true, 'items' => $localIndex->getItems()]; $result['indexes'][] = $items; $result['totalItems'] += $localIndex->getSize(); } $result['textAll'] = __('Show all %1 results →', $result['totalItems']); $result['textEmpty'] = __('Sorry, nothing found for "%1".', $result['query']); $result['noResults'] = $result['totalItems'] ? false : true; return $result; }
public function testGetMaxQueryLength() { $return = 'some_value'; $this->_scopeConfigMock->expects($this->once())->method('getValue')->with(\Magento\Search\Model\Query::XML_PATH_MAX_QUERY_LENGTH, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, null)->will($this->returnValue($return)); $this->assertEquals($return, $this->_model->getMaxQueryLength()); }
/** * @param int $maxQueryLength * @param int $minQueryLength * @return void */ private function mockQueryLengths($maxQueryLength, $minQueryLength) { $this->queryHelper->expects($this->once())->method('getMaxQueryLength')->willReturn($maxQueryLength); $this->queryHelper->expects($this->once())->method('getMinQueryLength')->willReturn($minQueryLength); }