public function testResolve()
 {
     $documentIds = [1, 2, 3];
     $attributeSetIds = [4, 5];
     $requestName = 'request_name';
     $this->attributeSetFinder->expects($this->once())->method('findAttributeSetIdsByProductIds')->with($documentIds)->willReturn($attributeSetIds);
     $searchCriteria = $this->getMock(SearchCriteriaInterface::class);
     $this->searchCriteriaBuilder->expects($this->once())->method('addFilter')->with('attribute_set_id', $attributeSetIds, 'in')->willReturnSelf();
     $this->searchCriteriaBuilder->expects($this->once())->method('create')->willReturn($searchCriteria);
     $attributeFirst = $this->getMock(ProductAttributeInterface::class);
     $attributeFirst->expects($this->once())->method('getAttributeCode')->willReturn('code_1');
     $attributeSecond = $this->getMock(ProductAttributeInterface::class);
     $attributeSecond->expects($this->once())->method('getAttributeCode')->willReturn('code_2');
     $searchResult = $this->getMock(ProductAttributeSearchResultsInterface::class);
     $searchResult->expects($this->once())->method('getItems')->willReturn([$attributeFirst, $attributeSecond]);
     $this->productAttributeRepository->expects($this->once())->method('getList')->with($searchCriteria)->willReturn($searchResult);
     $bucketFirst = $this->getMock(BucketInterface::class);
     $bucketFirst->expects($this->once())->method('getField')->willReturn('code_1');
     $bucketSecond = $this->getMock(BucketInterface::class);
     $bucketSecond->expects($this->once())->method('getField')->willReturn('some_another_code');
     $bucketThird = $this->getMock(BucketInterface::class);
     $bucketThird->expects($this->once())->method('getName')->willReturn('custom_not_attribute_field');
     $this->request->expects($this->once())->method('getAggregation')->willReturn([$bucketFirst, $bucketSecond, $bucketThird]);
     $this->request->expects($this->once())->method('getName')->willReturn($requestName);
     $this->config->expects($this->once())->method('get')->with($requestName)->willReturn(['aggregations' => ['custom_not_attribute_field' => []]]);
     $this->assertEquals([$bucketFirst, $bucketThird], $this->aggregationResolver->resolve($this->request, $documentIds));
 }
 /**
  * {@inheritdoc}
  */
 public function resolve(RequestInterface $request, array $documentIds)
 {
     $data = $this->config->get($request->getName());
     $bucketKeys = isset($data['aggregations']) ? array_keys($data['aggregations']) : [];
     $attributeCodes = $this->getApplicableAttributeCodes($documentIds);
     $resolvedAggregation = array_filter($request->getAggregation(), function ($bucket) use($attributeCodes, $bucketKeys) {
         /** @var BucketInterface $bucket */
         return in_array($bucket->getField(), $attributeCodes) || in_array($bucket->getName(), $bucketKeys);
     });
     return array_values($resolvedAggregation);
 }
Esempio n. 3
0
 /**
  * Invalidate indexer on attribute save (searchable flag change)
  *
  * @param \Magento\Catalog\Model\Resource\Attribute $subject
  * @param \Closure $proceed
  * @param \Magento\Framework\Model\AbstractModel $attribute
  *
  * @return \Magento\Catalog\Model\Resource\Attribute
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function aroundSave(\Magento\Catalog\Model\Resource\Attribute $subject, \Closure $proceed, \Magento\Framework\Model\AbstractModel $attribute)
 {
     $isNew = $attribute->isObjectNew();
     $needInvalidation = ($attribute->dataHasChangedFor('is_searchable') || $attribute->dataHasChangedFor('is_filterable') || $attribute->dataHasChangedFor('is_visible_in_advanced_search')) && !$isNew;
     $result = $proceed($attribute);
     if ($needInvalidation) {
         $this->indexerRegistry->get(Fulltext::INDEXER_ID)->invalidate();
     }
     if ($isNew || $needInvalidation) {
         $this->config->reset();
     }
     return $result;
 }
 public function testExecuteFull()
 {
     $stores = [0 => 'Store 1', 1 => 'Store 2'];
     $indexData = new \ArrayObject([]);
     $this->storeManager->expects($this->once())->method('getStores')->willReturn($stores);
     $this->saveHandler->expects($this->exactly(count($stores)))->method('cleanIndex');
     $this->saveHandler->expects($this->exactly(count($stores)))->method('saveIndex');
     $this->fullAction->expects($this->exactly(count($stores)))->method('rebuildStoreIndex')->willReturn($indexData);
     $this->fulltextResource->expects($this->once())->method('resetSearchResults');
     $this->searchRequestConfig->expects($this->once())->method('reset');
     $this->model->executeFull();
 }
Esempio n. 5
0
 /**
  * Execute full indexation
  *
  * @return void
  */
 public function executeFull()
 {
     $storeIds = array_keys($this->storeManager->getStores());
     /** @var IndexerHandler $saveHandler */
     $saveHandler = $this->indexerHandlerFactory->create(['data' => $this->data]);
     foreach ($storeIds as $storeId) {
         $dimension = $this->dimensionFactory->create(['name' => 'scope', 'value' => $storeId]);
         $saveHandler->cleanIndex([$dimension]);
         $saveHandler->saveIndex([$dimension], $this->fullAction->rebuildStoreIndex($storeId));
     }
     $this->fulltextResource->resetSearchResults();
     $this->searchRequestConfig->reset();
 }
Esempio n. 6
0
 /**
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function testCreate()
 {
     $data = ['dimensions' => ['scope' => ['name' => 'scope', 'value' => 'default']], 'queries' => ['filter_search_query' => ['name' => 'filter_search_query', 'filterReference' => [['ref' => 'boolFilter']], 'type' => 'filteredQuery']], 'filters' => ['boolFilter' => ['name' => 'boolFilter', 'filterReference' => [['clause' => 'should', 'ref' => 'from_to'], ['clause' => 'should', 'ref' => 'not_array'], ['clause' => 'should', 'ref' => 'like']], 'type' => 'boolFilter'], 'from_to' => ['name' => 'from_to', 'field' => 'product_id', 'type' => 'rangeFilter', 'from' => '$from_to.from$', 'to' => '$from_to.to$'], 'not_array' => ['name' => 'not_array', 'field' => 'product_id', 'type' => 'termFilter', 'value' => '$not_array$'], 'like' => ['name' => 'like', 'field' => 'product_id', 'type' => 'wildcardFilter', 'value' => '$like$'], 'in' => ['name' => 'in', 'field' => 'product_id', 'type' => 'termFilter', 'value' => '$in$'], 'in_set' => ['name' => 'in_set', 'field' => 'product_id', 'type' => 'termFilter', 'value' => '$in_set$']], 'from' => '10', 'size' => '10', 'query' => 'one_match_filters', 'index' => 'catalogsearch_fulltext', 'aggregations' => []];
     $requestName = 'rn';
     $bindData = ['dimensions' => ['scope' => 'default'], 'placeholder' => ['$from_to.from$' => 10, '$from_to.to$' => 20, '$not_array$' => 130, '$like$' => 'search_text', '$in$' => 23, '$in_set$' => [12, 23, 34, 45]], 'requestName' => $requestName, 'from' => 10, 'size' => 10];
     $this->requestBuilder->bindRequestValue('from_to', ['from' => 10, 'to' => 20]);
     $this->requestBuilder->bindRequestValue('not_array', 130);
     $this->requestBuilder->bindRequestValue('like', ['like' => '%search_text%']);
     $this->requestBuilder->bindRequestValue('in', ['in' => 23]);
     $this->requestBuilder->bindRequestValue('in_set', ['in_set' => [12, 23, 34, 45]]);
     $this->requestBuilder->setRequestName($requestName);
     $this->requestBuilder->setSize(10);
     $this->requestBuilder->setFrom(10);
     $this->requestBuilder->bindDimension('scope', 'default');
     $this->binder->expects($this->once())->method('bind')->withConsecutive([$data, $bindData])->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);
 }
Esempio n. 7
0
 /**
  * Create request object
  *
  * @return RequestInterface
  */
 public function create()
 {
     if (!isset($this->data['requestName'])) {
         throw new \InvalidArgumentException("Request name not defined.");
     }
     $requestName = $this->data['requestName'];
     /** @var array $data */
     $data = $this->config->get($requestName);
     if ($data === null) {
         throw new \InvalidArgumentException("Request name '{$requestName}' doesn't exist.");
     }
     $data = $this->binder->bind($data, $this->data);
     $data = $this->cleaner->clean($data);
     $this->clear();
     return $this->convert($data);
 }
Esempio n. 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);
 }
Esempio n. 9
0
 /**
  * Regenerate search index for store(s)
  *
  * @param int|array|null $productIds
  * @return void
  */
 protected function rebuildIndex($productIds = null)
 {
     $storeIds = array_keys($this->storeManager->getStores());
     foreach ($storeIds as $storeId) {
         $this->rebuildStoreIndex($storeId, $productIds);
     }
     $this->searchRequestConfig->reset();
 }
Esempio n. 10
0
 /**
  * Regenerate search index for all stores
  *
  * @param int|array|null $productIds
  * @return void
  */
 protected function rebuildIndex($productIds = null)
 {
     $storeIds = array_keys($this->storeManager->getStores());
     foreach ($storeIds as $storeId) {
         $dimension = $this->dimensionFactory->create(['name' => self::SCOPE_FIELD_NAME, 'value' => $storeId]);
         $this->indexHandler->deleteIndex([$dimension], $this->getIterator($productIds));
         $this->indexHandler->saveIndex([$dimension], $this->rebuildStoreIndex($storeId, $productIds));
     }
     $this->fulltextResource->resetSearchResults();
     $this->searchRequestConfig->reset();
 }
Esempio n. 11
0
 public function testReindexAll()
 {
     $this->storeManager->expects($this->once())->method('getStores')->willReturn([]);
     $this->searchRequestConfig->expects($this->once())->method('reset');
     $this->object->reindexAll();
 }
Esempio n. 12
0
 /**
  * @expectedException \InvalidArgumentException
  */
 public function testCreateInvalidArgumentException()
 {
     $requestName = 'rn';
     $this->config->expects($this->once())->method('get')->with($this->equalTo($requestName))->will($this->returnValue(null));
     $this->factory->create($requestName);
 }
Esempio n. 13
0
 /**
  * Rebuild whole fulltext index for all stores
  *
  * @return void
  */
 public function reindexAll()
 {
     $storeIds = array_keys($this->storeManager->getStores());
     foreach ($storeIds as $storeId) {
         $this->cleanIndex($storeId);
         $this->rebuildStoreIndex($storeId);
     }
     $this->searchRequestConfig->reset();
 }