| 1 |
<?php
|
| 2 |
// $Id: contact.test,v 1.37 2009/10/11 18:34:10 dries Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Test the sitewide contact form.
|
| 6 |
*/
|
| 7 |
class ContactSitewideTestCase extends DrupalWebTestCase {
|
| 8 |
public static function getInfo() {
|
| 9 |
return array(
|
| 10 |
'name' => 'Site-wide contact form',
|
| 11 |
'description' => 'Tests site-wide contact form functionality.',
|
| 12 |
'group' => 'Contact',
|
| 13 |
);
|
| 14 |
}
|
| 15 |
|
| 16 |
function setUp() {
|
| 17 |
parent::setUp('contact');
|
| 18 |
}
|
| 19 |
|
| 20 |
/**
|
| 21 |
* Test configuration options and site-wide contact form.
|
| 22 |
*/
|
| 23 |
function testSiteWideContact() {
|
| 24 |
// Create and login administrative user.
|
| 25 |
$admin_user = $this->drupalCreateUser(array('access site-wide contact form', 'administer contact forms', 'administer users'));
|
| 26 |
$this->drupalLogin($admin_user);
|
| 27 |
|
| 28 |
$flood_limit = 3;
|
| 29 |
variable_set('contact_threshold_limit', $flood_limit);
|
| 30 |
variable_set('contact_threshold_window', 600);
|
| 31 |
|
| 32 |
// Set settings.
|
| 33 |
$edit = array();
|
| 34 |
$edit['contact_default_status'] = TRUE;
|
| 35 |
$this->drupalPost('admin/config/people/accounts', $edit, t('Save configuration'));
|
| 36 |
$this->assertText(t('The configuration options have been saved.'), t('Setting successfully saved.'));
|
| 37 |
|
| 38 |
// Delete old categories to ensure that new categories are used.
|
| 39 |
$this->deleteCategories();
|
| 40 |
|
| 41 |
// Ensure that the contact form won't be shown without categories.
|
| 42 |
user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array('access site-wide contact form'));
|
| 43 |
$this->drupalLogout();
|
| 44 |
$this->drupalGet('contact');
|
| 45 |
$this->assertResponse(404);
|
| 46 |
$this->drupalLogin($admin_user);
|
| 47 |
$this->drupalGet('contact');
|
| 48 |
$this->assertResponse(200);
|
| 49 |
$this->assertText(t('The contact form has not been configured.'));
|
| 50 |
|
| 51 |
// Add categories.
|
| 52 |
// Test invalid recipients.
|
| 53 |
$invalid_recipients = array('invalid', 'invalid@', 'invalid@site.', '@site.', '@site.com');
|
| 54 |
foreach ($invalid_recipients as $invalid_recipient) {
|
| 55 |
$this->addCategory($this->randomName(16), $invalid_recipient, '', FALSE);
|
| 56 |
$this->assertRaw(t('%recipient is an invalid e-mail address.', array('%recipient' => $invalid_recipient)), t('Caught invalid recipient (' . $invalid_recipient . ').'));
|
| 57 |
}
|
| 58 |
|
| 59 |
// Test validation of empty category and recipients fields.
|
| 60 |
$this->addCategory($category = '', '', '', TRUE);
|
| 61 |
$this->assertText(t('Category field is required.'), t('Caught empty category field'));
|
| 62 |
$this->assertText(t('Recipients field is required.'), t('Caught empty recipients field.'));
|
| 63 |
|
| 64 |
// Create first valid category.
|
| 65 |
$recipients = array('simpletest@example.com', 'simpletest2@example.com', 'simpletest3@example.com');
|
| 66 |
$this->addCategory($category = $this->randomName(16), implode(',', array($recipients[0])), '', TRUE);
|
| 67 |
$this->assertRaw(t('Category %category has been saved.', array('%category' => $category)), t('Category successfully saved.'));
|
| 68 |
|
| 69 |
// Make sure the newly created category is included in the list of categories.
|
| 70 |
$this->assertNoUniqueText($category, t('New category included in categories list.'));
|
| 71 |
|
| 72 |
// Test update contact form category.
|
| 73 |
$categories = $this->getCategories();
|
| 74 |
$category_id = $this->updateCategory($categories, $category = $this->randomName(16), $recipients_str = implode(',', array($recipients[0], $recipients[1])), $reply = $this->randomName(30), FALSE);
|
| 75 |
$category_array = db_query("SELECT category, recipients, reply, selected FROM {contact} WHERE cid = :cid", array(':cid' => $category_id))->fetchAssoc();
|
| 76 |
$this->assertEqual($category_array['category'], $category);
|
| 77 |
$this->assertEqual($category_array['recipients'], $recipients_str);
|
| 78 |
$this->assertEqual($category_array['reply'], $reply);
|
| 79 |
$this->assertFalse($category_array['selected']);
|
| 80 |
$this->assertRaw(t('Category %category has been saved.', array('%category' => $category)), t('Category successfully saved.'));
|
| 81 |
|
| 82 |
// Ensure that the contact form is shown without a category selection input.
|
| 83 |
user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array('access site-wide contact form'));
|
| 84 |
$this->drupalLogout();
|
| 85 |
$this->drupalGet('contact');
|
| 86 |
$this->assertText(t('Your e-mail address'), t('Contact form is shown when there is one category.'));
|
| 87 |
$this->assertNoText(t('Category'), t('When there is only one category, the category selection element is hidden.'));
|
| 88 |
$this->drupalLogin($admin_user);
|
| 89 |
|
| 90 |
// Add more categories.
|
| 91 |
$this->addCategory($category = $this->randomName(16), implode(',', array($recipients[0], $recipients[1])), '', FALSE);
|
| 92 |
$this->assertRaw(t('Category %category has been saved.', array('%category' => $category)), t('Category successfully saved.'));
|
| 93 |
|
| 94 |
$this->addCategory($category = $this->randomName(16), implode(',', array($recipients[0], $recipients[1], $recipients[2])), '', FALSE);
|
| 95 |
$this->assertRaw(t('Category %category has been saved.', array('%category' => $category)), t('Category successfully saved.'));
|
| 96 |
|
| 97 |
// Clear flood table in preparation for flood test and allow other checks to complete.
|
| 98 |
db_delete('flood')->execute();
|
| 99 |
$num_records_after = db_query("SELECT COUNT(*) FROM {flood}")->fetchField();
|
| 100 |
$this->assertIdentical($num_records_after, '0', t('Flood table emptied.'));
|
| 101 |
$this->drupalLogout();
|
| 102 |
|
| 103 |
// Check to see that anonymous user cannot see contact page without permission.
|
| 104 |
user_role_revoke_permissions(DRUPAL_ANONYMOUS_RID, array('access site-wide contact form'));
|
| 105 |
$this->drupalGet('contact');
|
| 106 |
$this->assertResponse(403, t('Access denied to anonymous user without permission.'));
|
| 107 |
|
| 108 |
// Give anonymous user permission and see that page is viewable.
|
| 109 |
user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array('access site-wide contact form'));
|
| 110 |
$this->drupalGet('contact');
|
| 111 |
$this->assertResponse(200, t('Access granted to anonymous user with permission.'));
|
| 112 |
|
| 113 |
// Submit contact form with invalid values.
|
| 114 |
$this->submitContact('', $recipients[0], $this->randomName(16), $categories[0], $this->randomName(64));
|
| 115 |
$this->assertText(t('Your name field is required.'), t('Name required.'));
|
| 116 |
|
| 117 |
$this->submitContact($this->randomName(16), '', $this->randomName(16), $categories[0], $this->randomName(64));
|
| 118 |
$this->assertText(t('Your e-mail address field is required.'), t('E-mail required.'));
|
| 119 |
|
| 120 |
$this->submitContact($this->randomName(16), $invalid_recipients[0], $this->randomName(16), $categories[0], $this->randomName(64));
|
| 121 |
$this->assertText(t('You must enter a valid e-mail address.'), t('Valid e-mail required.'));
|
| 122 |
|
| 123 |
$this->submitContact($this->randomName(16), $recipients[0], '', $categories[0], $this->randomName(64));
|
| 124 |
$this->assertText(t('Subject field is required.'), t('Subject required.'));
|
| 125 |
|
| 126 |
$this->submitContact($this->randomName(16), $recipients[0], $this->randomName(16), $categories[0], '');
|
| 127 |
$this->assertText(t('Message field is required.'), t('Message required.'));
|
| 128 |
|
| 129 |
// Test contact form with no default category selected.
|
| 130 |
db_update('contact')
|
| 131 |
->fields(array('selected' => 0))
|
| 132 |
->execute();
|
| 133 |
$this->drupalGet('contact');
|
| 134 |
$this->assertRaw(t('- Please choose -'), t('Without selected categories the visitor is asked to chose a category.'));
|
| 135 |
|
| 136 |
// Submit contact form with invalid category id (cid 0).
|
| 137 |
$this->submitContact($this->randomName(16), $recipients[0], $this->randomName(16), 0, '');
|
| 138 |
$this->assertText(t('You must select a valid category.'), t('Valid category required.'));
|
| 139 |
|
| 140 |
// Submit contact form with correct values and check flood interval.
|
| 141 |
for ($i = 0; $i < $flood_limit; $i++) {
|
| 142 |
$this->submitContact($this->randomName(16), $recipients[0], $this->randomName(16), $categories[0], $this->randomName(64));
|
| 143 |
$this->assertText(t('Your message has been sent.'), t('Message sent.'));
|
| 144 |
}
|
| 145 |
// Submit contact form one over limit.
|
| 146 |
$this->drupalGet('contact');
|
| 147 |
$this->assertRaw(t('You cannot send more than %number messages in @interval. Please try again later.', array('%number' => variable_get('contact_threshold_limit', 3), '@interval' => format_interval(600))), t('Message threshold reached.'));
|
| 148 |
|
| 149 |
// Delete created categories.
|
| 150 |
$this->drupalLogin($admin_user);
|
| 151 |
$this->deleteCategories();
|
| 152 |
}
|
| 153 |
|
| 154 |
/**
|
| 155 |
* Test auto-reply on the site-wide contact form.
|
| 156 |
*/
|
| 157 |
function testAutoReply() {
|
| 158 |
// Create and login administrative user.
|
| 159 |
$admin_user = $this->drupalCreateUser(array('access site-wide contact form', 'administer contact forms', 'administer permissions', 'administer users'));
|
| 160 |
$this->drupalLogin($admin_user);
|
| 161 |
|
| 162 |
// Set up three categories, 2 with an auto-reply and one without.
|
| 163 |
$foo_autoreply = $this->randomString(40);
|
| 164 |
$bar_autoreply = $this->randomString(40);
|
| 165 |
$this->addCategory('foo', 'foo@example.com', $foo_autoreply, FALSE);
|
| 166 |
$this->addCategory('bar', 'bar@example.com', $bar_autoreply, FALSE);
|
| 167 |
$this->addCategory('no_autoreply', 'bar@example.com', '', FALSE);
|
| 168 |
|
| 169 |
// Test the auto-reply for category 'foo'.
|
| 170 |
$email = $this->randomName(32) . '@example.com';
|
| 171 |
$subject = $this->randomString(64);
|
| 172 |
$this->submitContact($this->randomName(16), $email, $subject, 2, $this->randomString(128));
|
| 173 |
|
| 174 |
// We are testing the auto-reply, so there should be one e-mail going to the sender.
|
| 175 |
$captured_emails = $this->drupalGetMails(array('id' => 'contact_page_autoreply', 'to' => $email, 'from' => 'foo@example.com'));
|
| 176 |
$this->assertEqual(count($captured_emails), 1, t('Auto-reply e-mail was sent to the sender for category "foo".'), t('Contact'));
|
| 177 |
$this->assertEqual($captured_emails[0]['body'], drupal_html_to_text($foo_autoreply), t('Auto-reply e-mail body is correct for category "foo".'), t('Contact'));
|
| 178 |
|
| 179 |
// Test the auto-reply for category 'bar'.
|
| 180 |
$email = $this->randomName(32) . '@example.com';
|
| 181 |
$this->submitContact($this->randomName(16), $email, $this->randomString(64), 3, $this->randomString(128));
|
| 182 |
|
| 183 |
// Auto-reply for category 'bar' should result in one auto-reply e-mail to the sender.
|
| 184 |
$captured_emails = $this->drupalGetMails(array('id' => 'contact_page_autoreply', 'to' => $email, 'from' => 'bar@example.com'));
|
| 185 |
$this->assertEqual(count($captured_emails), 1, t('Auto-reply e-mail was sent to the sender for category "bar".'), t('Contact'));
|
| 186 |
$this->assertEqual($captured_emails[0]['body'], drupal_html_to_text($bar_autoreply), t('Auto-reply e-mail body is correct for category "bar".'), t('Contact'));
|
| 187 |
|
| 188 |
// Verify that no auto-reply is sent when the auto-reply field is left blank.
|
| 189 |
$email = $this->randomName(32) . '@example.com';
|
| 190 |
$this->submitContact($this->randomName(16), $email, $this->randomString(64), 4, $this->randomString(128));
|
| 191 |
$captured_emails = $this->drupalGetMails(array('id' => 'contact_page_autoreply', 'to' => $email, 'from' => 'no_autoreply@example.com'));
|
| 192 |
$this->assertEqual(count($captured_emails), 0, t('No auto-reply e-mail was sent to the sender for category "no-autoreply".'), t('Contact'));
|
| 193 |
}
|
| 194 |
|
| 195 |
/**
|
| 196 |
* Add a category.
|
| 197 |
*
|
| 198 |
* @param string $category Name of category.
|
| 199 |
* @param string $recipients List of recipient e-mail addresses.
|
| 200 |
* @param string $reply Auto-reply text.
|
| 201 |
* @param boolean $selected Defautly selected.
|
| 202 |
*/
|
| 203 |
function addCategory($category, $recipients, $reply, $selected) {
|
| 204 |
$edit = array();
|
| 205 |
$edit['category'] = $category;
|
| 206 |
$edit['recipients'] = $recipients;
|
| 207 |
$edit['reply'] = $reply;
|
| 208 |
$edit['selected'] = ($selected ? '1' : '0');
|
| 209 |
$this->drupalPost('admin/structure/contact/add', $edit, t('Save'));
|
| 210 |
}
|
| 211 |
|
| 212 |
/**
|
| 213 |
* Update a category.
|
| 214 |
*
|
| 215 |
* @param string $category Name of category.
|
| 216 |
* @param string $recipients List of recipient e-mail addresses.
|
| 217 |
* @param string $reply Auto-reply text.
|
| 218 |
* @param boolean $selected Defautly selected.
|
| 219 |
*/
|
| 220 |
function updateCategory($categories, $category, $recipients, $reply, $selected) {
|
| 221 |
$category_id = $categories[array_rand($categories)];
|
| 222 |
$edit = array();
|
| 223 |
$edit['category'] = $category;
|
| 224 |
$edit['recipients'] = $recipients;
|
| 225 |
$edit['reply'] = $reply;
|
| 226 |
$edit['selected'] = ($selected ? '1' : '0');
|
| 227 |
$this->drupalPost('admin/structure/contact/edit/' . $category_id, $edit, t('Save'));
|
| 228 |
return ($category_id);
|
| 229 |
}
|
| 230 |
|
| 231 |
/**
|
| 232 |
* Submit contact form.
|
| 233 |
*
|
| 234 |
* @param string $name Name.
|
| 235 |
* @param string $mail E-mail address.
|
| 236 |
* @param string $subject Subject.
|
| 237 |
* @param integer $cid Category id.
|
| 238 |
* @param string $message Message.
|
| 239 |
*/
|
| 240 |
function submitContact($name, $mail, $subject, $cid, $message) {
|
| 241 |
$edit = array();
|
| 242 |
$edit['name'] = $name;
|
| 243 |
$edit['mail'] = $mail;
|
| 244 |
$edit['subject'] = $subject;
|
| 245 |
$edit['cid'] = $cid;
|
| 246 |
$edit['message'] = $message;
|
| 247 |
$this->drupalPost('contact', $edit, t('Send message'));
|
| 248 |
}
|
| 249 |
|
| 250 |
/**
|
| 251 |
* Delete all categories.
|
| 252 |
*/
|
| 253 |
function deleteCategories() {
|
| 254 |
$categories = $this->getCategories();
|
| 255 |
foreach ($categories as $category) {
|
| 256 |
$category_name = db_query("SELECT category FROM {contact} WHERE cid = :cid", array(':cid' => $category))->fetchField();
|
| 257 |
$this->drupalPost('admin/structure/contact/delete/' . $category, array(), t('Delete'));
|
| 258 |
$this->assertRaw(t('Category %category has been deleted.', array('%category' => $category_name)), t('Category deleted sucessfully.'));
|
| 259 |
}
|
| 260 |
}
|
| 261 |
|
| 262 |
/**
|
| 263 |
* Get list category ids.
|
| 264 |
*
|
| 265 |
* @return array Category ids.
|
| 266 |
*/
|
| 267 |
function getCategories() {
|
| 268 |
$categories = db_query('SELECT cid FROM {contact}')->fetchCol();
|
| 269 |
return $categories;
|
| 270 |
}
|
| 271 |
}
|
| 272 |
|
| 273 |
/**
|
| 274 |
* Test the personal contact form.
|
| 275 |
*/
|
| 276 |
class ContactPersonalTestCase extends DrupalWebTestCase {
|
| 277 |
private $admin_user;
|
| 278 |
private $web_user;
|
| 279 |
private $contact_user;
|
| 280 |
|
| 281 |
public static function getInfo() {
|
| 282 |
return array(
|
| 283 |
'name' => 'Personal contact form',
|
| 284 |
'description' => 'Tests personal contact form functionality.',
|
| 285 |
'group' => 'Contact',
|
| 286 |
);
|
| 287 |
}
|
| 288 |
|
| 289 |
function setUp() {
|
| 290 |
parent::setUp('contact');
|
| 291 |
|
| 292 |
// Create an admin user.
|
| 293 |
$this->admin_user = $this->drupalCreateUser(array('administer contact forms', 'administer users'));
|
| 294 |
|
| 295 |
// Create some normal users with their contact forms enabled by default.
|
| 296 |
variable_set('contact_default_status', TRUE);
|
| 297 |
$this->web_user = $this->drupalCreateUser(array('access user contact forms'));
|
| 298 |
$this->contact_user = $this->drupalCreateUser();
|
| 299 |
}
|
| 300 |
|
| 301 |
/**
|
| 302 |
* Test personal contact form access.
|
| 303 |
*/
|
| 304 |
function testPersonalContactAccess() {
|
| 305 |
// Test allowed access to user with contact form enabled.
|
| 306 |
$this->drupalLogin($this->web_user);
|
| 307 |
$this->drupalGet('user/' . $this->contact_user->uid . '/contact');
|
| 308 |
$this->assertResponse(200);
|
| 309 |
|
| 310 |
// Test denied access to the user's own contact form.
|
| 311 |
$this->drupalGet('user/' . $this->web_user->uid . '/contact');
|
| 312 |
$this->assertResponse(403);
|
| 313 |
|
| 314 |
// Test always denied access to the anonymous user contact form.
|
| 315 |
$this->drupalGet('user/0/contact');
|
| 316 |
$this->assertResponse(403);
|
| 317 |
|
| 318 |
// Test that anonymous users can access the contact form.
|
| 319 |
$this->drupalLogout();
|
| 320 |
user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array('access user contact forms'));
|
| 321 |
$this->drupalGet('user/' . $this->contact_user->uid . '/contact');
|
| 322 |
$this->assertResponse(200);
|
| 323 |
|
| 324 |
// Revoke the personal contact permission for the anonymous user.
|
| 325 |
user_role_revoke_permissions(DRUPAL_ANONYMOUS_RID, array('access user contact forms'));
|
| 326 |
$this->drupalGet('user/' . $this->contact_user->uid . '/contact');
|
| 327 |
$this->assertResponse(403);
|
| 328 |
|
| 329 |
// Disable the personal contact form.
|
| 330 |
$this->drupalLogin($this->admin_user);
|
| 331 |
$edit = array('contact_default_status' => FALSE);
|
| 332 |
$this->drupalPost('admin/config/people/accounts', $edit, t('Save configuration'));
|
| 333 |
$this->assertText(t('The configuration options have been saved.'), t('Setting successfully saved.'));
|
| 334 |
$this->drupalLogout();
|
| 335 |
|
| 336 |
// Re-create our contacted user with personal contact forms disabled by
|
| 337 |
// default.
|
| 338 |
$this->contact_user = $this->drupalCreateUser();
|
| 339 |
|
| 340 |
// Test denied access to a user with contact form disabled.
|
| 341 |
$this->drupalLogin($this->web_user);
|
| 342 |
$this->drupalGet('user/' . $this->contact_user->uid . '/contact');
|
| 343 |
$this->assertResponse(403);
|
| 344 |
|
| 345 |
// Test allowed access for admin user to a user with contact form disabled.
|
| 346 |
$this->drupalLogin($this->admin_user);
|
| 347 |
$this->drupalGet('user/' . $this->contact_user->uid . '/contact');
|
| 348 |
$this->assertResponse(200);
|
| 349 |
}
|
| 350 |
|
| 351 |
/**
|
| 352 |
* Test the personal contact form flood protection.
|
| 353 |
*/
|
| 354 |
function testPersonalContactFlood() {
|
| 355 |
$flood_limit = 3;
|
| 356 |
variable_set('contact_threshold_limit', $flood_limit);
|
| 357 |
|
| 358 |
// Clear flood table in preparation for flood test and allow other checks to complete.
|
| 359 |
db_delete('flood')->execute();
|
| 360 |
$num_records_flood = db_query("SELECT COUNT(*) FROM {flood}")->fetchField();
|
| 361 |
$this->assertIdentical($num_records_flood, '0', 'Flood table emptied.');
|
| 362 |
|
| 363 |
$this->drupalLogin($this->web_user);
|
| 364 |
|
| 365 |
// Submit contact form with correct values and check flood interval.
|
| 366 |
for ($i = 0; $i < $flood_limit; $i++) {
|
| 367 |
$this->submitPersonalContact($this->contact_user);
|
| 368 |
$this->assertText(t('Your message has been sent.'), 'Message sent.');
|
| 369 |
}
|
| 370 |
|
| 371 |
// Submit contact form one over limit.
|
| 372 |
$this->drupalGet('user/' . $this->contact_user->uid. '/contact');
|
| 373 |
$this->assertRaw(t('You cannot send more than %number messages in @interval. Please try again later.', array('%number' => $flood_limit, '@interval' => format_interval(variable_get('contact_threshold_window', 3600)))), 'Normal user denied access to flooded contact form.');
|
| 374 |
|
| 375 |
// Test that the admin user can still access the contact form even though
|
| 376 |
// the flood limit was reached.
|
| 377 |
$this->drupalLogin($this->admin_user);
|
| 378 |
$this->assertNoText('Please try again later.', 'Admin user not denied access to flooded contact form.');
|
| 379 |
}
|
| 380 |
|
| 381 |
/**
|
| 382 |
* Fill out a user's personal contact form and submit.
|
| 383 |
*
|
| 384 |
* @param $account
|
| 385 |
* A user object of the user being contacted.
|
| 386 |
* @param $message
|
| 387 |
* An optional array with the form fields being used.
|
| 388 |
*/
|
| 389 |
protected function submitPersonalContact($account, array $message = array()) {
|
| 390 |
$message += array(
|
| 391 |
'subject' => $this->randomName(16),
|
| 392 |
'message' => $this->randomName(64),
|
| 393 |
);
|
| 394 |
$this->drupalPost('user/' . $account->uid . '/contact', $message, t('Send message'));
|
| 395 |
}
|
| 396 |
}
|