public function CommentTestBase::postComment in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/comment/src/Tests/CommentTestBase.php \Drupal\comment\Tests\CommentTestBase::postComment()
Posts a comment.
Parameters
\Drupal\Core\Entity\EntityInterface|null $entity: Node to post comment on or NULL to post to the previously loaded page.
string $comment: Comment body.
string $subject: Comment subject.
string $contact: Set to NULL for no contact info, TRUE to ignore success checking, and array of values to set contact info.
string $field_name: (optional) Field name through which the comment should be posted. Defaults to 'comment'.
Return value
\Drupal\comment\CommentInterface|null The posted comment or NULL when posted comment was not found.
25 calls to CommentTestBase::postComment()
- CommentActionsTest::testCommentPublishUnpublishActions in core/modules/ comment/ src/ Tests/ CommentActionsTest.php 
- Tests comment publish and unpublish actions.
- CommentActionsTest::testCommentUnpublishByKeyword in core/modules/ comment/ src/ Tests/ CommentActionsTest.php 
- Tests the unpublish comment by keyword action.
- CommentAdminTest::testApprovalAdminInterface in core/modules/ comment/ src/ Tests/ CommentAdminTest.php 
- Test comment approval functionality through admin/content/comment.
- CommentAdminTest::testApprovalNodeInterface in core/modules/ comment/ src/ Tests/ CommentAdminTest.php 
- Tests comment approval functionality through the node interface.
- CommentAdminTest::testEditComment in core/modules/ comment/ src/ Tests/ CommentAdminTest.php 
- Tests editing a comment as an admin.
File
- core/modules/ comment/ src/ Tests/ CommentTestBase.php, line 114 
- Contains \Drupal\comment\Tests\CommentTestBase.
Class
- CommentTestBase
- Provides setup and helper methods for comment tests.
Namespace
Drupal\comment\TestsCode
public function postComment($entity, $comment, $subject = '', $contact = NULL, $field_name = 'comment') {
  $edit = array();
  $edit['comment_body[0][value]'] = $comment;
  if ($entity !== NULL) {
    $field = FieldConfig::loadByName('node', $entity
      ->bundle(), $field_name);
  }
  else {
    $field = FieldConfig::loadByName('node', 'article', $field_name);
  }
  $preview_mode = $field
    ->getSetting('preview');
  // Must get the page before we test for fields.
  if ($entity !== NULL) {
    $this
      ->drupalGet('comment/reply/node/' . $entity
      ->id() . '/' . $field_name);
  }
  // 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])) {
    \Drupal::entityManager()
      ->getStorage('comment')
      ->resetCache(array(
      $match[1],
    ));
    return Comment::load($match[1]);
  }
}