function EntityReferenceItemTest::testAutocreateValidation

Same name in other branches
  1. 9 core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceItemTest.php \Drupal\Tests\field\Kernel\EntityReference\EntityReferenceItemTest::testAutocreateValidation()
  2. 8.9.x core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceItemTest.php \Drupal\Tests\field\Kernel\EntityReference\EntityReferenceItemTest::testAutocreateValidation()
  3. 10 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 423

Class

EntityReferenceItemTest
Tests the new entity API for the entity reference field type.

Namespace

Drupal\Tests\field\Kernel\EntityReference

Code

public function testAutocreateValidation() : void {
    // 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->assertEquals('This value should not be null.', $errors[0]->getMessage());
    $this->assertEquals('field_test_taxonomy_term.0', $errors[0]->getPropertyPath());
    // 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->assertEquals(new FormattableMarkup('This entity (%type: %label) cannot be referenced.', [
        '%type' => 'node',
        '%label' => $title,
    ]), $errors[0]->getMessage());
    $this->assertEquals('field_test_node.0.entity', $errors[0]->getPropertyPath());
    // 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->assertEquals(new FormattableMarkup('This entity (%type: %label) cannot be referenced.', [
        '%type' => 'node',
        '%label' => $unsaved_unpublished_node_title,
    ]), $errors[0]->getMessage());
    $this->assertEquals('field_test_node.0.entity', $errors[0]->getPropertyPath());
    $this->assertEquals(new FormattableMarkup('This entity (%type: %label) cannot be referenced.', [
        '%type' => 'node',
        '%label' => $saved_unpublished_node->id(),
    ]), $errors[1]->getMessage());
    $this->assertEquals('field_test_node.1.target_id', $errors[1]->getPropertyPath());
    // Publish one of the nodes and try again.
    $saved_unpublished_node->setPublished();
    $saved_unpublished_node->save();
    $errors = $entity->validate();
    $this->assertCount(1, $errors);
    $this->assertEquals(new FormattableMarkup('This entity (%type: %label) cannot be referenced.', [
        '%type' => 'node',
        '%label' => $unsaved_unpublished_node_title,
    ]), $errors[0]->getMessage());
    $this->assertEquals('field_test_node.0.entity', $errors[0]->getPropertyPath());
    // 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->assertEquals(new FormattableMarkup('This entity (%type: %label) cannot be referenced.', [
        '%type' => 'comment',
        '%label' => $title,
    ]), $errors[0]->getMessage());
    $this->assertEquals('field_test_comment.0.entity', $errors[0]->getPropertyPath());
    // 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->assertEquals(new FormattableMarkup('This entity (%type: %label) cannot be referenced.', [
        '%type' => 'user',
        '%label' => $name,
    ]), $errors[0]->getMessage());
    $this->assertEquals('field_test_user.0.entity', $errors[0]->getPropertyPath());
    // 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->assertEquals(new FormattableMarkup('This entity (%type: %label) cannot be referenced.', [
        '%type' => 'file',
        '%label' => $filename,
    ]), $errors[0]->getMessage());
    $this->assertEquals('field_test_file.0.entity', $errors[0]->getPropertyPath());
    // 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.