Commit c1cb2412 authored by soxofaan's avatar soxofaan

added test for case sensitive/insensitive validation

parent d14b7a1e
......@@ -502,6 +502,23 @@ function captcha_captcha($op, $captcha_type = '') {
);
return $result;
}
elseif ($captcha_type == 'Test') {
// This challenge is not visible through the administrative interface
// as it is not listed in captcha_captcha('list'),
// but it is meant for debugging and testing purposes.
// TODO for Drupal 7 version: This should be done with a mock module,
// but Drupal 6 does not support this (mock modules can not be hidden).
$result = array(
'solution' => 'Test 123',
'form' => array(),
);
$result['form']['captcha_response'] = array(
'#type' => 'textfield',
'#title' => t('Test'),
'#required' => TRUE,
);
return $result;
}
break;
}
}
......@@ -9,7 +9,6 @@
// TODO: write test to check persistence
// TODO: write test for CAPTCHAs on admin pages
// TODO: test for default challenge type
// TODO: test about case sensitive/insensitive validation
class CapchaTestCase extends DrupalWebTestCase {
......@@ -26,6 +25,12 @@ class CapchaTestCase extends DrupalWebTestCase {
parent::setUp('captcha', 'comment');
module_load_include('inc', 'captcha');
// Create a normal user that can post comments, but can not skip CAPTCHA (if enabled).
$permissions = array(
'access comments', 'post comments', 'post comments without approval',
);
$this->normal_user = $this->drupalCreateUser($permissions);
}
/**
......@@ -64,6 +69,82 @@ class CapchaTestCase extends DrupalWebTestCase {
$this->assertField('pass', t('Password field found.'), 'CAPTCHA');
}
/**
* 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 comment_form) and checks if the result is as expected.
*
* @param $captcha_response the response on the CAPTCHA
* @param $should_pass boolean describing if the posting should pass or should be blocked
* @param $message message to prefix to nested asserts
*/
function assertCommentPosting($captcha_response, $should_pass, $message) {
// Make sure comments on pages can be saved directely without preview.
variable_set('comment_preview_page', COMMENT_PREVIEW_OPTIONAL);
// Create a node with comments enabled.
$node_settings = array(
'type' => 'page',
'comment' => COMMENT_NODE_READ_WRITE,
);
$node = $this->drupalCreateNode($node_settings);
// Post comment on node.
$edit = array(
'subject' => $this->randomName(32, 'subject_'),
'comment' => $this->randomName(256, 'comment_body'),
'captcha_response' => $captcha_response,
);
$this->drupalPost('comment/reply/' . $node->nid, $edit, t('Save'));
if ($should_pass) {
// There should be no error message.
$this->assertNoText(t('The answer you entered for the CAPTCHA was not correct.'),
$message .' Comment submission should pass.', 'CAPTCHA');
// Get node page and check that comment shows up.
$this->drupalGet('node/' . $node->nid);
$this->assertText($edit['comment'],
$message .' Comment should show up on node page.', 'CAPTCHA');
}
else {
// Check for error message.
$this->assertText(t('The answer you entered for the CAPTCHA was not correct.'),
$message .' Comment submission should be blocked.', 'CAPTCHA');
// Get node page and check that comment is not present.
$this->drupalGet('node/' . $node->nid);
$this->assertNoText($edit['comment'],
$message .' Comment should not show up on node page.', 'CAPTCHA');
}
}
/*
* Testing the case sensistive/insensitive validation.
*/
function testCaseInsensitiveValidation() {
// Set Test CAPTCHA on comment form
captcha_set_form_id_setting('comment_form', 'captcha/Test');
// Log in as normal user.
$this->drupalLogin($this->normal_user);
// Test case sensitive posting (the default)
variable_set('captcha_case_sensitive_validation', TRUE);
$this->assertCommentPosting('Test 123', TRUE, 'Case sensitive validation of right casing.');
$this->assertCommentPosting('test 123', FALSE, 'Case sensitive validation of wrong casing.');
$this->assertCommentPosting('TEST 123', FALSE, 'Case sensitive validation of wrong casing.');
// Test case insensitive posting (the default)
variable_set('captcha_case_sensitive_validation', FALSE);
$this->assertCommentPosting('Test 123', TRUE, 'Case insensitive validation of right casing.');
$this->assertCommentPosting('test 123', TRUE, 'Case insensitive validation of wrong casing.');
$this->assertCommentPosting('TEST 123', TRUE, 'Case insensitive validation of wrong casing.');
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment