1. 8.3.x core/tests/Drupal/KernelTests/Core/Entity/EntityQueryRelationshipTest.php
  2. 8.0.x core/modules/system/src/Tests/Entity/EntityQueryRelationshipTest.php
  3. 8.1.x core/tests/Drupal/KernelTests/Core/Entity/EntityQueryRelationshipTest.php
  4. 8.2.x core/tests/Drupal/KernelTests/Core/Entity/EntityQueryRelationshipTest.php
  5. 8.4.x core/tests/Drupal/KernelTests/Core/Entity/EntityQueryRelationshipTest.php

Namespace

Drupal\KernelTests\Core\Entity

File

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

Classes

Namesort descending Description
EntityQueryRelationshipTest Tests the Entity Query relationship API.