diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigStorageController.php b/core/lib/Drupal/Core/Config/Entity/ConfigStorageController.php index 2dd7ace..b2a94b0 100644 --- a/core/lib/Drupal/Core/Config/Entity/ConfigStorageController.php +++ b/core/lib/Drupal/Core/Config/Entity/ConfigStorageController.php @@ -428,7 +428,7 @@ protected function invokeHook($hook, EntityInterface $entity) { * Implements Drupal\Core\Entity\EntityStorageControllerInterface::getQueryServicename(). */ public function getQueryServicename() { - throw new \LogicException('Querying configuration entities is not supported.'); + return 'entity.query.config'; } /** diff --git a/core/lib/Drupal/Core/Config/Entity/Query/Condition.php b/core/lib/Drupal/Core/Config/Entity/Query/Condition.php new file mode 100644 index 0000000..47ec34d --- /dev/null +++ b/core/lib/Drupal/Core/Config/Entity/Query/Condition.php @@ -0,0 +1,128 @@ +conjunction) == 'AND'; + $simple_conditions = array(); + $complex_conditions = array(); + foreach ($this->conditions as $condition) { + if ($condition['field'] instanceOf ConditionInterface) { + // Complex conditions contain a group of conditions and are handled + // by recursive calls. + $complex_conditions[] = $condition; + } + else { + // Simple conditions just contain a field, a value and an operator and + // handled without an extra function call. + $simple_conditions[] = $condition; + } + } + $return = array(); + if ($simple_conditions) { + foreach ($entities as $entity_id => $entity) { + foreach ($simple_conditions as &$condition) { + if (!isset($condition['operator'])) { + $condition['operator'] = is_array($condition['value']) ? 'IN' : '='; + } + if (isset($entity->{$condition['field']})) { + $value = $entity->{$condition['field']}; + switch ($condition['operator']) { + case '=': + $match = $value == $condition['value']; + break; + case '>': + $match = $value > $condition['value']; + break; + case '<': + $match = $value < $condition['value']; + break; + case '>=': + $match = $value >= $condition['value']; + break; + case '<=': + $match = $value <= $condition['value']; + break; + case 'IN': + $match = array_search($value, $condition['value']) !== FALSE; + break; + case 'NOT IN': + $match = array_search($value, $condition['value']) === FALSE; + break; + case 'STARTS_WITH': + $match = strpos($value, $condition['value']) === 0; + break; + case 'CONTAINS': + $match = strpos($value, $condition['value']) !== FALSE; + break; + case 'ENDS_WITH': + $match = substr($value, -strlen($condition['value'])) === (string) $condition['value']; + break; + case 'IS NOT NULL': + $match = TRUE; + break; + } + } + else { + $match = $condition['operator'] === 'IS NULL'; + } + // If AND and it's not matching, then we know it will never match. + // If OR and it is matching, we know it already matches. + if ($and != $match ) { + break; + } + } + if ($match) { + $return[$entity_id] = $entity; + } + } + } + elseif (!$complex_conditions || $and) { + // If there were no simple conditions then either: + // - Complex conditions, OR: need to start from no entities. + // - Complex conditions, AND: need to start from all entities. + // - No complex conditions (AND/OR doesn't matter): need to return all + // entities. + $return = $entities; + } + foreach ($complex_conditions as $condition) { + $group_entities = $condition['field']->compile($entities); + if ($and) { + $return = array_intersect_key($return, $group_entities); + } + else { + $return = $return + $group_entities; + } + } + + return $return; + } + + /** + * Implements \Drupal\Core\Entity\Query\ConditionInterface::exists(). + */ + public function exists($field, $langcode = NULL) { + return $this->condition($field, NULL, 'IS NOT NULL', $langcode); + } + + /** + * Implements \Drupal\Core\Entity\Query\ConditionInterface::notExists(). + */ + public function notExists($field, $langcode = NULL) { + return $this->condition($field, NULL, 'IS NULL', $langcode); + } + +} diff --git a/core/lib/Drupal/Core/Config/Entity/Query/Query.php b/core/lib/Drupal/Core/Config/Entity/Query/Query.php new file mode 100644 index 0000000..73cdca4 --- /dev/null +++ b/core/lib/Drupal/Core/Config/Entity/Query/Query.php @@ -0,0 +1,62 @@ +storageController = $storageController; + } + + /** + * Implements \Drupal\Core\Entity\Query\QueryInterface::conditionGroupFactory(). + */ + public function conditionGroupFactory($conjunction = 'AND') { + return new Condition($conjunction); + } + + /** + * Implements \Drupal\Core\Entity\Query\QueryInterface::execute(). + */ + public function execute() { + $entities = $this->condition->compile($this->storageController->load()); + + // Apply sort settings. + foreach ($this->sort as $property => $sort) { + $direction = $sort['direction'] == 'ASC' ? -1 : 1; + uasort($entities, function($a, $b) use ($property, $direction) { + return ($a->get($property) <= $b->get($property)) ? $direction : -$direction; + }); + } + + // Let the pager do its work. + $this->initializePager(); + + if ($this->range) { + $entities = array_slice($entities, $this->range['start'], $this->range['length'], TRUE); + } + if ($this->count) { + return count($entities); + } + + return drupal_map_assoc(array_keys($entities)); + } + +} diff --git a/core/lib/Drupal/Core/Config/Entity/Query/QueryFactory.php b/core/lib/Drupal/Core/Config/Entity/Query/QueryFactory.php new file mode 100644 index 0000000..3bc4dba --- /dev/null +++ b/core/lib/Drupal/Core/Config/Entity/Query/QueryFactory.php @@ -0,0 +1,32 @@ +getStorageController($entity_type)); + } + +} diff --git a/core/lib/Drupal/Core/CoreBundle.php b/core/lib/Drupal/Core/CoreBundle.php index e70b1fc..b4f5bb6 100644 --- a/core/lib/Drupal/Core/CoreBundle.php +++ b/core/lib/Drupal/Core/CoreBundle.php @@ -157,7 +157,9 @@ public function build(ContainerBuilder $container) { // Add the entity query factory. $container->register('entity.query', 'Drupal\Core\Entity\Query\QueryFactory') - ->addArgument(new Reference('service_container')); + ->addArgument(new Reference('plugin.manager.entity')) + ->addMethodCall('setContainer', array(new Reference('service_container'))); + $container->register('entity.query.config', 'Drupal\Core\Config\Entity\Query\QueryFactory'); $container->register('router.dumper', 'Drupal\Core\Routing\MatcherDumper') ->addArgument(new Reference('database')); diff --git a/core/lib/Drupal/Core/Entity/Query/QueryFactory.php b/core/lib/Drupal/Core/Entity/Query/QueryFactory.php index d9b57bc..22c7799 100644 --- a/core/lib/Drupal/Core/Entity/Query/QueryFactory.php +++ b/core/lib/Drupal/Core/Entity/Query/QueryFactory.php @@ -7,24 +7,24 @@ namespace Drupal\Core\Entity\Query; -use Symfony\Component\DependencyInjection\ContainerInterface; +use Drupal\Core\Entity\EntityManager; +use Symfony\Component\DependencyInjection\ContainerAware; /** * Factory class Creating entity query objects. */ -class QueryFactory { +class QueryFactory extends ContainerAware { /** - * var \Symfony\Component\DependencyInjection\ContainerInterface + * @var \Drupal\Core\Entity\EntityManager */ - protected $container; - + protected $entityManager; /** - * @param \Symfony\Component\DependencyInjection\ContainerInterface $container + * @param \Drupal\Core\Entity\EntityManager $entity_manager */ - function __construct(ContainerInterface $container) { - $this->container = $container; + public function __construct(EntityManager $entity_manager) { + $this->entityManager = $entity_manager; } /** @@ -33,8 +33,8 @@ function __construct(ContainerInterface $container) { * @return QueryInterface */ public function get($entity_type, $conjunction = 'AND') { - $service_name = drupal_container()->get('plugin.manager.entity')->getStorageController($entity_type)->getQueryServicename(); - return $this->container->get($service_name)->get($entity_type, $conjunction); + $service_name = $this->entityManager->getStorageController($entity_type)->getQueryServicename(); + return $this->container->get($service_name)->get($entity_type, $conjunction, $this->entityManager); } } diff --git a/core/modules/config/tests/config_test/lib/Drupal/config_test/Plugin/Core/Entity/ConfigQueryTest.php b/core/modules/config/tests/config_test/lib/Drupal/config_test/Plugin/Core/Entity/ConfigQueryTest.php new file mode 100644 index 0000000..fe9ed9b --- /dev/null +++ b/core/modules/config/tests/config_test/lib/Drupal/config_test/Plugin/Core/Entity/ConfigQueryTest.php @@ -0,0 +1,43 @@ +entityType; + // @todo change to a method call once http://drupal.org/node/1892462 is in. $entity_info = entity_get_info($entity_type); if (!isset($entity_info['base_table'])) { throw new QueryException("No base table, invalid query."); diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/ConfigEntityQueryTest.php b/core/modules/system/lib/Drupal/system/Tests/Entity/ConfigEntityQueryTest.php new file mode 100644 index 0000000..472725d --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/ConfigEntityQueryTest.php @@ -0,0 +1,408 @@ + 'Config Entity Query', + 'description' => 'Tests Config Entity Query functionality.', + 'group' => 'Configuration', + ); + } + + protected function setUp() { + parent::setUp(); + $this->entities = array(); + $this->enableModules(array('entity'), TRUE); + $this->factory = $this->container->get('entity.query'); + + $entity = entity_create('config_query_test', array( + 'label' => $this->randomName(), + 'id' => 1, + 'number' => 31, + )); + $this->entities[] = $entity; + $entity->enforceIsNew(); + $entity->save(); + + $entity = entity_create('config_query_test', array( + 'label' => $this->randomName(), + 'id' => 2, + 'number' => 41, + )); + $this->entities[] = $entity; + $entity->enforceIsNew(); + $entity->save(); + + $entity = entity_create('config_query_test', array( + 'label' => 'test_prefix_' . $this->randomName(), + 'id' => 3, + 'number' => 59, + ) + ); + $this->entities[] = $entity; + $entity->enforceIsNew(); + $entity->save(); + + $entity = entity_create('config_query_test', array( + 'label' => $this->randomName() . '_test_suffix', + 'id' => 4, + 'number' => 26, + )); + $this->entities[] = $entity; + $entity->enforceIsNew(); + $entity->save(); + + $entity = entity_create('config_query_test', array( + 'label' => $this->randomName() . '_test_contains_' . $this->randomName(), + 'id' => 5, + 'number' => 53, + )); + $this->entities[] = $entity; + $entity->enforceIsNew(); + $entity->save(); + } + + /** + * Test basic functionality. + */ + public function testConfigEntityQuery() { + // Run a test without any condition. + $this->queryResults = $this->factory->get('config_query_test') + ->execute(); + $this->assertResults(array(1, 2, 3, 4, 5)); + // No conditions, OR. + $this->queryResults = $this->factory->get('config_query_test', 'OR') + ->execute(); + $this->assertResults(array(1, 2, 3, 4, 5)); + + // Filter by ID with equality. + $this->queryResults = $this->factory->get('config_query_test') + ->condition('id', 3) + ->execute(); + $this->assertResults(array(3)); + + // Filter by label with a known prefix. + $this->queryResults = $this->factory->get('config_query_test') + ->condition('label', 'test_prefix', 'STARTS_WITH') + ->execute(); + $this->assertResults(array(3)); + + // Filter by label with a known suffix. + $this->queryResults = $this->factory->get('config_query_test') + ->condition('label', 'test_suffix', 'ENDS_WITH') + ->execute(); + $this->assertResults(array(4)); + + // Filter by label with a known containing word. + $this->queryResults = $this->factory->get('config_query_test') + ->condition('label', 'test_contains', 'CONTAINS') + ->execute(); + $this->assertResults(array(5)); + + // Filter by ID with the IN operator. + $this->queryResults = $this->factory->get('config_query_test') + ->condition('id', array(2, 3), 'IN') + ->execute(); + $this->assertResults(array(2, 3)); + + // Filter by ID with the implicit IN operator. + $this->queryResults = $this->factory->get('config_query_test') + ->condition('id', array(2, 3)) + ->execute(); + $this->assertResults(array(2, 3)); + + // Filter by ID with the > operator. + $this->queryResults = $this->factory->get('config_query_test') + ->condition('id', 3, '>') + ->execute(); + $this->assertResults(array(4, 5)); + + // Filter by ID with the >= operator. + $this->queryResults = $this->factory->get('config_query_test') + ->condition('id', 3, '>=') + ->execute(); + $this->assertResults(array(3, 4, 5)); + + // Filter by ID with the < operator. + $this->queryResults = $this->factory->get('config_query_test') + ->condition('id', 3, '<') + ->execute(); + $this->assertResults(array(1, 2)); + + // Filter by ID with the <= operator. + $this->queryResults = $this->factory->get('config_query_test') + ->condition('id', 3, '<=') + ->execute(); + $this->assertResults(array(1, 2, 3)); + + // Filter by two conditions on the same field. + $this->queryResults = $this->factory->get('config_query_test') + ->condition('label', 'test_pref', 'STARTS_WITH') + ->condition('label', 'test_prefix', 'STARTS_WITH') + ->execute(); + $this->assertResults(array(3)); + + // Filter by two conditions on different fields. The first query matches for + // a different ID, so the result is empty. + $this->queryResults = $this->factory->get('config_query_test') + ->condition('label', 'test_prefix', 'STARTS_WITH') + ->condition('id', 5) + ->execute(); + $this->assertResults(array()); + + // Filter by two different conditions on different fields. This time the + // first condition matches on one item, but the second one does as well. + $this->queryResults = $this->factory->get('config_query_test') + ->condition('label', 'test_prefix', 'STARTS_WITH') + ->condition('id', 3) + ->execute(); + $this->assertResults(array(3)); + + // Filter by two different conditions, of which the first one matches for + // every entry, the second one as well, but just the third one filters so + // that just two are left. + $this->queryResults = $this->factory->get('config_query_test') + ->condition('id', 1, '>=') + ->condition('number', 10, '>=') + ->condition('number', 50, '>=') + ->execute(); + $this->assertResults(array(3, 5)); + + // Filter with an OR condition group. + $this->queryResults = $this->factory->get('config_query_test', 'OR') + ->condition('id', 1) + ->condition('id', 2) + ->execute(); + $this->assertResults(array(1, 2)); + + // Simplify it with IN. + $this->queryResults = $this->factory->get('config_query_test') + ->condition('id', array(1, 2)) + ->execute(); + $this->assertResults(array(1, 2)); + // Try explicit IN. + $this->queryResults = $this->factory->get('config_query_test') + ->condition('id', array(1, 2), 'IN') + ->execute(); + $this->assertResults(array(1, 2)); + // Try not IN. + $this->queryResults = $this->factory->get('config_query_test') + ->condition('id', array(1, 2), 'NOT IN') + ->execute(); + $this->assertResults(array(3, 4, 5)); + + // Filter with an OR condition group on different fields. + $this->queryResults = $this->factory->get('config_query_test', 'OR') + ->condition('id', 1) + ->condition('number', 41) + ->execute(); + $this->assertResults(array(1, 2)); + + // Filter with an OR condition group on different fields but matching on the + // same entity. + $this->queryResults = $this->factory->get('config_query_test', 'OR') + ->condition('id', 1) + ->condition('number', 31) + ->execute(); + $this->assertResults(array(1)); + + // NO simple conditions, YES complex conditions, 'AND'. + $query = $this->factory->get('config_query_test', 'AND'); + $and_condition_1 = $query->orConditionGroup() + ->condition('id', 2) + ->condition('label', $this->entities[0]->label); + $and_condition_2 = $query->orConditionGroup() + ->condition('id', 1) + ->condition('label', $this->entities[3]->label); + $this->queryResults = $query + ->condition($and_condition_1) + ->condition($and_condition_2) + ->execute(); + $this->assertResults(array(1)); + + // NO simple conditions, YES complex conditions, 'OR'. + $query = $this->factory->get('config_query_test', 'OR'); + $and_condition_1 = $query->andConditionGroup() + ->condition('id', 1) + ->condition('label', $this->entities[0]->label); + $and_condition_2 = $query->andConditionGroup() + ->condition('id', 2) + ->condition('label', $this->entities[1]->label); + $this->queryResults = $query + ->condition($and_condition_1) + ->condition($and_condition_2) + ->execute(); + $this->assertResults(array(1, 2)); + + // YES simple conditions, YES complex conditions, 'AND'. + $query = $this->factory->get('config_query_test', 'AND'); + $and_condition_1 = $query->orConditionGroup() + ->condition('id', 2) + ->condition('label', $this->entities[0]->label); + $and_condition_2 = $query->orConditionGroup() + ->condition('id', 1) + ->condition('label', $this->entities[3]->label); + $this->queryResults = $query + ->condition('number', 31) + ->condition($and_condition_1) + ->condition($and_condition_2) + ->execute(); + $this->assertResults(array(1)); + + // YES simple conditions, YES complex conditions, 'OR'. + $query = $this->factory->get('config_query_test', 'OR'); + $and_condition_1 = $query->orConditionGroup() + ->condition('id', 2) + ->condition('label', $this->entities[0]->label); + $and_condition_2 = $query->orConditionGroup() + ->condition('id', 1) + ->condition('label', $this->entities[3]->label); + $this->queryResults = $query + ->condition('number', 53) + ->condition($and_condition_1) + ->condition($and_condition_2) + ->execute(); + $this->assertResults(array(1, 2, 4, 5)); + } + + /** + * Test count query. + */ + protected function testCount() { + // Test count on no conditions. + $count = $this->factory->get('config_query_test') + ->count() + ->execute(); + $this->assertIdentical($count, count($this->entities)); + + // Test count on a complex query. + $query = $this->factory->get('config_query_test', 'OR'); + $and_condition_1 = $query->andConditionGroup() + ->condition('id', 1) + ->condition('label', $this->entities[0]->label); + $and_condition_2 = $query->andConditionGroup() + ->condition('id', 2) + ->condition('label', $this->entities[1]->label); + $count = $query + ->condition($and_condition_1) + ->condition($and_condition_2) + ->count() + ->execute(); + $this->assertIdentical($count, 2); + } + + /** + * Tests sorting and range on config entity queries. + */ + protected function testSortRange() { + // Sort by simple ascending/descending. + $this->queryResults = $this->factory->get('config_query_test') + ->sort('number', 'DESC') + ->execute(); + $this->assertIdentical(array_keys($this->queryResults), array(3, 5, 2, 1, 4)); + + $this->queryResults = $this->factory->get('config_query_test') + ->sort('number', 'ASC') + ->execute(); + $this->assertIdentical(array_keys($this->queryResults), array(4, 1, 2, 5, 3)); + + // Apply some filters and sort. + $this->queryResults = $this->factory->get('config_query_test') + ->condition('id', 3, '>') + ->sort('number', 'DESC') + ->execute(); + $this->assertIdentical(array_keys($this->queryResults), array(5, 4)); + + $this->queryResults = $this->factory->get('config_query_test') + ->condition('id', 3, '>') + ->sort('number', 'ASC') + ->execute(); + $this->assertIdentical(array_keys($this->queryResults), array(4, 5)); + + // Apply a pager and sort. + $this->queryResults = $this->factory->get('config_query_test') + ->sort('number', 'DESC') + ->range(2, 2) + ->execute(); + $this->assertIdentical(array_keys($this->queryResults), array(2, 1)); + + $this->queryResults = $this->factory->get('config_query_test') + ->sort('number', 'ASC') + ->range(2, 2) + ->execute(); + $this->assertIdentical(array_keys($this->queryResults), array(2, 5)); + + // Add a range to a query without a start parameter. + $this->queryResults = $this->factory->get('config_query_test') + ->range(0, 3) + ->sort('id', 'ASC') + ->execute(); + $this->assertIdentical(array_keys($this->queryResults), array(1, 2, 3)); + + // Apply a pager with limit 4. + $this->queryResults = $this->factory->get('config_query_test') + ->pager(4, 0) + ->sort('id', 'ASC') + ->execute(); + $this->assertIdentical(array_keys($this->queryResults), array(1, 2, 3, 4)); + } + + /** + * Asserts the results as expected regardless of order. + * + * @param array $expected + * Array of expected entity IDs. + */ + function assertResults($expected) { + $this->assertIdentical(count($this->queryResults), count($expected)); + foreach ($expected as $value) { + // This also tests whether $this->queryResults[$value] is even set at + // all. + $this->assertIdentical($this->queryResults[$value], $value); + } + } + +}