You are here

protected function CaptchaTestCase::assertCommentPosting in CAPTCHA 7

Same name and namespace in other branches
  1. 6.2 captcha.test \CaptchaTestCase::assertCommentPosting()

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

$captcha_response the response on the CAPTCHA:

$should_pass boolean describing if the posting should pass or should be blocked:

$message message to prefix to nested asserts:

1 call to CaptchaTestCase::assertCommentPosting()
CaptchaTestCase::testCaseInsensitiveValidation in ./captcha.test

File

./captcha.test, line 275
Tests for CAPTCHA module.

Class

CaptchaTestCase

Code

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

  // Make sure comments on pages can be saved directely without preview.
  variable_set('comment_preview_page', DRUPAL_OPTIONAL);

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

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

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

    // Get node page and check that comment shows up.
    $this
      ->drupalGet('node/' . $node->nid);
    $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(t(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->nid);
    $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');
  }
}