From 1f9eabbd92bb45ff5914925be3d835e44c1669e1 Mon Sep 17 00:00:00 2001
From: xjm <xjm@65776.no-reply.drupal.org>
Date: Mon, 27 Apr 2015 17:05:36 -0500
Subject: [PATCH] Issue #2405165 by yched, alexpott, xjm:
 Entity::setOriginalId() does enforceIsNew(FALSE), that is wrong for
 ConfigEntities

---
 .../Drupal/Core/Config/Entity/ConfigEntityBase.php    |  8 ++++++--
 core/lib/Drupal/Core/Entity/EntityDisplayBase.php     |  2 --
 .../Core/Config/Entity/ConfigEntityBaseUnitTest.php   | 11 +++++++++--
 3 files changed, 15 insertions(+), 6 deletions(-)

diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php b/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php
index 67405415aa70..0ca35a539caa 100644
--- a/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php
+++ b/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php
@@ -130,9 +130,13 @@ public function getOriginalId() {
    * {@inheritdoc}
    */
   public function setOriginalId($id) {
+    // Do not call the parent method since that would mark this entity as no
+    // longer new. Unlike content entities, new configuration entities have an
+    // ID.
+    // @todo https://www.drupal.org/node/2478811 Document the entity life cycle
+    //   and the differences between config and content.
     $this->originalId = $id;
-
-    return parent::setOriginalId($id);
+    return $this;
   }
 
   /**
diff --git a/core/lib/Drupal/Core/Entity/EntityDisplayBase.php b/core/lib/Drupal/Core/Entity/EntityDisplayBase.php
index 4636c2a28c73..ca17e6410f8a 100644
--- a/core/lib/Drupal/Core/Entity/EntityDisplayBase.php
+++ b/core/lib/Drupal/Core/Entity/EntityDisplayBase.php
@@ -472,7 +472,6 @@ public function __sleep() {
    * {@inheritdoc}
    */
   public function __wakeup() {
-    $is_new = $this->isNew();
     // Determine what were the properties from toArray() that were saved in
     // __sleep().
     $keys = $this->_serializedKeys;
@@ -481,7 +480,6 @@ public function __wakeup() {
     // Run those values through the __construct(), as if they came from a
     // regular entity load.
     $this->__construct($values, $this->entityTypeId);
-    $this->enforceIsNew($is_new);
   }
 
 }
diff --git a/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php b/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php
index 9456d33c81c5..caf51ddcbbb5 100644
--- a/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php
+++ b/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php
@@ -7,9 +7,7 @@
 
 namespace Drupal\Tests\Core\Config\Entity;
 
-use Drupal\Component\Plugin\ConfigurablePluginInterface;
 use Drupal\Core\DependencyInjection\ContainerBuilder;
-use Drupal\Component\Plugin\PluginBase;
 use Drupal\Core\Language\Language;
 use Drupal\Tests\Core\Plugin\Fixtures\TestConfigurablePlugin;
 use Drupal\Tests\UnitTestCase;
@@ -317,6 +315,15 @@ public function testGetOriginalId() {
     $this->assertSame($this->id, $this->entity->getOriginalId());
     $this->assertSame($this->entity, $this->entity->setOriginalId($new_id));
     $this->assertSame($new_id, $this->entity->getOriginalId());
+
+    // Check that setOriginalId() does not change the entity "isNew" status.
+    $this->assertFalse($this->entity->isNew());
+    $this->entity->setOriginalId($this->randomMachineName());
+    $this->assertFalse($this->entity->isNew());
+    $this->entity->enforceIsNew();
+    $this->assertTrue($this->entity->isNew());
+    $this->entity->setOriginalId($this->randomMachineName());
+    $this->assertTrue($this->entity->isNew());
   }
 
   /**
-- 
GitLab