You are here

protected function AsciiArtCaptchaTestCase::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 AsciiArtCaptchaTestCase::assertCommentPosting()
AsciiArtCaptchaTestCase::testAsciiArtCaseInsensitiveValidation in ascii_art_captcha/src/Tests/AsciiArtCaptchaTestCase.php
Testing the case sensitive/insensitive validation.

File

ascii_art_captcha/src/Tests/AsciiArtCaptchaTestCase.php, line 78

Class

AsciiArtCaptchaTestCase
Tests Ascii art CAPTCHA main test case sensitivity.

Namespace

Drupal\ascii_art_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');
  }
}