diff --git a/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php index df2b2ca..2b10907 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php @@ -1062,11 +1062,11 @@ public function __clone() { $translations = $this->translations; $this->translations = &$translations; - // Ensure the enforceIsNew property is actually cloned by overwriting the - // original reference with one pointing to a copy of it. + // Ensure the enforceIsNew and newRevision properties are actually cloned + // by overwriting the original references with ones pointing to copies of + // them. $enforce_is_new = $this->enforceIsNew; $this->enforceIsNew = &$enforce_is_new; - $new_revision = $this->newRevision; $this->newRevision = &$new_revision; } diff --git a/core/tests/Drupal/KernelTests/Core/Entity/ContentEntityCloneTest.php b/core/tests/Drupal/KernelTests/Core/Entity/ContentEntityCloneTest.php index cca7eb1..b64f833 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/ContentEntityCloneTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/ContentEntityCloneTest.php @@ -3,6 +3,7 @@ namespace Drupal\KernelTests\Core\Entity; use Drupal\entity_test\Entity\EntityTestMul; +use Drupal\entity_test\Entity\EntityTestMulRev; use Drupal\language\Entity\ConfigurableLanguage; /** @@ -27,6 +28,7 @@ protected function setUp() { ConfigurableLanguage::createFromLangcode('de')->save(); $this->installEntitySchema('entity_test_mul'); + $this->installEntitySchema('entity_test_mulrev'); } /** @@ -78,15 +80,47 @@ public function testEnforceIsNewOnClonedEntityTranslation() { // The entity is not new anymore. $this->assertFalse($entity_translation->isNew()); - // The clone should not be new as well. + // The clone should not be new either. $clone = clone $entity_translation; $this->assertFalse($clone->isNew()); - // After enforcing the clone to be new only it should be flagged as new, - // but the original entity should not be flagged as new. + // After forcing the clone to be new only it should be flagged as new, but + // the original entity should not. $clone->enforceIsNew(); $this->assertTrue($clone->isNew()); $this->assertFalse($entity_translation->isNew()); } + /** + * Tests that the flag for enforcing a new revision is not shared. + */ + public function testNewRevisionOnCloneEntityTranslation() { + // Create a test entity. + $entity = EntityTestMulRev::create([ + 'name' => $this->randomString(), + 'language' => 'en', + ]); + $entity->save(); + $entity->addTranslation('de'); + $entity->save(); + + // Reload the entity as ContentEntityBase::postCreate() forces the entity to + // be a new revision. + $entity = EntityTestMulRev::load($entity->id()); + $entity_translation = $entity->getTranslation('de'); + + // The entity is not set to be a new revision. + $this->assertFalse($entity_translation->isNewRevision()); + + // The clone should not be set to be a new revision either. + $clone = clone $entity_translation; + $this->assertFalse($clone->isNewRevision()); + + // After forcing the clone to be a new revision only it should be flagged + // as a new revision, but the original entity should not. + $clone->setNewRevision(); + $this->assertTrue($clone->isNewRevision()); + $this->assertFalse($entity_translation->isNewRevision()); + } + }