function CommentTestBase::postComment

Posts a comment.

Parameters

Drupal\node\Node|null $node: Node to post comment on or NULL to post to the previusly 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.

21 calls to CommentTestBase::postComment()
CommentActionsTest::testCommentPublishUnpublishActions in drupal/core/modules/comment/lib/Drupal/comment/Tests/CommentActionsTest.php
Tests comment publish and unpublish actions.
CommentAnonymousTest::testAnonymous in drupal/core/modules/comment/lib/Drupal/comment/Tests/CommentAnonymousTest.php
Tests anonymous comment functionality.
CommentApprovalTest::testApprovalAdminInterface in drupal/core/modules/comment/lib/Drupal/comment/Tests/CommentApprovalTest.php
Test comment approval functionality through admin/content/comment.
CommentApprovalTest::testApprovalNodeInterface in drupal/core/modules/comment/lib/Drupal/comment/Tests/CommentApprovalTest.php
Tests comment approval functionality through the node interface.
CommentAttributesTest::testCommentRdfaMarkup in drupal/core/modules/rdf/lib/Drupal/rdf/Tests/CommentAttributesTest.php
Tests if RDFa markup for meta information is present in comments.

... See full list

File

drupal/core/modules/comment/lib/Drupal/comment/Tests/CommentTestBase.php, line 92
Contains Drupal\comment\Tests\CommentTestBase.

Class

CommentTestBase
Provides setup and helper methods for comment tests.

Namespace

Drupal\comment\Tests

Code

function postComment($node, $comment, $subject = '', $contact = NULL) {
  $langcode = LANGUAGE_NOT_SPECIFIED;
  $edit = array();
  $edit['comment_body[' . $langcode . '][0][value]'] = $comment;
  $preview_mode = variable_get('comment_preview_article', DRUPAL_OPTIONAL);
  $subject_mode = variable_get('comment_subject_field_article', 1);

  // Must get the page before we test for fields.
  if ($node !== NULL) {
    $this
      ->drupalGet('comment/reply/' . $node->nid);
  }
  if ($subject_mode == TRUE) {
    $edit['subject'] = $subject;
  }
  else {
    $this
      ->assertNoFieldByName('subject', '', 'Subject field not found.');
  }
  if ($contact !== NULL && is_array($contact)) {
    $edit += $contact;
  }
  switch ($preview_mode) {
    case DRUPAL_REQUIRED:

      // Preview required so no save button should be found.
      $this
        ->assertNoFieldByName('op', t('Save'), 'Save button not found.');
      $this
        ->drupalPost(NULL, $edit, t('Preview'));

    // Don't break here so that we can test post-preview field presence and
    // function below.
    case DRUPAL_OPTIONAL:
      $this
        ->assertFieldByName('op', t('Preview'), 'Preview button found.');
      $this
        ->assertFieldByName('op', t('Save'), 'Save button found.');
      $this
        ->drupalPost(NULL, $edit, t('Save'));
      break;
    case DRUPAL_DISABLED:
      $this
        ->assertNoFieldByName('op', t('Preview'), 'Preview button not found.');
      $this
        ->assertFieldByName('op', t('Save'), 'Save button found.');
      $this
        ->drupalPost(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) {
      $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,
    ));
  }
}