You are here

protected function BotchaTestCase::assertCommentPosting in BOTCHA Spam Prevention 7

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

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

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

Parameters

$node existing node to post comments to (if NULL, will be created):

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

$message message to prefix to nested asserts:

$button name of button to click (t('Save') by default):

1 call to BotchaTestCase::assertCommentPosting()
BotchaTestCase::testBotchaValidation in ./botcha.test
Testing the case sensistive/insensitive validation.

File

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

Class

BotchaTestCase

Code

protected function assertCommentPosting($node, $should_pass, $message, $button = '') {
  $langcode = LANGUAGE_NONE;

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

    // Create a node with comments enabled.
    $node = $this
      ->createNodeWithCommentsEnabled();
  }
  if (empty($button)) {
    $button = t('Save');
  }

  // 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();
  if (!$should_pass) {

    // 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, $button);
  if ($should_pass) {

    // 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, $message . ' Comment should show up on node page.', 'BOTCHA');
    $this
      ->assertText($comment_body, $message . ' Comment should show up on node page.', 'BOTCHA');
  }
  else {

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

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

    // 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.', 'BOTCHA');
    $this
      ->assertNoText($comment_body, $message . ' Comment should not show up on node page.', 'BOTCHA');
  }
  return $node;
}