use Drupal\entity_test\Entity\EntityTestMulRev;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
+use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Vocabulary;
*/
class EntityQueryTest extends EntityKernelTestBase {
+ use EntityReferenceTestTrait;
+
/**
* Modules to enable.
*
}
// Each unit is a list of field name, langcode and a column-value array.
$units[] = [$figures, 'en', [
- 'color' => 'red',
- 'shape' => 'triangle',
- ]];
+ 'color' => 'red',
+ 'shape' => 'triangle',
+ ],
+ ];
$units[] = [$figures, 'en', [
- 'color' => 'blue',
- 'shape' => 'circle',
- ]];
+ 'color' => 'blue',
+ 'shape' => 'circle',
+ ],
+ ];
// To make it easier to test sorting, the greetings get formats according
// to their langcode.
$units[] = [$greetings, 'tr', [
- 'value' => 'merhaba',
- 'format' => 'format-tr'
- ]];
+ 'value' => 'merhaba',
+ 'format' => 'format-tr',
+ ],
+ ];
$units[] = [$greetings, 'pl', [
- 'value' => 'siema',
- 'format' => 'format-pl'
- ]];
+ 'value' => 'siema',
+ 'format' => 'format-pl',
+ ],
+ ];
// Make these languages available to the greetings field.
ConfigurableLanguage::createFromLangcode('tr')->save();
ConfigurableLanguage::createFromLangcode('pl')->save();
// Now we get everything.
$assert = [4 => '4', 5 => '5', 6 => '6', 7 => '7', 8 => '8', 9 => '9', 10 => '10', 11 => '11', 12 => '12', 20 => '12', 13 => '13', 21 => '13', 14 => '14', 22 => '14', 15 => '15', 23 => '15'];
$this->assertIdentical($results, $assert);
+
+ // Check that a query on the latest revisions without any condition returns
+ // the correct results.
+ $results = $this->factory->get('entity_test_mulrev')
+ ->latestRevision()
+ ->sort('id')
+ ->sort('revision_id')
+ ->execute();
+ $expected = [1 => '1', 2 => '2', 3 => '3', 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->assertSame($expected, $results);
}
/**
)->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();
$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(
'description' => [
'value' => $this->randomString(),
'format' => 'format1',
- ]]);
+ ],
+ ]);
$term1->save();
$term2 = Term::create([
'description' => [
'value' => $this->randomString(),
'format' => 'format2',
- ]]);
+ ],
+ ]);
$term2->save();
$ids = \Drupal::entityQuery('taxonomy_term')
}
/**
- * Test forward-revisions.
+ * Test pending revisions.
*/
- public function testForwardRevisions() {
+ public function testPendingRevisions() {
// Ensure entity 14 is returned.
$result = \Drupal::entityQuery('entity_test_mulrev')
->condition('id', [14], 'IN')
->execute();
$this->assertEqual(count($result), 1);
- // Verify that field conditions on the default and forward revision are
+ // Verify that field conditions on the default and pending revision are
// work as expected.
$result = \Drupal::entityQuery('entity_test_mulrev')
->condition('id', [14], 'IN')
->allRevisions()
->execute();
$this->assertEqual($result, [16 => '14']);
+
+ // Add another pending revision on the same entity and repeat the checks.
+ $entity->setNewRevision(TRUE);
+ $entity->isDefaultRevision(FALSE);
+ $entity->{$this->figures}->setValue([
+ 'color' => 'red',
+ 'shape' => 'square'
+ ]);
+ $entity->save();
+
+ // A non-revisioned entity query should still return entity 14.
+ $result = $this->factory->get('entity_test_mulrev')
+ ->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')
+ ->condition('id', [14], 'IN')
+ ->latestRevision()
+ ->execute();
+ $this->assertCount(1, $result);
+ $this->assertSame([17 => '14'], $result);
+
+ // Verify that field conditions on the default and pending revision still
+ // work as expected.
+ $result = $this->factory->get('entity_test_mulrev')
+ ->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')
+ ->condition('id', [14], 'IN')
+ ->condition("$this->figures.color", 'red')
+ ->allRevisions()
+ ->execute();
+ $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')
+ ->condition('id', [14], 'IN')
+ ->condition("$this->figures.color", 'red')
+ ->latestRevision()
+ ->execute();
+ $this->assertSame([17 => '14'], $result);
}
/**
}
}
+ /**
+ * Tests that EntityQuery works when querying the same entity from two fields.
+ */
+ public function testWithTwoEntityReferenceFieldsToSameEntityType() {
+ // Create two entity reference fields referring 'entity_test' entities.
+ $this->createEntityReferenceField('entity_test', 'entity_test', 'ref1', $this->randomMachineName(), 'entity_test');
+ $this->createEntityReferenceField('entity_test', 'entity_test', 'ref2', $this->randomMachineName(), 'entity_test');
+
+ // Create two entities to be referred.
+ $ref1 = EntityTest::create(['type' => 'entity_test']);
+ $ref1->save();
+ $ref2 = EntityTest::create(['type' => 'entity_test']);
+ $ref2->save();
+
+ // Create a main entity referring the previous created entities.
+ $entity = EntityTest::create([
+ 'type' => 'entity_test',
+ 'ref1' => $ref1->id(),
+ 'ref2' => $ref2->id(),
+ ]);
+ $entity->save();
+
+ // Check that works when referring with "{$field_name}".
+ $result = $this->factory->get('entity_test')
+ ->condition('type', 'entity_test')
+ ->condition('ref1', $ref1->id())
+ ->condition('ref2', $ref2->id())
+ ->execute();
+ $this->assertCount(1, $result);
+ $this->assertEquals($entity->id(), reset($result));
+
+ // Check that works when referring with "{$field_name}.target_id".
+ $result = $this->factory->get('entity_test')
+ ->condition('type', 'entity_test')
+ ->condition('ref1.target_id', $ref1->id())
+ ->condition('ref2.target_id', $ref2->id())
+ ->execute();
+ $this->assertCount(1, $result);
+ $this->assertEquals($entity->id(), reset($result));
+
+ // Check that works when referring with "{$field_name}.entity.id".
+ $result = $this->factory->get('entity_test')
+ ->condition('type', 'entity_test')
+ ->condition('ref1.entity.id', $ref1->id())
+ ->condition('ref2.entity.id', $ref2->id())
+ ->execute();
+ $this->assertCount(1, $result);
+ $this->assertEquals($entity->id(), reset($result));
+ }
+
}