You are here

public function CommentNonNodeTest::postComment in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/comment/tests/src/Functional/CommentNonNodeTest.php \Drupal\Tests\comment\Functional\CommentNonNodeTest::postComment()
  2. 10 core/modules/comment/tests/src/Functional/CommentNonNodeTest.php \Drupal\Tests\comment\Functional\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/tests/src/Functional/CommentNonNodeTest.php
Tests anonymous comment functionality.

File

core/modules/comment/tests/src/Functional/CommentNonNodeTest.php, line 124

Class

CommentNonNodeTest
Tests commenting on a test entity.

Namespace

Drupal\Tests\comment\Functional

Code

public function postComment(EntityInterface $entity, $comment, $subject = '', $contact = NULL) {
  $edit = [];
  $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.
  $display_repository = $this->container
    ->get('entity_display.repository');
  if ($display_repository
    ->getFormDisplay('comment', 'comment')
    ->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 = [];

  // 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]);
  }
}