function CommentTestBase::postComment

Same name in this branch
  1. 8.9.x core/modules/comment/tests/src/Functional/CommentTestBase.php \Drupal\Tests\comment\Functional\CommentTestBase::postComment()
Same name in other branches
  1. 9 core/modules/comment/tests/src/Functional/CommentTestBase.php \Drupal\Tests\comment\Functional\CommentTestBase::postComment()
  2. 10 core/modules/comment/tests/src/Functional/CommentTestBase.php \Drupal\Tests\comment\Functional\CommentTestBase::postComment()
  3. 11.x core/modules/comment/tests/src/Functional/CommentTestBase.php \Drupal\Tests\comment\Functional\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.

File

core/modules/comment/src/Tests/CommentTestBase.php, line 124

Class

CommentTestBase
Provides setup and helper methods for comment tests.

Namespace

Drupal\comment\Tests

Code

public function postComment($entity, $comment, $subject = '', $contact = NULL, $field_name = 'comment') {
    $edit = [];
    $edit['comment_body[0][value]'] = $comment;
    if ($entity !== NULL) {
        $field = FieldConfig::loadByName($entity->getEntityTypeId(), $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/' . $entity->getEntityTypeId() . '/' . $entity->id() . '/' . $field_name);
    }
    // Determine the visibility of subject form field.
    if (\Drupal::service('entity_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])) {
        \Drupal::entityTypeManager()->getStorage('comment')
            ->resetCache([
            $match[1],
        ]);
        return Comment::load($match[1]);
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.