You are here

protected function PhraseCaptchaTestCase::assertCommentPosting in CAPTCHA Pack 8

Assert function for testing if comment posting works as it should.

Creates node with comment writing enabled, tries to post comment with given CAPTCHA response (caller should enable the desired challenge on page node comment forms) and checks if the result is as expected.

Parameters

string $captcha_response: The response on the CAPTCHA.

bool $should_pass: Describing if the posting should pass or should be blocked.

string $message: To prefix to nested asserts.

1 call to PhraseCaptchaTestCase::assertCommentPosting()
PhraseCaptchaTestCase::testPhraseCaseInsensitiveValidation in text_captcha/modules/phrase_captcha/src/Test/PhraseCaptchaTestCase.php
Testing the case sensitive/insensitive validation.

File

text_captcha/modules/phrase_captcha/src/Test/PhraseCaptchaTestCase.php, line 78

Class

PhraseCaptchaTestCase
Phrase CAPTCHA main test case sensitivity.

Namespace

Drupal\phrase_captcha\Tests

Code

protected function assertCommentPosting($captcha_response, $should_pass, $message) {

  // Make sure comments on pages can be saved directly without preview.
  $this->container
    ->get('state')
    ->set('comment_preview_page', DRUPAL_OPTIONAL);

  // Create a node with comments enabled.
  $node = $this
    ->drupalCreateNode();

  // Post comment on node.
  $edit = $this
    ->getCommentFormValues();
  $comment_subject = $edit['subject[0][value]'];
  $comment_body = $edit['comment_body[0][value]'];
  $edit['captcha_response'] = $captcha_response;
  $this
    ->drupalPostForm('comment/reply/node/' . $node
    ->id() . '/comment', $edit, t('Save'), [], [], 'comment-form');
  if ($should_pass) {

    // There should be no error message.
    $this
      ->assertCaptchaResponseAccepted();

    // Get node page and check that comment shows up.
    $this
      ->drupalGet('node/' . $node
      ->id());
    $this
      ->assertText($comment_subject, $message . ' Comment should show up on node page.', 'CAPTCHA');
    $this
      ->assertText($comment_body, $message . ' Comment should show up on node page.', 'CAPTCHA');
  }
  else {

    // Check for error message.
    $this
      ->assertText(self::CAPTCHA_WRONG_RESPONSE_ERROR_MESSAGE, $message . ' Comment submission should be blocked.', 'CAPTCHA');

    // Get node page and check that comment is not present.
    $this
      ->drupalGet('node/' . $node
      ->id());
    $this
      ->assertNoText($comment_subject, $message . ' Comment should not show up on node page.', 'CAPTCHA');
    $this
      ->assertNoText($comment_body, $message . ' Comment should not show up on node page.', 'CAPTCHA');
  }
}