Exemplo n.º 1
0
 /**
  * Ensure the index is invalidated when synonyms are updated.
  *
  * @param QueryModel $subject Saved query.
  * @param \Closure   $proceed Original save method.
  *
  * @return QueryModel
  */
 public function aroundSave(QueryModel $subject, \Closure $proceed)
 {
     $needReindex = $subject->dataHasChangedFor('synonym_for');
     $result = $proceed();
     if ($needReindex) {
         $this->indexerRegistry->get(ThesaurusIndexer::INDEXER_ID)->invalidate();
     }
     return $result;
 }
Exemplo n.º 2
0
 /**
  * Save query with number of results and is spellchecked.
  *
  * @param QueryModel $query Search query object.
  *
  * @return void
  */
 public function saveSearchResults(QueryModel $query)
 {
     $adapter = $this->getConnection();
     $table = $this->getMainTable();
     $numResults = $query->getNumResults();
     $isSpellchecked = (int) $query->getIsSpellchecked();
     $saveData = ['store_id' => $query->getStoreId(), 'query_text' => $query->getQueryText(), 'num_results' => $numResults, 'is_spellchecked' => $isSpellchecked];
     $updateData = ['num_results' => $numResults, 'is_spellchecked' => $isSpellchecked];
     $adapter->insertOnDuplicate($table, $saveData, $updateData);
 }
Exemplo n.º 3
0
 /**
  * Number results for specified search query
  *
  * @param string $queryText
  * @return int
  */
 public function getNumResults($queryText = null)
 {
     if ($queryText == null) {
         return $this->query->getNumResults();
     }
     return 1;
 }
Exemplo n.º 4
0
 public function testExecuteException()
 {
     $queryId = 1;
     $queryText = 'search';
     $this->mockGetRequestData($queryText, $queryId);
     $this->request->expects($this->at(4))->method('getPost')->with('store_id', false)->willReturn(1);
     $this->query->expects($this->once())->method('setStoreId');
     $this->query->expects($this->once())->method('loadByQueryText')->willThrowException(new \Exception());
     $this->messageManager->expects($this->once())->method('addException');
     $this->session->expects($this->once())->method('setPageData');
     $this->redirect->expects($this->once())->method('setPath')->willReturnSelf();
     $this->assertSame($this->redirect, $this->controller->execute());
 }
Exemplo n.º 5
0
 /**
  * @depends testGetNewQuery
  * @param $query
  */
 public function testGetQueryTwice($query)
 {
     $queryId = null;
     $this->mapScopeConfig([self::XML_PATH_MAX_QUERY_LENGTH => 120]);
     $rawQueryText = 'Simple product';
     $preparedQueryText = $rawQueryText;
     $this->requestMock->expects($this->once())->method('getParam')->with($this->equalTo(self::QUERY_VAR_NAME))->will($this->returnValue($rawQueryText));
     $this->objectManagerMock->expects($this->once())->method('create')->with($this->equalTo('Magento\\Search\\Model\\Query'))->will($this->returnValue($this->queryMock));
     $this->queryMock->expects($this->once())->method('loadByQuery')->with($this->equalTo($preparedQueryText))->will($this->returnSelf());
     $this->queryMock->expects($this->once())->method('getId')->will($this->returnValue($queryId));
     $this->queryMock->expects($this->once())->method('setQueryText')->with($preparedQueryText)->will($this->returnSelf());
     $this->queryMock->expects($this->once())->method('setIsQueryTextExceeded')->with($this->equalTo(false))->will($this->returnSelf());
     $query = $this->queryFactory->get();
     $this->assertSame($this->queryMock, $query);
     $this->assertSame($query, $this->queryFactory->get());
 }
Exemplo n.º 6
0
 /**
  * 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;
 }
Exemplo n.º 7
0
 public function testSaveIncrementalPopularity()
 {
     $this->resource->expects($this->once())->method('saveIncrementalPopularity')->with($this->model);
     $result = $this->model->saveIncrementalPopularity();
     $this->assertEquals($this->model, $result);
 }
Exemplo n.º 8
0
 /**
  * Save query with number of results
  *
  * @param QueryModel $query
  * @return void
  *
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 public function saveNumResults(QueryModel $query)
 {
     $adapter = $this->getConnection();
     $table = $this->getMainTable();
     $numResults = $query->getNumResults();
     $saveData = ['store_id' => $query->getStoreId(), 'query_text' => $query->getQueryText(), 'num_results' => $numResults];
     $updateData = ['num_results' => $numResults];
     $adapter->insertOnDuplicate($table, $saveData, $updateData);
 }
Exemplo n.º 9
0
 /**
  * @param string $cleanedRawText
  * @param void
  */
 private function mockSetQueryTextOnceExecute($cleanedRawText)
 {
     $this->query->expects($this->once())->method('setQueryText')->withConsecutive([$cleanedRawText])->willReturnSelf();
 }
 public function getObject(Query $suggestion)
 {
     $suggestion_obj = ['objectID' => $suggestion->getData('query_id'), 'query' => $suggestion->getData('query_text'), 'number_of_results' => (int) $suggestion->getData('num_results'), 'popularity' => (int) $suggestion->getData('popularity'), 'updated_at' => (int) strtotime($suggestion->getData('updated_at'))];
     return $suggestion_obj;
 }