You are here

public function EntityReferenceItemTest::testAutocreateValidation in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/field/src/Tests/EntityReference/EntityReferenceItemTest.php \Drupal\field\Tests\EntityReference\EntityReferenceItemTest::testAutocreateValidation()

Tests ValidReferenceConstraint with newly created and unsaved entities.

File

core/modules/field/src/Tests/EntityReference/EntityReferenceItemTest.php, line 352
Contains \Drupal\field\Tests\EntityReference\EntityReferenceItemTest.

Class

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

Namespace

Drupal\field\Tests\EntityReference

Code

public function testAutocreateValidation() {

  // The term entity is unsaved here.
  $term = Term::create(array(
    '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
    ->assertEqual(0, count($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
    ->assertEqual(1, count($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
    ->assertEqual(0, count($errors));

  // Test with an unpublished and unsaved node.
  $title = $this
    ->randomString();
  $node = Node::create([
    'title' => $title,
    'type' => 'node',
    'status' => NODE_NOT_PUBLISHED,
  ]);
  $entity = EntityTest::create([
    'field_test_node' => [
      'entity' => $node,
    ],
  ]);
  $errors = $entity
    ->validate();
  $this
    ->assertEqual(1, count($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(TRUE);
  $errors = $entity
    ->validate();
  $this
    ->assertEqual(0, count($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
    ->assertEqual(1, count($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(TRUE);
  $errors = $entity
    ->validate();
  $this
    ->assertEqual(0, count($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
    ->assertEqual(1, count($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
    ->assertEqual(0, count($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
    ->assertEqual(1, count($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
    ->assertEqual(0, count($errors));
}