예제 #1
0
 /**
  * Test a search using 'like' condition
  */
 public function testLike()
 {
     $attributeCode = 'description';
     $attributeCodeId = 42;
     $attribute = $this->getMock('Magento\\Catalog\\Model\\ResourceModel\\Eav\\Attribute', [], [], '', false);
     $attribute->expects($this->once())->method('getAttributeCode')->willReturn($attributeCode);
     $this->eavConfig->expects($this->once())->method('getAttribute')->with(Product::ENTITY, $attributeCodeId)->willReturn($attribute);
     $filtersData = ['catalog_product_entity_text' => [$attributeCodeId => ['like' => 'search text']]];
     $this->filterBuilder->expects($this->once())->method('setField')->with($attributeCode)->willReturn($this->filterBuilder);
     $this->filterBuilder->expects($this->once())->method('setValue')->with('search text')->willReturn($this->filterBuilder);
     $filter = $this->getMock('Magento\\Framework\\Api\\Filter');
     $this->filterBuilder->expects($this->once())->method('create')->willReturn($filter);
     $criteria = $this->getMock('Magento\\Framework\\Api\\Search\\SearchCriteria', [], [], '', false);
     $this->criteriaBuilder->expects($this->once())->method('create')->willReturn($criteria);
     $criteria->expects($this->once())->method('setRequestName')->with('advanced_search_container');
     $tempTable = $this->getMock('Magento\\Framework\\DB\\Ddl\\Table', [], [], '', false);
     $temporaryStorage = $this->getMock('Magento\\Framework\\Search\\Adapter\\Mysql\\TemporaryStorage', [], [], '', false);
     $temporaryStorage->expects($this->once())->method('storeApiDocuments')->willReturn($tempTable);
     $this->temporaryStorageFactory->expects($this->once())->method('create')->willReturn($temporaryStorage);
     $searchResult = $this->getMock('Magento\\Framework\\Api\\Search\\SearchResultInterface', [], [], '', false);
     $this->search->expects($this->once())->method('search')->willReturn($searchResult);
     // addFieldsToFilter will load filters,
     //   then loadWithFilter will trigger _renderFiltersBefore code in Advanced/Collection
     $this->assertSame($this->advancedCollection, $this->advancedCollection->addFieldsToFilter($filtersData)->loadWithFilter());
 }
 /**
  * @inheritdoc
  */
 protected function _renderFiltersBefore()
 {
     $this->getSearchCriteriaBuilder();
     $this->getFilterBuilder();
     $this->getSearch();
     if ($this->queryText) {
         $this->filterBuilder->setField('search_term');
         $this->filterBuilder->setValue($this->queryText);
         $this->searchCriteriaBuilder->addFilter($this->filterBuilder->create());
     }
     $priceRangeCalculation = $this->_scopeConfig->getValue(\Magento\Catalog\Model\Layer\Filter\Dynamic\AlgorithmFactory::XML_PATH_RANGE_CALCULATION, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
     if ($priceRangeCalculation) {
         $this->filterBuilder->setField('price_dynamic_algorithm');
         $this->filterBuilder->setValue($priceRangeCalculation);
         $this->searchCriteriaBuilder->addFilter($this->filterBuilder->create());
     }
     $searchCriteria = $this->searchCriteriaBuilder->create();
     $searchCriteria->setRequestName($this->searchRequestName);
     $this->searchResult = $this->search->search($searchCriteria);
     $temporaryStorage = $this->temporaryStorageFactory->create();
     $table = $temporaryStorage->storeApiDocuments($this->searchResult->getItems());
     $this->getSelect()->joinInner(['search_result' => $table->getName()], 'e.entity_id = search_result.' . TemporaryStorage::FIELD_ENTITY_ID, []);
     $this->_totalRecords = $this->searchResult->getTotalCount();
     if ($this->order && 'relevance' === $this->order['field']) {
         $this->getSelect()->order('search_result.' . TemporaryStorage::FIELD_SCORE . ' ' . $this->order['dir']);
     }
     return parent::_renderFiltersBefore();
 }
 /**
  * Returns search criteria
  *
  * @return \Magento\Framework\Api\Search\SearchCriteria
  */
 public function getSearchCriteria()
 {
     if (!$this->searchCriteria) {
         $this->searchCriteria = $this->searchCriteriaBuilder->create();
         $this->searchCriteria->setRequestName($this->name);
     }
     return $this->searchCriteria;
 }
 /**
  * Add attributes that have a range (from,to) to the SearchCriteriaBuilder.
  *
  * @param string $attributeCode
  * @param array|string $attributeValue
  * @return void
  */
 private function addRangeAttributeToSearch($attributeCode, $attributeValue)
 {
     if (isset($attributeValue['from']) && '' !== $attributeValue['from']) {
         $this->filterBuilder->setField("{$attributeCode}.from")->setValue($attributeValue['from']);
         $this->searchCriteriaBuilder->addFilter($this->filterBuilder->create());
     }
     if (isset($attributeValue['to']) && '' !== $attributeValue['to']) {
         $this->filterBuilder->setField("{$attributeCode}.to")->setValue($attributeValue['to']);
         $this->searchCriteriaBuilder->addFilter($this->filterBuilder->create());
     }
 }
 /**
  * Add sales rules.
  */
 public function init()
 {
     $crit = $this->_builderSearchCriteria->create();
     $crit->setSortOrders(null);
     /** @var \Magento\Framework\Api\SearchResults $all */
     $all = $this->_repoRule->getList($crit);
     $total = $all->getTotalCount();
     if ($total) {
         /** @var \Magento\SalesRule\Model\Data\Rule $item */
         foreach ($all->getItems() as $item) {
             $this->_repoRule->deleteById($item->getRuleId());
         }
     }
     $this->_loadCustomerGroups();
     $desc = 'This rule is added by initialization script for test proposes.';
     $this->_createRuleBySku('10% off to Bee Royal', $desc, '212San', 10);
     $this->_createRuleBySku('20% off to BoostIron', $desc, '10674San', 20);
     $this->_createRuleForOrder('$20 off to cart subtotal above $100', $desc, 100, 20);
 }
예제 #6
0
 /**
  * @inheritdoc
  */
 protected function _renderFiltersBefore()
 {
     $this->getSearchCriteriaBuilder();
     $this->getFilterBuilder();
     $this->getSearch();
     if ($this->queryText) {
         $this->filterBuilder->setField('search_term');
         $this->filterBuilder->setValue($this->queryText);
         $this->searchCriteriaBuilder->addFilter($this->filterBuilder->create());
     }
     $priceRangeCalculation = $this->_scopeConfig->getValue(\Magento\Catalog\Model\Layer\Filter\Dynamic\AlgorithmFactory::XML_PATH_RANGE_CALCULATION, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
     if ($priceRangeCalculation) {
         $this->filterBuilder->setField('price_dynamic_algorithm');
         $this->filterBuilder->setValue($priceRangeCalculation);
         $this->searchCriteriaBuilder->addFilter($this->filterBuilder->create());
     }
     $searchCriteria = $this->searchCriteriaBuilder->create();
     $searchCriteria->setRequestName($this->searchRequestName);
     try {
         $this->searchResult = $this->getSearch()->search($searchCriteria);
     } catch (EmptyRequestDataException $e) {
         /** @var \Magento\Framework\Api\Search\SearchResultInterface $searchResult */
         $this->searchResult = $this->searchResultFactory->create()->setItems([]);
     } catch (NonExistingRequestNameException $e) {
         $this->_logger->error($e->getMessage());
         throw new LocalizedException(__('Sorry, something went wrong. You can find out more in the error log.'));
     }
     $temporaryStorage = $this->temporaryStorageFactory->create();
     $table = $temporaryStorage->storeApiDocuments($this->searchResult->getItems());
     $this->getSelect()->joinInner(['search_result' => $table->getName()], 'e.entity_id = search_result.' . TemporaryStorage::FIELD_ENTITY_ID, []);
     $this->_totalRecords = $this->searchResult->getTotalCount();
     if ($this->order && 'relevance' === $this->order['field']) {
         $this->getSelect()->order('search_result.' . TemporaryStorage::FIELD_SCORE . ' ' . $this->order['dir']);
     }
     return parent::_renderFiltersBefore();
 }