Commit cc3f37cc authored by alexpott's avatar alexpott

Issue #2640496 by Berdir, catch, amateescu: Revision ID in {node} and...

Issue #2640496 by Berdir, catch, amateescu: Revision ID in {node} and {node_revision} can get out of sync
parent faa40160
......@@ -295,7 +295,22 @@ protected function doPreSave(EntityInterface $entity) {
$entity->updateLoadedRevisionId();
}
return parent::doPreSave($entity);
$id = parent::doPreSave($entity);
if (!$entity->isNew()) {
// If the ID changed then original can't be loaded, throw an exception
// in that case.
if (empty($entity->original) || $entity->id() != $entity->original->id()) {
throw new EntityStorageException("Update existing '{$this->entityTypeId}' entity while changing the ID is not supported.");
}
// Do not allow changing the revision ID when resaving the current
// revision.
if (!$entity->isNewRevision() && $entity->getRevisionId() != $entity->getLoadedRevisionId()) {
throw new EntityStorageException("Update existing '{$this->entityTypeId}' entity revision while changing the revision ID is not supported.");
}
}
return $id;
}
/**
......
......@@ -491,6 +491,10 @@ public function testRevisionChanged() {
'Changed flag of French translation is set when adding the translation and a new revision.'
);
// Since above a clone of the entity was saved and then this entity is saved
// again, we have to update the revision ID to the current one.
$german->set('revision_id', $form_entity_builder_clone->getRevisionId());
$german->updateLoadedRevisionId();
$german->setOwner($user1);
$german->setRevisionTranslationAffected(FALSE);
$entity->save();
......
......@@ -175,4 +175,38 @@ public function testEntityStorageExceptionHandling() {
}
}
/**
* Tests that resaving a revision with a different revision ID throws an exception.
*/
public function testUpdateWithRevisionId() {
$storage = \Drupal::entityTypeManager()->getStorage('entity_test_mulrev');
// Create a new entity.
/** @var \Drupal\entity_test\Entity\EntityTestMulRev $entity */
$entity = $storage->create(['name' => 'revision_test']);
$entity->save();
$this->setExpectedException(EntityStorageException::class, "Update existing 'entity_test_mulrev' entity revision while changing the revision ID is not supported.");
$entity->revision_id = 60;
$entity->save();
}
/**
* Tests that resaving an entity with a different entity ID throws an exception.
*/
public function testUpdateWithId() {
$storage = \Drupal::entityTypeManager()->getStorage('entity_test_mulrev');
// Create a new entity.
/** @var \Drupal\entity_test\Entity\EntityTestMulRev $entity */
$entity = $storage->create(['name' => 'revision_test']);
$entity->save();
$this->setExpectedException(EntityStorageException::class, "Update existing 'entity_test_mulrev' entity while changing the ID is not supported.");
$entity->id = 60;
$entity->save();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment