function CommentTestBase::postComment

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

29 calls to CommentTestBase::postComment()
CommentAdminTest::testApprovalAdminInterface in core/modules/comment/tests/src/Functional/Views/CommentAdminTest.php
Tests comment approval functionality through admin/content/comment.
CommentAdminTest::testApprovalAdminInterface in core/modules/comment/tests/src/Functional/CommentAdminTest.php
Tests comment approval functionality through admin/content/comment.
CommentAdminTest::testApprovalNodeInterface in core/modules/comment/tests/src/Functional/CommentAdminTest.php
Tests comment approval functionality through the node interface.
CommentAdminTest::testCommentedEntityLabel in core/modules/comment/tests/src/Functional/Views/CommentAdminTest.php
Tests commented entity label of admin view.
CommentAdminTest::testCommentedTranslationDeletion in core/modules/comment/tests/src/Functional/CommentAdminTest.php
Tests commented translation deletion admin view.

... See full list

File

core/modules/comment/tests/src/Functional/CommentTestBase.php, line 121

Class

CommentTestBase
Provides setup and helper methods for comment tests.

Namespace

Drupal\Tests\comment\Functional

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.
    $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()
            ->fieldNotExists('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);
    }
    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.