public function DynamicEntityReferenceItemTest::testContentEntityReferenceItem in Dynamic Entity Reference 8
Same name and namespace in other branches
- 8.2 tests/src/Kernel/DynamicEntityReferenceItemTest.php \Drupal\Tests\dynamic_entity_reference\Kernel\DynamicEntityReferenceItemTest::testContentEntityReferenceItem()
Tests the der field type for referencing content entities.
File
- tests/
src/ Kernel/ DynamicEntityReferenceItemTest.php, line 104
Class
- DynamicEntityReferenceItemTest
- Tests the new entity API for the dynamic entity reference field type.
Namespace
Drupal\Tests\dynamic_entity_reference\KernelCode
public function testContentEntityReferenceItem() {
$tid = $this->term
->id();
$entity_type_id = $this->term
->getEntityTypeId();
// Just being able to create the entity like this verifies a lot of code.
$entity = EntityTest::create();
$entity->field_der->target_type = $entity_type_id;
$entity->field_der->target_id = $tid;
$entity->name->value = $this
->randomMachineName();
$entity
->save();
$entity = EntityTest::load($entity
->id());
$this
->assertTrue($entity->field_der instanceof FieldItemListInterface, 'Field implements interface.');
$this
->assertTrue($entity->field_der[0] instanceof FieldItemInterface, 'Field item implements interface.');
$this
->assertEquals($entity->field_der->target_id, $tid);
$this
->assertEquals($entity->field_der->target_type, $entity_type_id);
$this
->assertEquals($entity->field_der->entity
->getName(), $this->term
->getName());
$this
->assertEquals($entity->field_der->entity
->id(), $tid);
$this
->assertEquals($entity->field_der->entity
->uuid(), $this->term
->uuid());
// Change the name of the term via the reference.
$new_name = $this
->randomMachineName();
$entity->field_der->entity
->setName($new_name);
$entity->field_der->entity
->save();
// Verify it is the correct name.
$term = Term::load($tid);
$this
->assertEquals($term
->getName(), $new_name);
// 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_der->target_type = $entity_type_id;
$entity->field_der->target_id = $term
->id();
$this
->assertEquals($entity->field_der->entity
->id(), $term
->id());
$this
->assertEquals($entity->field_der->entity
->getName(), $term
->getName());
$entity->field_der = [
'target_id' => $term2
->id(),
'target_type' => $entity_type_id,
];
$this
->assertEquals($entity->field_der->entity
->id(), $term2
->id());
$this
->assertEquals($entity->field_der->entity
->getName(), $term2
->getName());
// Test value assignment via the computed 'entity' property.
$entity->field_der->entity = $term;
$this
->assertEquals($entity->field_der->target_id, $term
->id());
$this
->assertEquals($entity->field_der->entity
->getName(), $term
->getName());
$entity->field_der
->appendItem($term2);
$this
->assertEquals($entity->field_der[1]->target_id, $term2
->id());
$this
->assertEquals($entity->field_der[1]->entity
->getName(), $term2
->getName());
$entity->field_der = [
'entity' => $term2,
];
$this
->assertEquals($entity->field_der->target_id, $term2
->id());
$this
->assertEquals($entity->field_der->entity
->getName(), $term2
->getName());
$entity->field_der
->appendItem([
'entity' => $term,
]);
$this
->assertEquals($entity->field_der[1]->target_id, $term
->id());
$this
->assertEquals($entity->field_der[1]->entity
->getName(), $term
->getName());
// Test assigning an invalid item throws an exception.
try {
$entity->field_der = [
'target_id' => $term
->id(),
'target_type' => '',
];
$this
->assertTrue(FALSE, 'Assigning an item without target type throws an exception.');
} catch (\InvalidArgumentException $e) {
$this
->assertTrue(TRUE, 'Assigning an item without target type throws an exception.');
}
// Test assigning an invalid item throws an exception.
try {
$entity->field_der = [
'target_id' => 'invalid',
'target_type' => $entity_type_id,
'entity' => $term2,
];
$this
->assertTrue(FALSE, 'Assigning an invalid item throws an exception.');
} catch (\InvalidArgumentException $e) {
$this
->assertTrue(TRUE, 'Assigning an invalid item throws an exception.');
}
// Test assigning an invalid item throws an exception.
try {
$entity->field_der = [
'target_id' => $term2
->id(),
'target_type' => 'invalid',
'entity' => $term2,
];
$this
->fail('Assigning an invalid item throws an exception.');
} catch (\InvalidArgumentException $e) {
$this
->assertTrue(TRUE, 'Assigning an invalid item throws an exception.');
}
$entity->field_der->target_type = $entity_type_id;
$entity->field_der->target_id = $term2
->id();
$this
->assertEquals($entity->field_der->entity
->id(), $term2
->id());
$this
->assertEquals($entity->field_der->entity
->getName(), $term2
->getName());
// 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();
// Created a term because the generateSampleItems relies on having terms in
// vocabulary.
Term::create([
'name' => $this
->randomMachineName(),
'vid' => $this->term
->bundle(),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
])
->save();
$entity->field_der
->generateSampleItems();
$entity->field_der
->generateSampleItems();
$this
->entityValidateAndSave($entity);
}