function CommentNonNodeTest::postComment

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

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->assertSession()
            ->fieldValueNotEquals('subject[0][value]', '');
    }
    if ($contact !== NULL && is_array($contact)) {
        $edit += $contact;
    }
    switch ($preview_mode) {
        case DRUPAL_REQUIRED:
            // Preview required so no save button should be found.
            $this->assertSession()
                ->buttonNotExists('Save');
            $this->submitForm($edit, 'Preview');
        // Don't break here so that we can test post-preview field presence and
        // function below.
        case DRUPAL_OPTIONAL:
            $this->assertSession()
                ->buttonExists('Preview');
            $this->assertSession()
                ->buttonExists('Save');
            $this->submitForm($edit, 'Save');
            break;
        case DRUPAL_DISABLED:
            $this->assertSession()
                ->buttonNotExists('Preview');
            $this->assertSession()
                ->buttonExists('Save');
            $this->submitForm($edit, '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->assertSession()
                ->pageTextContains($subject);
        }
        $this->assertSession()
            ->pageTextContains($comment);
        // Check the comment ID was extracted.
        $this->assertArrayHasKey(1, $match);
    }
    return Comment::load($match[1]);
}

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