diff --git a/modules/comment/comment.module b/modules/comment/comment.module index 3c94200..de83ef0 100644 --- a/modules/comment/comment.module +++ b/modules/comment/comment.module @@ -2068,7 +2068,10 @@ function comment_preview($comment) { if (!form_get_errors()) { $comment_body = field_get_items('comment', $comment, 'comment_body'); - $comment->format = $comment_body[0]['format']; + // Empty comments and plain text comments do not have a format. + if ($comment_body && isset($comment_body[0]['format'])) { + $comment->format = $comment_body[0]['format']; + } // Attach the user and time information. if (!empty($comment->name)) { $account = user_load_by_name($comment->name); @@ -2193,17 +2196,22 @@ function comment_submit($comment) { // 2) Strip out all HTML tags // 3) Convert entities back to plain-text. $field = field_info_field('comment_body'); + // Do not try to extract from comment_body if no such field exists. $langcode = field_is_translatable('comment', $field) ? entity_language('comment', $comment) : LANGUAGE_NONE; - $comment_body = $comment->comment_body[$langcode][0]; - if (isset($comment_body['format'])) { - $comment_text = check_markup($comment_body['value'], $comment_body['format']); - } - else { - $comment_text = check_plain($comment_body['value']); + if ($field && isset($comment->comment_body[$langcode][0])) { + $comment_body = $comment->comment_body[$langcode][0]; + if (isset($comment_body['format'])) { + $comment_text = check_markup($comment_body['value'], $comment_body['format']); + } + elseif(isset($comment_body['value'])){ + $comment_text = check_plain($comment_body['value']); + } + if(isset($comment_text)){ + $comment->subject = truncate_utf8(trim(decode_entities(strip_tags($comment_text))), 29, TRUE); + } } - $comment->subject = truncate_utf8(trim(decode_entities(strip_tags($comment_text))), 29, TRUE); - // Edge cases where the comment body is populated only by HTML tags will - // require a default subject. + // Edge cases where the comment body is populated only by HTML tags or does + // not exist will require a default subject. if ($comment->subject == '') { $comment->subject = t('(No subject)'); } diff --git a/modules/comment/comment.test b/modules/comment/comment.test index 9e69ba6..cbf15b4 100644 --- a/modules/comment/comment.test +++ b/modules/comment/comment.test @@ -2102,6 +2102,63 @@ class CommentFieldsTest extends CommentHelperCase { $edit = array('comment_body[und][0][value]' => $this->randomName(8)); $this->drupalPost('node/' . $this->node->nid, $edit, t('Save')); } + + /** + * Test that saving an empty, required body generates a warning, and cannot be saved. + */ + function testCommentEmptyRequiredBody($required = TRUE) { + // Set the requirement-indicator of the comment_body field. + $this->drupalLogin($this->admin_user); + $edit = array('instance[required]' => $required); + $this->drupalPost('admin/structure/types/manage/article/comment/fields/comment_body', $edit, t('Save settings')); + + // Preview an empty comment, and check if an error is shown. + $this->drupalPost('node/' . $this->node->nid, array(), t('Preview')); + $this->assertText('Comment field is required.', 'Cannot preview comment with empty required body.'); + + // Post an empty comment, and check if an error is shown. + $this->drupalPost('node/' . $this->node->nid, array(), t('Save')); + $this->assertText('Comment field is required.', 'Cannot save comment with empty required body.'); + } + + /** + * Test that saving an empty, NON-required body generates a warning, and cannot be saved. + */ + function testCommentEmptyNonRequiredBody($required = FALSE) { + // Set the requirement-indicator of the comment_body field. + $this->drupalLogin($this->admin_user); + $edit = array('instance[required]' => $required); + $this->drupalPost('admin/structure/types/manage/article/comment/fields/comment_body', $edit, t('Save settings')); + + // Preview an empty comment, and check if an error is shown. + $this->drupalPost('node/' . $this->node->nid, array(), t('Preview')); + $this->assertNoText('Comment field is required.', 'Can preview comment with empty required body.'); + + // Post an empty comment, and check if an error is shown. + $this->drupalPost('node/' . $this->node->nid, array(), t('Save')); + $this->assertNoText('Comment field is required.', 'Can save comment with empty required body.'); + $this->assertText('Your comment has been posted.', 'Can save comment with empty required body.'); + } + + /** + * Test that a comment can be previewed and saved without the comment_body field, + * and without generating warnings. + */ + function testCommentDeletedBody() { + // Delete the comment_body field. + $this->drupalLogin($this->admin_user); + $this->drupalPost('admin/structure/types/manage/article/comment/fields/comment_body/delete', array(), t('Delete')); + + // Preview an empty comment. + $this->drupalPost('node/' . $this->node->nid, array(), t('Preview')); + + // Post an empty comment. + $this->drupalPost('node/' . $this->node->nid, array(), t('Save')); + + // View the comment administration overview page. + $this->drupalGet('admin/content/comment'); + } + } /**