Skip to content

Commit

Permalink
Issue #2766135 by hchonov, jeroen.b, amateescu, neclimdul, gordon, ch…
Browse files Browse the repository at this point in the history
…ipway, jmuzz, timmillwood, daffie: EntityQuery with condition on the revision field leads to wrong results

(cherry picked from commit f717ef85c76ae82ff8a2b9e01955a685241becf4)
  • Loading branch information
catch committed May 26, 2020
1 parent d866fb8 commit 6cdd8c0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
13 changes: 7 additions & 6 deletions lib/Drupal/Core/Entity/Query/Sql/Tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,14 @@ public function addField($field, $type, $langcode) {
$column = NULL;
}

// If there is revision support, only the current revisions are being
// queried, and the field is revisionable then use the revision id.
// Otherwise, the entity id will do.
if (($revision_key = $entity_type->getKey('revision')) && $all_revisions && $field_storage && $field_storage->isRevisionable()) {
// If there is revision support, all the revisions are being queried, and
// the field is revisionable or the revision ID field itself, then use the
// revision ID. Otherwise, the entity ID will do.
$query_revisions = $all_revisions && $field_storage && ($field_storage->isRevisionable() || $field_storage->getName() === $entity_type->getKey('revision'));
if ($query_revisions) {
// This contains the relevant SQL field to be used when joining entity
// tables.
$entity_id_field = $revision_key;
$entity_id_field = $entity_type->getKey('revision');
// This contains the relevant SQL field to be used when joining field
// tables.
$field_id_field = 'revision_id';
Expand Down Expand Up @@ -201,7 +202,7 @@ public function addField($field, $type, $langcode) {
// it gets added before the base table.
$entity_tables = [];
$revision_table = NULL;
if ($all_revisions && $field_storage && $field_storage->isRevisionable()) {
if ($query_revisions) {
$data_table = $entity_type->getRevisionDataTable();
$entity_base_table = $entity_type->getRevisionTable();
}
Expand Down
8 changes: 8 additions & 0 deletions tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,14 @@ public function testEntityQuery() {
$entity->name->value .= 'x';
$entity->save();
}
// Test querying all revisions with a condition on the revision ID field.
$this->queryResults = $this->storage
->getQuery()
->condition('revision_id', $first_entity->getRevisionId())
->allRevisions()
->execute();
$this->assertCount(1, $this->queryResults);
$this->assertEquals($first_entity->getRevisionId(), key($this->queryResults));
// We changed the entity names, so the current revision should not match.
$this->queryResults = $this->storage
->getQuery()
Expand Down

0 comments on commit 6cdd8c0

Please sign in to comment.