3 namespace Drupal\KernelTests\Core\Entity;
5 use Drupal\Component\Plugin\Exception\PluginNotFoundException;
6 use Drupal\Component\Utility\Unicode;
7 use Drupal\entity_test\Entity\EntityTest;
8 use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait;
9 use Drupal\taxonomy\Entity\Vocabulary;
10 use Drupal\taxonomy\Entity\Term;
13 * Tests the Entity Query relationship API.
17 class EntityQueryRelationshipTest extends EntityKernelTestBase {
19 use EntityReferenceTestTrait;
26 public static $modules = ['taxonomy'];
29 * @var \Drupal\Core\Entity\Query\QueryFactory
48 * entity_test entities.
55 * The name of the taxonomy field used for test.
62 * The results returned by EntityQuery.
66 protected $queryResults;
68 protected function setUp() {
71 $this->installEntitySchema('taxonomy_term');
73 // We want an entity reference field. It needs a vocabulary, terms, a field
74 // storage and a field. First, create the vocabulary.
75 $vocabulary = Vocabulary::create([
76 'vid' => Unicode::strtolower($this->randomMachineName()),
80 // Second, create the field.
81 entity_test_create_bundle('test_bundle');
82 $this->fieldName = strtolower($this->randomMachineName());
85 $vocabulary->id() => $vocabulary->id(),
87 'auto_create' => TRUE,
89 $this->createEntityReferenceField('entity_test', 'test_bundle', $this->fieldName, NULL, 'taxonomy_term', 'default', $handler_settings);
91 // Create two terms and also two accounts.
92 for ($i = 0; $i <= 1; $i++) {
93 $term = Term::create([
94 'name' => $this->randomMachineName(),
95 'vid' => $vocabulary->id(),
98 $this->terms[] = $term;
99 $this->accounts[] = $this->createUser();
101 // Create three entity_test entities, the 0th entity will point to the
102 // 0th account and 0th term, the 1st and 2nd entity will point to the
103 // 1st account and 1st term.
104 for ($i = 0; $i <= 2; $i++) {
105 $entity = EntityTest::create(['type' => 'test_bundle']);
106 $entity->name->value = $this->randomMachineName();
108 $entity->user_id->target_id = $this->accounts[$index]->id();
109 $entity->{$this->fieldName}->target_id = $this->terms[$index]->id();
111 $this->entities[] = $entity;
113 $this->factory = \Drupal::service('entity.query');
119 public function testQuery() {
120 // This returns the 0th entity as that's the only one pointing to the 0th
122 $this->queryResults = $this->factory->get('entity_test')
123 ->condition("user_id.entity.name", $this->accounts[0]->getUsername())
125 $this->assertResults([0]);
126 // This returns the 1st and 2nd entity as those point to the 1st account.
127 $this->queryResults = $this->factory->get('entity_test')
128 ->condition("user_id.entity.name", $this->accounts[0]->getUsername(), '<>')
130 $this->assertResults([1, 2]);
131 // This returns all three entities because all of them point to an
133 $this->queryResults = $this->factory->get('entity_test')
134 ->exists("user_id.entity.name")
136 $this->assertResults([0, 1, 2]);
137 // This returns no entities because all of them point to an account.
138 $this->queryResults = $this->factory->get('entity_test')
139 ->notExists("user_id.entity.name")
141 $this->assertEqual(count($this->queryResults), 0);
142 // This returns the 0th entity as that's only one pointing to the 0th
143 // term (test without specifying the field column).
144 $this->queryResults = $this->factory->get('entity_test')
145 ->condition("$this->fieldName.entity.name", $this->terms[0]->name->value)
147 $this->assertResults([0]);
148 // This returns the 0th entity as that's only one pointing to the 0th
149 // term (test with specifying the column name).
150 $this->queryResults = $this->factory->get('entity_test')
151 ->condition("$this->fieldName.target_id.entity.name", $this->terms[0]->name->value)
153 $this->assertResults([0]);
154 // This returns the 1st and 2nd entity as those point to the 1st term.
155 $this->queryResults = $this->factory->get('entity_test')
156 ->condition("$this->fieldName.entity.name", $this->terms[0]->name->value, '<>')
158 $this->assertResults([1, 2]);
159 // This returns the 0th entity as that's only one pointing to the 0th
161 $this->queryResults = $this->factory->get('entity_test')
162 ->condition("user_id.entity:user.name", $this->accounts[0]->getUsername())
164 $this->assertResults([0]);
165 // This returns the 1st and 2nd entity as those point to the 1st account.
166 $this->queryResults = $this->factory->get('entity_test')
167 ->condition("user_id.entity:user.name", $this->accounts[0]->getUsername(), '<>')
169 $this->assertResults([1, 2]);
170 // This returns all three entities because all of them point to an
172 $this->queryResults = $this->factory->get('entity_test')
173 ->exists("user_id.entity:user.name")
175 $this->assertResults([0, 1, 2]);
176 // This returns no entities because all of them point to an account.
177 $this->queryResults = $this->factory->get('entity_test')
178 ->notExists("user_id.entity:user.name")
180 $this->assertEqual(count($this->queryResults), 0);
181 // This returns the 0th entity as that's only one pointing to the 0th
182 // term (test without specifying the field column).
183 $this->queryResults = $this->factory->get('entity_test')
184 ->condition("$this->fieldName.entity:taxonomy_term.name", $this->terms[0]->name->value)
186 $this->assertResults([0]);
187 // This returns the 0th entity as that's only one pointing to the 0th
188 // term (test with specifying the column name).
189 $this->queryResults = $this->factory->get('entity_test')
190 ->condition("$this->fieldName.target_id.entity:taxonomy_term.name", $this->terms[0]->name->value)
192 $this->assertResults([0]);
193 // This returns the 1st and 2nd entity as those point to the 1st term.
194 $this->queryResults = $this->factory->get('entity_test')
195 ->condition("$this->fieldName.entity:taxonomy_term.name", $this->terms[0]->name->value, '<>')
197 $this->assertResults([1, 2]);
201 * Tests the invalid specifier in the query relationship.
203 public function testInvalidSpecifier() {
204 $this->setExpectedException(PluginNotFoundException::class);
206 ->get('taxonomy_term')
207 ->condition('langcode.language.foo', 'bar')
212 * Assert the results.
214 * @param array $expected
215 * A list of indexes in the $this->entities array.
217 protected function assertResults($expected) {
218 $this->assertEqual(count($this->queryResults), count($expected));
219 foreach ($expected as $key) {
220 $id = $this->entities[$key]->id();
221 $this->assertEqual($this->queryResults[$id], $id);