diff --git a/core/modules/comment/src/Tests/CommentOrphanedTest.php b/core/modules/comment/src/Tests/CommentOrphanedTest.php
new file mode 100644
index 0000000..595826f
--- /dev/null
+++ b/core/modules/comment/src/Tests/CommentOrphanedTest.php
@@ -0,0 +1,58 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\comment\Tests\CommentOrphanedTest.
+ */
+
+namespace Drupal\comment\Tests;
+
+/**
+ * Tests operations on orphaned comments.
+ *
+ * @group comment
+ */
+class CommentOrphanedTest extends CommentTestBase {
+
+  /**
+   * Modules to install. We would like to have every module installed that
+   * implements hook_comment_storage_load(), so we can implicitly test it.
+   * At this moment, RDF is the only core module implementing the hook.
+   *
+   * @var array
+   */
+  public static $modules = ['comment_test', 'rdf', 'comment', 'node'];
+
+  /**
+   * Test the new comments field plugin.
+   */
+  public function testDeleteOrphanedComment() {
+    /** @var \Drupal\Core\Entity\EntityManagerInterface $manager */
+    $manager = $this->container->get('entity.manager');
+
+    //  We only know how to orphan comments on SQL based entity storage.
+    $storage = $manager->getStorage('comment');
+    if (in_array('Drupal\Core\Entity\Sql\SqlEntityStorageInterface', class_implements($storage))) {
+
+      // Make sure we have a comment.
+      $this->drupalLogin($this->webUser);
+      $comment_text = $this->randomMachineName();
+      $subject = $this->randomMachineName();
+      $this->postComment($this->node, $comment_text, $subject);
+
+      // Mess with the comments to make them orphaned.
+      $manager->clearCachedDefinitions();
+      $manager->getDefinition('comment')->setHandlerClass('storage', 'Drupal\comment_test\CommentTestSqlEntityStorage');
+      $storage = $manager->getStorage('comment');
+
+      $ids = $storage->orphanComments();
+
+      // Now load and delete these comments, and implicitly test that nothing
+      // breaks.
+      $entities = $storage->loadMultiple($ids);
+      $storage->delete($entities);
+    }
+
+  }
+
+}
diff --git a/core/modules/comment/tests/modules/comment_test/src/CommentTestSqlEntityStorage.php b/core/modules/comment/tests/modules/comment_test/src/CommentTestSqlEntityStorage.php
new file mode 100644
index 0000000..336b18b
--- /dev/null
+++ b/core/modules/comment/tests/modules/comment_test/src/CommentTestSqlEntityStorage.php
@@ -0,0 +1,56 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\comment_test\CommentTestSqlEntityStorage.
+ */
+
+namespace Drupal\comment_test;
+
+use Drupal\comment\CommentStorage;
+
+/**
+ * Comment storage handler for the comment_test.module.
+ */
+class CommentTestSqlEntityStorage extends CommentStorage {
+
+  /**
+   * Intentionally makes reference data (commented entity, parent comment,
+   * author) invalid, in the SQL storage backend.
+   *
+   * @param array $comment_ids
+   *   IDs of comments to orphan. If not provided, one comment will be taken
+   *   semi randomly (if one exists).
+   *
+   * @return array
+   *   The orphaned comment ID(s), i.e. the input argument if it was nonempty.
+   */
+  public function orphanComments(array $comment_ids = array()) {
+
+    // Get the maximum parent/author and set it one higher; this emulates
+    // a parent/author which has been deleted after creating.
+    $query = $this->database->select('comment_field_data', 'c');
+    $query->addExpression('MAX(cid)', 'cid_max');
+    $query->addExpression('MAX(pid)', 'pid_max');
+    $query->addExpression('MAX(uid)', 'uid_max');
+    $query->addExpression('MAX(entity_id)', 'entity_id_max');
+    $row = $query->execute()->fetchObject();
+    if (!isset($row->pid_max) && isset($row->cid_max)) {
+      $row->pid_max = $row->cid_max;
+    }
+
+    if (empty($comment_ids) && isset($row->cid_max)) {
+      $comment_ids = array($row->cid_max);
+    }
+
+    if (!empty($comment_ids)) {
+      $this->database->update('comment_field_data')
+        ->fields(array('entity_id' => $row->entity_id_max + 1, 'pid' => $row->pid_max + 1, 'uid' => $row->uid_max + 1))
+        ->condition('cid', $comment_ids)
+        ->execute();
+
+      $this->resetCache();
+    }
+  }
+
+}
