You are here

function CommentNonNodeTest::postComment in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/comment/src/Tests/CommentNonNodeTest.php \Drupal\comment\Tests\CommentNonNodeTest::postComment()

Posts a comment.

Parameters

\Drupal\Core\Entity\EntityInterface|null $entity: Entity to post comment on or NULL to post to the previously loaded page.

string $comment: Comment body.

string $subject: Comment subject.

mixed $contact: Set to NULL for no contact info, TRUE to ignore success checking, and array of values to set contact info.

Return value

\Drupal\comment\CommentInterface The new comment entity.

1 call to CommentNonNodeTest::postComment()
CommentNonNodeTest::testCommentFunctionality in core/modules/comment/src/Tests/CommentNonNodeTest.php
Tests anonymous comment functionality.

File

core/modules/comment/src/Tests/CommentNonNodeTest.php, line 115
Contains \Drupal\comment\Tests\CommentNonNodeTest.

Class

CommentNonNodeTest
Tests commenting on a test entity.

Namespace

Drupal\comment\Tests

Code

function postComment(EntityInterface $entity, $comment, $subject = '', $contact = NULL) {
  $edit = array();
  $edit['comment_body[0][value]'] = $comment;
  $field = FieldConfig::loadByName('entity_test', 'entity_test', 'comment');
  $preview_mode = $field
    ->getSetting('preview');

  // Must get the page before we test for fields.
  if ($entity !== NULL) {
    $this
      ->drupalGet('comment/reply/entity_test/' . $entity
      ->id() . '/comment');
  }

  // Determine the visibility of subject form field.
  if (entity_get_form_display('comment', 'comment', 'default')
    ->getComponent('subject')) {

    // Subject input allowed.
    $edit['subject[0][value]'] = $subject;
  }
  else {
    $this
      ->assertNoFieldByName('subject[0][value]', '', '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
        ->drupalPostForm(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
        ->drupalPostForm(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
        ->drupalPostForm(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 Comment::load($match[1]);
  }
}