public function DynamicEntityReferenceBaseFieldTest::testEntityReferenceFieldValidation in Dynamic Entity Reference 8
Same name and namespace in other branches
- 8.2 tests/src/Kernel/DynamicEntityReferenceBaseFieldTest.php \Drupal\Tests\dynamic_entity_reference\Kernel\DynamicEntityReferenceBaseFieldTest::testEntityReferenceFieldValidation()
Tests reference field validation.
File
- tests/
src/ Kernel/ DynamicEntityReferenceBaseFieldTest.php, line 59
Class
- DynamicEntityReferenceBaseFieldTest
- Tests for the dynamic entity reference base field.
Namespace
Drupal\Tests\dynamic_entity_reference\KernelCode
public function testEntityReferenceFieldValidation() {
\Drupal::state()
->set('dynamic_entity_reference_entity_test_cardinality', 1);
\Drupal::state()
->set('dynamic_entity_reference_entity_test_exclude', [
$this->entityType,
]);
$this
->enableModules([
'dynamic_entity_reference_entity_test',
]);
$this
->installEntitySchema('entity_test_mul');
$entity_type_manager = \Drupal::entityTypeManager();
// Test a valid reference.
$referenced_entity = $entity_type_manager
->getStorage($this->referencedEntityType)
->create([
'type' => $this->bundle,
]);
$referenced_entity
->save();
$entity = $entity_type_manager
->getStorage($this->entityType)
->create([
'type' => $this->bundle,
]);
$entity->{$this->fieldName}->target_type = $referenced_entity
->getEntityTypeId();
$entity->{$this->fieldName}->target_id = $referenced_entity
->id();
$violations = $entity->{$this->fieldName}
->validate();
$this
->assertEquals($violations
->count(), 0, 'Validation passes.');
$entity = $entity_type_manager
->getStorage($this->entityType)
->create([
'type' => $this->bundle,
]);
$entity->{$this->fieldName}->entity = $referenced_entity;
$violations = $entity->{$this->fieldName}
->validate();
$this
->assertEquals($violations
->count(), 0, 'Validation passes.');
// Test an invalid reference.
$entity = $entity_type_manager
->getStorage($this->entityType)
->create([
'type' => $this->bundle,
]);
$entity->{$this->fieldName}->target_type = $referenced_entity
->getEntityTypeId();
$entity->{$this->fieldName}->target_id = 9999;
$violations = $entity->{$this->fieldName}
->validate();
$this
->assertEquals($violations
->count(), 1, 'Validation throws a violation.');
$this
->assertEquals($violations[0]
->getMessage(), t('The referenced entity (%type: %id) does not exist.', [
'%type' => $this->referencedEntityType,
'%id' => 9999,
]));
// Test an invalid target_type.
$entity = $entity_type_manager
->getStorage($this->entityType)
->create([
'type' => $this->bundle,
]);
$entity->{$this->fieldName}->target_type = $entity
->getEntityTypeId();
$entity->{$this->fieldName}->target_id = $referenced_entity
->id();
$violations = $entity->{$this->fieldName}
->validate();
$this
->assertEquals($violations
->count(), 1, 'Validation throws a violation.');
$this
->assertEquals($violations[0]
->getMessage(), t('The referenced entity type (%type) is not allowed for this field.', [
'%type' => $entity
->getEntityTypeId(),
]));
// Test an invalid entity.
$entity = $entity_type_manager
->getStorage($this->entityType)
->create([
'type' => $this->bundle,
]);
$entity->{$this->fieldName}->entity = $entity;
$violations = $entity->{$this->fieldName}
->validate();
$this
->assertEquals($violations
->count(), 1, 'Validation throws a violation.');
$this
->assertEquals($violations[0]
->getMessage(), t('The referenced entity type (%type) is not allowed for this field.', [
'%type' => $entity
->getEntityTypeId(),
]));
// @todo Implement a test case for invalid bundle references after
// https://drupal.org/node/2064191 is fixed
}