1.20.x comment.test | CommentHelperCase::postComment($node, $comment, $subject = '', $contact = NULL) |
Posts a comment.
Parameters
Node|NULL $node: Node to post comment on or NULL to post to the previously loaded page.
$comment: Comment body.
$subject: Comment subject.
$contact: Set to NULL for no contact info, TRUE to ignore success checking, and array of values to set contact info.
File
- modules/
comment/ tests/ comment.test, line 91 - Tests for the Comment module.
Class
Code
function postComment($node, $comment, $subject = '', $contact = NULL) {
$langcode = LANGUAGE_NONE;
$edit = array();
$edit['comment_body[' . $langcode . '][0][value]'] = $comment;
$node_type = node_type_get_type('post');
$preview_mode = $node_type->settings['comment_preview'];
$subject_mode = $node_type->settings['comment_subject_field'];
// Must get the page before we test for fields.
if ($node !== NULL) {
$this->backdropGet('comment/reply/' . $node->nid);
}
if ($subject_mode == TRUE) {
$edit['subject'] = $subject;
}
else {
$this->assertNoFieldByName('subject', '', 'Title field not found.');
}
if ($contact !== NULL && is_array($contact)) {
$edit += $contact;
}
switch ($preview_mode) {
case BACKDROP_REQUIRED:
// Preview required so no save button should be found.
$this->assertNoFieldByName('op', t('Save'), 'Save button not found.');
$this->backdropPost(NULL, $edit, t('Preview'));
// Don't break here so that we can test post-preview field presence and
// function below.
case BACKDROP_OPTIONAL:
$this->assertFieldByName('op', t('Preview'), 'Preview button found.');
$this->assertFieldByName('op', t('Save'), 'Save button found.');
$this->backdropPost(NULL, $edit, t('Save'));
break;
case BACKDROP_DISABLED:
$this->assertNoFieldByName('op', t('Preview'), 'Preview button not found.');
$this->assertFieldByName('op', t('Save'), 'Save button found.');
$this->backdropPost(NULL, $edit, t('Save'));
break;
}
$match = array();
// Get comment ID
preg_match('/#comment-([0-9]+)/', $this->getURL(), $match);
// Get comment.
if ($contact !== TRUE) { // If true then attempting to find error message.
if ($subject && $subject_mode) {
$this->assertText($subject, 'Comment subject posted.');
}
$this->assertText($comment, 'Comment body posted.');
$this->assertTrue((!empty($match) && !empty($match[1])), 'Comment id found.');
}
if (isset($match[1])) {
return entity_create('comment', array('id' => $match[1], 'subject' => $subject, 'comment' => $comment));
}
}