EntityQueryRelationshipTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. namespace Drupal\KernelTests\Core\Entity;
  3. use Drupal\Component\Plugin\Exception\PluginNotFoundException;
  4. use Drupal\entity_test\Entity\EntityTest;
  5. use Drupal\taxonomy\Entity\Vocabulary;
  6. use Drupal\taxonomy\Entity\Term;
  7. use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
  8. /**
  9. * Tests the Entity Query relationship API.
  10. *
  11. * @group Entity
  12. */
  13. class EntityQueryRelationshipTest extends EntityKernelTestBase {
  14. use EntityReferenceTestTrait;
  15. /**
  16. * Modules to enable.
  17. *
  18. * @var array
  19. */
  20. public static $modules = ['taxonomy'];
  21. /**
  22. * Term entities.
  23. *
  24. * @var array
  25. */
  26. protected $terms;
  27. /**
  28. * User entities.
  29. *
  30. * @var array
  31. */
  32. public $accounts;
  33. /**
  34. * entity_test entities.
  35. *
  36. * @var array
  37. */
  38. protected $entities;
  39. /**
  40. * The name of the taxonomy field used for test.
  41. *
  42. * @var string
  43. */
  44. protected $fieldName;
  45. /**
  46. * The results returned by EntityQuery.
  47. *
  48. * @var array
  49. */
  50. protected $queryResults;
  51. protected function setUp() {
  52. parent::setUp();
  53. $this->installEntitySchema('taxonomy_term');
  54. // We want an entity reference field. It needs a vocabulary, terms, a field
  55. // storage and a field. First, create the vocabulary.
  56. $vocabulary = Vocabulary::create([
  57. 'vid' => mb_strtolower($this->randomMachineName()),
  58. ]);
  59. $vocabulary->save();
  60. // Second, create the field.
  61. entity_test_create_bundle('test_bundle');
  62. $this->fieldName = strtolower($this->randomMachineName());
  63. $handler_settings = [
  64. 'target_bundles' => [
  65. $vocabulary->id() => $vocabulary->id(),
  66. ],
  67. 'auto_create' => TRUE,
  68. ];
  69. $this->createEntityReferenceField('entity_test', 'test_bundle', $this->fieldName, NULL, 'taxonomy_term', 'default', $handler_settings);
  70. // Create two terms and also two accounts.
  71. for ($i = 0; $i <= 1; $i++) {
  72. $term = Term::create([
  73. 'name' => $this->randomMachineName(),
  74. 'vid' => $vocabulary->id(),
  75. ]);
  76. $term->save();
  77. $this->terms[] = $term;
  78. $this->accounts[] = $this->createUser();
  79. }
  80. // Create three entity_test entities, the 0th entity will point to the
  81. // 0th account and 0th term, the 1st and 2nd entity will point to the
  82. // 1st account and 1st term.
  83. for ($i = 0; $i <= 2; $i++) {
  84. $entity = EntityTest::create(['type' => 'test_bundle']);
  85. $entity->name->value = $this->randomMachineName();
  86. $index = $i ? 1 : 0;
  87. $entity->user_id->target_id = $this->accounts[$index]->id();
  88. $entity->{$this->fieldName}->target_id = $this->terms[$index]->id();
  89. $entity->save();
  90. $this->entities[] = $entity;
  91. }
  92. }
  93. /**
  94. * Tests querying.
  95. */
  96. public function testQuery() {
  97. $storage = $this->container->get('entity_type.manager')->getStorage('entity_test');
  98. // This returns the 0th entity as that's the only one pointing to the 0th
  99. // account.
  100. $this->queryResults = $storage->getQuery()
  101. ->condition("user_id.entity.name", $this->accounts[0]->getAccountName())
  102. ->execute();
  103. $this->assertResults([0]);
  104. // This returns the 1st and 2nd entity as those point to the 1st account.
  105. $this->queryResults = $storage->getQuery()
  106. ->condition("user_id.entity.name", $this->accounts[0]->getAccountName(), '<>')
  107. ->execute();
  108. $this->assertResults([1, 2]);
  109. // This returns all three entities because all of them point to an
  110. // account.
  111. $this->queryResults = $storage->getQuery()
  112. ->exists("user_id.entity.name")
  113. ->execute();
  114. $this->assertResults([0, 1, 2]);
  115. // This returns no entities because all of them point to an account.
  116. $this->queryResults = $storage->getQuery()
  117. ->notExists("user_id.entity.name")
  118. ->execute();
  119. $this->assertCount(0, $this->queryResults);
  120. // This returns the 0th entity as that's only one pointing to the 0th
  121. // term (test without specifying the field column).
  122. $this->queryResults = $storage->getQuery()
  123. ->condition("$this->fieldName.entity.name", $this->terms[0]->name->value)
  124. ->execute();
  125. $this->assertResults([0]);
  126. // This returns the 0th entity as that's only one pointing to the 0th
  127. // term (test with specifying the column name).
  128. $this->queryResults = $storage->getQuery()
  129. ->condition("$this->fieldName.target_id.entity.name", $this->terms[0]->name->value)
  130. ->execute();
  131. $this->assertResults([0]);
  132. // This returns the 1st and 2nd entity as those point to the 1st term.
  133. $this->queryResults = $storage->getQuery()
  134. ->condition("$this->fieldName.entity.name", $this->terms[0]->name->value, '<>')
  135. ->execute();
  136. $this->assertResults([1, 2]);
  137. // This returns the 0th entity as that's only one pointing to the 0th
  138. // account.
  139. $this->queryResults = $storage->getQuery()
  140. ->condition("user_id.entity:user.name", $this->accounts[0]->getAccountName())
  141. ->execute();
  142. $this->assertResults([0]);
  143. // This returns the 1st and 2nd entity as those point to the 1st account.
  144. $this->queryResults = $storage->getQuery()
  145. ->condition("user_id.entity:user.name", $this->accounts[0]->getAccountName(), '<>')
  146. ->execute();
  147. $this->assertResults([1, 2]);
  148. // This returns all three entities because all of them point to an
  149. // account.
  150. $this->queryResults = $storage->getQuery()
  151. ->exists("user_id.entity:user.name")
  152. ->execute();
  153. $this->assertResults([0, 1, 2]);
  154. // This returns no entities because all of them point to an account.
  155. $this->queryResults = $storage->getQuery()
  156. ->notExists("user_id.entity:user.name")
  157. ->execute();
  158. $this->assertCount(0, $this->queryResults);
  159. // This returns the 0th entity as that's only one pointing to the 0th
  160. // term (test without specifying the field column).
  161. $this->queryResults = $storage->getQuery()
  162. ->condition("$this->fieldName.entity:taxonomy_term.name", $this->terms[0]->name->value)
  163. ->execute();
  164. $this->assertResults([0]);
  165. // This returns the 0th entity as that's only one pointing to the 0th
  166. // term (test with specifying the column name).
  167. $this->queryResults = $storage->getQuery()
  168. ->condition("$this->fieldName.target_id.entity:taxonomy_term.name", $this->terms[0]->name->value)
  169. ->execute();
  170. $this->assertResults([0]);
  171. // This returns the 1st and 2nd entity as those point to the 1st term.
  172. $this->queryResults = $storage->getQuery()
  173. ->condition("$this->fieldName.entity:taxonomy_term.name", $this->terms[0]->name->value, '<>')
  174. ->execute();
  175. $this->assertResults([1, 2]);
  176. }
  177. /**
  178. * Tests the invalid specifier in the query relationship.
  179. */
  180. public function testInvalidSpecifier() {
  181. $this->expectException(PluginNotFoundException::class);
  182. $this->container
  183. ->get('entity_type.manager')
  184. ->getStorage('taxonomy_term')
  185. ->getQuery()
  186. ->condition('langcode.language.foo', 'bar')
  187. ->execute();
  188. }
  189. /**
  190. * Assert the results.
  191. *
  192. * @param array $expected
  193. * A list of indexes in the $this->entities array.
  194. */
  195. protected function assertResults($expected) {
  196. $this->assertEqual(count($this->queryResults), count($expected));
  197. foreach ($expected as $key) {
  198. $id = $this->entities[$key]->id();
  199. $this->assertEqual($this->queryResults[$id], $id);
  200. }
  201. }
  202. }