public function EntityReferenceItemTest::testContentEntityReferenceItem in Drupal 9
Same name and namespace in other branches
- 8 core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceItemTest.php \Drupal\Tests\field\Kernel\EntityReference\EntityReferenceItemTest::testContentEntityReferenceItem()
- 10 core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceItemTest.php \Drupal\Tests\field\Kernel\EntityReference\EntityReferenceItemTest::testContentEntityReferenceItem()
Tests the entity reference field type for referencing content entities.
File
- core/
modules/ field/ tests/ src/ Kernel/ EntityReference/ EntityReferenceItemTest.php, line 130
Class
- EntityReferenceItemTest
- Tests the new entity API for the entity reference field type.
Namespace
Drupal\Tests\field\Kernel\EntityReferenceCode
public function testContentEntityReferenceItem() {
$tid = $this->term
->id();
// Just being able to create the entity like this verifies a lot of code.
$entity = EntityTest::create();
$entity->field_test_taxonomy_term->target_id = $tid;
$entity->name->value = $this
->randomMachineName();
$entity
->save();
$entity = EntityTest::load($entity
->id());
$this
->assertInstanceOf(FieldItemListInterface::class, $entity->field_test_taxonomy_term);
$this
->assertInstanceOf(FieldItemInterface::class, $entity->field_test_taxonomy_term[0]);
$this
->assertEquals($tid, $entity->field_test_taxonomy_term->target_id);
$this
->assertEquals($this->term
->getName(), $entity->field_test_taxonomy_term->entity
->getName());
$this
->assertEquals($tid, $entity->field_test_taxonomy_term->entity
->id());
$this
->assertEquals($this->term
->uuid(), $entity->field_test_taxonomy_term->entity
->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
->assertInstanceOf(TranslatableMarkup::class, $label);
$this
->assertEquals('Taxonomy term ID', $label
->render());
// 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
->assertEquals($new_name, $term
->getName());
// Make sure the computed term reflects updates to the term id.
$term2 = Term::create([
'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
->assertEquals($term
->id(), $entity->field_test_taxonomy_term->entity
->id());
$this
->assertEquals($term
->getName(), $entity->field_test_taxonomy_term->entity
->getName());
$entity->field_test_taxonomy_term = [
[
'target_id' => $term2
->id(),
],
];
$this
->assertEquals($term2
->id(), $entity->field_test_taxonomy_term->entity
->id());
$this
->assertEquals($term2
->getName(), $entity->field_test_taxonomy_term->entity
->getName());
// Test value assignment via the computed 'entity' property.
$entity->field_test_taxonomy_term->entity = $term;
$this
->assertEquals($term
->id(), $entity->field_test_taxonomy_term->target_id);
$this
->assertEquals($term
->getName(), $entity->field_test_taxonomy_term->entity
->getName());
$entity->field_test_taxonomy_term = [
[
'entity' => $term2,
],
];
$this
->assertEquals($term2
->id(), $entity->field_test_taxonomy_term->target_id);
$this
->assertEquals($term2
->getName(), $entity->field_test_taxonomy_term->entity
->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 (\Exception $e) {
$this
->assertInstanceOf(\InvalidArgumentException::class, $e);
}
// Delete terms so we have nothing to reference and try again
$term
->delete();
$term2
->delete();
$entity = EntityTest::create([
'name' => $this
->randomMachineName(),
]);
$entity
->save();
// Test the generateSampleValue() method.
$entity = EntityTest::create();
$entity->field_test_taxonomy_term
->generateSampleItems();
$entity->field_test_taxonomy_vocabulary
->generateSampleItems();
$this
->entityValidateAndSave($entity);
// Tests that setting an integer target ID together with an entity object
// succeeds and does not cause any exceptions. There is no assertion here,
// as the assignment should not throw any exceptions and if it does the
// test will fail.
// @see \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::setValue().
$user = User::create([
'name' => $this
->randomString(),
]);
$user
->save();
$entity = EntityTest::create([
'user_id' => [
'target_id' => (int) $user
->id(),
'entity' => $user,
],
]);
}