public function DynamicEntityReferenceFieldTest::testEntityReferenceFieldValidation in Dynamic Entity Reference 8
Same name and namespace in other branches
- 8.2 tests/src/Kernel/DynamicEntityReferenceFieldTest.php \Drupal\Tests\dynamic_entity_reference\Kernel\DynamicEntityReferenceFieldTest::testEntityReferenceFieldValidation()
Tests reference field validation.
File
- tests/
src/ Kernel/ DynamicEntityReferenceFieldTest.php, line 97
Class
- DynamicEntityReferenceFieldTest
- Tests for the dynamic entity reference field.
Namespace
Drupal\Tests\dynamic_entity_reference\KernelCode
public function testEntityReferenceFieldValidation() {
$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' => $this->entityType,
]));
// 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(),
]));
// Test bundle validation with empty array. Empty array means no bundle is
// allowed.
$field_config = $this->container
->get('entity_type.manager')
->getStorage('field_config')
->load($this->entityType . '.' . $this->bundle . '.' . $this->fieldName);
// Empty array means no target bundles are allowed.
$settings = [
'handler' => 'default:' . $this->referencedEntityType,
'handler_settings' => [
'target_bundles' => [],
],
];
$field_config
->setSetting('entity_test_with_bundle', $settings);
$field_config
->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(), 1, 'Validation throws a violation.');
$this
->assertEquals($violations[0]
->getMessage(), t('No bundle is allowed for (%type)', [
'%type' => $this->referencedEntityType,
]));
// Test with wrong bundle.
$bundle = EntityTestBundle::create([
'id' => 'newbundle',
'label' => 'New Bundle',
'revision' => FALSE,
]);
$bundle
->save();
$field_config = $this->container
->get('entity_type.manager')
->getStorage('field_config')
->load($this->entityType . '.' . $this->bundle . '.' . $this->fieldName);
$settings = [
'handler' => 'default:' . $this->referencedEntityType,
'handler_settings' => [
'target_bundles' => [
'newbundle',
],
],
];
$field_config
->setSetting('entity_test_with_bundle', $settings);
$field_config
->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(), 1, 'Validation throws a violation.');
$this
->assertEquals($violations[0]
->getMessage(), t('Referenced entity %label does not belong to one of the supported bundles (%bundles).', [
'%label' => $referenced_entity
->label(),
'%bundles' => 'newbundle',
]));
}