public function EntityReferenceFieldTest::testReferencedEntitiesMultipleLoad in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/system/src/Tests/Entity/EntityReferenceFieldTest.php \Drupal\system\Tests\Entity\EntityReferenceFieldTest::testReferencedEntitiesMultipleLoad()
Tests the multiple target entities loader.
File
- core/
modules/ system/ src/ Tests/ Entity/ EntityReferenceFieldTest.php, line 122 - Contains \Drupal\system\Tests\Entity\EntityReferenceFieldTest.
Class
- EntityReferenceFieldTest
- Tests for the entity reference field.
Namespace
Drupal\system\Tests\EntityCode
public function testReferencedEntitiesMultipleLoad() {
// Create the parent entity.
$entity = entity_create($this->entityType, array(
'type' => $this->bundle,
));
// Create three target entities and attach them to parent field.
$target_entities = array();
$reference_field = array();
for ($i = 0; $i < 3; $i++) {
$target_entity = entity_create($this->referencedEntityType, array(
'type' => $this->bundle,
));
$target_entity
->save();
$target_entities[] = $target_entity;
$reference_field[]['target_id'] = $target_entity
->id();
}
// Also attach a non-existent entity and a NULL target id.
$reference_field[3]['target_id'] = 99999;
$target_entities[3] = NULL;
$reference_field[4]['target_id'] = NULL;
$target_entities[4] = NULL;
// Attach the first created target entity as the sixth item ($delta == 5) of
// the parent entity field. We want to test the case when the same target
// entity is referenced twice (or more times) in the same entity reference
// field.
$reference_field[5] = $reference_field[0];
$target_entities[5] = $target_entities[0];
// Create a new target entity that is not saved, thus testing the
// "autocreate" feature.
$target_entity_unsaved = entity_create($this->referencedEntityType, array(
'type' => $this->bundle,
'name' => $this
->randomString(),
));
$reference_field[6]['entity'] = $target_entity_unsaved;
$target_entities[6] = $target_entity_unsaved;
// Set the field value.
$entity->{$this->fieldName}
->setValue($reference_field);
// Load the target entities using EntityReferenceField::referencedEntities().
$entities = $entity->{$this->fieldName}
->referencedEntities();
// Test returned entities:
// - Deltas must be preserved.
// - Non-existent entities must not be retrieved in target entities result.
foreach ($target_entities as $delta => $target_entity) {
if (!empty($target_entity)) {
if (!$target_entity
->isNew()) {
// There must be an entity in the loaded set having the same id for
// the same delta.
$this
->assertEqual($target_entity
->id(), $entities[$delta]
->id());
}
else {
// For entities that were not yet saved, there must an entity in the
// loaded set having the same label for the same delta.
$this
->assertEqual($target_entity
->label(), $entities[$delta]
->label());
}
}
else {
// A non-existent or NULL entity target id must not return any item in
// the target entities set.
$this
->assertFalse(isset($entities[$delta]));
}
}
}