385 lines
17 KiB
Plaintext
385 lines
17 KiB
Plaintext
<?php
|
|
// $Id$
|
|
|
|
/**
|
|
* Test the sitewide contact form.
|
|
*/
|
|
class ContactSitewideTestCase extends DrupalWebTestCase {
|
|
public static function getInfo() {
|
|
return array(
|
|
'name' => t('Site-wide contact form'),
|
|
'description' => t('Tests site-wide contact form functionality.'),
|
|
'group' => t('Contact'),
|
|
);
|
|
}
|
|
|
|
function setUp() {
|
|
parent::setUp('contact');
|
|
}
|
|
|
|
/**
|
|
* Test configuration options and site-wide contact form.
|
|
*/
|
|
function testSiteWideContact() {
|
|
// Create and login administrative user.
|
|
$admin_user = $this->drupalCreateUser(array('access site-wide contact form', 'administer site-wide contact form', 'administer permissions'));
|
|
$this->drupalLogin($admin_user);
|
|
|
|
// Set settings.
|
|
$edit = array();
|
|
$edit['contact_hourly_threshold'] = 3;
|
|
$edit['contact_default_status'] = TRUE;
|
|
$this->drupalPost('admin/build/contact/settings', $edit, t('Save configuration'));
|
|
$this->assertText(t('The configuration options have been saved.'), t('Setting successfully saved.'));
|
|
|
|
// Delete old categories to ensure that new categories are used.
|
|
$this->deleteCategories();
|
|
|
|
// Ensure that the contact form won't be shown without categories.
|
|
$this->setPermission('anonymous user', array('access site-wide contact form' => TRUE));
|
|
$this->drupalLogout();
|
|
$this->drupalGet('contact');
|
|
$this->assertResponse(404);
|
|
$this->drupalLogin($admin_user);
|
|
$this->drupalGet('contact');
|
|
$this->assertResponse(200);
|
|
$this->assertText(t('The contact form has not been configured.'));
|
|
|
|
// Add categories.
|
|
// Test invalid recipients.
|
|
$invalid_recipients = array('invalid', 'invalid@', 'invalid@site.', '@site.', '@site.com');
|
|
foreach ($invalid_recipients as $invalid_recipient) {
|
|
$this->addCategory($this->randomName(16), $invalid_recipient, '', FALSE);
|
|
$this->assertRaw(t('%recipient is an invalid e-mail address.', array('%recipient' => $invalid_recipient)), t('Caught invalid recipient (' . $invalid_recipient . ').'));
|
|
}
|
|
|
|
// Test validation of empty category and recipients fields.
|
|
$this->addCategory($category = '', '', '', TRUE);
|
|
$this->assertText(t('Category field is required.'), t('Caught empty category field'));
|
|
$this->assertText(t('Recipients field is required.'), t('Caught empty recipients field.'));
|
|
|
|
// Create first valid category.
|
|
$recipients = array('simpletest@example.com', 'simpletest2@example.com', 'simpletest3@example.com');
|
|
$this->addCategory($category = $this->randomName(16), implode(',', array($recipients[0])), '', TRUE);
|
|
$this->assertRaw(t('Category %category has been added.', array('%category' => $category)), t('Category successfully added.'));
|
|
|
|
// Make sure the newly created category is included in the list of categories.
|
|
$this->assertNoUniqueText($category, t('New category included in categories list.'));
|
|
|
|
// Test update contact form category.
|
|
$categories = $this->getCategories();
|
|
$category_id = $this->updateCategory($categories, $category = $this->randomName(16), $recipients_str = implode(',', array($recipients[0], $recipients[1])), $reply = $this->randomName(30), FALSE);
|
|
$category_array = db_query("SELECT category, recipients, reply, selected FROM {contact} WHERE cid = :cid", array(':cid' => $category_id))->fetchAssoc();
|
|
$this->assertEqual($category_array['category'], $category);
|
|
$this->assertEqual($category_array['recipients'], $recipients_str);
|
|
$this->assertEqual($category_array['reply'], $reply);
|
|
$this->assertFalse($category_array['selected']);
|
|
$this->assertRaw(t('Category %category has been updated.', array('%category' => $category)), t('Category successfully updated.'));
|
|
|
|
// Ensure that the contact form is shown without a category selection input.
|
|
$this->setPermission('anonymous user', array('access site-wide contact form' => TRUE));
|
|
$this->drupalLogout();
|
|
$this->drupalGet('contact');
|
|
$this->assertText(t('Your e-mail address'), t('Contact form is shown when there is one category.'));
|
|
$this->assertNoText(t('Category'), t('When there is only one category, the category selection element is hidden.'));
|
|
$this->drupalLogin($admin_user);
|
|
|
|
// Add more categories.
|
|
$this->addCategory($category = $this->randomName(16), implode(',', array($recipients[0], $recipients[1])), '', FALSE);
|
|
$this->assertRaw(t('Category %category has been added.', array('%category' => $category)), t('Category successfully added.'));
|
|
|
|
$this->addCategory($category = $this->randomName(16), implode(',', array($recipients[0], $recipients[1], $recipients[2])), '', FALSE);
|
|
$this->assertRaw(t('Category %category has been added.', array('%category' => $category)), t('Category successfully added.'));
|
|
|
|
// Clear flood table in preparation for flood test and allow other checks to complete.
|
|
db_delete('flood')->execute();
|
|
$num_records_after = db_query("SELECT COUNT(*) FROM {flood}")->fetchField();
|
|
$this->assertIdentical($num_records_after, '0', t('Flood table emptied.'));
|
|
|
|
// Check to see that anonymous user cannot see contact page without permission.
|
|
$this->setPermission('anonymous user', array('access site-wide contact form' => FALSE));
|
|
$this->drupalLogout();
|
|
|
|
$this->drupalGet('contact');
|
|
$this->assertResponse(403, t('Access denied to anonymous user without permission.'));
|
|
|
|
// Give anonymous user permission and see that page is viewable.
|
|
$this->drupalLogin($admin_user);
|
|
$this->setPermission('anonymous user', array('access site-wide contact form' => TRUE));
|
|
$this->drupalLogout();
|
|
|
|
$this->drupalGet('contact');
|
|
$this->assertResponse(200, t('Access granted to anonymous user with permission.'));
|
|
|
|
// Submit contact form with invalid values.
|
|
$this->submitContact('', $recipients[0], $this->randomName(16), $categories[0], $this->randomName(64));
|
|
$this->assertText(t('Your name field is required.'), t('Name required.'));
|
|
|
|
$this->submitContact($this->randomName(16), '', $this->randomName(16), $categories[0], $this->randomName(64));
|
|
$this->assertText(t('Your e-mail address field is required.'), t('E-mail required.'));
|
|
|
|
$this->submitContact($this->randomName(16), $invalid_recipients[0], $this->randomName(16), $categories[0], $this->randomName(64));
|
|
$this->assertText(t('You must enter a valid e-mail address.'), t('Valid e-mail required.'));
|
|
|
|
$this->submitContact($this->randomName(16), $recipients[0], '', $categories[0], $this->randomName(64));
|
|
$this->assertText(t('Subject field is required.'), t('Subject required.'));
|
|
|
|
$this->submitContact($this->randomName(16), $recipients[0], $this->randomName(16), $categories[0], '');
|
|
$this->assertText(t('Message field is required.'), t('Message required.'));
|
|
|
|
// Test contact form with no default category selected.
|
|
db_update('contact')
|
|
->fields(array('selected' => 0))
|
|
->execute();
|
|
$this->drupalGet('contact');
|
|
$this->assertRaw(t('- Please choose -'), t('Without selected categories the visitor is asked to chose a category.'));
|
|
|
|
// Submit contact form with invalid category id (cid 0).
|
|
$this->submitContact($this->randomName(16), $recipients[0], $this->randomName(16), 0, '');
|
|
$this->assertText(t('You must select a valid category.'), t('Valid category required.'));
|
|
|
|
// Submit contact form with correct values and check flood interval.
|
|
for ($i = 0; $i < $edit['contact_hourly_threshold']; $i++) {
|
|
$this->submitContact($this->randomName(16), $recipients[0], $this->randomName(16), $categories[0], $this->randomName(64));
|
|
$this->assertText(t('Your message has been sent.'), t('Message sent.'));
|
|
}
|
|
// Submit contact form one over limit.
|
|
$this->drupalGet('contact');
|
|
$this->assertRaw(t('You cannot send more than %number messages per hour. Please try again later.', array('%number' => $edit['contact_hourly_threshold'])), t('Message threshold reached.'));
|
|
|
|
// Delete created categories.
|
|
$this->drupalLogin($admin_user);
|
|
$this->deleteCategories();
|
|
}
|
|
|
|
/**
|
|
* Test auto-reply on the site-wide contact form.
|
|
*/
|
|
function testAutoReply() {
|
|
// Create and login administrative user.
|
|
$admin_user = $this->drupalCreateUser(array('access site-wide contact form', 'administer site-wide contact form', 'administer permissions'));
|
|
$this->drupalLogin($admin_user);
|
|
|
|
// Set up three categories, 2 with an auto-reply and one without.
|
|
$foo_autoreply = $this->randomString(40);
|
|
$bar_autoreply = $this->randomString(40);
|
|
$this->addCategory('foo', 'foo@example.com', $foo_autoreply, FALSE);
|
|
$this->addCategory('bar', 'bar@example.com', $bar_autoreply, FALSE);
|
|
$this->addCategory('no_autoreply', 'bar@example.com', '', FALSE);
|
|
|
|
// Test the auto-reply for category 'foo'.
|
|
$email = $this->randomName(32) . '@example.com';
|
|
$subject = $this->randomString(64);
|
|
$this->submitContact($this->randomName(16), $email, $subject, 1, $this->randomString(128));
|
|
|
|
// We are testing the auto-reply, so there should be one e-mail going to the sender.
|
|
$captured_emails = $this->drupalGetMails(array('id' => 'contact_page_autoreply', 'to' => $email, 'from' => 'foo@example.com'));
|
|
$this->assertEqual(count($captured_emails), 1, t('Auto-reply e-mail was sent to the sender for category "foo".'), t('Contact'));
|
|
$this->assertEqual($captured_emails[0]['body'], $foo_autoreply, t('Auto-reply e-mail body is correct for category "foo".'), t('Contact'));
|
|
|
|
// Test the auto-reply for category 'bar'.
|
|
$email = $this->randomName(32) . '@example.com';
|
|
$this->submitContact($this->randomName(16), $email, $this->randomString(64), 2, $this->randomString(128));
|
|
|
|
// Auto-reply for category 'bar' should result in one auto-reply e-mail to the sender.
|
|
$captured_emails = $this->drupalGetMails(array('id' => 'contact_page_autoreply', 'to' => $email, 'from' => 'bar@example.com'));
|
|
$this->assertEqual(count($captured_emails), 1, t('Auto-reply e-mail was sent to the sender for category "bar".'), t('Contact'));
|
|
$this->assertEqual($captured_emails[0]['body'], $bar_autoreply, t('Auto-reply e-mail body is correct for category "bar".'), t('Contact'));
|
|
|
|
// Verify that no auto-reply is sent when the auto-reply field is left blank.
|
|
$email = $this->randomName(32) . '@example.com';
|
|
$this->submitContact($this->randomName(16), $email, $this->randomString(64), 3, $this->randomString(128));
|
|
$captured_emails = $this->drupalGetMails(array('id' => 'contact_page_autoreply', 'to' => $email, 'from' => 'no_autoreply@example.com'));
|
|
$this->assertEqual(count($captured_emails), 0, t('No auto-reply e-mail was sent to the sender for category "no-autoreply".'), t('Contact'));
|
|
}
|
|
|
|
/**
|
|
* Add a category.
|
|
*
|
|
* @param string $category Name of category.
|
|
* @param string $recipients List of recipient e-mail addresses.
|
|
* @param string $reply Auto-reply text.
|
|
* @param boolean $selected Defautly selected.
|
|
*/
|
|
function addCategory($category, $recipients, $reply, $selected) {
|
|
$edit = array();
|
|
$edit['category'] = $category;
|
|
$edit['recipients'] = $recipients;
|
|
$edit['reply'] = $reply;
|
|
$edit['selected'] = ($selected ? '1' : '0');
|
|
$this->drupalPost('admin/build/contact/add', $edit, t('Save'));
|
|
}
|
|
|
|
/**
|
|
* Update a category.
|
|
*
|
|
* @param string $category Name of category.
|
|
* @param string $recipients List of recipient e-mail addresses.
|
|
* @param string $reply Auto-reply text.
|
|
* @param boolean $selected Defautly selected.
|
|
*/
|
|
function updateCategory($categories, $category, $recipients, $reply, $selected) {
|
|
$category_id = $categories[array_rand($categories)];
|
|
$edit = array();
|
|
$edit['category'] = $category;
|
|
$edit['recipients'] = $recipients;
|
|
$edit['reply'] = $reply;
|
|
$edit['selected'] = ($selected ? '1' : '0');
|
|
$this->drupalPost('admin/build/contact/edit/' . $category_id, $edit, t('Save'));
|
|
return ($category_id);
|
|
}
|
|
|
|
/**
|
|
* Submit contact form.
|
|
*
|
|
* @param string $name Name.
|
|
* @param string $mail E-mail address.
|
|
* @param string $subject Subject.
|
|
* @param integer $cid Category id.
|
|
* @param string $message Message.
|
|
*/
|
|
function submitContact($name, $mail, $subject, $cid, $message) {
|
|
$edit = array();
|
|
$edit['name'] = $name;
|
|
$edit['mail'] = $mail;
|
|
$edit['subject'] = $subject;
|
|
$edit['cid'] = $cid;
|
|
$edit['message'] = $message;
|
|
$this->drupalPost('contact', $edit, t('Send message'));
|
|
}
|
|
|
|
/**
|
|
* Delete all categories.
|
|
*/
|
|
function deleteCategories() {
|
|
$categories = $this->getCategories();
|
|
foreach ($categories as $category) {
|
|
$category_name = db_query("SELECT category FROM {contact} WHERE cid = :cid", array(':cid' => $category))->fetchField();
|
|
$this->drupalPost('admin/build/contact/delete/' . $category, array(), t('Delete'));
|
|
$this->assertRaw(t('Category %category has been deleted.', array('%category' => $category_name)), t('Category deleted sucessfully.'));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get list category ids.
|
|
*
|
|
* @return array Category ids.
|
|
*/
|
|
function getCategories() {
|
|
$categories = db_query('SELECT cid FROM {contact}')->fetchCol();
|
|
return $categories;
|
|
}
|
|
|
|
/**
|
|
* Set permission.
|
|
*
|
|
* @param string $role User role to set permissions for.
|
|
* @param array $permissions Key-value array of permissions to set.
|
|
*/
|
|
function setPermission($role, $permissions) {
|
|
// Get role id (rid) for specified role.
|
|
$rid = db_query("SELECT rid FROM {role} WHERE name = :name", array(':name' => $role))->fetchField();
|
|
if ($rid === FALSE) {
|
|
$this->fail(t(' [permission] Role "' . $role . '" not found.'));
|
|
}
|
|
|
|
// Create edit array from permission.
|
|
$edit = array();
|
|
foreach ($permissions as $name => $value) {
|
|
$edit[$rid . '[' . $name . ']'] = $value;
|
|
}
|
|
|
|
$this->drupalPost('admin/user/permissions', $edit, t('Save permissions'));
|
|
$this->assertText(t('The changes have been saved.'), t(' [permission] Saved changes.'));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Test the personal contact form.
|
|
*/
|
|
class ContactPersonalTestCase extends DrupalWebTestCase {
|
|
public static function getInfo() {
|
|
return array(
|
|
'name' => t('Personal contact form'),
|
|
'description' => t('Tests personal contact form functionality.'),
|
|
'group' => t('Contact'),
|
|
);
|
|
}
|
|
|
|
function setUp() {
|
|
parent::setUp('contact');
|
|
}
|
|
|
|
/**
|
|
* Test personal contact form.
|
|
*/
|
|
function testPersonalContact() {
|
|
$admin_user = $this->drupalCreateUser(array('administer site-wide contact form'));
|
|
$this->drupalLogin($admin_user);
|
|
|
|
// Enable the personal contact form.
|
|
$flood_control = 3;
|
|
$edit = array();
|
|
$edit['contact_default_status'] = TRUE;
|
|
$edit['contact_hourly_threshold'] = $flood_control;
|
|
$this->drupalPost('admin/build/contact/settings', $edit, t('Save configuration'));
|
|
$this->assertText(t('The configuration options have been saved.'), t('Setting successfully saved.'));
|
|
|
|
// Reload variables.
|
|
$this->drupalLogout();
|
|
|
|
// Create web users and attempt to use personal contact forms with default set to true.
|
|
$web_user1 = $this->drupalCreateUser(array());
|
|
$web_user2 = $this->drupalCreateUser(array());
|
|
|
|
$this->drupalLogin($web_user1);
|
|
|
|
$this->drupalGet('user/' . $web_user2->uid . '/contact');
|
|
$this->assertResponse(200, t('Access to personal contact form granted.'));
|
|
|
|
$edit = array();
|
|
$edit['subject'] = $this->randomName(16);
|
|
$edit['message'] = $this->randomName(64);
|
|
$this->drupalPost(NULL, $edit, t('Send message'));
|
|
$this->assertText(t('Your message has been sent.'), t('Message sent.'));
|
|
|
|
// Clear flood table in preparation for flood test and allow other checks to complete.
|
|
db_delete('flood')->execute();
|
|
$num_records_flood = db_query("SELECT COUNT(*) FROM {flood}")->fetchField();
|
|
$this->assertIdentical($num_records_flood, '0', t('Flood table emptied.'));
|
|
|
|
// Submit contact form with correct values and check flood interval.
|
|
for ($i = 0; $i < $flood_control; $i++) {
|
|
$this->drupalGet('user/' . $web_user2->uid . '/contact');
|
|
$this->drupalPost(NULL, $edit, t('Send message'));
|
|
$this->assertText(t('Your message has been sent.'), t('Message sent.'));
|
|
}
|
|
|
|
// Submit contact form one over limit.
|
|
$this->drupalGet('user/' . $web_user2->uid . '/contact');
|
|
$this->assertRaw(t('You cannot send more than %number messages per hour. Please try again later.', array('%number' => $flood_control)), t('Message threshold reached.'));
|
|
|
|
$this->drupalLogout();
|
|
|
|
$this->drupalLogin($admin_user);
|
|
|
|
// Disable the personal contact form.
|
|
$edit = array();
|
|
$edit['contact_default_status'] = FALSE;
|
|
$this->drupalPost('admin/build/contact/settings', $edit, t('Save configuration'));
|
|
$this->assertText(t('The configuration options have been saved.'), t('Setting successfully saved.'));
|
|
|
|
// Reload variables.
|
|
$this->drupalLogout();
|
|
|
|
// Create web users and attempt to use personal contact forms with default set to false.
|
|
$web_user3 = $this->drupalCreateUser(array());
|
|
$web_user4 = $this->drupalCreateUser(array());
|
|
|
|
$this->drupalLogin($web_user3);
|
|
|
|
$this->drupalGet('user/' . $web_user4->uid . '/contact');
|
|
$this->assertResponse(403, t('Access to personal contact form denied.'));
|
|
}
|
|
}
|