t('Mollom tests'),
);
}
function setUp() {
// Setup some test keys. These keys will only work in test mode so consult the
// Mollom API documentation for details:
$this->drupalVariableSet('mollom_public_key', '9cc3d2e43971de758ecddad61a3d12ec');
$this->drupalVariableSet('mollom_private_key', '603a8d11099f17faaab49139bfc7d00a');
// Enable the modules requires for our tests:
$this->drupalModuleEnable('comment');
$this->drupalModuleEnable('contact');
$this->drupalModuleEnable('mollom');
parent::setUp();
}
/**
* Test the key to see if it is working properly.
*/
function testVerifyKey() {
// Call the mollom.verifyKey function directly:
$value = mollom('mollom.verifyKey');
if ($value !== TRUE) {
$this->fail(t('The key pair specified in the mollom.test
file is not valid, or the Mollom servers were unreachable.'));
}
// Validate that the Mollom settings page works for users with
// the 'administer site configuration' permission, and that is
// reports a working Mollom key pair:
$admin = $this->drupalCreateUserRolePerm(array('administer site configuration'));
$this->drupalLoginUser($admin);
$this->drupalGet('admin/settings/mollom');
$this->assertResponse(array(200), 'Testing access to Mollom settings page.');
$this->assertText('correctly', 'Testing the Mollom key pair.');
}
function testFallbackMechanismBlock() {
// We set the fallback strategy to 'blocking mode':
$this->drupalVariableSet('mollom_fallback', MOLLOM_FALLBACK_BLOCK);
// We configure Mollom to use a non-existing server as that should trigger the fallback mechanism:
$this->drupalVariableSet('mollom_servers', array('http://fake-host'));
// We enable Mollom for the request password form:
$this->drupalVariableSet('mollom_user_pass', 1);
// Try to request the user's password:
$this->drupalGet('user/password');
$this->assertNoUnwantedRaw('edit-captcha', 'Check absence of CAPTCHA on request password form.');
$this->assertText('The spam filter that is installed on this site is currently not available.', 'Testing the blocking fallback strategy.');
}
function testFallbackMechanismAccept() {
// We set the fallback strategy to 'acccept mode':
$this->drupalVariableSet('mollom_fallback', MOLLOM_FALLBACK_ACCEPT);
// We configure Mollom to use a non-existing server as that should trigger the fallback mechanism:
$this->drupalVariableSet('mollom_servers', array('http://fake-host'));
// We enable Mollom for the request password form:
$this->drupalVariableSet('mollom_user_pass', 1);
// Try to request the user's password:
$this->drupalGet('user/password');
$this->assertNoUnwantedRaw('edit-captcha', 'Check absence of CAPTCHA on request password form.');
$this->assertNoUnwantedRaw('The spam filter that is installed on this site is currently not available.', 'Testing the blocking fallback strategy.');
}
function testProtectRequestPassword() {
// We first enable Mollom for the request password form:
$this->drupalVariableSet('mollom_user_pass', 1);
// Validate that the request password form has a CAPTCHA text field:
$this->drupalGet('user/password');
$this->assertWantedRaw('edit-captcha', 'Check presence of CAPTCHA on request password form.');
// Create a new user:
$user = $this->drupalCreateUserRolePerm(array('access content'));
// Try to reset the user's password by specifying an invalid CAPTCHA:
$edit = array('name' => $user->name, 'captcha' => 'incorrect');
$this->drupalPost('user/password', $edit, 'E-mail new password');
$this->assertText('The entered CAPTCHA solution is not correct.', 'Testing an inccorect CAPTCHA.');
// Try to reset the user's password by specifying a valid CAPTCHA:
$edit = array('name' => $user->name, 'captcha' => 'correct');
$this->drupalPost('user/password', $edit, 'E-mail new password');
$this->assertText('Further instructions have been sent to your e-mail address.', 'Testing a ccorect CAPTCHA.');
}
function testProtectRegisterUser() {
// We first enable Mollom for the request password form:
$this->drupalVariableSet('mollom_user_register', 1);
// Validate that the request password form has a CAPTCHA text field:
$this->drupalGet('user/register');
$this->assertWantedRaw('edit-captcha', 'Check presence of CAPTCHA on register user form.');
}
function testCommentForm() {
// Enable Mollom for comments:
$this->drupalVariableSet('mollom_comment_form', 1);
$this->drupalVariableSet('comment_preview_story', COMMENT_PREVIEW_OPTIONAL);
// Create a user that can post stories and comments:
$user = $this->drupalCreateUserRolePerm(array('access comments', 'post comments', 'create story content'));
$this->drupalLoginUser($user);
// Create a new post:
$node = $this->drupalCreateNode(array('type' => 'story'));
// Request the comment reply form. Initially, there should be no CAPTCHA:
$this->drupalGet('comment/reply/'. $node->nid);
$this->assertNoUnwantedRaw('edit-captcha', 'Check absence of CAPTCHA on comment form.');
// Preview a comment that is 'unsure' and make sure there is both a CAPTCHA form:
$this->drupalPost('comment/reply/'. $node->nid, array('comment' => 'unsure'), t('Preview'));
$this->assertWantedRaw('edit-session-id', 'Check presence of the Mollom session ID om comment form.');
$this->assertWantedRaw('edit-captcha', 'Check presence of CAPTCHA on comment form.');
// Post a comment that is 'unsure' and make sure there is both a CAPTCHA form:
$this->drupalPost('comment/reply/'. $node->nid, array('comment' => 'unsure'), t('Save'));
$this->assertWantedRaw('edit-session-id', 'Check presence of the Mollom session ID om comment form.');
$this->assertWantedRaw('edit-captcha', 'Check presence of CAPTCHA on comment form.');
// Retrieve the Mollom session ID:
$session_id = $this->_browser->getFieldById('edit-session-id');
// TODO: the session ID is not properly maintained from drupalPost() to drupalPost().
// Preview a comment that is 'spam' and make sure there is no CAPTCHA form:
$this->drupalPost('comment/reply/'. $node->nid, array('comment' => 'spam'), t('Preview'));
$this->assertWantedRaw(t('Your submission has triggered the installed spam filter and will not be accepted.'), 'Check presence of blocked message.');
$this->assertNoUnwantedRaw('edit-captcha', 'Check absence of CAPTCHA on comment form.');
// $this->assertField('edit-session-id', $session_id, 'Check whether the session ID remained unchanged.');
// Post a comment that is 'spam' and make sure there is no CAPTCHA form:
$this->drupalPost('comment/reply/'. $node->nid, array('comment' => 'spam'), t('Save'));
$this->assertWantedRaw(t('Your submission has triggered the installed spam filter and will not be accepted.'), 'Check presence of blocked message.');
$this->assertNoUnwantedRaw('edit-captcha', 'Check absence of CAPTCHA on comment form.');
// $this->assertField('edit-session-id', $session_id, 'Check whether the session ID remained unchanged.');
// Preview a comment that is 'ham' and make sure there is no CAPTCHA form:
$this->drupalPost('comment/reply/'. $node->nid, array('comment' => 'ham'), t('Preview'));
$this->assertNoUnwantedRaw('edit-captcha', 'Check absence of CAPTCHA on comment form.');
// $this->assertField('edit-session-id', $session_id, 'Check whether the session ID remained unchanged.');
// Post a comment that is 'ham' and make sure there is no CAPTCHA form:
$this->drupalPost('comment/reply/'. $node->nid, array('comment' => 'ham'), t('Save'));
$this->assertNoUnwantedRaw('edit-captcha', 'Check absence of CAPTCHA on comment form.');
// Only the last post operation should store a comment in the database:
$this->assertWantedRaw('
ham
', 'Check whether the comment was posted.'); } function testContactForm() { // TODO: difficult to test until we can hijack drupal_mail(). } }