reroute_email_test.module 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. /**
  3. * @file
  4. * Provides Mail hook implementations for testing the Reroute Email module.
  5. */
  6. /**
  7. * Implements hook_mail().
  8. *
  9. * This function allows testing Reroute Email's handling of a string passed for
  10. * message's body instead of an Array as required by drupal_mail. It also
  11. * allows testing the robustness of the handling of Cc and Bcc header keys.
  12. * Body, Cc and Bcc values are initialized from test case through $params.
  13. */
  14. function reroute_email_test_mail($key, &$message, $params) {
  15. if ($key != 'test_reroute_email') {
  16. return;
  17. }
  18. $message['subject'] = "Reroute Email Test: Message body is a string, Cc and Bcc header keys have a special case";
  19. // Body is provided as a string.
  20. if (!empty($params['body'])) {
  21. $message['body'] = $params['body'];
  22. }
  23. // Provide Cc and Bcc headers with an unexpected case.
  24. if (!empty($params['headers']['test_cc_key']) && !empty($params['headers'][$params['headers']['test_cc_key']])) {
  25. $message['headers'][$params['headers']['test_cc_key']] = $params['headers'][$params['headers']['test_cc_key']];
  26. }
  27. if (!empty($params['headers']['test_bcc_key']) && !empty($params['headers'][$params['headers']['test_bcc_key']])) {
  28. $message['headers'][$params['headers']['test_bcc_key']] = $params['headers'][$params['headers']['test_bcc_key']];
  29. }
  30. }
  31. /**
  32. * Implements hook_mail_alter().
  33. *
  34. * This helper function is necessary to catch message's body if it is a string
  35. * to make it an array to be compliant with drupal_mail and prevent a Warning:
  36. * implode(): Invalid arguments passed in DefaultMailSystem->format().
  37. */
  38. function reroute_email_test_mail_alter(&$message) {
  39. // Only alter the email for the key test_reroute_email.
  40. if ($message['key'] != 'test_reroute_email') {
  41. return;
  42. }
  43. // Prevent Warning from drupal_mail because body is not an array.
  44. if (is_string($message['body'])) {
  45. // Record to be checked in test in the log entries.
  46. watchdog('reroute_email_test', 'A String was detected in the body: <pre>!body</pre>', array('!body' => $message['body']), WATCHDOG_NOTICE);
  47. // Convert body to an Array.
  48. $message['body'] = array($message['body']);
  49. }
  50. }