Example #1
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 testWhere()
 {
     $query = new Query();
     $query->setMappingConfig($this->config);
     $query->from('Oro\\Bundle\\DataBundle\\Entity\\Product');
     $query->where('or', 'all_data', '=', 'test', 'string');
     $queryParams = $query->getOptions();
     $this->assertEquals('or', $queryParams[0]['type']);
     $this->assertEquals('all_data', $queryParams[0]['fieldName']);
 }
 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 #4
0
 public function testWhere()
 {
     $query = new Query();
     $query->setMappingConfig($this->config);
     $query->from('Oro\\Bundle\\DataBundle\\Entity\\Product');
     $query->where('or', 'all_data', '=', 'test', 'string');
     $whereExpression = $query->getCriteria()->getWhereExpression();
     $this->assertEquals('string.all_data', $whereExpression->getField());
     $this->assertEquals(Comparison::EQ, $whereExpression->getOperator());
     $this->assertEquals('test', $whereExpression->getValue()->getValue());
 }
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
 /**
  * Applies ACL conditions to the search query
  *
  * @param Query  $query
  * @param string $permission
  *
  * @return Query
  */
 public function apply(Query $query, $permission = 'VIEW')
 {
     $queryFromEntities = $query->getFrom();
     // in query, from record !== '*'
     if ($queryFromEntities[0] === '*') {
         $queryFromEntities = $this->mappingProvider->getEntitiesListAliases();
     }
     $allowedAliases = [];
     $ownerExpressions = [];
     $expr = $query->getCriteria()->expr();
     if (!empty($queryFromEntities)) {
         foreach ($queryFromEntities as $entityAlias) {
             $className = $this->mappingProvider->getEntityClass($entityAlias);
             if ($className) {
                 $ownerField = sprintf('%s_owner', $entityAlias);
                 $condition = $this->ownershipDataBuilder->getAclConditionData($className, $permission);
                 if (count($condition) === 0 || !($condition[0] === null && $condition[3] === null)) {
                     $allowedAliases[] = $entityAlias;
                     // in case if we should not limit data for entity
                     if (count($condition) === 0 || $condition[1] === null) {
                         $ownerExpressions[] = $expr->gte('integer.' . $ownerField, SearchListener::EMPTY_OWNER_ID);
                         continue;
                     }
                     $owners = [SearchListener::EMPTY_OWNER_ID];
                     if (!empty($condition[1])) {
                         $owners = $condition[1];
                         if (is_array($owners) && count($owners) === 1) {
                             $owners = $owners[0];
                         }
                     }
                     if (is_array($owners)) {
                         $ownerExpressions[] = $expr->in('integer.' . $ownerField, $owners);
                     } else {
                         $ownerExpressions[] = $expr->eq('integer.' . $ownerField, $owners);
                     }
                 }
             }
         }
     }
     if (!empty($ownerExpressions)) {
         $query->getCriteria()->andWhere(new CompositeExpression(CompositeExpression::TYPE_OR, $ownerExpressions));
     }
     $query->from($allowedAliases);
     $this->addOrganizationLimits($query, $expr);
     return $query;
 }
Example #7
0
 /**
  *  Parse from statement of expression and fills Query's from.
  */
 protected function parseFromExpression()
 {
     $this->stream->expect(Token::KEYWORD_TYPE, Query::KEYWORD_FROM);
     switch (true) {
         // if got string token after "from" - pass it directly into Query
         case true === $this->stream->current->test(Token::STRING_TYPE):
             $this->query->from($this->stream->current->value);
             $this->stream->next();
             break;
             // if got operator (only '*' is supported in from statement)
         // if got operator (only '*' is supported in from statement)
         case true === $this->stream->current->test(Token::OPERATOR_TYPE, '*'):
             $this->query->from(['*']);
             $this->stream->next();
             break;
             // if got opening bracket (punctuation '(') - collect all arguments
         // if got opening bracket (punctuation '(') - collect all arguments
         case true === $this->stream->current->test(Token::PUNCTUATION_TYPE, '('):
             $this->query->from($this->parseArguments());
             break;
         default:
             throw new ExpressionSyntaxError(sprintf('Wrong "from" statement of the expression.'), $this->stream->current->cursor);
     }
 }
Example #8
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 #9
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 #10
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 #11
0
 /**
  * Parse from statement
  *
  * @param Query  $query
  * @param string $inputString
  *
  * @return string
  */
 private function from(Query $query, $inputString)
 {
     if (substr($inputString, 0, 1) == '(') {
         $fromString = $this->getWord($inputString, ')');
         $inputString = $this->trimString($inputString, $fromString . ')');
         $fromString = str_replace(array('(', ')'), '', $fromString);
         $query->from(explode(', ', $fromString));
     } else {
         $from = $this->getWord($inputString);
         $inputString = $this->trimString($inputString, $from);
         $query->from($from);
     }
     return $inputString;
 }