contact.test 20 KB

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