You are here

public function EntityReferenceItemTest::testContentEntityReferenceItem 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::testContentEntityReferenceItem()

Tests the entity reference field type for referencing content entities.

File

core/modules/field/src/Tests/EntityReference/EntityReferenceItemTest.php, line 112
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 testContentEntityReferenceItem() {
  $tid = $this->term
    ->id();

  // Just being able to create the entity like this verifies a lot of code.
  $entity = entity_create('entity_test');
  $entity->field_test_taxonomy_term->target_id = $tid;
  $entity->name->value = $this
    ->randomMachineName();
  $entity
    ->save();
  $entity = entity_load('entity_test', $entity
    ->id());
  $this
    ->assertTrue($entity->field_test_taxonomy_term instanceof FieldItemListInterface, 'Field implements interface.');
  $this
    ->assertTrue($entity->field_test_taxonomy_term[0] instanceof FieldItemInterface, 'Field item implements interface.');
  $this
    ->assertEqual($entity->field_test_taxonomy_term->target_id, $tid);
  $this
    ->assertEqual($entity->field_test_taxonomy_term->entity
    ->getName(), $this->term
    ->getName());
  $this
    ->assertEqual($entity->field_test_taxonomy_term->entity
    ->id(), $tid);
  $this
    ->assertEqual($entity->field_test_taxonomy_term->entity
    ->uuid(), $this->term
    ->uuid());

  // Verify that the label for the target ID property definition is correct.
  $label = $entity->field_test_taxonomy_term
    ->getFieldDefinition()
    ->getFieldStorageDefinition()
    ->getPropertyDefinition('target_id')
    ->getLabel();
  $this
    ->assertTrue($label instanceof TranslatableMarkup);
  $this
    ->assertEqual($label
    ->render(), 'Taxonomy term ID');

  // Change the name of the term via the reference.
  $new_name = $this
    ->randomMachineName();
  $entity->field_test_taxonomy_term->entity
    ->setName($new_name);
  $entity->field_test_taxonomy_term->entity
    ->save();

  // Verify it is the correct name.
  $term = Term::load($tid);
  $this
    ->assertEqual($term
    ->getName(), $new_name);

  // Make sure the computed term reflects updates to the term id.
  $term2 = entity_create('taxonomy_term', array(
    'name' => $this
      ->randomMachineName(),
    'vid' => $this->term
      ->bundle(),
    'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
  ));
  $term2
    ->save();

  // Test all the possible ways of assigning a value.
  $entity->field_test_taxonomy_term->target_id = $term
    ->id();
  $this
    ->assertEqual($entity->field_test_taxonomy_term->entity
    ->id(), $term
    ->id());
  $this
    ->assertEqual($entity->field_test_taxonomy_term->entity
    ->getName(), $term
    ->getName());
  $entity->field_test_taxonomy_term = [
    [
      'target_id' => $term2
        ->id(),
    ],
  ];
  $this
    ->assertEqual($entity->field_test_taxonomy_term->entity
    ->id(), $term2
    ->id());
  $this
    ->assertEqual($entity->field_test_taxonomy_term->entity
    ->getName(), $term2
    ->getName());

  // Test value assignment via the computed 'entity' property.
  $entity->field_test_taxonomy_term->entity = $term;
  $this
    ->assertEqual($entity->field_test_taxonomy_term->target_id, $term
    ->id());
  $this
    ->assertEqual($entity->field_test_taxonomy_term->entity
    ->getName(), $term
    ->getName());
  $entity->field_test_taxonomy_term = [
    [
      'entity' => $term2,
    ],
  ];
  $this
    ->assertEqual($entity->field_test_taxonomy_term->target_id, $term2
    ->id());
  $this
    ->assertEqual($entity->field_test_taxonomy_term->entity
    ->getName(), $term2
    ->getName());

  // Test assigning an invalid item throws an exception.
  try {
    $entity->field_test_taxonomy_term = [
      'target_id' => 'invalid',
      'entity' => $term2,
    ];
    $this
      ->fail('Assigning an invalid item throws an exception.');
  } catch (\InvalidArgumentException $e) {
    $this
      ->pass('Assigning an invalid item throws an exception.');
  }

  // Delete terms so we have nothing to reference and try again
  $term
    ->delete();
  $term2
    ->delete();
  $entity = entity_create('entity_test', array(
    'name' => $this
      ->randomMachineName(),
  ));
  $entity
    ->save();

  // Test the generateSampleValue() method.
  $entity = entity_create('entity_test');
  $entity->field_test_taxonomy_term
    ->generateSampleItems();
  $entity->field_test_taxonomy_vocabulary
    ->generateSampleItems();
  $this
    ->entityValidateAndSave($entity);
}