You are here

function BotchaTestCase::testBotchaResubmit in BOTCHA Spam Prevention 7

Same name and namespace in other branches
  1. 6 botcha.test \BotchaTestCase::testBotchaResubmit()

Test if BOTCHA is applied correctly when failing first and then resubmitting comments: comment form should have BOTCHA again and pass correct submission. (We use to fail BOTCHA as it is impossible to fail comment form on its own)

\see testBotchaAfterNodePreview()

File

./botcha.test, line 425
Tests for BOTCHA module.

Class

BotchaTestCase

Code

function testBotchaResubmit() {
  $langcode = LANGUAGE_NONE;

  // Set Test BOTCHA on comment form.
  botcha_set_form_id_setting(self::COMMENT_FORM_ID, 'test');

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

  // Log in as normal user.
  $this
    ->drupalLogin($this->normal_user);

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

  // Check if there is a BOTCHA on the comment form.
  $this
    ->drupalGet('comment/reply/' . $node->nid);
  $this
    ->assertBotchaPresence(TRUE);

  // Post comment on node.
  $edit = $this
    ->setCommentFormValues();

  // Screw up fields (like a bot would do)
  $edit['botcha_response'] = 'xx';
  $comment_subject = $edit['subject'];
  $comment_body = $edit["comment_body[{$langcode}][0][value]"];
  $this
    ->drupalPost('comment/reply/' . $node->nid, $edit, t('Save'));

  // Check for error message.
  $this
    ->assertText(t(BOTCHA_WRONG_RESPONSE_ERROR_MESSAGE), 'Comment submission should be blocked.', 'BOTCHA');

  // Check that there is still BOTCHA after failed submit.
  $this
    ->assertBotchaPresence(TRUE);

  // Copy all values from the form into new one.
  $edit = $this
    ->getCommentFormValuesFromForm();

  // Get node page and check that comment is not present.
  $this
    ->drupalGet('node/' . $node->nid);
  $this
    ->assertNoText($comment_subject, 'Comment should not show up on node page.', 'BOTCHA');
  $this
    ->assertNoText($comment_body, 'Comment should not show up on node page.', 'BOTCHA');
  $comment_subject = $edit['subject'];
  $comment_body = $edit["comment_body[{$langcode}][0][value]"];

  // Save comment again with correct BOTCHA.
  $this
    ->drupalPost('comment/reply/' . $node->nid, $edit, t('Save'));

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

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