From 6f8f239d8e495715f250d692a6747760cb1e0554 Mon Sep 17 00:00:00 2001 From: Nathaniel Catchpole <catch@35733.no-reply.drupal.org> Date: Thu, 17 May 2018 13:18:31 +0100 Subject: [PATCH] Issue #2968519 by alexpott, amateescu, martin107: The entity.query service is essentially deprecated because it relies on \Drupal\Core\Entity\Query\QueryFactory which is --- core/core.services.yml | 1 + .../Drupal/Core/Entity/Query/QueryFactory.php | 7 +- .../Field/ModerationStateFieldItemList.php | 2 +- .../field/tests/src/Kernel/BulkDeleteTest.php | 12 +- .../src/Functional/MediaRevisionTest.php | 5 +- .../src/Functional/MediaUiFunctionalTest.php | 5 +- .../FunctionalJavascript/MediaDisplayTest.php | 5 +- .../src/Unit/process/DedupeEntityTest.php | 2 +- .../process/MakeUniqueEntityFieldTest.php | 2 +- core/modules/options/options.module | 3 +- .../Core/Entity/ConfigEntityQueryTest.php | 153 ++++---- .../Core/Entity/EntityQueryAggregateTest.php | 90 +++-- .../Entity/EntityQueryRelationshipTest.php | 41 +-- .../Core/Entity/EntityQueryTest.php | 339 +++++++++++------- .../Listeners/DeprecationListenerTrait.php | 1 - 15 files changed, 375 insertions(+), 293 deletions(-) diff --git a/core/core.services.yml b/core/core.services.yml index ced39ccd59d9..d9388365ec98 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -904,6 +904,7 @@ services: arguments: ['@entity.manager'] calls: - [setContainer, ['@service_container']] + deprecated: The "%service_id%" service is deprecated. Use the 'entity_type.manager' service to get an entity type's storage object and then call \Drupal\Core\Entity\EntityStorageInterface::getQuery() or \Drupal\Core\Entity\EntityStorageInterface::getAggregateQuery() instead. See https://www.drupal.org/node/2849874 entity.query.config: class: Drupal\Core\Config\Entity\Query\QueryFactory arguments: ['@config.factory', '@keyvalue', '@config.manager'] diff --git a/core/lib/Drupal/Core/Entity/Query/QueryFactory.php b/core/lib/Drupal/Core/Entity/Query/QueryFactory.php index a3c14b277f47..79e80cd1f3d2 100644 --- a/core/lib/Drupal/Core/Entity/Query/QueryFactory.php +++ b/core/lib/Drupal/Core/Entity/Query/QueryFactory.php @@ -2,6 +2,8 @@ namespace Drupal\Core\Entity\Query; +@trigger_error('The ' . __NAMESPACE__ . '\QueryFactory class is deprecated in Drupal 8.3.0, will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityStorageInterface::getQuery() or \Drupal\Core\Entity\EntityStorageInterface::getAggregateQuery() instead. See https://www.drupal.org/node/2849874.', E_USER_DEPRECATED); + use Drupal\Core\Entity\EntityManagerInterface; use Symfony\Component\DependencyInjection\ContainerAwareInterface; use Symfony\Component\DependencyInjection\ContainerAwareTrait; @@ -12,11 +14,12 @@ * Any implementation of this service must call getQuery()/getAggregateQuery() * of the corresponding entity storage. * - * @see \Drupal\Core\Entity\EntityStorageBase::getQuery() - * * @deprecated in Drupal 8.3.0, will be removed before Drupal 9.0.0. Use * \Drupal\Core\Entity\EntityStorageInterface::getQuery() or * \Drupal\Core\Entity\EntityStorageInterface::getAggregateQuery() instead. + * + * @see https://www.drupal.org/node/2849874 + * @see \Drupal\Core\Entity\EntityStorageBase::getQuery() */ class QueryFactory implements ContainerAwareInterface { diff --git a/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php b/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php index 8b7afc9ebf17..c33ccacdeec1 100644 --- a/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php +++ b/core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php @@ -88,7 +88,7 @@ protected function loadContentModerationStateRevision(ContentEntityInterface $en $moderation_info = \Drupal::service('content_moderation.moderation_information'); $content_moderation_storage = \Drupal::entityTypeManager()->getStorage('content_moderation_state'); - $revisions = \Drupal::service('entity.query')->get('content_moderation_state') + $revisions = $content_moderation_storage->getQuery() ->condition('content_entity_type_id', $entity->getEntityTypeId()) ->condition('content_entity_id', $entity->id()) // Ensure the correct revision is loaded in scenarios where a revision is diff --git a/core/modules/field/tests/src/Kernel/BulkDeleteTest.php b/core/modules/field/tests/src/Kernel/BulkDeleteTest.php index f3532d36d811..8c3ee5f26cff 100644 --- a/core/modules/field/tests/src/Kernel/BulkDeleteTest.php +++ b/core/modules/field/tests/src/Kernel/BulkDeleteTest.php @@ -162,10 +162,11 @@ public function testDeleteField() { $bundle = reset($this->bundles); $field_storage = reset($this->fieldStorages); $field_name = $field_storage->getName(); - $factory = \Drupal::service('entity.query'); + $storage = \Drupal::entityTypeManager()->getStorage('entity_test'); // There are 10 entities of this bundle. - $found = $factory->get('entity_test') + $found = $storage + ->getQuery() ->condition('type', $bundle) ->execute(); $this->assertEqual(count($found), 10, 'Correct number of entities found before deleting'); @@ -181,7 +182,6 @@ public function testDeleteField() { $this->assertEqual($field->getTargetBundle(), $bundle, 'The deleted field is for the correct bundle'); // Check that the actual stored content did not change during delete. - $storage = \Drupal::entityManager()->getStorage($this->entityTypeId); /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */ $table_mapping = $storage->getTableMapping(); $table = $table_mapping->getDedicatedDataTableName($field_storage); @@ -194,7 +194,8 @@ public function testDeleteField() { } // There are 0 entities of this bundle with non-deleted data. - $found = $factory->get('entity_test') + $found = $storage + ->getQuery() ->condition('type', $bundle) ->condition("$field_name.deleted", 0) ->execute(); @@ -202,7 +203,8 @@ public function testDeleteField() { // There are 10 entities of this bundle when deleted fields are allowed, and // their values are correct. - $found = $factory->get('entity_test') + $found = $storage + ->getQuery() ->condition('type', $bundle) ->condition("$field_name.deleted", 1) ->sort('id') diff --git a/core/modules/media/tests/src/Functional/MediaRevisionTest.php b/core/modules/media/tests/src/Functional/MediaRevisionTest.php index 265e50fcb84f..001d59b07c95 100644 --- a/core/modules/media/tests/src/Functional/MediaRevisionTest.php +++ b/core/modules/media/tests/src/Functional/MediaRevisionTest.php @@ -190,8 +190,9 @@ protected function assertRevisionCount(EntityInterface $entity, $expected_revisi $entity_type = $entity->getEntityType(); $count = $this->container - ->get('entity.query') - ->get($entity_type->id()) + ->get('entity_type.manager') + ->getStorage($entity_type->id()) + ->getQuery() ->count() ->allRevisions() ->condition($entity_type->getKey('id'), $entity->id()) diff --git a/core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php b/core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php index 3c943120148c..c885ea4f5ed3 100644 --- a/core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php +++ b/core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php @@ -61,7 +61,10 @@ public function testMediaWithOnlyOneMediaType() { $source_field = $this->randomString(); $page->fillField('field_media_test[0][value]', $source_field); $page->pressButton('Save'); - $media_id = $this->container->get('entity.query')->get('media')->execute(); + $media_id = $this->container->get('entity_type.manager') + ->getStorage('media') + ->getQuery() + ->execute(); $media_id = reset($media_id); /** @var \Drupal\media\MediaInterface $media */ $media = $this->container->get('entity_type.manager') diff --git a/core/modules/media/tests/src/FunctionalJavascript/MediaDisplayTest.php b/core/modules/media/tests/src/FunctionalJavascript/MediaDisplayTest.php index 47bcf287dde4..fd508cf0fd54 100644 --- a/core/modules/media/tests/src/FunctionalJavascript/MediaDisplayTest.php +++ b/core/modules/media/tests/src/FunctionalJavascript/MediaDisplayTest.php @@ -73,7 +73,10 @@ public function testMediaDisplay() { $this->assertNotEmpty($result); $page->fillField('field_media_image[0][alt]', 'Image Alt Text 1'); $page->pressButton('Save'); - $image_media_id = $this->container->get('entity.query')->get('media') + $image_media_id = $this->container + ->get('entity_type.manager') + ->getStorage('media') + ->getQuery() ->sort('mid', 'DESC') ->execute(); $image_media_id = reset($image_media_id); diff --git a/core/modules/migrate/tests/src/Unit/process/DedupeEntityTest.php b/core/modules/migrate/tests/src/Unit/process/DedupeEntityTest.php index 2d0157ad3cd6..6dd8795305a7 100644 --- a/core/modules/migrate/tests/src/Unit/process/DedupeEntityTest.php +++ b/core/modules/migrate/tests/src/Unit/process/DedupeEntityTest.php @@ -17,7 +17,7 @@ class DedupeEntityTest extends MigrateProcessTestCase { /** * The mock entity query. * - * @var \Drupal\Core\Entity\Query\QueryInterface|\Drupal\Core\Entity\Query\QueryFactory + * @var \Drupal\Core\Entity\Query\QueryInterface */ protected $entityQuery; diff --git a/core/modules/migrate/tests/src/Unit/process/MakeUniqueEntityFieldTest.php b/core/modules/migrate/tests/src/Unit/process/MakeUniqueEntityFieldTest.php index 0b4ebd5f92c8..9d3ab06da56f 100644 --- a/core/modules/migrate/tests/src/Unit/process/MakeUniqueEntityFieldTest.php +++ b/core/modules/migrate/tests/src/Unit/process/MakeUniqueEntityFieldTest.php @@ -16,7 +16,7 @@ class MakeUniqueEntityFieldTest extends MigrateProcessTestCase { /** * The mock entity query. * - * @var \Drupal\Core\Entity\Query\QueryInterface|\Drupal\Core\Entity\Query\QueryFactory + * @var \Drupal\Core\Entity\Query\QueryInterface */ protected $entityQuery; diff --git a/core/modules/options/options.module b/core/modules/options/options.module index 7cf36355293d..c4ebf84f8371 100644 --- a/core/modules/options/options.module +++ b/core/modules/options/options.module @@ -122,8 +122,7 @@ function options_field_storage_config_update_forbid(FieldStorageConfigInterface */ function _options_values_in_use($entity_type, $field_name, $values) { if ($values) { - $factory = \Drupal::service('entity.query'); - $result = $factory->get($entity_type) + $result = \Drupal::entityQuery($entity_type) ->condition($field_name . '.value', $values, 'IN') ->count() ->accessCheck(FALSE) diff --git a/core/tests/Drupal/KernelTests/Core/Entity/ConfigEntityQueryTest.php b/core/tests/Drupal/KernelTests/Core/Entity/ConfigEntityQueryTest.php index 8dc101a4659e..839917aff0d4 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/ConfigEntityQueryTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/ConfigEntityQueryTest.php @@ -31,10 +31,17 @@ class ConfigEntityQueryTest extends KernelTestBase { /** * The query factory used to construct all queries in the test. * - * @var \Drupal\Core\Entity\Query\QueryFactory + * @var \Drupal\Core\Config\Entity\Query\QueryFactory */ protected $factory; + /** + * The entity storage used for testing. + * + * @var \Drupal\Core\Entity\EntityStorageInterface + */ + protected $entityStorage; + /** * Stores all config entities created for the test. * @@ -46,7 +53,7 @@ protected function setUp() { parent::setUp(); $this->entities = []; - $this->factory = $this->container->get('entity.query'); + $this->entityStorage = $this->container->get('entity_type.manager')->getStorage('config_query_test'); // These two are here to make sure that matchArray needs to go over several // non-matches on every levels. @@ -114,82 +121,82 @@ protected function setUp() { */ public function testConfigEntityQuery() { // Run a test without any condition. - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->execute(); $this->assertResults(['1', '2', '3', '4', '5']); // No conditions, OR. - $this->queryResults = $this->factory->get('config_query_test', 'OR') + $this->queryResults = $this->entityStorage->getQuery('OR') ->execute(); $this->assertResults(['1', '2', '3', '4', '5']); // Filter by ID with equality. - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->condition('id', '3') ->execute(); $this->assertResults(['3']); // Filter by label with a known prefix. - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->condition('label', 'test_prefix', 'STARTS_WITH') ->execute(); $this->assertResults(['3']); // Filter by label with a known suffix. - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->condition('label', 'test_suffix', 'ENDS_WITH') ->execute(); $this->assertResults(['4']); // Filter by label with a known containing word. - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->condition('label', 'test_contains', 'CONTAINS') ->execute(); $this->assertResults(['5']); // Filter by ID with the IN operator. - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->condition('id', ['2', '3'], 'IN') ->execute(); $this->assertResults(['2', '3']); // Filter by ID with the implicit IN operator. - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->condition('id', ['2', '3']) ->execute(); $this->assertResults(['2', '3']); // Filter by ID with the > operator. - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->condition('id', '3', '>') ->execute(); $this->assertResults(['4', '5']); // Filter by ID with the >= operator. - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->condition('id', '3', '>=') ->execute(); $this->assertResults(['3', '4', '5']); // Filter by ID with the <> operator. - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->condition('id', '3', '<>') ->execute(); $this->assertResults(['1', '2', '4', '5']); // Filter by ID with the < operator. - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->condition('id', '3', '<') ->execute(); $this->assertResults(['1', '2']); // Filter by ID with the <= operator. - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->condition('id', '3', '<=') ->execute(); $this->assertResults(['1', '2', '3']); // Filter by two conditions on the same field. - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->condition('label', 'test_pref', 'STARTS_WITH') ->condition('label', 'test_prefix', 'STARTS_WITH') ->execute(); @@ -197,7 +204,7 @@ public function testConfigEntityQuery() { // 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') + $this->queryResults = $this->entityStorage->getQuery() ->condition('label', 'test_prefix', 'STARTS_WITH') ->condition('id', '5') ->execute(); @@ -205,7 +212,7 @@ public function testConfigEntityQuery() { // 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') + $this->queryResults = $this->entityStorage->getQuery() ->condition('label', 'test_prefix', 'STARTS_WITH') ->condition('id', '3') ->execute(); @@ -214,7 +221,7 @@ public function testConfigEntityQuery() { // 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') + $this->queryResults = $this->entityStorage->getQuery() ->condition('id', '1', '>=') ->condition('number', 10, '>=') ->condition('number', 50, '>=') @@ -222,30 +229,30 @@ public function testConfigEntityQuery() { $this->assertResults(['3', '5']); // Filter with an OR condition group. - $this->queryResults = $this->factory->get('config_query_test', 'OR') + $this->queryResults = $this->entityStorage->getQuery('OR') ->condition('id', 1) ->condition('id', '2') ->execute(); $this->assertResults(['1', '2']); // Simplify it with IN. - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->condition('id', ['1', '2']) ->execute(); $this->assertResults(['1', '2']); // Try explicit IN. - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->condition('id', ['1', '2'], 'IN') ->execute(); $this->assertResults(['1', '2']); // Try not IN. - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->condition('id', ['1', '2'], 'NOT IN') ->execute(); $this->assertResults(['3', '4', '5']); // Filter with an OR condition group on different fields. - $this->queryResults = $this->factory->get('config_query_test', 'OR') + $this->queryResults = $this->entityStorage->getQuery('OR') ->condition('id', 1) ->condition('number', 41) ->execute(); @@ -253,14 +260,14 @@ public function testConfigEntityQuery() { // Filter with an OR condition group on different fields but matching on the // same entity. - $this->queryResults = $this->factory->get('config_query_test', 'OR') + $this->queryResults = $this->entityStorage->getQuery('OR') ->condition('id', 1) ->condition('number', 31) ->execute(); $this->assertResults(['1']); // NO simple conditions, YES complex conditions, 'AND'. - $query = $this->factory->get('config_query_test', 'AND'); + $query = $this->entityStorage->getQuery('AND'); $and_condition_1 = $query->orConditionGroup() ->condition('id', '2') ->condition('label', $this->entities[0]->label); @@ -274,7 +281,7 @@ public function testConfigEntityQuery() { $this->assertResults(['1']); // NO simple conditions, YES complex conditions, 'OR'. - $query = $this->factory->get('config_query_test', 'OR'); + $query = $this->entityStorage->getQuery('OR'); $and_condition_1 = $query->andConditionGroup() ->condition('id', 1) ->condition('label', $this->entities[0]->label); @@ -288,7 +295,7 @@ public function testConfigEntityQuery() { $this->assertResults(['1', '2']); // YES simple conditions, YES complex conditions, 'AND'. - $query = $this->factory->get('config_query_test', 'AND'); + $query = $this->entityStorage->getQuery('AND'); $and_condition_1 = $query->orConditionGroup() ->condition('id', '2') ->condition('label', $this->entities[0]->label); @@ -303,7 +310,7 @@ public function testConfigEntityQuery() { $this->assertResults(['1']); // YES simple conditions, YES complex conditions, 'OR'. - $query = $this->factory->get('config_query_test', 'OR'); + $query = $this->entityStorage->getQuery('OR'); $and_condition_1 = $query->orConditionGroup() ->condition('id', '2') ->condition('label', $this->entities[0]->label); @@ -318,22 +325,22 @@ public function testConfigEntityQuery() { $this->assertResults(['1', '2', '4', '5']); // Test the exists and notExists conditions. - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->exists('id') ->execute(); $this->assertResults(['1', '2', '3', '4', '5']); - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->exists('non-existent') ->execute(); $this->assertResults([]); - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->notExists('id') ->execute(); $this->assertResults([]); - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->notExists('non-existent') ->execute(); $this->assertResults(['1', '2', '3', '4', '5']); @@ -353,43 +360,43 @@ public function testStringIdConditions() { $entity->save(); // Test 'STARTS_WITH' condition. - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->condition('id', 'foo.bar', 'STARTS_WITH') ->execute(); $this->assertResults(['foo.bar']); - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->condition('id', 'f', 'STARTS_WITH') ->execute(); $this->assertResults(['foo.bar']); - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->condition('id', 'miss', 'STARTS_WITH') ->execute(); $this->assertResults([]); // Test 'CONTAINS' condition. - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->condition('id', 'foo.bar', 'CONTAINS') ->execute(); $this->assertResults(['foo.bar']); - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->condition('id', 'oo.ba', 'CONTAINS') ->execute(); $this->assertResults(['foo.bar']); - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->condition('id', 'miss', 'CONTAINS') ->execute(); $this->assertResults([]); // Test 'ENDS_WITH' condition. - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->condition('id', 'foo.bar', 'ENDS_WITH') ->execute(); $this->assertResults(['foo.bar']); - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->condition('id', 'r', 'ENDS_WITH') ->execute(); $this->assertResults(['foo.bar']); - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->condition('id', 'miss', 'ENDS_WITH') ->execute(); $this->assertResults([]); @@ -400,13 +407,13 @@ public function testStringIdConditions() { */ public function testCount() { // Test count on no conditions. - $count = $this->factory->get('config_query_test') + $count = $this->entityStorage->getQuery() ->count() ->execute(); $this->assertIdentical($count, count($this->entities)); // Test count on a complex query. - $query = $this->factory->get('config_query_test', 'OR'); + $query = $this->entityStorage->getQuery('OR'); $and_condition_1 = $query->andConditionGroup() ->condition('id', 1) ->condition('label', $this->entities[0]->label); @@ -426,51 +433,51 @@ public function testCount() { */ public function testSortRange() { // Sort by simple ascending/descending. - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->sort('number', 'DESC') ->execute(); $this->assertIdentical(array_values($this->queryResults), ['3', '5', '2', '1', '4']); - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->sort('number', 'ASC') ->execute(); $this->assertIdentical(array_values($this->queryResults), ['4', '1', '2', '5', '3']); // Apply some filters and sort. - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->condition('id', '3', '>') ->sort('number', 'DESC') ->execute(); $this->assertIdentical(array_values($this->queryResults), ['5', '4']); - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->condition('id', '3', '>') ->sort('number', 'ASC') ->execute(); $this->assertIdentical(array_values($this->queryResults), ['4', '5']); // Apply a pager and sort. - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->sort('number', 'DESC') ->range('2', '2') ->execute(); $this->assertIdentical(array_values($this->queryResults), ['2', '1']); - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->sort('number', 'ASC') ->range('2', '2') ->execute(); $this->assertIdentical(array_values($this->queryResults), ['2', '5']); // Add a range to a query without a start parameter. - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->range(0, '3') ->sort('id', 'ASC') ->execute(); $this->assertIdentical(array_values($this->queryResults), ['1', '2', '3']); // Apply a pager with limit 4. - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->pager('4', 0) ->sort('id', 'ASC') ->execute(); @@ -488,28 +495,28 @@ public function testTableSort() { // Sort key: id // Sorting with 'DESC' upper case - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->tableSort($header) ->sort('id', 'DESC') ->execute(); $this->assertIdentical(array_values($this->queryResults), ['5', '4', '3', '2', '1']); // Sorting with 'ASC' upper case - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->tableSort($header) ->sort('id', 'ASC') ->execute(); $this->assertIdentical(array_values($this->queryResults), ['1', '2', '3', '4', '5']); // Sorting with 'desc' lower case - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->tableSort($header) ->sort('id', 'desc') ->execute(); $this->assertIdentical(array_values($this->queryResults), ['5', '4', '3', '2', '1']); // Sorting with 'asc' lower case - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->tableSort($header) ->sort('id', 'asc') ->execute(); @@ -517,28 +524,28 @@ public function testTableSort() { // Sort key: number // Sorting with 'DeSc' mixed upper and lower case - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->tableSort($header) ->sort('number', 'DeSc') ->execute(); $this->assertIdentical(array_values($this->queryResults), ['3', '5', '2', '1', '4']); // Sorting with 'AsC' mixed upper and lower case - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->tableSort($header) ->sort('number', 'AsC') ->execute(); $this->assertIdentical(array_values($this->queryResults), ['4', '1', '2', '5', '3']); // Sorting with 'dEsC' mixed upper and lower case - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->tableSort($header) ->sort('number', 'dEsC') ->execute(); $this->assertIdentical(array_values($this->queryResults), ['3', '5', '2', '1', '4']); // Sorting with 'aSc' mixed upper and lower case - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->tableSort($header) ->sort('number', 'aSc') ->execute(); @@ -549,53 +556,53 @@ public function testTableSort() { * Tests dotted path matching. */ public function testDotted() { - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->condition('array.level1.*', 1) ->execute(); $this->assertResults(['1', '3']); - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->condition('*.level1.level2', 2) ->execute(); $this->assertResults(['2', '4']); - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->condition('array.level1.*', 3) ->execute(); $this->assertResults(['5']); - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->condition('array.level1.level2', 3) ->execute(); $this->assertResults(['5']); // Make sure that values on the wildcard level do not match if there are // sub-keys defined. This must not find anything even if entity 2 has a // top-level key number with value 41. - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->condition('*.level1.level2', 41) ->execute(); $this->assertResults([]); // Make sure that "IS NULL" and "IS NOT NULL" work correctly with // array-valued fields/keys. $all = ['1', '2', '3', '4', '5']; - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->exists('array.level1.level2') ->execute(); $this->assertResults($all); - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->exists('array.level1') ->execute(); $this->assertResults($all); - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->exists('array') ->execute(); $this->assertResults($all); - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->notExists('array.level1.level2') ->execute(); $this->assertResults([]); - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->notExists('array.level1') ->execute(); $this->assertResults([]); - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->notExists('array') ->execute(); $this->assertResults([]); @@ -606,12 +613,12 @@ public function testDotted() { */ public function testCaseSensitivity() { // Filter by label with a known containing case-sensitive word. - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->condition('label', 'TEST', 'CONTAINS') ->execute(); $this->assertResults(['3', '4', '5']); - $this->queryResults = $this->factory->get('config_query_test') + $this->queryResults = $this->entityStorage->getQuery() ->condition('label', 'test', 'CONTAINS') ->execute(); $this->assertResults(['3', '4', '5']); diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityQueryAggregateTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityQueryAggregateTest.php index b6362fbcae6f..9e8de3617360 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntityQueryAggregateTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityQueryAggregateTest.php @@ -23,7 +23,7 @@ class EntityQueryAggregateTest extends EntityKernelTestBase { /** * The entity_test storage to create the test entities. * - * @var \Drupal\entity_test\EntityTestStorage + * @var \Drupal\Core\Entity\EntityStorageInterface */ protected $entityStorage; @@ -34,18 +34,10 @@ class EntityQueryAggregateTest extends EntityKernelTestBase { */ protected $queryResult; - /** - * The query factory to create entity queries. - * - * @var \Drupal\Core\Entity\Query\QueryFactory - */ - public $factory; - protected function setUp() { parent::setUp(); $this->entityStorage = $this->entityManager->getStorage('entity_test'); - $this->factory = $this->container->get('entity.query'); // Add some fieldapi fields to be used in the test. for ($i = 1; $i <= 2; $i++) { @@ -120,7 +112,7 @@ protected function setUp() { */ public function testAggregation() { // Apply a simple groupby. - $this->queryResult = $this->factory->getAggregate('entity_test') + $this->queryResult = $this->entityStorage->getAggregateQuery() ->groupBy('user_id') ->execute(); @@ -139,14 +131,14 @@ public function testAggregation() { // Apply a simple aggregation for different aggregation functions. foreach ($function_expected as $aggregation_function => $expected) { - $this->queryResult = $this->factory->getAggregate('entity_test') + $this->queryResult = $this->entityStorage->getAggregateQuery() ->aggregate('id', $aggregation_function) ->execute(); $this->assertEqual($this->queryResult, $expected); } // Apply aggregation and groupby on the same query. - $this->queryResult = $this->factory->getAggregate('entity_test') + $this->queryResult = $this->entityStorage->getAggregateQuery() ->aggregate('id', 'COUNT') ->groupBy('user_id') ->execute(); @@ -157,7 +149,7 @@ public function testAggregation() { ]); // Apply aggregation and a condition which matches. - $this->queryResult = $this->factory->getAggregate('entity_test') + $this->queryResult = $this->entityStorage->getAggregateQuery() ->aggregate('id', 'COUNT') ->groupBy('id') ->conditionAggregate('id', 'COUNT', 8) @@ -165,14 +157,14 @@ public function testAggregation() { $this->assertResults([]); // Don't call aggregate to test the implicit aggregate call. - $this->queryResult = $this->factory->getAggregate('entity_test') + $this->queryResult = $this->entityStorage->getAggregateQuery() ->groupBy('id') ->conditionAggregate('id', 'COUNT', 8) ->execute(); $this->assertResults([]); // Apply aggregation and a condition which matches. - $this->queryResult = $this->factory->getAggregate('entity_test') + $this->queryResult = $this->entityStorage->getAggregateQuery() ->aggregate('id', 'count') ->groupBy('id') ->conditionAggregate('id', 'COUNT', 6) @@ -181,7 +173,7 @@ public function testAggregation() { // Apply aggregation, a groupby and a condition which matches partially via // the operator '='. - $this->queryResult = $this->factory->getAggregate('entity_test') + $this->queryResult = $this->entityStorage->getAggregateQuery() ->aggregate('id', 'count') ->conditionAggregate('id', 'count', 2) ->groupBy('user_id') @@ -190,7 +182,7 @@ public function testAggregation() { // Apply aggregation, a groupby and a condition which matches partially via // the operator '>'. - $this->queryResult = $this->factory->getAggregate('entity_test') + $this->queryResult = $this->entityStorage->getAggregateQuery() ->aggregate('id', 'count') ->conditionAggregate('id', 'COUNT', 1, '>') ->groupBy('user_id') @@ -202,20 +194,20 @@ public function testAggregation() { // Apply aggregation and a sort. This might not be useful, but have a proper // test coverage. - $this->queryResult = $this->factory->getAggregate('entity_test') + $this->queryResult = $this->entityStorage->getAggregateQuery() ->aggregate('id', 'COUNT') ->sortAggregate('id', 'COUNT') ->execute(); $this->assertSortedResults([['id_count' => 6]]); // Don't call aggregate to test the implicit aggregate call. - $this->queryResult = $this->factory->getAggregate('entity_test') + $this->queryResult = $this->entityStorage->getAggregateQuery() ->sortAggregate('id', 'COUNT') ->execute(); $this->assertSortedResults([['id_count' => 6]]); // Apply aggregation, groupby and a sort descending. - $this->queryResult = $this->factory->getAggregate('entity_test') + $this->queryResult = $this->entityStorage->getAggregateQuery() ->aggregate('id', 'COUNT') ->groupBy('user_id') ->sortAggregate('id', 'COUNT', 'DESC') @@ -227,7 +219,7 @@ public function testAggregation() { ]); // Apply aggregation, groupby and a sort ascending. - $this->queryResult = $this->factory->getAggregate('entity_test') + $this->queryResult = $this->entityStorage->getAggregateQuery() ->aggregate('id', 'COUNT') ->groupBy('user_id') ->sortAggregate('id', 'COUNT', 'ASC') @@ -240,7 +232,7 @@ public function testAggregation() { // Apply aggregation, groupby, an aggregation condition and a sort with the // operator '='. - $this->queryResult = $this->factory->getAggregate('entity_test') + $this->queryResult = $this->entityStorage->getAggregateQuery() ->aggregate('id', 'COUNT') ->groupBy('user_id') ->sortAggregate('id', 'COUNT') @@ -250,7 +242,7 @@ public function testAggregation() { // Apply aggregation, groupby, an aggregation condition and a sort with the // operator '<' and order ASC. - $this->queryResult = $this->factory->getAggregate('entity_test') + $this->queryResult = $this->entityStorage->getAggregateQuery() ->aggregate('id', 'COUNT') ->groupBy('user_id') ->sortAggregate('id', 'COUNT', 'ASC') @@ -263,7 +255,7 @@ public function testAggregation() { // Apply aggregation, groupby, an aggregation condition and a sort with the // operator '<' and order DESC. - $this->queryResult = $this->factory->getAggregate('entity_test') + $this->queryResult = $this->entityStorage->getAggregateQuery() ->aggregate('id', 'COUNT') ->groupBy('user_id') ->sortAggregate('id', 'COUNT', 'DESC') @@ -277,7 +269,7 @@ public function testAggregation() { // Test aggregation/groupby support for fieldapi fields. // Just group by a fieldapi field. - $this->queryResult = $this->factory->getAggregate('entity_test') + $this->queryResult = $this->entityStorage->getAggregateQuery() ->groupBy('field_test_1') ->execute(); $this->assertResults([ @@ -287,7 +279,7 @@ public function testAggregation() { ]); // Group by a fieldapi field and aggregate a normal property. - $this->queryResult = $this->factory->getAggregate('entity_test') + $this->queryResult = $this->entityStorage->getAggregateQuery() ->aggregate('user_id', 'COUNT') ->groupBy('field_test_1') ->execute(); @@ -299,7 +291,7 @@ public function testAggregation() { ]); // Group by a normal property and aggregate a fieldapi field. - $this->queryResult = $this->factory->getAggregate('entity_test') + $this->queryResult = $this->entityStorage->getAggregateQuery() ->aggregate('field_test_1', 'COUNT') ->groupBy('user_id') ->execute(); @@ -310,7 +302,7 @@ public function testAggregation() { ['user_id' => 3, 'field_test_1_count' => 2], ]); - $this->queryResult = $this->factory->getAggregate('entity_test') + $this->queryResult = $this->entityStorage->getAggregateQuery() ->aggregate('field_test_1', 'SUM') ->groupBy('user_id') ->execute(); @@ -321,7 +313,7 @@ public function testAggregation() { ]); // Aggregate by two different fieldapi fields. - $this->queryResult = $this->factory->getAggregate('entity_test') + $this->queryResult = $this->entityStorage->getAggregateQuery() ->aggregate('field_test_1', 'SUM') ->aggregate('field_test_2', 'SUM') ->groupBy('user_id') @@ -333,7 +325,7 @@ public function testAggregation() { ]); // This time aggregate the same field twice. - $this->queryResult = $this->factory->getAggregate('entity_test') + $this->queryResult = $this->entityStorage->getAggregateQuery() ->aggregate('field_test_1', 'SUM') ->aggregate('field_test_1', 'COUNT') ->groupBy('user_id') @@ -345,7 +337,7 @@ public function testAggregation() { ]); // Group by and aggregate by a fieldapi field. - $this->queryResult = $this->factory->getAggregate('entity_test') + $this->queryResult = $this->entityStorage->getAggregateQuery() ->groupBy('field_test_1') ->aggregate('field_test_2', 'COUNT') ->execute(); @@ -357,7 +349,7 @@ public function testAggregation() { // Group by and aggregate by a fieldapi field and use multiple aggregate // functions. - $this->queryResult = $this->factory->getAggregate('entity_test') + $this->queryResult = $this->entityStorage->getAggregateQuery() ->groupBy('field_test_1') ->aggregate('field_test_2', 'COUNT') ->aggregate('field_test_2', 'SUM') @@ -370,7 +362,7 @@ public function testAggregation() { // Apply an aggregate condition for a fieldapi field and group by a simple // property. - $this->queryResult = $this->factory->getAggregate('entity_test') + $this->queryResult = $this->entityStorage->getAggregateQuery() ->conditionAggregate('field_test_1', 'COUNT', 3) ->groupBy('user_id') ->execute(); @@ -379,7 +371,7 @@ public function testAggregation() { ['user_id' => 3, 'field_test_1_count' => 2], ]); - $this->queryResult = $this->factory->getAggregate('entity_test') + $this->queryResult = $this->entityStorage->getAggregateQuery() ->aggregate('field_test_1', 'SUM') ->conditionAggregate('field_test_1', 'COUNT', 2, '>') ->groupBy('user_id') @@ -391,7 +383,7 @@ public function testAggregation() { // Apply an aggregate condition for a simple property and a group by a // fieldapi field. - $this->queryResult = $this->factory->getAggregate('entity_test') + $this->queryResult = $this->entityStorage->getAggregateQuery() ->conditionAggregate('user_id', 'COUNT', 2) ->groupBy('field_test_1') ->execute(); @@ -399,7 +391,7 @@ public function testAggregation() { ['field_test_1' => 1, 'user_id_count' => 2], ]); - $this->queryResult = $this->factory->getAggregate('entity_test') + $this->queryResult = $this->entityStorage->getAggregateQuery() ->conditionAggregate('user_id', 'COUNT', 2, '>') ->groupBy('field_test_1') ->execute(); @@ -409,14 +401,14 @@ public function testAggregation() { ]); // Apply an aggregate condition and a group by fieldapi fields. - $this->queryResult = $this->factory->getAggregate('entity_test') + $this->queryResult = $this->entityStorage->getAggregateQuery() ->groupBy('field_test_1') ->conditionAggregate('field_test_2', 'COUNT', 2) ->execute(); $this->assertResults([ ['field_test_1' => 1, 'field_test_2_count' => 2], ]); - $this->queryResult = $this->factory->getAggregate('entity_test') + $this->queryResult = $this->entityStorage->getAggregateQuery() ->groupBy('field_test_1') ->conditionAggregate('field_test_2', 'COUNT', 2, '>') ->execute(); @@ -427,7 +419,7 @@ public function testAggregation() { // Apply an aggregate condition and a group by fieldapi fields with multiple // conditions via AND. - $this->queryResult = $this->factory->getAggregate('entity_test') + $this->queryResult = $this->entityStorage->getAggregateQuery() ->groupBy('field_test_1') ->conditionAggregate('field_test_2', 'COUNT', 2) ->conditionAggregate('field_test_2', 'SUM', 8) @@ -436,7 +428,7 @@ public function testAggregation() { // Apply an aggregate condition and a group by fieldapi fields with multiple // conditions via OR. - $this->queryResult = $this->factory->getAggregate('entity_test', 'OR') + $this->queryResult = $this->entityStorage->getAggregateQuery('OR') ->groupBy('field_test_1') ->conditionAggregate('field_test_2', 'COUNT', 2) ->conditionAggregate('field_test_2', 'SUM', 8) @@ -448,7 +440,7 @@ public function testAggregation() { // Group by a normal property and aggregate a fieldapi field and sort by the // groupby field. - $this->queryResult = $this->factory->getAggregate('entity_test') + $this->queryResult = $this->entityStorage->getAggregateQuery() ->aggregate('field_test_1', 'COUNT') ->groupBy('user_id') ->sort('user_id', 'DESC') @@ -459,7 +451,7 @@ public function testAggregation() { ['user_id' => 1, 'field_test_1_count' => 1], ]); - $this->queryResult = $this->factory->getAggregate('entity_test') + $this->queryResult = $this->entityStorage->getAggregateQuery() ->aggregate('field_test_1', 'COUNT') ->groupBy('user_id') ->sort('user_id', 'ASC') @@ -470,7 +462,7 @@ public function testAggregation() { ['user_id' => 3, 'field_test_1_count' => 2], ]); - $this->queryResult = $this->factory->getAggregate('entity_test') + $this->queryResult = $this->entityStorage->getAggregateQuery() ->conditionAggregate('field_test_1', 'COUNT', 2, '>') ->groupBy('user_id') ->sort('user_id', 'ASC') @@ -482,7 +474,7 @@ public function testAggregation() { // Group by a normal property, aggregate a fieldapi field, and sort by the // aggregated field. - $this->queryResult = $this->factory->getAggregate('entity_test') + $this->queryResult = $this->entityStorage->getAggregateQuery() ->sortAggregate('field_test_1', 'COUNT', 'DESC') ->groupBy('user_id') ->execute(); @@ -492,7 +484,7 @@ public function testAggregation() { ['user_id' => 1, 'field_test_1_count' => 1], ]); - $this->queryResult = $this->factory->getAggregate('entity_test') + $this->queryResult = $this->entityStorage->getAggregateQuery() ->sortAggregate('field_test_1', 'COUNT', 'ASC') ->groupBy('user_id') ->execute(); @@ -503,7 +495,7 @@ public function testAggregation() { ]); // Group by and aggregate by fieldapi field, and sort by the groupby field. - $this->queryResult = $this->factory->getAggregate('entity_test') + $this->queryResult = $this->entityStorage->getAggregateQuery() ->groupBy('field_test_1') ->aggregate('field_test_2', 'COUNT') ->sort('field_test_1', 'ASC') @@ -514,7 +506,7 @@ public function testAggregation() { ['field_test_1' => 3, 'field_test_2_count' => 1], ]); - $this->queryResult = $this->factory->getAggregate('entity_test') + $this->queryResult = $this->entityStorage->getAggregateQuery() ->groupBy('field_test_1') ->aggregate('field_test_2', 'COUNT') ->sort('field_test_1', 'DESC') @@ -527,7 +519,7 @@ public function testAggregation() { // Groupby and aggregate by fieldapi field, and sort by the aggregated // field. - $this->queryResult = $this->factory->getAggregate('entity_test') + $this->queryResult = $this->entityStorage->getAggregateQuery() ->groupBy('field_test_1') ->sortAggregate('field_test_2', 'COUNT', 'DESC') ->execute(); @@ -537,7 +529,7 @@ public function testAggregation() { ['field_test_1' => 3, 'field_test_2_count' => 1], ]); - $this->queryResult = $this->factory->getAggregate('entity_test') + $this->queryResult = $this->entityStorage->getAggregateQuery() ->groupBy('field_test_1') ->sortAggregate('field_test_2', 'COUNT', 'ASC') ->execute(); diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityQueryRelationshipTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityQueryRelationshipTest.php index 1f0073e107fb..4e1f9d1e5d49 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntityQueryRelationshipTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityQueryRelationshipTest.php @@ -24,11 +24,6 @@ class EntityQueryRelationshipTest extends EntityKernelTestBase { */ public static $modules = ['taxonomy']; - /** - * @var \Drupal\Core\Entity\Query\QueryFactory - */ - protected $factory; - /** * Term entities. * @@ -109,88 +104,88 @@ protected function setUp() { $entity->save(); $this->entities[] = $entity; } - $this->factory = \Drupal::service('entity.query'); } /** * Tests querying. */ public function testQuery() { + $storage = $this->container->get('entity_type.manager')->getStorage('entity_test'); // This returns the 0th entity as that's the only one pointing to the 0th // account. - $this->queryResults = $this->factory->get('entity_test') + $this->queryResults = $storage->getQuery() ->condition("user_id.entity.name", $this->accounts[0]->getUsername()) ->execute(); $this->assertResults([0]); // This returns the 1st and 2nd entity as those point to the 1st account. - $this->queryResults = $this->factory->get('entity_test') + $this->queryResults = $storage->getQuery() ->condition("user_id.entity.name", $this->accounts[0]->getUsername(), '<>') ->execute(); $this->assertResults([1, 2]); // This returns all three entities because all of them point to an // account. - $this->queryResults = $this->factory->get('entity_test') + $this->queryResults = $storage->getQuery() ->exists("user_id.entity.name") ->execute(); $this->assertResults([0, 1, 2]); // This returns no entities because all of them point to an account. - $this->queryResults = $this->factory->get('entity_test') + $this->queryResults = $storage->getQuery() ->notExists("user_id.entity.name") ->execute(); $this->assertEqual(count($this->queryResults), 0); // This returns the 0th entity as that's only one pointing to the 0th // term (test without specifying the field column). - $this->queryResults = $this->factory->get('entity_test') + $this->queryResults = $storage->getQuery() ->condition("$this->fieldName.entity.name", $this->terms[0]->name->value) ->execute(); $this->assertResults([0]); // This returns the 0th entity as that's only one pointing to the 0th // term (test with specifying the column name). - $this->queryResults = $this->factory->get('entity_test') + $this->queryResults = $storage->getQuery() ->condition("$this->fieldName.target_id.entity.name", $this->terms[0]->name->value) ->execute(); $this->assertResults([0]); // This returns the 1st and 2nd entity as those point to the 1st term. - $this->queryResults = $this->factory->get('entity_test') + $this->queryResults = $storage->getQuery() ->condition("$this->fieldName.entity.name", $this->terms[0]->name->value, '<>') ->execute(); $this->assertResults([1, 2]); // This returns the 0th entity as that's only one pointing to the 0th // account. - $this->queryResults = $this->factory->get('entity_test') + $this->queryResults = $storage->getQuery() ->condition("user_id.entity:user.name", $this->accounts[0]->getUsername()) ->execute(); $this->assertResults([0]); // This returns the 1st and 2nd entity as those point to the 1st account. - $this->queryResults = $this->factory->get('entity_test') + $this->queryResults = $storage->getQuery() ->condition("user_id.entity:user.name", $this->accounts[0]->getUsername(), '<>') ->execute(); $this->assertResults([1, 2]); // This returns all three entities because all of them point to an // account. - $this->queryResults = $this->factory->get('entity_test') + $this->queryResults = $storage->getQuery() ->exists("user_id.entity:user.name") ->execute(); $this->assertResults([0, 1, 2]); // This returns no entities because all of them point to an account. - $this->queryResults = $this->factory->get('entity_test') + $this->queryResults = $storage->getQuery() ->notExists("user_id.entity:user.name") ->execute(); $this->assertEqual(count($this->queryResults), 0); // This returns the 0th entity as that's only one pointing to the 0th // term (test without specifying the field column). - $this->queryResults = $this->factory->get('entity_test') + $this->queryResults = $storage->getQuery() ->condition("$this->fieldName.entity:taxonomy_term.name", $this->terms[0]->name->value) ->execute(); $this->assertResults([0]); // This returns the 0th entity as that's only one pointing to the 0th // term (test with specifying the column name). - $this->queryResults = $this->factory->get('entity_test') + $this->queryResults = $storage->getQuery() ->condition("$this->fieldName.target_id.entity:taxonomy_term.name", $this->terms[0]->name->value) ->execute(); $this->assertResults([0]); // This returns the 1st and 2nd entity as those point to the 1st term. - $this->queryResults = $this->factory->get('entity_test') + $this->queryResults = $storage->getQuery() ->condition("$this->fieldName.entity:taxonomy_term.name", $this->terms[0]->name->value, '<>') ->execute(); $this->assertResults([1, 2]); @@ -201,8 +196,10 @@ public function testQuery() { */ public function testInvalidSpecifier() { $this->setExpectedException(PluginNotFoundException::class); - $this->factory - ->get('taxonomy_term') + $this->container + ->get('entity_type.manager') + ->getStorage('taxonomy_term') + ->getQuery() ->condition('langcode.language.foo', 'bar') ->execute(); } diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php index 3f30b2b8996d..88bdf50a7403 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php @@ -33,11 +33,6 @@ class EntityQueryTest extends EntityKernelTestBase { */ protected $queryResults; - /** - * @var \Drupal\Core\Entity\Query\QueryFactory - */ - protected $factory; - /** * A list of bundle machine names created for this test. * @@ -59,6 +54,13 @@ class EntityQueryTest extends EntityKernelTestBase { */ public $figures; + /** + * The entity_test_mulrev entity storage. + * + * @var \Drupal\Core\Entity\EntityStorageInterface + */ + protected $storage; + protected function setUp() { parent::setUp(); @@ -145,7 +147,7 @@ protected function setUp() { $this->bundles = $bundles; $this->figures = $figures; $this->greetings = $greetings; - $this->factory = \Drupal::service('entity.query'); + $this->storage = $this->container->get('entity_type.manager')->getStorage('entity_test_mulrev'); } /** @@ -154,7 +156,8 @@ protected function setUp() { public function testEntityQuery() { $greetings = $this->greetings; $figures = $this->figures; - $this->queryResults = $this->factory->get('entity_test_mulrev') + $this->queryResults = $this->storage + ->getQuery() ->exists($greetings, 'tr') ->condition("$figures.color", 'red') ->sort('id') @@ -163,7 +166,8 @@ public function testEntityQuery() { // bit 0 and bit 2 needs to be set. $this->assertResult(5, 7, 13, 15); - $query = $this->factory->get('entity_test_mulrev', 'OR') + $query = $this->storage + ->getQuery('OR') ->exists($greetings, 'tr') ->condition("$figures.color", 'red') ->sort('id'); @@ -175,7 +179,8 @@ public function testEntityQuery() { $this->assertResult(1, 3, 4, 5, 6, 7, 9, 11, 12, 13, 14, 15); // Test cloning of query conditions. - $query = $this->factory->get('entity_test_mulrev') + $query = $this->storage + ->getQuery() ->condition("$figures.color", 'red') ->sort('id'); $cloned_query = clone $query; @@ -188,7 +193,7 @@ public function testEntityQuery() { $this->queryResults = $cloned_query->execute(); $this->assertResult(); - $query = $this->factory->get('entity_test_mulrev'); + $query = $this->storage->getQuery(); $group = $query->orConditionGroup() ->exists($greetings, 'tr') ->condition("$figures.color", 'red'); @@ -201,7 +206,8 @@ public function testEntityQuery() { $this->assertResult(9, 11, 12, 13, 14, 15); // No figure has both the colors blue and red at the same time. - $this->queryResults = $this->factory->get('entity_test_mulrev') + $this->queryResults = $this->storage + ->getQuery() ->condition("$figures.color", 'blue') ->condition("$figures.color", 'red') ->sort('id') @@ -209,7 +215,7 @@ public function testEntityQuery() { $this->assertResult(); // But an entity might have a red and a blue figure both. - $query = $this->factory->get('entity_test_mulrev'); + $query = $this->storage->getQuery(); $group_blue = $query->andConditionGroup()->condition("$figures.color", 'blue'); $group_red = $query->andConditionGroup()->condition("$figures.color", 'red'); $this->queryResults = $query @@ -221,7 +227,7 @@ public function testEntityQuery() { $this->assertResult(3, 7, 11, 15); // Do the same test but with IN operator. - $query = $this->factory->get('entity_test_mulrev'); + $query = $this->storage->getQuery(); $group_blue = $query->andConditionGroup()->condition("$figures.color", ['blue'], 'IN'); $group_red = $query->andConditionGroup()->condition("$figures.color", ['red'], 'IN'); $this->queryResults = $query @@ -233,14 +239,16 @@ public function testEntityQuery() { $this->assertResult(3, 7, 11, 15); // An entity might have either red or blue figure. - $this->queryResults = $this->factory->get('entity_test_mulrev') + $this->queryResults = $this->storage + ->getQuery() ->condition("$figures.color", ['blue', 'red'], 'IN') ->sort('id') ->execute(); // Bit 0 or 1 is on. $this->assertResult(1, 2, 3, 5, 6, 7, 9, 10, 11, 13, 14, 15); - $this->queryResults = $this->factory->get('entity_test_mulrev') + $this->queryResults = $this->storage + ->getQuery() ->exists("$figures.color") ->notExists("$greetings.value") ->sort('id') @@ -249,7 +257,8 @@ public function testEntityQuery() { $this->assertResult(1, 2, 3); // Now update the 'merhaba' string to xsiemax which is not a meaningful // word but allows us to test revisions and string operations. - $ids = $this->factory->get('entity_test_mulrev') + $ids = $this->storage + ->getQuery() ->condition("$greetings.value", 'merhaba') ->sort('id') ->execute(); @@ -263,30 +272,35 @@ public function testEntityQuery() { $entity->save(); } // We changed the entity names, so the current revision should not match. - $this->queryResults = $this->factory->get('entity_test_mulrev') + $this->queryResults = $this->storage + ->getQuery() ->condition('name.value', $old_name) ->execute(); $this->assertResult(); // Only if all revisions are queried, we find the old revision. - $this->queryResults = $this->factory->get('entity_test_mulrev') + $this->queryResults = $this->storage + ->getQuery() ->condition('name.value', $old_name) ->allRevisions() ->sort('revision_id') ->execute(); $this->assertRevisionResult([$first_entity->id()], [$first_entity->id()]); // When querying current revisions, this string is no longer found. - $this->queryResults = $this->factory->get('entity_test_mulrev') + $this->queryResults = $this->storage + ->getQuery() ->condition("$greetings.value", 'merhaba') ->execute(); $this->assertResult(); - $this->queryResults = $this->factory->get('entity_test_mulrev') + $this->queryResults = $this->storage + ->getQuery() ->condition("$greetings.value", 'merhaba') ->allRevisions() ->sort('revision_id') ->execute(); // The query only matches the original revisions. $this->assertRevisionResult([4, 5, 6, 7, 12, 13, 14, 15], [4, 5, 6, 7, 12, 13, 14, 15]); - $results = $this->factory->get('entity_test_mulrev') + $results = $this->storage + ->getQuery() ->condition("$greetings.value", 'siema', 'CONTAINS') ->sort('id') ->execute(); @@ -294,21 +308,24 @@ public function testEntityQuery() { // revisions are returned for some entities. $assert = [16 => '4', 17 => '5', 18 => '6', 19 => '7', 8 => '8', 9 => '9', 10 => '10', 11 => '11', 20 => '12', 21 => '13', 22 => '14', 23 => '15']; $this->assertIdentical($results, $assert); - $results = $this->factory->get('entity_test_mulrev') + $results = $this->storage + ->getQuery() ->condition("$greetings.value", 'siema', 'STARTS_WITH') ->sort('revision_id') ->execute(); // Now we only get the ones that originally were siema, entity id 8 and // above. $this->assertIdentical($results, array_slice($assert, 4, 8, TRUE)); - $results = $this->factory->get('entity_test_mulrev') + $results = $this->storage + ->getQuery() ->condition("$greetings.value", 'a', 'ENDS_WITH') ->sort('revision_id') ->execute(); // It is very important that we do not get the ones which only have // xsiemax despite originally they were merhaba, ie. ended with a. $this->assertIdentical($results, array_slice($assert, 4, 8, TRUE)); - $results = $this->factory->get('entity_test_mulrev') + $results = $this->storage + ->getQuery() ->condition("$greetings.value", 'a', 'ENDS_WITH') ->allRevisions() ->sort('id') @@ -320,7 +337,8 @@ public function testEntityQuery() { // Check that a query on the latest revisions without any condition returns // the correct results. - $results = $this->factory->get('entity_test_mulrev') + $results = $this->storage + ->getQuery() ->latestRevision() ->sort('id') ->sort('revision_id') @@ -338,15 +356,18 @@ public function testSort() { $greetings = $this->greetings; $figures = $this->figures; // Order up and down on a number. - $this->queryResults = $this->factory->get('entity_test_mulrev') + $this->queryResults = $this->storage + ->getQuery() ->sort('id') ->execute(); $this->assertResult(range(1, 15)); - $this->queryResults = $this->factory->get('entity_test_mulrev') + $this->queryResults = $this->storage + ->getQuery() ->sort('id', 'DESC') ->execute(); $this->assertResult(range(15, 1)); - $query = $this->factory->get('entity_test_mulrev') + $query = $this->storage + ->getQuery() ->sort("$figures.color") ->sort("$greetings.format") ->sort('id'); @@ -396,7 +417,8 @@ public function testSort() { 'page' => '0,2', ]); \Drupal::getContainer()->get('request_stack')->push($request); - $this->queryResults = $this->factory->get('entity_test_mulrev') + $this->queryResults = $this->storage + ->getQuery() ->sort("$figures.color") ->sort("$greetings.format") ->sort('id') @@ -405,7 +427,8 @@ public function testSort() { $this->assertResult(15, 6, 7, 1); // Now test the reversed order. - $query = $this->factory->get('entity_test_mulrev') + $query = $this->storage + ->getQuery() ->sort("$figures.color", 'DESC') ->sort("$greetings.format", 'DESC') ->sort('id', 'DESC'); @@ -434,7 +457,8 @@ public function testTableSort() { 'type' => ['data' => 'Type', 'specifier' => 'type'], ]; - $this->queryResults = array_values($this->factory->get('entity_test_mulrev') + $this->queryResults = array_values($this->storage + ->getQuery() ->tableSort($header) ->execute()); $this->assertBundleOrder('asc'); @@ -448,7 +472,8 @@ public function testTableSort() { 'id' => ['data' => 'Id', 'specifier' => 'id'], 'type' => ['data' => 'Type', 'specifier' => 'type'], ]; - $this->queryResults = array_values($this->factory->get('entity_test_mulrev') + $this->queryResults = array_values($this->storage + ->getQuery() ->tableSort($header) ->execute()); $this->assertBundleOrder('desc'); @@ -458,7 +483,8 @@ public function testTableSort() { 'order' => 'Id', ]); \Drupal::getContainer()->get('request_stack')->push($request); - $this->queryResults = $this->factory->get('entity_test_mulrev') + $this->queryResults = $this->storage + ->getQuery() ->tableSort($header) ->execute(); $this->assertResult(range(15, 1)); @@ -493,7 +519,9 @@ public function testCount() { // As the single entity of this type we just saved does not have a value // in the color field, the result should be 0. - $count = $this->factory->get('entity_test') + $count = $this->container->get('entity_type.manager') + ->getStorage('entity_test') + ->getQuery() ->exists("$field_name.color") ->count() ->execute(); @@ -506,7 +534,7 @@ public function testCount() { public function testNestedConditionGroups() { // Query for all entities of the first bundle that have either a red // triangle as a figure or the Turkish greeting as a greeting. - $query = $this->factory->get('entity_test_mulrev'); + $query = $this->storage->getQuery(); $first_and = $query->andConditionGroup() ->condition($this->figures . '.color', 'red') @@ -534,14 +562,16 @@ public function testNestedConditionGroups() { public function testDelta() { $figures = $this->figures; // Test numeric delta value in field condition. - $this->queryResults = $this->factory->get('entity_test_mulrev') + $this->queryResults = $this->storage + ->getQuery() ->condition("$figures.0.color", 'red') ->sort('id') ->execute(); // As unit 0 at delta 0 was the red triangle bit 0 needs to be set. $this->assertResult(1, 3, 5, 7, 9, 11, 13, 15); - $this->queryResults = $this->factory->get('entity_test_mulrev') + $this->queryResults = $this->storage + ->getQuery() ->condition("$figures.1.color", 'red') ->sort('id') ->execute(); @@ -549,7 +579,7 @@ public function testDelta() { $this->assertResult(); // Test on two different deltas. - $query = $this->factory->get('entity_test_mulrev'); + $query = $this->storage->getQuery(); $or = $query->andConditionGroup() ->condition("$figures.0.color", 'red') ->condition("$figures.1.color", 'blue'); @@ -560,7 +590,8 @@ public function testDelta() { $this->assertResult(3, 7, 11, 15); // Test the delta range condition. - $this->queryResults = $this->factory->get('entity_test_mulrev') + $this->queryResults = $this->storage + ->getQuery() ->condition("$figures.%delta.color", ['blue', 'red'], 'IN') ->condition("$figures.%delta", [0, 1], 'IN') ->sort('id') @@ -569,7 +600,8 @@ public function testDelta() { $this->assertResult(1, 2, 3, 5, 6, 7, 9, 10, 11, 13, 14, 15); // Test the delta range condition without conditions on the value. - $this->queryResults = $this->factory->get('entity_test_mulrev') + $this->queryResults = $this->storage + ->getQuery() ->condition("$figures.%delta", 1) ->sort('id') ->execute(); @@ -578,12 +610,14 @@ public function testDelta() { // Numeric delta on single value base field should return results only if // the first item is being targeted. - $this->queryResults = $this->factory->get('entity_test_mulrev') + $this->queryResults = $this->storage + ->getQuery() ->condition("id.0.value", [1, 3, 5], 'IN') ->sort('id') ->execute(); $this->assertResult(1, 3, 5); - $this->queryResults = $this->factory->get('entity_test_mulrev') + $this->queryResults = $this->storage + ->getQuery() ->condition("id.1.value", [1, 3, 5], 'IN') ->sort('id') ->execute(); @@ -591,18 +625,21 @@ public function testDelta() { // Delta range condition on single value base field should return results // only if just the field value is targeted. - $this->queryResults = $this->factory->get('entity_test_mulrev') + $this->queryResults = $this->storage + ->getQuery() ->condition("id.%delta.value", [1, 3, 5], 'IN') ->sort('id') ->execute(); $this->assertResult(1, 3, 5); - $this->queryResults = $this->factory->get('entity_test_mulrev') + $this->queryResults = $this->storage + ->getQuery() ->condition("id.%delta.value", [1, 3, 5], 'IN') ->condition("id.%delta", 0, '=') ->sort('id') ->execute(); $this->assertResult(1, 3, 5); - $this->queryResults = $this->factory->get('entity_test_mulrev') + $this->queryResults = $this->storage + ->getQuery() ->condition("id.%delta.value", [1, 3, 5], 'IN') ->condition("id.%delta", 1, '=') ->sort('id') @@ -659,7 +696,7 @@ protected function assertBundleOrder($order) { * The tags and metadata should propagate to the SQL query object. */ public function testMetaData() { - $query = \Drupal::entityQuery('entity_test_mulrev'); + $query = $this->storage->getQuery(); $query ->addTag('efq_metadata_test') ->addMetaData('foo', 'bar') @@ -733,134 +770,157 @@ public function testCaseSensitivity() { ])->save(); // Check the case insensitive field, = operator. - $result = \Drupal::entityQuery('entity_test_mulrev')->condition( - 'field_ci', $fixtures[0]['lowercase'] . $fixtures[1]['lowercase'] - )->execute(); + $result = $this->storage + ->getQuery() + ->condition('field_ci', $fixtures[0]['lowercase'] . $fixtures[1]['lowercase']) + ->execute(); $this->assertIdentical(count($result), 1, 'Case insensitive, lowercase'); - $result = \Drupal::entityQuery('entity_test_mulrev')->condition( - 'field_ci', $fixtures[0]['uppercase'] . $fixtures[1]['uppercase'] - )->execute(); + $result = $this->storage + ->getQuery() + ->condition('field_ci', $fixtures[0]['uppercase'] . $fixtures[1]['uppercase']) + ->execute(); $this->assertIdentical(count($result), 1, 'Case insensitive, uppercase'); - $result = \Drupal::entityQuery('entity_test_mulrev')->condition( - 'field_ci', $fixtures[0]['uppercase'] . $fixtures[1]['lowercase'] - )->execute(); + $result = $this->storage + ->getQuery() + ->condition('field_ci', $fixtures[0]['uppercase'] . $fixtures[1]['lowercase']) + ->execute(); $this->assertIdentical(count($result), 1, 'Case insensitive, mixed.'); // Check the case sensitive field, = operator. - $result = \Drupal::entityQuery('entity_test_mulrev')->condition( - 'field_cs', $fixtures[0]['lowercase'] . $fixtures[1]['lowercase'] - )->execute(); + $result = $this->storage + ->getQuery() + ->condition('field_cs', $fixtures[0]['lowercase'] . $fixtures[1]['lowercase']) + ->execute(); $this->assertIdentical(count($result), 0, 'Case sensitive, lowercase.'); - $result = \Drupal::entityQuery('entity_test_mulrev')->condition( - 'field_cs', $fixtures[0]['uppercase'] . $fixtures[1]['uppercase'] - )->execute(); + $result = $this->storage + ->getQuery() + ->condition('field_cs', $fixtures[0]['uppercase'] . $fixtures[1]['uppercase']) + ->execute(); $this->assertIdentical(count($result), 0, 'Case sensitive, uppercase.'); - $result = \Drupal::entityQuery('entity_test_mulrev')->condition( - 'field_cs', $fixtures[0]['uppercase'] . $fixtures[1]['lowercase'] - )->execute(); + $result = $this->storage + ->getQuery() + ->condition('field_cs', $fixtures[0]['uppercase'] . $fixtures[1]['lowercase']) + ->execute(); $this->assertIdentical(count($result), 1, 'Case sensitive, exact match.'); // Check the case insensitive field, IN operator. - $result = \Drupal::entityQuery('entity_test_mulrev')->condition( - 'field_ci', [$fixtures[0]['lowercase'] . $fixtures[1]['lowercase']], 'IN' - )->execute(); + $result = $this->storage + ->getQuery() + ->condition('field_ci', [$fixtures[0]['lowercase'] . $fixtures[1]['lowercase']], 'IN') + ->execute(); $this->assertIdentical(count($result), 1, 'Case insensitive, lowercase'); - $result = \Drupal::entityQuery('entity_test_mulrev')->condition( - 'field_ci', [$fixtures[0]['uppercase'] . $fixtures[1]['uppercase']], 'IN' - )->execute(); + $result = $this->storage + ->getQuery() + ->condition('field_ci', [$fixtures[0]['uppercase'] . $fixtures[1]['uppercase']], 'IN')->execute(); $this->assertIdentical(count($result), 1, 'Case insensitive, uppercase'); - $result = \Drupal::entityQuery('entity_test_mulrev')->condition( - 'field_ci', [$fixtures[0]['uppercase'] . $fixtures[1]['lowercase']], 'IN' - )->execute(); + $result = $this->storage + ->getQuery() + ->condition('field_ci', [$fixtures[0]['uppercase'] . $fixtures[1]['lowercase']], 'IN') + ->execute(); $this->assertIdentical(count($result), 1, 'Case insensitive, mixed'); // Check the case sensitive field, IN operator. - $result = \Drupal::entityQuery('entity_test_mulrev')->condition( - 'field_cs', [$fixtures[0]['lowercase'] . $fixtures[1]['lowercase']], 'IN' - )->execute(); + $result = $this->storage + ->getQuery() + ->condition('field_cs', [$fixtures[0]['lowercase'] . $fixtures[1]['lowercase']], 'IN') + ->execute(); $this->assertIdentical(count($result), 0, 'Case sensitive, lowercase'); - $result = \Drupal::entityQuery('entity_test_mulrev')->condition( - 'field_cs', [$fixtures[0]['uppercase'] . $fixtures[1]['uppercase']], 'IN' - )->execute(); + $result = $this->storage + ->getQuery() + ->condition('field_cs', [$fixtures[0]['uppercase'] . $fixtures[1]['uppercase']], 'IN') + ->execute(); $this->assertIdentical(count($result), 0, 'Case sensitive, uppercase'); - $result = \Drupal::entityQuery('entity_test_mulrev')->condition( - 'field_cs', [$fixtures[0]['uppercase'] . $fixtures[1]['lowercase']], 'IN' - )->execute(); + $result = $this->storage + ->getQuery() + ->condition('field_cs', [$fixtures[0]['uppercase'] . $fixtures[1]['lowercase']], 'IN') + ->execute(); $this->assertIdentical(count($result), 1, 'Case sensitive, mixed'); // Check the case insensitive field, STARTS_WITH operator. - $result = \Drupal::entityQuery('entity_test_mulrev')->condition( - 'field_ci', $fixtures[0]['lowercase'], 'STARTS_WITH' - )->execute(); + $result = $this->storage + ->getQuery() + ->condition('field_ci', $fixtures[0]['lowercase'], 'STARTS_WITH') + ->execute(); $this->assertIdentical(count($result), 1, 'Case sensitive, lowercase.'); - $result = \Drupal::entityQuery('entity_test_mulrev')->condition( - 'field_ci', $fixtures[0]['uppercase'], 'STARTS_WITH' - )->execute(); + $result = $this->storage + ->getQuery() + ->condition('field_ci', $fixtures[0]['uppercase'], 'STARTS_WITH') + ->execute(); $this->assertIdentical(count($result), 1, 'Case sensitive, exact match.'); // Check the case sensitive field, STARTS_WITH operator. - $result = \Drupal::entityQuery('entity_test_mulrev')->condition( - 'field_cs', $fixtures[0]['lowercase'], 'STARTS_WITH' - )->execute(); + $result = $this->storage + ->getQuery() + ->condition('field_cs', $fixtures[0]['lowercase'], 'STARTS_WITH') + ->execute(); $this->assertIdentical(count($result), 0, 'Case sensitive, lowercase.'); - $result = \Drupal::entityQuery('entity_test_mulrev')->condition( - 'field_cs', $fixtures[0]['uppercase'], 'STARTS_WITH' - )->execute(); + $result = $this->storage + ->getQuery() + ->condition('field_cs', $fixtures[0]['uppercase'], 'STARTS_WITH') + ->execute(); $this->assertIdentical(count($result), 1, 'Case sensitive, exact match.'); // Check the case insensitive field, ENDS_WITH operator. - $result = \Drupal::entityQuery('entity_test_mulrev')->condition( - 'field_ci', $fixtures[1]['lowercase'], 'ENDS_WITH' - )->execute(); + $result = $this->storage + ->getQuery() + ->condition('field_ci', $fixtures[1]['lowercase'], 'ENDS_WITH') + ->execute(); $this->assertIdentical(count($result), 1, 'Case sensitive, lowercase.'); - $result = \Drupal::entityQuery('entity_test_mulrev')->condition( - 'field_ci', $fixtures[1]['uppercase'], 'ENDS_WITH' - )->execute(); + $result = $this->storage + ->getQuery() + ->condition('field_ci', $fixtures[1]['uppercase'], 'ENDS_WITH') + ->execute(); $this->assertIdentical(count($result), 1, 'Case sensitive, exact match.'); // Check the case sensitive field, ENDS_WITH operator. - $result = \Drupal::entityQuery('entity_test_mulrev')->condition( - 'field_cs', $fixtures[1]['lowercase'], 'ENDS_WITH' - )->execute(); + $result = $this->storage + ->getQuery() + ->condition('field_cs', $fixtures[1]['lowercase'], 'ENDS_WITH') + ->execute(); $this->assertIdentical(count($result), 1, 'Case sensitive, lowercase.'); - $result = \Drupal::entityQuery('entity_test_mulrev')->condition( - 'field_cs', $fixtures[1]['uppercase'], 'ENDS_WITH' - )->execute(); + $result = $this->storage + ->getQuery() + ->condition('field_cs', $fixtures[1]['uppercase'], 'ENDS_WITH') + ->execute(); $this->assertIdentical(count($result), 0, 'Case sensitive, exact match.'); // Check the case insensitive field, CONTAINS operator, use the inner 8 // characters of the uppercase and lowercase strings. - $result = \Drupal::entityQuery('entity_test_mulrev')->condition( - 'field_ci', mb_substr($fixtures[0]['uppercase'] . $fixtures[1]['lowercase'], 4, 8), 'CONTAINS' - )->execute(); + $result = $this->storage + ->getQuery() + ->condition('field_ci', mb_substr($fixtures[0]['uppercase'] . $fixtures[1]['lowercase'], 4, 8), 'CONTAINS') + ->execute(); $this->assertIdentical(count($result), 1, 'Case sensitive, lowercase.'); - $result = \Drupal::entityQuery('entity_test_mulrev')->condition( - 'field_ci', mb_strtolower(mb_substr($fixtures[0]['uppercase'] . $fixtures[1]['lowercase'], 4, 8)), 'CONTAINS' - )->execute(); + $result = $this->storage + ->getQuery() + ->condition('field_ci', mb_strtolower(mb_substr($fixtures[0]['uppercase'] . $fixtures[1]['lowercase'], 4, 8)), 'CONTAINS') + ->execute(); $this->assertIdentical(count($result), 1, 'Case sensitive, exact match.'); // Check the case sensitive field, CONTAINS operator. - $result = \Drupal::entityQuery('entity_test_mulrev')->condition( - 'field_cs', mb_substr($fixtures[0]['uppercase'] . $fixtures[1]['lowercase'], 4, 8), 'CONTAINS' - )->execute(); + $result = $this->storage + ->getQuery() + ->condition('field_cs', mb_substr($fixtures[0]['uppercase'] . $fixtures[1]['lowercase'], 4, 8), 'CONTAINS') + ->execute(); $this->assertIdentical(count($result), 1, 'Case sensitive, lowercase.'); - $result = \Drupal::entityQuery('entity_test_mulrev')->condition( - 'field_cs', mb_strtolower(mb_substr($fixtures[0]['uppercase'] . $fixtures[1]['lowercase'], 4, 8)), 'CONTAINS' - )->execute(); + $result = $this->storage + ->getQuery() + ->condition('field_cs', mb_strtolower(mb_substr($fixtures[0]['uppercase'] . $fixtures[1]['lowercase'], 4, 8)), 'CONTAINS') + ->execute(); $this->assertIdentical(count($result), 0, 'Case sensitive, exact match.'); } @@ -894,7 +954,9 @@ public function testBaseFieldMultipleColumns() { ]); $term2->save(); - $ids = \Drupal::entityQuery('taxonomy_term') + $ids = $this->container->get('entity_type.manager') + ->getStorage('taxonomy_term') + ->getQuery() ->condition('description.format', 'format1') ->execute(); @@ -907,7 +969,8 @@ public function testBaseFieldMultipleColumns() { */ public function testPendingRevisions() { // Ensure entity 14 is returned. - $result = \Drupal::entityQuery('entity_test_mulrev') + $result = $this->storage + ->getQuery() ->condition('id', [14], 'IN') ->execute(); $this->assertEqual(count($result), 1); @@ -925,19 +988,22 @@ public function testPendingRevisions() { $entity->save(); // Entity query should still return entity 14. - $result = \Drupal::entityQuery('entity_test_mulrev') + $result = $this->storage + ->getQuery() ->condition('id', [14], 'IN') ->execute(); $this->assertEqual(count($result), 1); // Verify that field conditions on the default and pending revision are // work as expected. - $result = \Drupal::entityQuery('entity_test_mulrev') + $result = $this->storage + ->getQuery() ->condition('id', [14], 'IN') ->condition("$this->figures.color", $current_values[0]['color']) ->execute(); $this->assertEqual($result, [14 => '14']); - $result = $this->factory->get('entity_test_mulrev') + $result = $this->storage + ->getQuery() ->condition('id', [14], 'IN') ->condition("$this->figures.color", 'red') ->allRevisions() @@ -954,14 +1020,16 @@ public function testPendingRevisions() { $entity->save(); // A non-revisioned entity query should still return entity 14. - $result = $this->factory->get('entity_test_mulrev') + $result = $this->storage + ->getQuery() ->condition('id', [14], 'IN') ->execute(); $this->assertCount(1, $result); $this->assertSame([14 => '14'], $result); // Now check an entity query on the latest revision. - $result = $this->factory->get('entity_test_mulrev') + $result = $this->storage + ->getQuery() ->condition('id', [14], 'IN') ->latestRevision() ->execute(); @@ -970,14 +1038,16 @@ public function testPendingRevisions() { // Verify that field conditions on the default and pending revision still // work as expected. - $result = $this->factory->get('entity_test_mulrev') + $result = $this->storage + ->getQuery() ->condition('id', [14], 'IN') ->condition("$this->figures.color", $current_values[0]['color']) ->execute(); $this->assertSame([14 => '14'], $result); // Now there are two revisions with same value for the figure color. - $result = $this->factory->get('entity_test_mulrev') + $result = $this->storage + ->getQuery() ->condition('id', [14], 'IN') ->condition("$this->figures.color", 'red') ->allRevisions() @@ -985,7 +1055,8 @@ public function testPendingRevisions() { $this->assertSame([16 => '14', 17 => '14'], $result); // Check that querying for the latest revision returns the correct one. - $result = $this->factory->get('entity_test_mulrev') + $result = $this->storage + ->getQuery() ->condition('id', [14], 'IN') ->condition("$this->figures.color", 'red') ->latestRevision() @@ -999,7 +1070,8 @@ public function testPendingRevisions() { */ public function testInjectionInCondition() { try { - $this->queryResults = $this->factory->get('entity_test_mulrev') + $this->queryResults = $this->storage + ->getQuery() ->condition('1 ; -- ', [0, 1], 'IN') ->sort('id') ->execute(); @@ -1018,6 +1090,9 @@ public function testWithTwoEntityReferenceFieldsToSameEntityType() { $this->createEntityReferenceField('entity_test', 'entity_test', 'ref1', $this->randomMachineName(), 'entity_test'); $this->createEntityReferenceField('entity_test', 'entity_test', 'ref2', $this->randomMachineName(), 'entity_test'); + $storage = $this->container->get('entity_type.manager') + ->getStorage('entity_test'); + // Create two entities to be referred. $ref1 = EntityTest::create(['type' => 'entity_test']); $ref1->save(); @@ -1033,7 +1108,7 @@ public function testWithTwoEntityReferenceFieldsToSameEntityType() { $entity->save(); // Check that works when referring with "{$field_name}". - $result = $this->factory->get('entity_test') + $result = $storage->getQuery() ->condition('type', 'entity_test') ->condition('ref1', $ref1->id()) ->condition('ref2', $ref2->id()) @@ -1042,7 +1117,7 @@ public function testWithTwoEntityReferenceFieldsToSameEntityType() { $this->assertEquals($entity->id(), reset($result)); // Check that works when referring with "{$field_name}.target_id". - $result = $this->factory->get('entity_test') + $result = $storage->getQuery() ->condition('type', 'entity_test') ->condition('ref1.target_id', $ref1->id()) ->condition('ref2.target_id', $ref2->id()) @@ -1051,7 +1126,7 @@ public function testWithTwoEntityReferenceFieldsToSameEntityType() { $this->assertEquals($entity->id(), reset($result)); // Check that works when referring with "{$field_name}.entity.id". - $result = $this->factory->get('entity_test') + $result = $storage->getQuery() ->condition('type', 'entity_test') ->condition('ref1.entity.id', $ref1->id()) ->condition('ref2.entity.id', $ref2->id()) diff --git a/core/tests/Drupal/Tests/Listeners/DeprecationListenerTrait.php b/core/tests/Drupal/Tests/Listeners/DeprecationListenerTrait.php index f2737571b38c..bce77f620fdf 100644 --- a/core/tests/Drupal/Tests/Listeners/DeprecationListenerTrait.php +++ b/core/tests/Drupal/Tests/Listeners/DeprecationListenerTrait.php @@ -105,7 +105,6 @@ public static function getSkippedDeprecations() { 'The revision_user revision metadata key is not set.', 'The revision_created revision metadata key is not set.', 'The revision_log_message revision metadata key is not set.', - 'The "entity.query" service relies on the deprecated "Drupal\Core\Entity\Query\QueryFactory" class. It should either be deprecated or its implementation upgraded.', 'MigrateCckField is deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.x. Use \Drupal\migrate_drupal\Annotation\MigrateField instead.', 'MigrateCckFieldPluginManager is deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.x. Use \Drupal\migrate_drupal\Annotation\MigrateFieldPluginManager instead.', 'MigrateCckFieldPluginManagerInterface is deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.x. Use \Drupal\migrate_drupal\Annotation\MigrateFieldPluginManagerInterface instead.', -- GitLab