diff --git a/core/modules/content_moderation/src/EntityOperations.php b/core/modules/content_moderation/src/EntityOperations.php index 8c6d3325089840426b950cfebeaef7f20fe03ad8..52d2ca3909fa4928cb64a24ecae99beee7c92396 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 4b7d75be4dba4ab2db6258bccb6912c1650fee8d..a1c383206dfa6bd2e45516283daa25b1c8140f85 100644 --- a/core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php +++ b/core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php @@ -238,6 +238,30 @@ 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->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. *