function EntityReferenceItemTest::testAutocreateValidation
Same name in other branches
- 9 core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceItemTest.php \Drupal\Tests\field\Kernel\EntityReference\EntityReferenceItemTest::testAutocreateValidation()
- 10 core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceItemTest.php \Drupal\Tests\field\Kernel\EntityReference\EntityReferenceItemTest::testAutocreateValidation()
- 11.x core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceItemTest.php \Drupal\Tests\field\Kernel\EntityReference\EntityReferenceItemTest::testAutocreateValidation()
Tests ValidReferenceConstraint with newly created and unsaved entities.
File
-
core/
modules/ field/ tests/ src/ Kernel/ EntityReference/ EntityReferenceItemTest.php, line 410
Class
- EntityReferenceItemTest
- Tests the new entity API for the entity reference field type.
Namespace
Drupal\Tests\field\Kernel\EntityReferenceCode
public function testAutocreateValidation() {
// The term entity is unsaved here.
$term = Term::create([
'name' => $this->randomMachineName(),
'vid' => $this->term
->bundle(),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
]);
$entity = EntityTest::create([
'field_test_taxonomy_term' => [
'entity' => $term,
'target_id' => NULL,
],
]);
$errors = $entity->validate();
// Using target_id of NULL is valid with an unsaved entity.
$this->assertCount(0, $errors);
// Using target_id of NULL is not valid with a saved entity.
$term->save();
$entity = EntityTest::create([
'field_test_taxonomy_term' => [
'entity' => $term,
'target_id' => NULL,
],
]);
$errors = $entity->validate();
$this->assertCount(1, $errors);
$this->assertEqual($errors[0]->getMessage(), 'This value should not be null.');
$this->assertEqual($errors[0]->getPropertyPath(), 'field_test_taxonomy_term.0');
// This should rectify the issue, favoring the entity over the target_id.
$entity->save();
$errors = $entity->validate();
$this->assertCount(0, $errors);
// Test with an unpublished and unsaved node.
$title = $this->randomString();
$node = Node::create([
'title' => $title,
'type' => 'node',
'status' => NodeInterface::NOT_PUBLISHED,
]);
$entity = EntityTest::create([
'field_test_node' => [
'entity' => $node,
],
]);
$errors = $entity->validate();
$this->assertCount(1, $errors);
$this->assertEqual($errors[0]->getMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', [
'%type' => 'node',
'%label' => $title,
]));
$this->assertEqual($errors[0]->getPropertyPath(), 'field_test_node.0.entity');
// Publish the node and try again.
$node->setPublished();
$errors = $entity->validate();
$this->assertCount(0, $errors);
// Test with a mix of valid and invalid nodes.
$unsaved_unpublished_node_title = $this->randomString();
$unsaved_unpublished_node = Node::create([
'title' => $unsaved_unpublished_node_title,
'type' => 'node',
'status' => NodeInterface::NOT_PUBLISHED,
]);
$saved_unpublished_node_title = $this->randomString();
$saved_unpublished_node = Node::create([
'title' => $saved_unpublished_node_title,
'type' => 'node',
'status' => NodeInterface::NOT_PUBLISHED,
]);
$saved_unpublished_node->save();
$saved_published_node_title = $this->randomString();
$saved_published_node = Node::create([
'title' => $saved_published_node_title,
'type' => 'node',
'status' => NodeInterface::PUBLISHED,
]);
$saved_published_node->save();
$entity = EntityTest::create([
'field_test_node' => [
[
'entity' => $unsaved_unpublished_node,
],
[
'target_id' => $saved_unpublished_node->id(),
],
[
'target_id' => $saved_published_node->id(),
],
],
]);
$errors = $entity->validate();
$this->assertCount(2, $errors);
$this->assertEqual($errors[0]->getMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', [
'%type' => 'node',
'%label' => $unsaved_unpublished_node_title,
]));
$this->assertEqual($errors[0]->getPropertyPath(), 'field_test_node.0.entity');
$this->assertEqual($errors[1]->getMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', [
'%type' => 'node',
'%label' => $saved_unpublished_node->id(),
]));
$this->assertEqual($errors[1]->getPropertyPath(), 'field_test_node.1.target_id');
// Publish one of the nodes and try again.
$saved_unpublished_node->setPublished();
$saved_unpublished_node->save();
$errors = $entity->validate();
$this->assertCount(1, $errors);
$this->assertEqual($errors[0]->getMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', [
'%type' => 'node',
'%label' => $unsaved_unpublished_node_title,
]));
$this->assertEqual($errors[0]->getPropertyPath(), 'field_test_node.0.entity');
// Publish the last invalid node and try again.
$unsaved_unpublished_node->setPublished();
$errors = $entity->validate();
$this->assertCount(0, $errors);
// Test with an unpublished and unsaved comment.
$title = $this->randomString();
$comment = Comment::create([
'subject' => $title,
'comment_type' => 'comment',
'status' => 0,
]);
$entity = EntityTest::create([
'field_test_comment' => [
'entity' => $comment,
],
]);
$errors = $entity->validate();
$this->assertCount(1, $errors);
$this->assertEqual($errors[0]->getMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', [
'%type' => 'comment',
'%label' => $title,
]));
$this->assertEqual($errors[0]->getPropertyPath(), 'field_test_comment.0.entity');
// Publish the comment and try again.
$comment->setPublished();
$errors = $entity->validate();
$this->assertCount(0, $errors);
// Test with an inactive and unsaved user.
$name = $this->randomString();
$user = User::create([
'name' => $name,
'status' => 0,
]);
$entity = EntityTest::create([
'field_test_user' => [
'entity' => $user,
],
]);
$errors = $entity->validate();
$this->assertCount(1, $errors);
$this->assertEqual($errors[0]->getMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', [
'%type' => 'user',
'%label' => $name,
]));
$this->assertEqual($errors[0]->getPropertyPath(), 'field_test_user.0.entity');
// Activate the user and try again.
$user->activate();
$errors = $entity->validate();
$this->assertCount(0, $errors);
// Test with a temporary and unsaved file.
$filename = $this->randomMachineName() . '.txt';
$file = File::create([
'filename' => $filename,
'status' => 0,
]);
$entity = EntityTest::create([
'field_test_file' => [
'entity' => $file,
],
]);
$errors = $entity->validate();
$this->assertCount(1, $errors);
$this->assertEqual($errors[0]->getMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', [
'%type' => 'file',
'%label' => $filename,
]));
$this->assertEqual($errors[0]->getPropertyPath(), 'field_test_file.0.entity');
// Set the file as permanent and try again.
$file->setPermanent();
$errors = $entity->validate();
$this->assertCount(0, $errors);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.