From d78aa97227294317fb4530debe3fbfd91f0b4593 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Mon, 25 Mar 2019 15:14:52 +0000
Subject: [PATCH] Issue #3003238 by Sam152, Berdir, amateescu, catch, jibran:
 EntityStorageException: Default revision can not be deleted in
 content_moderation_entity_revision_delete()

(cherry picked from commit c7beade985dc4a6285f7a328079cea40ef6fa612)
---
 .../src/EntityOperations.php                  |  9 ++++---
 .../src/Kernel/ContentModerationStateTest.php | 25 +++++++++++++++++++
 2 files changed, 30 insertions(+), 4 deletions(-)

diff --git a/core/modules/content_moderation/src/EntityOperations.php b/core/modules/content_moderation/src/EntityOperations.php
index 8c6d33250898..52d2ca3909fa 100644
--- a/core/modules/content_moderation/src/EntityOperations.php
+++ b/core/modules/content_moderation/src/EntityOperations.php
@@ -235,10 +235,11 @@ public function entityDelete(EntityInterface $entity) {
    * @see hook_entity_revision_delete()
    */
   public function entityRevisionDelete(EntityInterface $entity) {
-    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
-    if (!$entity->isDefaultRevision()) {
-      $content_moderation_state = ContentModerationStateEntity::loadFromModeratedEntity($entity);
-      if ($content_moderation_state) {
+    if ($content_moderation_state = ContentModerationStateEntity::loadFromModeratedEntity($entity)) {
+      if ($content_moderation_state->isDefaultRevision()) {
+        $content_moderation_state->delete();
+      }
+      else {
         $this->entityTypeManager
           ->getStorage('content_moderation_state')
           ->deleteRevision($content_moderation_state->getRevisionId());
diff --git a/core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php b/core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php
index 5311d88f5c9b..86c43895977e 100644
--- a/core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php
+++ b/core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php
@@ -240,6 +240,31 @@ public function testContentModerationStatePendingRevisionDataRemoval($entity_typ
     $this->assertFalse($content_moderation_state);
   }
 
+  /**
+   * Tests removal of content moderation state entities for preexisting content.
+   */
+  public function testExistingContentModerationStateDataRemoval() {
+    $storage = $this->entityTypeManager->getStorage('entity_test_mulrevpub');
+
+    $entity = $storage->create([]);
+    $entity->save();
+    $original_revision_id = $entity->getRevisionId();
+
+    $workflow = $this->createEditorialWorkflow();
+    $workflow->getTypePlugin()->addEntityTypeAndBundle($entity->getEntityTypeId(), $entity->bundle());
+    $workflow->save();
+
+    $entity = $this->reloadEntity($entity);
+    $entity->moderation_state = 'draft';
+    $entity->save();
+
+    $storage->deleteRevision($entity->getRevisionId());
+
+    $entity = $this->reloadEntity($entity);
+    $this->assertEquals('published', $entity->moderation_state->value);
+    $this->assertEquals($original_revision_id, $storage->getLatestRevisionId($entity->id()));
+  }
+
   /**
    * Tests removal of content moderation state translations.
    *
-- 
GitLab