function EntityReferenceFieldTest::testAutocreateApi 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::testAutocreateApi()
Tests all the possible ways to autocreate an entity via the API.
File
- core/
modules/ system/ src/ Tests/ Entity/ EntityReferenceFieldTest.php, line 231 - Contains \Drupal\system\Tests\Entity\EntityReferenceFieldTest.
Class
- EntityReferenceFieldTest
- Tests for the entity reference field.
Namespace
Drupal\system\Tests\EntityCode
function testAutocreateApi() {
$entity = $this->entityManager
->getStorage($this->entityType)
->create(array(
'name' => $this
->randomString(),
));
// Test content entity autocreation.
$this
->assertUserAutocreate($entity, function (EntityInterface $entity, UserInterface $user) {
$entity
->set('user_id', $user);
});
$this
->assertUserAutocreate($entity, function (EntityInterface $entity, UserInterface $user) {
$entity
->set('user_id', $user, FALSE);
});
$this
->assertUserAutocreate($entity, function (EntityInterface $entity, UserInterface $user) {
$entity->user_id
->setValue($user);
});
$this
->assertUserAutocreate($entity, function (EntityInterface $entity, UserInterface $user) {
$entity->user_id[0]
->get('entity')
->setValue($user);
});
$this
->assertUserAutocreate($entity, function (EntityInterface $entity, UserInterface $user) {
$entity->user_id
->setValue(array(
'entity' => $user,
'target_id' => NULL,
));
});
try {
$message = 'Setting both the entity and an invalid target_id property fails.';
$this
->assertUserAutocreate($entity, function (EntityInterface $entity, UserInterface $user) {
$user
->save();
$entity->user_id
->setValue(array(
'entity' => $user,
'target_id' => $this
->generateRandomEntityId(),
));
});
$this
->fail($message);
} catch (\InvalidArgumentException $e) {
$this
->pass($message);
}
$this
->assertUserAutocreate($entity, function (EntityInterface $entity, UserInterface $user) {
$entity->user_id = $user;
});
$this
->assertUserAutocreate($entity, function (EntityInterface $entity, UserInterface $user) {
$entity->user_id->entity = $user;
});
// Test config entity autocreation.
$this
->assertUserRoleAutocreate($entity, function (EntityInterface $entity, RoleInterface $role) {
$entity
->set('user_role', $role);
});
$this
->assertUserRoleAutocreate($entity, function (EntityInterface $entity, RoleInterface $role) {
$entity
->set('user_role', $role, FALSE);
});
$this
->assertUserRoleAutocreate($entity, function (EntityInterface $entity, RoleInterface $role) {
$entity->user_role
->setValue($role);
});
$this
->assertUserRoleAutocreate($entity, function (EntityInterface $entity, RoleInterface $role) {
$entity->user_role[0]
->get('entity')
->setValue($role);
});
$this
->assertUserRoleAutocreate($entity, function (EntityInterface $entity, RoleInterface $role) {
$entity->user_role
->setValue(array(
'entity' => $role,
'target_id' => NULL,
));
});
try {
$message = 'Setting both the entity and an invalid target_id property fails.';
$this
->assertUserRoleAutocreate($entity, function (EntityInterface $entity, RoleInterface $role) {
$role
->save();
$entity->user_role
->setValue(array(
'entity' => $role,
'target_id' => $this
->generateRandomEntityId(TRUE),
));
});
$this
->fail($message);
} catch (\InvalidArgumentException $e) {
$this
->pass($message);
}
$this
->assertUserRoleAutocreate($entity, function (EntityInterface $entity, RoleInterface $role) {
$entity->user_role = $role;
});
$this
->assertUserRoleAutocreate($entity, function (EntityInterface $entity, RoleInterface $role) {
$entity->user_role->entity = $role;
});
// Test target entity saving after setting it as new.
$storage = $this->entityManager
->getStorage('user');
$user_id = $this
->generateRandomEntityId();
$user = $storage
->create(array(
'uid' => $user_id,
'name' => $this
->randomString(),
));
$entity->user_id = $user;
$user
->save();
$entity
->save();
$this
->assertEqual($entity->user_id->target_id, $user
->id());
}