diff --git a/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php index cef3742ac1..8ab867e375 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php @@ -376,8 +376,10 @@ protected function doSave($id, EntityInterface $entity) { $this->populateAffectedRevisionTranslations($entity); - // Populate the "revision_default" flag. - if ($this->entityType->isRevisionable()) { + // Populate the "revision_default" flag. We skip this when we are resaving + // the revision because this is only allowed for default revisions, and + // these cannot be made non-default. + if ($this->entityType->isRevisionable() && $entity->isNewRevision()) { $revision_default_key = $this->entityType->getRevisionMetadataKey('revision_default'); $entity->set($revision_default_key, $entity->isDefaultRevision()); } diff --git a/core/tests/Drupal/KernelTests/Core/Entity/RevisionableContentEntityBaseTest.php b/core/tests/Drupal/KernelTests/Core/Entity/RevisionableContentEntityBaseTest.php index b147f4ae4d..d478d19d32 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/RevisionableContentEntityBaseTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/RevisionableContentEntityBaseTest.php @@ -141,22 +141,39 @@ public function testWasDefaultRevision() { $entity = $storage->loadUnchanged($entity->id()); $this->assertTrue($entity->wasDefaultRevision()); + // Check that the "revision_default" flag cannot be changed once set. + /** @var \Drupal\entity_test_revlog\Entity\EntityTestMulWithRevisionLog $entity2 */ + $entity2 = EntityTestMulWithRevisionLog::create([ + 'type' => $entity_type_id, + ]); + $entity2->save(); + $this->assertTrue($entity2->wasDefaultRevision()); + $entity2->isDefaultRevision(FALSE); + $entity2->save(); + $this->assertTrue($entity2->wasDefaultRevision()); + // Simulate missing field data and verify an error is triggered in this // case, while the return value matches the entity object's default value // state. + /** @var \Drupal\entity_test_revlog\Entity\EntityTestMulWithRevisionLog $entity3 */ + $entity3 = EntityTestMulWithRevisionLog::create([ + 'type' => $entity_type_id, + ]); + $entity3->save(); /** @var \Drupal\Core\Database\Connection $database */ $database = $this->container->get('database'); $database->update($entity_type_id . '_revision') ->fields(['revision_default' => NULL]) + ->condition('id', $entity3->id()) ->execute(); - $storage->resetCache([$entity->id()]); - $entity = $storage->load($entity->id()); - $entity->isDefaultRevision(TRUE); - $this->assertTrue(@$entity->wasDefaultRevision()); - $entity->isDefaultRevision(FALSE); - $this->assertFalse(@$entity->wasDefaultRevision()); + $storage->resetCache([$entity3->id()]); + $entity3 = $storage->load($entity3->id()); + $entity3->isDefaultRevision(TRUE); + $this->assertTrue(@$entity3->wasDefaultRevision()); + $entity3->isDefaultRevision(FALSE); + $this->assertFalse(@$entity3->wasDefaultRevision()); $this->setExpectedException('\PHPUnit_Framework_Error', "Missing data for the 'revision_default' field on the entity $entity_type_id:{$entity->id()}."); - $entity->wasDefaultRevision(); + $entity3->wasDefaultRevision(); } /**