public function testSearch()
 {
     $requestName = 'requestName';
     $scope = 333;
     $filterField = 'filterField';
     $filterValue = 'filterValue';
     $filter = $this->getMockBuilder('Magento\\Framework\\Api\\Filter')->disableOriginalConstructor()->getMock();
     $filter->expects($this->once())->method('getField')->willReturn($filterField);
     $filter->expects($this->once())->method('getValue')->willReturn($filterValue);
     $filterGroup = $this->getMockBuilder('Magento\\Framework\\Api\\Search\\FilterGroup')->disableOriginalConstructor()->getMock();
     $filterGroup->expects($this->once())->method('getFilters')->willReturn([$filter]);
     $searchCriteria = $this->getMockBuilder('Magento\\Framework\\Api\\Search\\SearchCriteriaInterface')->disableOriginalConstructor()->getMockForAbstractClass();
     $searchCriteria->expects($this->once())->method('getRequestName')->willReturn($requestName);
     $searchCriteria->expects($this->once())->method('getFilterGroups')->willReturn([$filterGroup]);
     $searchResult = $this->getMockBuilder('Magento\\Framework\\Api\\Search\\SearchResult')->disableOriginalConstructor()->getMockForAbstractClass();
     $request = $this->getMockBuilder('Magento\\Framework\\Search\\RequestInterface')->disableOriginalConstructor()->getMockForAbstractClass();
     $response = $this->getMockBuilder('Magento\\Framework\\Search\\ResponseInterface')->disableOriginalConstructor()->getMockForAbstractClass();
     $this->requestBuilder->expects($this->once())->method('setRequestName')->with($requestName);
     $this->requestBuilder->expects($this->once())->method('bindDimension')->with('scope', $scope);
     $this->requestBuilder->expects($this->any())->method('bind');
     $this->requestBuilder->expects($this->once())->method('create')->willReturn($request);
     $this->searchEngine->expects($this->once())->method('search')->with($request)->willReturn($response);
     $this->searchResponseBuilder->expects($this->once())->method('build')->with($response)->willReturn($searchResult);
     $this->scopeResolver->expects($this->once())->method('getScope')->willReturn($scope);
     $searchResult = $this->model->search($searchCriteria);
     $this->assertInstanceOf('Magento\\Framework\\Api\\Search\\SearchResultInterface', $searchResult);
 }
示例#2
0
 public function testSearch()
 {
     $requestName = 'requestName';
     $scopeId = 333;
     $filters = [$this->createFilterMock('array_filter', ['arrayValue1', 'arrayValue2']), $this->createFilterMock('simple_filter', 'filterValue'), $this->createFilterMock('from_filter', ['from' => 30]), $this->createFilterMock('to_filter', ['to' => 100]), $this->createFilterMock('range_filter', ['from' => 60, 'to' => 82])];
     $scope = $this->getMockBuilder('Magento\\Framework\\App\\ScopeInterface')->disableOriginalConstructor()->getMockForAbstractClass();
     $filterGroup = $this->getMockBuilder('Magento\\Framework\\Api\\Search\\FilterGroup')->disableOriginalConstructor()->getMock();
     $filterGroup->expects($this->once())->method('getFilters')->willReturn($filters);
     $searchCriteria = $this->getMockBuilder('Magento\\Framework\\Api\\Search\\SearchCriteriaInterface')->disableOriginalConstructor()->getMockForAbstractClass();
     $searchCriteria->expects($this->once())->method('getRequestName')->willReturn($requestName);
     $searchCriteria->expects($this->once())->method('getFilterGroups')->willReturn([$filterGroup]);
     $searchResult = $this->getMockBuilder('Magento\\Framework\\Api\\Search\\SearchResult')->disableOriginalConstructor()->getMockForAbstractClass();
     $request = $this->getMockBuilder('Magento\\Framework\\Search\\RequestInterface')->disableOriginalConstructor()->getMockForAbstractClass();
     $response = $this->getMockBuilder('Magento\\Framework\\Search\\ResponseInterface')->disableOriginalConstructor()->getMockForAbstractClass();
     $this->requestBuilder->expects($this->once())->method('setRequestName')->with($requestName);
     $this->requestBuilder->expects($this->once())->method('bindDimension')->with('scope', $scopeId);
     $this->requestBuilder->expects($this->exactly(6))->method('bind');
     $this->requestBuilder->expects($this->once())->method('create')->willReturn($request);
     $this->searchEngine->expects($this->once())->method('search')->with($request)->willReturn($response);
     $this->searchResponseBuilder->expects($this->once())->method('build')->with($response)->willReturn($searchResult);
     $this->scopeResolver->expects($this->once())->method('getScope')->willReturn($scope);
     $scope->expects($this->once())->method('getId')->willReturn($scopeId);
     $searchResult = $this->model->search($searchCriteria);
     $this->assertInstanceOf('Magento\\Framework\\Api\\Search\\SearchResultInterface', $searchResult);
 }
示例#3
0
 /**
  * Apply attribute filter to facet collection
  *
  * @param string $field
  * @param string|array|null $condition
  * @return $this
  */
 private function addFieldToFilter($field, $condition = null)
 {
     if (!is_array($condition) || !in_array(key($condition), ['from', 'to'])) {
         $this->requestBuilder->bind($field, $condition);
     } else {
         if (!empty($condition['from'])) {
             $this->requestBuilder->bind("{$field}.from", $condition['from']);
         }
         if (!empty($condition['to'])) {
             $this->requestBuilder->bind("{$field}.to", $condition['to']);
         }
     }
     return $this;
 }
示例#4
0
 /**
  * @inheritdoc
  */
 protected function _renderFiltersBefore()
 {
     $this->requestBuilder->bindDimension('scope', $this->getStoreId());
     if ($this->queryText) {
         $this->requestBuilder->bind('search_term', $this->queryText);
     }
     $priceRangeCalculation = $this->_scopeConfig->getValue(\Magento\Catalog\Model\Layer\Filter\Dynamic\AlgorithmFactory::XML_PATH_RANGE_CALCULATION, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
     if ($priceRangeCalculation) {
         $this->requestBuilder->bind('price_dynamic_algorithm', $priceRangeCalculation);
     }
     $this->requestBuilder->setRequestName('quick_search_container');
     $queryRequest = $this->requestBuilder->create();
     $this->queryResponse = $this->searchEngine->search($queryRequest);
     $ids = [0];
     /** @var \Magento\Framework\Search\Document $document */
     foreach ($this->queryResponse as $document) {
         $ids[] = $document->getId();
     }
     parent::addFieldToFilter('entity_id', ['in' => $ids]);
     $this->_totalRecords = count($ids) - 1;
     if ($this->order && $this->order['field'] == 'relevance') {
         $this->getSelect()->order(new \Zend_Db_Expr($this->_conn->quoteInto('FIELD(e.entity_id, ?) ' . $this->order['dir'], $ids)));
     }
     return parent::_renderFiltersBefore();
 }
示例#5
0
 /**
  * Advanced search request using date product attribute
  *
  * @param $rangeFilter
  * @param $expectedRecordsCount
  * @magentoDataFixture Magento/Framework/Search/_files/date_attribute.php
  * @magentoConfigFixture current_store catalog/search/engine mysql
  * @dataProvider dateDataProvider
  */
 public function testAdvancedSearchDateField($rangeFilter, $expectedRecordsCount)
 {
     $this->requestBuilder->bind('date.from', $rangeFilter['from']);
     $this->requestBuilder->bind('date.to', $rangeFilter['to']);
     $this->requestBuilder->setRequestName('advanced_search_date_field');
     $queryResponse = $this->executeQuery();
     $this->assertEquals($expectedRecordsCount, $queryResponse->count());
 }
示例#6
0
 /**
  * Sample Advanced search request test
  *
  * @dataProvider advancedSearchDataProvider
  * @magentoDbIsolation disabled
  * @magentoAppIsolation enabled
  * @magentoConfigFixture current_store catalog/search/engine mysql
  * @magentoConfigFixture current_store catalog/search/search_type 2
  * @magentoDataFixture Magento/Framework/Search/_files/products.php
  */
 public function testSimpleAdvancedSearch($nameQuery, $descriptionQuery, $rangeFilter, $expectedRecordsCount)
 {
     $this->requestBuilder->bind('name_query', $nameQuery);
     $this->requestBuilder->bind('description_query', $descriptionQuery);
     $this->requestBuilder->bind('request.from_price', $rangeFilter['from']);
     $this->requestBuilder->bind('request.to_price', $rangeFilter['to']);
     $this->requestBuilder->setRequestName('advanced_search_test');
     $queryResponse = $this->executeQuery();
     $this->assertEquals($expectedRecordsCount, $queryResponse->count());
 }
示例#7
0
 /**
  * @inheritdoc
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 protected function _renderFiltersBefore()
 {
     if ($this->filters) {
         $this->requestBuilder->bindDimension('scope', $this->getStoreId());
         $this->requestBuilder->setRequestName('advanced_search_container');
         foreach ($this->filters as $attributes) {
             foreach ($attributes as $attributeCode => $attributeValue) {
                 if (is_numeric($attributeCode)) {
                     $attributeCode = $this->_eavConfig->getAttribute(Product::ENTITY, $attributeCode)->getAttributeCode();
                 }
                 if (isset($attributeValue['from']) || isset($attributeValue['to'])) {
                     if (isset($attributeValue['from']) && '' !== $attributeValue['from']) {
                         $this->requestBuilder->bind("{$attributeCode}.from", $attributeValue['from']);
                     }
                     if (isset($attributeValue['to']) && '' !== $attributeValue['to']) {
                         $this->requestBuilder->bind("{$attributeCode}.to", $attributeValue['to']);
                     }
                 } elseif (!is_array($attributeValue)) {
                     $this->requestBuilder->bind($attributeCode, $attributeValue);
                 } elseif (isset($attributeValue['like'])) {
                     $this->requestBuilder->bind($attributeCode, trim($attributeValue['like'], '%'));
                 } elseif (isset($attributeValue['in'])) {
                     $this->requestBuilder->bind($attributeCode, $attributeValue['in']);
                 } elseif (isset($attributeValue['in_set'])) {
                     $this->requestBuilder->bind($attributeCode, $attributeValue['in_set']);
                 }
             }
         }
         $queryRequest = $this->requestBuilder->create();
         $queryResponse = $this->searchEngine->search($queryRequest);
         $ids = [0];
         /** @var \Magento\Framework\Search\Document $document */
         foreach ($queryResponse as $document) {
             $ids[] = $document->getId();
         }
         $this->addIdFilter($ids);
     }
     return parent::_renderFiltersBefore();
 }
示例#8
0
 /**
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function testCreate()
 {
     $data = ['dimensions' => ['scope' => ['name' => 'scope', 'value' => 'default']], 'queries' => ['one_match_filters' => ['name' => 'one_match_filters', 'boost' => '2', 'queryReference' => [['clause' => 'must', 'ref' => 'fulltext_search_query'], ['clause' => 'must', 'ref' => 'fulltext_search_query2']], 'type' => 'boolQuery'], 'fulltext_search_query' => ['name' => 'fulltext_search_query', 'boost' => '5', 'value' => '$fulltext_search_query$', 'match' => [['field' => 'data_index', 'boost' => '2']], 'type' => 'matchQuery'], 'fulltext_search_query2' => ['name' => 'fulltext_search_query2', 'filterReference' => [['ref' => 'pid']], 'type' => 'filteredQuery']], 'filters' => ['pid' => ['name' => 'pid', 'filterReference' => [['clause' => 'should', 'ref' => 'pidm'], ['clause' => 'should', 'ref' => 'pidsh']], 'type' => 'boolFilter'], 'pidm' => ['name' => 'pidm', 'field' => 'product_id', 'type' => 'rangeFilter', 'from' => '$pidm_from$', 'to' => '$pidm_to$'], 'pidsh' => ['name' => 'pidsh', 'field' => 'product_id', 'type' => 'termFilter', 'value' => '$pidsh$']], 'from' => '10', 'size' => '10', 'query' => 'one_match_filters', 'index' => 'catalogsearch_fulltext', 'aggregations' => []];
     $requestName = 'rn';
     $this->requestBuilder->bind('fulltext_search_query', 'socks');
     $this->requestBuilder->bind('pidsh', 4);
     $this->requestBuilder->bind('pidm_from', 1);
     $this->requestBuilder->bind('pidm_to', 3);
     $this->requestBuilder->setRequestName($requestName);
     $this->requestBuilder->setSize(10);
     $this->requestBuilder->setFrom(10);
     $this->requestBuilder->bindDimension('scope', 'default');
     $this->binder->expects($this->once())->method('bind')->willReturn($data);
     $this->cleaner->expects($this->once())->method('clean')->willReturn($data);
     $this->requestMapper->expects($this->once())->method('getRootQuery')->willReturn([]);
     $this->objectManager->expects($this->at(0))->method('create')->willReturn($this->requestMapper);
     $this->objectManager->expects($this->at(2))->method('create')->willReturn($this->request);
     $this->config->expects($this->once())->method('get')->with($this->equalTo($requestName))->willReturn($data);
     $result = $this->requestBuilder->create();
     $this->assertInstanceOf('\\Magento\\Framework\\Search\\Request', $result);
 }
 /**
  * @magentoDbIsolation disabled
  * @magentoAppIsolation enabled
  * @magentoConfigFixture current_store catalog/search/engine mysql
  * @magentoConfigFixture current_store catalog/search/search_type 2
  * @magentoDataFixture Magento/Framework/Search/_files/filterable_attribute.php
  */
 public function testCustomFilterableAttribute()
 {
     /** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute */
     $attribute = $this->objectManager->get('Magento\\Catalog\\Model\\ResourceModel\\Eav\\Attribute')->loadByCode(\Magento\Catalog\Model\Product::ENTITY, 'select_attribute');
     /** @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection $selectOptions */
     $selectOptions = $this->objectManager->create('Magento\\Eav\\Model\\ResourceModel\\Entity\\Attribute\\Option\\Collection')->setAttributeFilter($attribute->getId());
     $attribute->loadByCode(\Magento\Catalog\Model\Product::ENTITY, 'multiselect_attribute');
     /** @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection $multiselectOptions */
     $multiselectOptions = $this->objectManager->create('Magento\\Eav\\Model\\ResourceModel\\Entity\\Attribute\\Option\\Collection')->setAttributeFilter($attribute->getId());
     $this->requestBuilder->bind('select_attribute', $selectOptions->getLastItem()->getId());
     $this->requestBuilder->bind('multiselect_attribute', $multiselectOptions->getLastItem()->getId());
     $this->requestBuilder->bind('price.from', 9);
     $this->requestBuilder->bind('price.to', 12);
     $this->requestBuilder->bind('category_ids', 2);
     $this->requestBuilder->setRequestName('filterable_custom_attributes');
     $queryResponse = $this->executeQuery();
     $this->assertEquals(1, $queryResponse->count());
 }
示例#10
0
 /**
  * @inheritdoc
  */
 protected function _renderFiltersBefore()
 {
     $this->requestBuilder->bindDimension('scope', $this->getStoreId());
     if ($this->queryText) {
         $this->requestBuilder->bind('search_term', $this->queryText);
     }
     $priceRangeCalculation = $this->_scopeConfig->getValue(\Magento\Catalog\Model\Layer\Filter\Dynamic\AlgorithmFactory::XML_PATH_RANGE_CALCULATION, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
     if ($priceRangeCalculation) {
         $this->requestBuilder->bind('price_dynamic_algorithm', $priceRangeCalculation);
     }
     $this->requestBuilder->setRequestName($this->searchRequestName);
     $queryRequest = $this->requestBuilder->create();
     $this->queryResponse = $this->searchEngine->search($queryRequest);
     $temporaryStorage = $this->temporaryStorageFactory->create();
     $table = $temporaryStorage->storeDocuments($this->queryResponse->getIterator());
     $this->getSelect()->joinInner(['search_result' => $table->getName()], 'e.entity_id = search_result.' . TemporaryStorage::FIELD_ENTITY_ID, []);
     $this->_totalRecords = $this->queryResponse->count();
     if ($this->order && 'relevance' === $this->order['field']) {
         $this->getSelect()->order('search_result.' . TemporaryStorage::FIELD_SCORE . ' ' . $this->order['dir']);
     }
     return parent::_renderFiltersBefore();
 }