/**
  * @param SearchQueryBuilder $searchQueryBuilder
  * @param string[]           $emails
  */
 protected function prepareSearchEmailCriteria(SearchQueryBuilder $searchQueryBuilder, $emails = [])
 {
     $searchCriteria = $searchQueryBuilder->getCriteria();
     foreach ($emails as $email) {
         $searchCriteria->orWhere($searchCriteria->expr()->contains('email', $email));
     }
 }
Example #2
0
 /**
  * @dataProvider applyTestCases
  *
  * @param mixed  $from
  * @param string $expectedQuery
  */
 public function testApply($from, $expectedQuery)
 {
     $query = new Query();
     $query->from($from);
     $this->aclHelper->apply($query);
     $this->assertEquals($expectedQuery, $query->getStringQuery());
 }
 public function testGetEntityByAlias()
 {
     $query = new Query();
     $query->setMappingConfig($this->config);
     $result = $query->getEntityByAlias('test_alias');
     $this->assertEquals('Oro\\Bundle\\DataBundle\\Entity\\Product', $result);
     $this->assertFalse($query->getEntityByAlias('bad alias'));
 }
 public function testEvent()
 {
     $query = new Query();
     $event = new BeforeSearchEvent($query);
     $this->assertEquals($query, $event->getQuery());
     $anotherQuery = new Query();
     $anotherQuery->from('test');
     $event->setQuery($anotherQuery);
     $this->assertEquals($anotherQuery, $event->getQuery());
 }
Example #5
0
 public function testBeforeSearchEvent()
 {
     $query = new Query();
     $query->from('testEntity')->andWhere('name', '~', 'string');
     $this->securityFacade->expects($this->once())->method('getOrganizationId')->will($this->returnValue(5));
     $event = new BeforeSearchEvent($query);
     $this->listener->beforeSearchEvent($event);
     $wherePart = $query->getOptions();
     $this->assertCount(2, $wherePart);
     $expexted = ['fieldName' => 'organization', 'condition' => 'in', 'fieldValue' => [5, 0], 'fieldType' => 'integer', 'type' => 'and'];
     $this->assertEquals($expexted, $wherePart[1]);
 }
Example #6
0
 public function testStringCleanup()
 {
     $testString = 'Re: FW: Test Sample - One äöü ßü abc 3 – Testing again';
     $clearedValue = Query::clearString($testString);
     $textAllDataField = sprintf('%s %s', $testString, $clearedValue);
     $result = implode(Query::DELIMITER, array_unique(explode(Query::DELIMITER, $textAllDataField)));
     $this->assertEquals($testString, $result);
 }
Example #7
0
 protected function setUp()
 {
     if (!interface_exists('Doctrine\\Common\\Persistence\\ObjectManager')) {
         $this->markTestSkipped('Doctrine Common has to be installed for this test to run.');
     }
     $this->om = $this->getMock('Doctrine\\Common\\Persistence\\ObjectManager');
     $this->repository = $this->getMock('Doctrine\\Common\\Persistence\\ObjectRepository');
     $this->om->expects($this->any())->method('getRepository')->with($this->equalTo('OroTestBundle:test'))->will($this->returnValue($this->repository));
     $this->product = new Product();
     $this->product->setName('test product');
     $this->repository->expects($this->any())->method('find')->will($this->returnValue($this->product));
     $items[] = new Item($this->om, 'OroTestBundle:test', 1, 'test title', 'http://example.com', 'test text', array('alias' => 'test_product', 'label' => 'test product', 'fields' => array(array('name' => 'name', 'target_type' => 'text'))));
     $items[] = new Item($this->om, 'OroTestBundle:test', 2, 'test title 2', 'http://example.com', 'test text', array('alias' => 'test_product', 'label' => 'test product', 'fields' => array(array('name' => 'name', 'target_type' => 'text'))));
     $items[] = new Item($this->om, 'OroTestBundle:test', 3, 'test title 3', 'http://example.com', 'test text', array('alias' => 'test_product', 'label' => 'test product', 'fields' => array(array('name' => 'name', 'target_type' => 'text'))));
     $query = new Query();
     $query->createQuery(Query::SELECT)->from(array('OroTestBundle:test', 'OroTestBundle:product'))->andWhere('name', Query::OPERATOR_CONTAINS, 'test string', Query::TYPE_TEXT);
     $this->result = new Result($query, $items, 3);
     $this->result1 = new Result($query, array(), 0);
 }
 public function testDoSearch()
 {
     $query = new Query();
     $query->createQuery(Query::SELECT)->from('test')->andWhere('name', '~', 'test value', Query::TYPE_TEXT);
     $searchRepo = $this->getMockBuilder('Oro\\Bundle\\SearchBundle\\Entity\\Repository\\SearchIndexRepository')->disableOriginalConstructor()->getMock();
     $this->om->expects($this->once())->method('getRepository')->with($this->equalTo('OroSearchBundle:Item'))->will($this->returnValue($searchRepo));
     $this->om->expects($this->once())->method('persist');
     $this->om->expects($this->once())->method('flush');
     $this->container->expects($this->once())->method('getParameter')->with($this->equalTo('oro_search.engine_orm'))->will($this->returnValue('test_orm'));
     $searchRepo->expects($this->once())->method('setDriversClasses');
     $result = $this->orm->search($query);
     $this->assertEquals(0, $result->getRecordsCount());
     $searchOptions = $result->getQuery()->getOptions();
     $this->assertEquals('name', $searchOptions[0]['fieldName']);
     $this->assertEquals(Query::OPERATOR_CONTAINS, $searchOptions[0]['condition']);
     $this->assertEquals('test value', $searchOptions[0]['fieldValue']);
     $this->assertEquals(Query::TYPE_TEXT, $searchOptions[0]['fieldType']);
     $this->assertEquals(Query::KEYWORD_AND, $searchOptions[0]['type']);
 }
Example #9
0
 /**
  * Parse max_results statement of expression.
  */
 protected function parseMaxResultsExpression()
 {
     $this->stream->next();
     /** @var Token $token */
     $token = $this->stream->current;
     if ($token->test(Token::NUMBER_TYPE)) {
         $this->query->getCriteria()->setMaxResults($token->value);
         $this->stream->next();
     } else {
         throw new ExpressionSyntaxError(sprintf('Unexpected token "%s", value "%s" in offset statements', $token->type, $token->value), $token->cursor);
     }
 }
Example #10
0
 /**
  * @return mixed
  */
 public function getSortOrder()
 {
     return $this->query->getOrderDirection();
 }
Example #11
0
 /**
  * Apply ACL to search Query.
  * Removes all entities of the request to which the user has no access
  *
  * @param Query $query
  */
 protected function applyAclToQuery(Query $query)
 {
     $allowedEntities = $this->getAllowedEntitiesListAliases();
     $queryFromEntities = $query->getFrom();
     $entitiesList = array_values($allowedEntities);
     // in query, from record !== '*'
     if (!empty($queryFromEntities) && $queryFromEntities[0] !== '*') {
         foreach ($queryFromEntities as $key => $fromEntityAlias) {
             if (!in_array($fromEntityAlias, $entitiesList)) {
                 unset($queryFromEntities[$key]);
             }
         }
         $query->from($queryFromEntities);
     } else {
         $query->from($allowedEntities);
     }
 }
Example #12
0
 /**
  * Set order by for search query
  *
  * @param \Oro\Bundle\SearchBundle\Query\Query $query
  * @param \Doctrine\ORM\QueryBuilder           $qb
  */
 protected function addOrderBy(Query $query, QueryBuilder $qb)
 {
     $orderBy = $query->getOrderBy();
     if ($orderBy) {
         $orderRelation = $query->getOrderType() . 'Fields';
         $qb->leftJoin('search.' . $orderRelation, 'orderTable', 'WITH', 'orderTable.field = :orderField')->orderBy('orderTable.value', $query->getOrderDirection())->setParameter('orderField', $orderBy);
     }
 }
Example #13
0
 /**
  * Get search query 'from' aliases
  *
  * @param Query $query
  *
  * @return array Return search aliases from Query. In case if from part = *, return all search aliases
  */
 protected function getSearchAliases(Query $query)
 {
     $queryAliases = $query->getFrom();
     if ($queryAliases[0] === '*') {
         $queryAliases = $this->mappingProvider->getEntitiesListAliases();
     }
     return $queryAliases;
 }
Example #14
0
 /**
  * Apply special behavior of class inheritance processing
  *
  * @param Query $query
  */
 protected function applyModesBehavior(Query $query)
 {
     // process abstract indexes
     // make hashes increasing performance
     $fromParts = (array) $query->getFrom();
     $fromHash = array_combine($fromParts, $fromParts);
     $aliases = $this->mapper->getEntitiesListAliases();
     $aliasesHash = array_flip($aliases);
     if (!isset($fromHash['*'])) {
         foreach ($fromParts as $part) {
             $entityName = $part;
             $isAlias = false;
             if (isset($aliasesHash[$part])) {
                 // find real name by alias
                 $entityName = $aliasesHash[$part];
                 $isAlias = true;
             }
             $mode = $this->mapper->getEntityModeConfig($entityName);
             $descendants = $this->mapper->getRegisteredDescendants($entityName);
             if (false !== $descendants) {
                 // add descendants to from clause
                 foreach ($descendants as $fromPart) {
                     if ($isAlias) {
                         $fromPart = $aliases[$fromPart];
                     }
                     if (!isset($fromHash[$fromPart])) {
                         $fromHash[$fromPart] = $fromPart;
                     }
                 }
             }
             if ($mode === Mode::ONLY_DESCENDANTS) {
                 unset($fromHash[$part]);
             }
         }
     }
     $collectedParts = array_values($fromHash);
     if ($collectedParts !== $fromParts) {
         $query->from($collectedParts);
     }
 }
Example #15
0
 /**
  * Set order by for search query
  *
  * @param \Oro\Bundle\SearchBundle\Query\Query $query
  * @param \Doctrine\ORM\QueryBuilder           $qb
  */
 protected function addOrderBy(Query $query, QueryBuilder $qb)
 {
     $orderBy = $query->getCriteria()->getOrderings();
     if ($orderBy) {
         $direction = reset($orderBy);
         list($fieldType, $fieldName) = Criteria::explodeFieldTypeName(key($orderBy));
         $orderRelation = $fieldType . 'Fields';
         $qb->leftJoin('search.' . $orderRelation, 'orderTable', 'WITH', 'orderTable.field = :orderField')->orderBy('orderTable.value', $direction)->setParameter('orderField', $fieldName);
     }
 }
Example #16
0
 /**
  * @param Query $query
  * @return string
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 protected function combineQueryString(Query $query)
 {
     $selectString = $query->getQuery();
     $fromString = '';
     if ($query->getFrom()) {
         $fromString .= ' from ' . implode(', ', $query->getFrom());
     }
     $whereParts = array();
     foreach ($query->getOptions() as $whereOptions) {
         if (is_array($whereOptions['fieldValue'])) {
             $whereOptions['fieldValue'] = '(' . implode(', ', $whereOptions['fieldValue']) . ')';
         }
         $whereParts[] = sprintf('%s((%s)%s %s %s)', $whereOptions['type'], $whereOptions['fieldType'], $whereOptions['fieldName'], $whereOptions['condition'], $whereOptions['fieldValue']);
     }
     $whereString = '';
     if ($whereParts) {
         $whereString .= ' where ' . implode(' ', $whereParts);
     }
     $orderByString = '';
     if ($query->getOrderBy()) {
         $orderByString .= ' ' . $query->getOrderBy();
     }
     if ($query->getOrderDirection()) {
         $orderByString .= ' ' . $query->getOrderDirection();
     }
     if ($orderByString) {
         $orderByString = ' order by' . $orderByString;
     }
     $limitString = '';
     if ($query->getMaxResults() && $query->getMaxResults() != Query::INFINITY) {
         $limitString = ' limit ' . $query->getMaxResults();
     }
     $offsetString = '';
     if ($query->getFirstResult()) {
         $offsetString .= ' offset ' . $query->getFirstResult();
     }
     return $selectString . $fromString . $whereString . $orderByString . $limitString . $offsetString;
 }
Example #17
0
 /**
  * Ensure that items loaded to search index
  *
  * @throws \LogicException
  */
 protected function ensureItemsLoaded()
 {
     $query = new Query();
     $query->from('oro_test_item');
     $requestCounts = 20;
     do {
         $result = $this->container->get('oro_search.search.engine')->search($query);
         $isLoaded = $result->getRecordsCount() == self::COUNT;
         if (!$isLoaded) {
             $requestCounts++;
             sleep(1);
         }
     } while (!$isLoaded && $requestCounts > 0);
     if (!$isLoaded) {
         throw new \LogicException('Search items are not loaded');
     }
 }
Example #18
0
 /**
  * Set value for meta fields by type
  *
  * @param string $alias
  * @param array  $objectData
  * @param array  $fieldConfig
  * @param mixed  $value
  * @param bool   $isArray
  *
  * @return array
  */
 protected function setDataValue($alias, $objectData, $fieldConfig, $value, $isArray = false)
 {
     if ($value) {
         //check if field have target_fields parameter
         $targetFields = isset($fieldConfig['target_fields']) ? $fieldConfig['target_fields'] : [$fieldConfig['name']];
         if ($fieldConfig['target_type'] != Query::TYPE_TEXT) {
             foreach ($targetFields as $targetField) {
                 if ($isArray) {
                     $objectData[$fieldConfig['target_type']][$targetField][] = $value;
                 } else {
                     $objectData[$fieldConfig['target_type']][$targetField] = $value;
                 }
             }
         } else {
             foreach ($targetFields as $targetField) {
                 if (!isset($objectData[$fieldConfig['target_type']][$targetField])) {
                     $objectData[$fieldConfig['target_type']][$targetField] = '';
                 }
                 $objectData[$fieldConfig['target_type']][$targetField] .= sprintf(' %s ', $value);
             }
             $textAllDataField = '';
             if (isset($objectData[$fieldConfig['target_type']][Indexer::TEXT_ALL_DATA_FIELD])) {
                 $textAllDataField = $objectData[$fieldConfig['target_type']][Indexer::TEXT_ALL_DATA_FIELD];
             }
             $clearedValue = Query::clearString($value);
             $textAllDataField .= sprintf(' %s %s ', $value, $clearedValue);
             $objectData[$fieldConfig['target_type']][Indexer::TEXT_ALL_DATA_FIELD] = implode(Query::DELIMITER, array_unique(explode(Query::DELIMITER, $textAllDataField)));
             $objectData[$fieldConfig['target_type']] = array_map('trim', $objectData[$fieldConfig['target_type']]);
         }
     }
     return $objectData;
 }
Example #19
0
 /**
  * Parse where statement
  *
  * @param Query  $query
  * @param string $keyWord
  * @param string $inputString
  *
  * @return string
  */
 private function where(Query $query, $keyWord, $inputString)
 {
     $typeWord = $this->getWord($inputString);
     if (!in_array($typeWord, $this->types)) {
         $typeWord = Query::TYPE_TEXT;
     } else {
         $inputString = $this->trimString($inputString, $typeWord);
     }
     //parse field name
     $fieldName = $this->getWord($inputString);
     $inputString = $this->trimString($inputString, $fieldName);
     //parse operator
     $operatorWord = $this->getWord($inputString);
     // check operator
     if (!in_array($operatorWord, $this->typeOperators[$typeWord])) {
         throw new \InvalidArgumentException('Type ' . $typeWord . ' does not support operator "' . $operatorWord . '"');
     }
     $inputString = $this->trimString($inputString, $operatorWord);
     if (in_array($operatorWord, array(Query::OPERATOR_IN, Query::OPERATOR_NOT_IN))) {
         $fromString = $this->getWord($inputString, ')');
         $inputString = $this->trimString($inputString, $fromString . ')');
         $fromString = str_replace(array('(', ')'), '', $fromString);
         $value = explode(', ', $fromString);
     } else {
         if (substr($inputString, 0, 1) == '"') {
             $inputString = substr($inputString, 1, strlen($inputString));
             $value = $this->getWord($inputString, '"');
             $inputString = $this->trimString($inputString, $value . '"');
         } else {
             $value = $this->getWord($inputString);
             $inputString = $this->trimString($inputString, $value);
         }
     }
     $query->where($keyWord, $fieldName, $operatorWord, $value, $typeWord);
     return $inputString;
 }
Example #20
0
 /**
  * @param Query $query
  * @param $expr
  */
 protected function addOrganizationLimits(Query $query, $expr)
 {
     $organizationId = $this->getOrganizationId();
     if ($organizationId) {
         $query->getCriteria()->andWhere($expr->in('integer.organization', [$organizationId, SearchListener::EMPTY_ORGANIZATION_ID]));
     }
 }