diff --git a/core/modules/comment/src/Tests/CommentOrphanedTest.php b/core/modules/comment/src/Tests/CommentOrphanedTest.php index 962e834..595826f 100644 --- a/core/modules/comment/src/Tests/CommentOrphanedTest.php +++ b/core/modules/comment/src/Tests/CommentOrphanedTest.php @@ -35,7 +35,10 @@ public function testDeleteOrphanedComment() { if (in_array('Drupal\Core\Entity\Sql\SqlEntityStorageInterface', class_implements($storage))) { // Make sure we have a comment. - $this->postComment($this->node, $this->randomMachineName()); + $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(); @@ -46,7 +49,6 @@ public function testDeleteOrphanedComment() { // Now load and delete these comments, and implicitly test that nothing // breaks. - $storage->resetCache(); $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 index f6d3a15..336b18b 100644 --- a/core/modules/comment/tests/modules/comment_test/src/CommentTestSqlEntityStorage.php +++ b/core/modules/comment/tests/modules/comment_test/src/CommentTestSqlEntityStorage.php @@ -15,8 +15,8 @@ class CommentTestSqlEntityStorage extends CommentStorage { /** - * Intentionally makes reference data (author, parent comment) invalid, in the - * SQL storage backend. + * 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 @@ -27,30 +27,26 @@ class CommentTestSqlEntityStorage extends CommentStorage { */ public function orphanComments(array $comment_ids = array()) { - if (empty($comment_ids)) { - $query = $this->database->select('comment_field_data', 'c'); - $query->addExpression('MAX(cid)'); - $cid = $query->execute()->fetchField(); - if ($cid) { - $comment_ids = array($cid); - } + // 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)) { - // 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(entity_id)', 'entity_id_max'); - $query->addExpression('MAX(pid)', 'pid_max'); - $query->addExpression('MAX(uid)', 'uid_max'); - $row = $query->execute()->fetchObject(); - if (!isset($row->pid_max)) { - $row->pid_max = 0; - } + 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, 'pid' => $row->pid_max, 'uid' => $row->uid_max)) - ->condition('cid', array($comment_ids)) + ->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();