EntityReferenceFieldTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. <?php
  2. namespace Drupal\KernelTests\Core\Entity;
  3. use Drupal\Tests\SchemaCheckTestTrait;
  4. use Drupal\Core\Entity\EntityInterface;
  5. use Drupal\Core\Entity\EntityStorageException;
  6. use Drupal\Core\Field\BaseFieldDefinition;
  7. use Drupal\Core\Field\FieldStorageDefinitionInterface;
  8. use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait;
  9. use Drupal\field\Entity\FieldConfig;
  10. use Drupal\field\Entity\FieldStorageConfig;
  11. use Drupal\user\Entity\Role;
  12. use Drupal\user\Entity\User;
  13. use Drupal\user\RoleInterface;
  14. use Drupal\user\UserInterface;
  15. use Drupal\entity_test\Entity\EntityTestStringId;
  16. /**
  17. * Tests for the entity reference field.
  18. *
  19. * @group Entity
  20. */
  21. class EntityReferenceFieldTest extends EntityKernelTestBase {
  22. use SchemaCheckTestTrait;
  23. use EntityReferenceTestTrait;
  24. /**
  25. * The entity type used in this test.
  26. *
  27. * @var string
  28. */
  29. protected $entityType = 'entity_test';
  30. /**
  31. * The entity type that is being referenced.
  32. *
  33. * @var string
  34. */
  35. protected $referencedEntityType = 'entity_test_rev';
  36. /**
  37. * The bundle used in this test.
  38. *
  39. * @var string
  40. */
  41. protected $bundle = 'entity_test';
  42. /**
  43. * The name of the field used in this test.
  44. *
  45. * @var string
  46. */
  47. protected $fieldName = 'field_test';
  48. /**
  49. * Modules to install.
  50. *
  51. * @var array
  52. */
  53. public static $modules = ['entity_reference_test', 'entity_test_update'];
  54. /**
  55. * {@inheritdoc}
  56. */
  57. protected function setUp() {
  58. parent::setUp();
  59. $this->installEntitySchema('entity_test_rev');
  60. // Create a field.
  61. $this->createEntityReferenceField(
  62. $this->entityType,
  63. $this->bundle,
  64. $this->fieldName,
  65. 'Field test',
  66. $this->referencedEntityType,
  67. 'default',
  68. ['target_bundles' => [$this->bundle]],
  69. FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED
  70. );
  71. }
  72. /**
  73. * Tests reference field validation.
  74. */
  75. public function testEntityReferenceFieldValidation() {
  76. // Test a valid reference.
  77. $referenced_entity = $this->container->get('entity_type.manager')
  78. ->getStorage($this->referencedEntityType)
  79. ->create(['type' => $this->bundle]);
  80. $referenced_entity->save();
  81. $entity = $this->container->get('entity_type.manager')
  82. ->getStorage($this->entityType)
  83. ->create(['type' => $this->bundle]);
  84. $entity->{$this->fieldName}->target_id = $referenced_entity->id();
  85. $violations = $entity->{$this->fieldName}->validate();
  86. $this->assertEqual($violations->count(), 0, 'Validation passes.');
  87. // Test an invalid reference.
  88. $entity->{$this->fieldName}->target_id = 9999;
  89. $violations = $entity->{$this->fieldName}->validate();
  90. $this->assertEqual($violations->count(), 1, 'Validation throws a violation.');
  91. $this->assertEqual($violations[0]->getMessage(), t('The referenced entity (%type: %id) does not exist.', ['%type' => $this->referencedEntityType, '%id' => 9999]));
  92. // Test a non-referenceable bundle.
  93. entity_test_create_bundle('non_referenceable', NULL, $this->referencedEntityType);
  94. $referenced_entity = entity_create($this->referencedEntityType, ['type' => 'non_referenceable']);
  95. $referenced_entity->save();
  96. $entity->{$this->fieldName}->target_id = $referenced_entity->id();
  97. $violations = $entity->{$this->fieldName}->validate();
  98. $this->assertEqual($violations->count(), 1, 'Validation throws a violation.');
  99. $this->assertEqual($violations[0]->getMessage(), t('This entity (%type: %id) cannot be referenced.', ['%type' => $this->referencedEntityType, '%id' => $referenced_entity->id()]));
  100. }
  101. /**
  102. * Tests the multiple target entities loader.
  103. */
  104. public function testReferencedEntitiesMultipleLoad() {
  105. // Create the parent entity.
  106. $entity = $this->container->get('entity_type.manager')
  107. ->getStorage($this->entityType)
  108. ->create(['type' => $this->bundle]);
  109. // Create three target entities and attach them to parent field.
  110. $target_entities = [];
  111. $reference_field = [];
  112. for ($i = 0; $i < 3; $i++) {
  113. $target_entity = $this->container->get('entity_type.manager')
  114. ->getStorage($this->referencedEntityType)
  115. ->create(['type' => $this->bundle]);
  116. $target_entity->save();
  117. $target_entities[] = $target_entity;
  118. $reference_field[]['target_id'] = $target_entity->id();
  119. }
  120. // Also attach a non-existent entity and a NULL target id.
  121. $reference_field[3]['target_id'] = 99999;
  122. $target_entities[3] = NULL;
  123. $reference_field[4]['target_id'] = NULL;
  124. $target_entities[4] = NULL;
  125. // Attach the first created target entity as the sixth item ($delta == 5) of
  126. // the parent entity field. We want to test the case when the same target
  127. // entity is referenced twice (or more times) in the same entity reference
  128. // field.
  129. $reference_field[5] = $reference_field[0];
  130. $target_entities[5] = $target_entities[0];
  131. // Create a new target entity that is not saved, thus testing the
  132. // "autocreate" feature.
  133. $target_entity_unsaved = $this->container->get('entity_type.manager')
  134. ->getStorage($this->referencedEntityType)
  135. ->create(['type' => $this->bundle, 'name' => $this->randomString()]);
  136. $reference_field[6]['entity'] = $target_entity_unsaved;
  137. $target_entities[6] = $target_entity_unsaved;
  138. // Set the field value.
  139. $entity->{$this->fieldName}->setValue($reference_field);
  140. // Load the target entities using EntityReferenceField::referencedEntities().
  141. $entities = $entity->{$this->fieldName}->referencedEntities();
  142. // Test returned entities:
  143. // - Deltas must be preserved.
  144. // - Non-existent entities must not be retrieved in target entities result.
  145. foreach ($target_entities as $delta => $target_entity) {
  146. if (!empty($target_entity)) {
  147. if (!$target_entity->isNew()) {
  148. // There must be an entity in the loaded set having the same id for
  149. // the same delta.
  150. $this->assertEqual($target_entity->id(), $entities[$delta]->id());
  151. }
  152. else {
  153. // For entities that were not yet saved, there must an entity in the
  154. // loaded set having the same label for the same delta.
  155. $this->assertEqual($target_entity->label(), $entities[$delta]->label());
  156. }
  157. }
  158. else {
  159. // A non-existent or NULL entity target id must not return any item in
  160. // the target entities set.
  161. $this->assertFalse(isset($entities[$delta]));
  162. }
  163. }
  164. }
  165. /**
  166. * Tests referencing entities with string IDs.
  167. */
  168. public function testReferencedEntitiesStringId() {
  169. $field_name = 'entity_reference_string_id';
  170. $this->installEntitySchema('entity_test_string_id');
  171. $this->createEntityReferenceField(
  172. $this->entityType,
  173. $this->bundle,
  174. $field_name,
  175. 'Field test',
  176. 'entity_test_string_id',
  177. 'default',
  178. ['target_bundles' => [$this->bundle]],
  179. FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED
  180. );
  181. // Create the parent entity.
  182. $entity = $this->container->get('entity_type.manager')
  183. ->getStorage($this->entityType)
  184. ->create(['type' => $this->bundle]);
  185. // Create the default target entity.
  186. $target_entity = EntityTestStringId::create([
  187. 'id' => $this->randomString(),
  188. 'type' => $this->bundle
  189. ]);
  190. $target_entity->save();
  191. // Set the field value.
  192. $entity->{$field_name}->setValue([['target_id' => $target_entity->id()]]);
  193. // Load the target entities using EntityReferenceField::referencedEntities().
  194. $entities = $entity->{$field_name}->referencedEntities();
  195. $this->assertEqual($entities[0]->id(), $target_entity->id());
  196. // Test that a string ID works as a default value and the field's config
  197. // schema is correct.
  198. $field = FieldConfig::loadByName($this->entityType, $this->bundle, $field_name);
  199. $field->setDefaultValue($target_entity->id());
  200. $field->save();
  201. $this->assertConfigSchema(\Drupal::service('config.typed'), 'field.field.' . $field->id(), $field->toArray());
  202. // Test that the default value works.
  203. $entity = $this->container->get('entity_type.manager')
  204. ->getStorage($this->entityType)
  205. ->create(['type' => $this->bundle]);
  206. $entities = $entity->{$field_name}->referencedEntities();
  207. $this->assertEqual($entities[0]->id(), $target_entity->id());
  208. }
  209. /**
  210. * Tests all the possible ways to autocreate an entity via the API.
  211. */
  212. public function testAutocreateApi() {
  213. $entity = $this->entityManager
  214. ->getStorage($this->entityType)
  215. ->create(['name' => $this->randomString()]);
  216. // Test content entity autocreation.
  217. $this->assertUserAutocreate($entity, function (EntityInterface $entity, UserInterface $user) {
  218. $entity->set('user_id', $user);
  219. });
  220. $this->assertUserAutocreate($entity, function (EntityInterface $entity, UserInterface $user) {
  221. $entity->set('user_id', $user, FALSE);
  222. });
  223. $this->assertUserAutocreate($entity, function (EntityInterface $entity, UserInterface $user) {
  224. $entity->user_id->setValue($user);
  225. });
  226. $this->assertUserAutocreate($entity, function (EntityInterface $entity, UserInterface $user) {
  227. $entity->user_id[0]->get('entity')->setValue($user);
  228. });
  229. $this->assertUserAutocreate($entity, function (EntityInterface $entity, UserInterface $user) {
  230. $entity->user_id->setValue(['entity' => $user, 'target_id' => NULL]);
  231. });
  232. try {
  233. $message = 'Setting both the entity and an invalid target_id property fails.';
  234. $this->assertUserAutocreate($entity, function (EntityInterface $entity, UserInterface $user) {
  235. $user->save();
  236. $entity->user_id->setValue(['entity' => $user, 'target_id' => $this->generateRandomEntityId()]);
  237. });
  238. $this->fail($message);
  239. }
  240. catch (\InvalidArgumentException $e) {
  241. $this->pass($message);
  242. }
  243. $this->assertUserAutocreate($entity, function (EntityInterface $entity, UserInterface $user) {
  244. $entity->user_id = $user;
  245. });
  246. $this->assertUserAutocreate($entity, function (EntityInterface $entity, UserInterface $user) {
  247. $entity->user_id->entity = $user;
  248. });
  249. // Test config entity autocreation.
  250. $this->assertUserRoleAutocreate($entity, function (EntityInterface $entity, RoleInterface $role) {
  251. $entity->set('user_role', $role);
  252. });
  253. $this->assertUserRoleAutocreate($entity, function (EntityInterface $entity, RoleInterface $role) {
  254. $entity->set('user_role', $role, FALSE);
  255. });
  256. $this->assertUserRoleAutocreate($entity, function (EntityInterface $entity, RoleInterface $role) {
  257. $entity->user_role->setValue($role);
  258. });
  259. $this->assertUserRoleAutocreate($entity, function (EntityInterface $entity, RoleInterface $role) {
  260. $entity->user_role[0]->get('entity')->setValue($role);
  261. });
  262. $this->assertUserRoleAutocreate($entity, function (EntityInterface $entity, RoleInterface $role) {
  263. $entity->user_role->setValue(['entity' => $role, 'target_id' => NULL]);
  264. });
  265. try {
  266. $message = 'Setting both the entity and an invalid target_id property fails.';
  267. $this->assertUserRoleAutocreate($entity, function (EntityInterface $entity, RoleInterface $role) {
  268. $role->save();
  269. $entity->user_role->setValue(['entity' => $role, 'target_id' => $this->generateRandomEntityId(TRUE)]);
  270. });
  271. $this->fail($message);
  272. }
  273. catch (\InvalidArgumentException $e) {
  274. $this->pass($message);
  275. }
  276. $this->assertUserRoleAutocreate($entity, function (EntityInterface $entity, RoleInterface $role) {
  277. $entity->user_role = $role;
  278. });
  279. $this->assertUserRoleAutocreate($entity, function (EntityInterface $entity, RoleInterface $role) {
  280. $entity->user_role->entity = $role;
  281. });
  282. // Test target entity saving after setting it as new.
  283. $storage = $this->entityManager->getStorage('user');
  284. $user_id = $this->generateRandomEntityId();
  285. $user = $storage->create(['uid' => $user_id, 'name' => $this->randomString()]);
  286. $entity->user_id = $user;
  287. $user->save();
  288. $entity->save();
  289. $this->assertEqual($entity->user_id->target_id, $user->id());
  290. }
  291. /**
  292. * Asserts that the setter callback performs autocreation for users.
  293. *
  294. * @param \Drupal\Core\Entity\EntityInterface $entity
  295. * The referencing entity.
  296. * @param $setter_callback
  297. * A callback setting the target entity on the referencing entity.
  298. *
  299. * @return bool
  300. * TRUE if the user was autocreated, FALSE otherwise.
  301. */
  302. protected function assertUserAutocreate(EntityInterface $entity, $setter_callback) {
  303. $storage = $this->entityManager->getStorage('user');
  304. $user_id = $this->generateRandomEntityId();
  305. $user = $storage->create(['uid' => $user_id, 'name' => $this->randomString()]);
  306. $setter_callback($entity, $user);
  307. $entity->save();
  308. $storage->resetCache();
  309. $user = User::load($user_id);
  310. return $this->assertEqual($entity->user_id->target_id, $user->id());
  311. }
  312. /**
  313. * Asserts that the setter callback performs autocreation for user roles.
  314. *
  315. * @param \Drupal\Core\Entity\EntityInterface $entity
  316. * The referencing entity.
  317. * @param $setter_callback
  318. * A callback setting the target entity on the referencing entity.
  319. *
  320. * @return bool
  321. * TRUE if the user was autocreated, FALSE otherwise.
  322. */
  323. protected function assertUserRoleAutocreate(EntityInterface $entity, $setter_callback) {
  324. $storage = $this->entityManager->getStorage('user_role');
  325. $role_id = $this->generateRandomEntityId(TRUE);
  326. $role = $storage->create(['id' => $role_id, 'label' => $this->randomString()]);
  327. $setter_callback($entity, $role);
  328. $entity->save();
  329. $storage->resetCache();
  330. $role = Role::load($role_id);
  331. return $this->assertEqual($entity->user_role->target_id, $role->id());
  332. }
  333. /**
  334. * Tests that the target entity is not unnecessarily loaded.
  335. */
  336. public function testTargetEntityNoLoad() {
  337. // Setup a test entity type with an entity reference field to itself. We use
  338. // a special storage class throwing exceptions when a load operation is
  339. // triggered to be able to detect them.
  340. $entity_type = clone $this->entityManager->getDefinition('entity_test_update');
  341. $entity_type->setHandlerClass('storage', '\Drupal\entity_test\EntityTestNoLoadStorage');
  342. $this->state->set('entity_test_update.entity_type', $entity_type);
  343. $definitions = [
  344. 'target_reference' => BaseFieldDefinition::create('entity_reference')
  345. ->setSetting('target_type', $entity_type->id())
  346. ->setSetting('handler', 'default')
  347. ];
  348. $this->state->set('entity_test_update.additional_base_field_definitions', $definitions);
  349. $this->entityManager->clearCachedDefinitions();
  350. $this->installEntitySchema($entity_type->id());
  351. // Create the target entity.
  352. $storage = $this->entityManager->getStorage($entity_type->id());
  353. $target_id = $this->generateRandomEntityId();
  354. $target = $storage->create(['id' => $target_id, 'name' => $this->randomString()]);
  355. $target->save();
  356. $this->assertEqual($target_id, $target->id(), 'The target entity has a random identifier.');
  357. // Check that populating the reference with an existing target id does not
  358. // trigger a load operation.
  359. $message = 'The target entity was not loaded.';
  360. try {
  361. $entity = $this->entityManager
  362. ->getStorage($entity_type->id())
  363. ->create(['name' => $this->randomString()]);
  364. $entity->target_reference = $target_id;
  365. $this->pass($message);
  366. }
  367. catch (EntityStorageException $e) {
  368. $this->fail($message);
  369. }
  370. // Check that the storage actually triggers the expected exception when
  371. // trying to load the target entity.
  372. $message = 'An exception is thrown when trying to load the target entity';
  373. try {
  374. $storage->load($target_id);
  375. $this->fail($message);
  376. }
  377. catch (EntityStorageException $e) {
  378. $this->pass($message);
  379. }
  380. }
  381. /**
  382. * Tests the dependencies entity reference fields are created with.
  383. */
  384. public function testEntityReferenceFieldDependencies() {
  385. $field_name = 'user_reference_field';
  386. $entity_type = 'entity_test';
  387. $field_storage = FieldStorageConfig::create([
  388. 'field_name' => $field_name,
  389. 'type' => 'entity_reference',
  390. 'entity_type' => $entity_type,
  391. 'settings' => [
  392. 'target_type' => 'user',
  393. ],
  394. ]);
  395. $field_storage->save();
  396. $this->assertEqual(['module' => ['entity_test', 'user']], $field_storage->getDependencies());
  397. $field = FieldConfig::create([
  398. 'field_name' => $field_name,
  399. 'entity_type' => $entity_type,
  400. 'bundle' => 'entity_test',
  401. 'label' => $field_name,
  402. 'settings' => [
  403. 'handler' => 'default',
  404. ],
  405. ]);
  406. $field->save();
  407. $this->assertEqual(['config' => ['field.storage.entity_test.user_reference_field'], 'module' => ['entity_test']], $field->getDependencies());
  408. }
  409. }