Issue #2640496 by Berdir, catch, amateescu: Revision ID in {node} and {node_revision} can get out of sync

8.3.x
Alex Pott 2017-01-05 10:30:18 +00:00
parent faa401606c
commit cc3f37cca8
3 changed files with 54 additions and 1 deletions

View File

@ -295,7 +295,22 @@ abstract class ContentEntityStorageBase extends EntityStorageBase implements Con
$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;
}
/**

View File

@ -491,6 +491,10 @@ class ContentEntityChangedTest extends EntityKernelTestBase {
'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();

View File

@ -175,4 +175,38 @@ class EntityApiTest extends EntityKernelTestBase {
}
}
/**
* 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();
}
}