/**
* {@inheritdoc}
*/ publicfunctionisEntityValidationRequired(FieldableEntityInterface $entity){ // Prioritize the entity method over migration config because it won't be
// possible to save that entity unvalidated.
/* @see \Drupal\Core\Entity\ContentEntityBase::preSave() */ return$entity->isValidationRequired() || !empty($this->configuration['validate']); }
/**
* {@inheritdoc}
*/ publicfunctionvalidateEntity(FieldableEntityInterface $entity){ // Entity validation can require the user that owns the entity. Switch to
// use that user during validation.
// As an example:
// @see \Drupal\Core\Entity\Plugin\Validation\Constraint\ValidReferenceConstraint
$account = $entity instanceof EntityOwnerInterface ? $entity->getOwner() : NULL;
publicfunctiontestImport(){ $bundles = []; $destination = newEntityTestDestination([], '', [], $this->migration->reveal(), $this->storage->reveal(), $bundles, $this->entityFieldManager->reveal(), $this->prophesize(FieldTypePluginManagerInterface::class)->reveal(), $this->prophesize(AccountSwitcherInterface::class)->reveal() ); $entity = $this->prophesize(ContentEntityInterface::class); $entity->isValidationRequired() ->shouldBeCalledTimes(1); // Assert that save is called.
$entity->save() ->shouldBeCalledTimes(1); // Set an id for the entity
$entity->id() ->willReturn(5); $destination->setEntity($entity->reveal()); // Ensure the id is saved entity id is returned from import.
$this->assertEquals([5], $destination->import(newRow())); // Assert that import set the rollback action.
// Check that entities can be saved normally when validation is not
// required.
$this->assertFalse($this->entity->isValidationRequired()); $this->entity->save();
// Make validation required and check that if the entity is validated, it
// can be saved normally.
$this->entity->setValidationRequired(TRUE); $this->assertTrue($this->entity->isValidationRequired()); $this->entity->validate(); $this->entity->save();
// Check that the "validated" status is reset after saving the entity and
// that trying to save a non-validated entity when validation is required