smtp.unit.test 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * @file
  4. * Some tests for the SMTP module.
  5. */
  6. class SmtpUnitTest extends DrupalWebTestCase {
  7. /**
  8. * {@inheritdoc}
  9. */
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'SMTP unit tests',
  13. 'description' => 'Test the SMTP module.',
  14. 'group' => 'SMTP',
  15. );
  16. }
  17. /**
  18. * {@inheritdoc}
  19. */
  20. function setUp(array $modules = array()) {
  21. // Requirements.
  22. $modules[] = 'smtp';
  23. // Some extra logic for fully testing the module.
  24. $modules[] = 'smtp_tests';
  25. // This module is used to log all emails so that the delivery can be
  26. // confirmed.
  27. $modules[] = 'maillog';
  28. parent::setUp($modules);
  29. // Take over the email system.
  30. variable_set('mail_system', array('default-system' => 'SmtpMailSystem'));
  31. // Turn on the mail module.
  32. variable_set('smtp_on', TRUE);
  33. // Do not actually deliver the emails.
  34. variable_set('smtp_deliver', FALSE);
  35. // Use Maillog to log all emails.
  36. variable_set('maillog_log', TRUE);
  37. }
  38. /**
  39. * Confirm that SMTP has taken over the 'mail_system' variable.
  40. */
  41. function testSetup() {
  42. $enabled = variable_get('mail_system', array());
  43. $should_be = array(
  44. 'default-system' => 'SmtpMailSystem',
  45. );
  46. $this->assertEqual($enabled, $should_be, 'SMTP is controlling mail delivery.');
  47. $delivery = variable_get('smtp_on', TRUE);
  48. $this->assertEqual($delivery, TRUE, 'SMTP is enabled.');
  49. $delivery = variable_get('smtp_deliver', FALSE);
  50. $this->assertEqual($delivery, FALSE, 'Email delivery is disabled.');
  51. $logging = variable_get('maillog_log', TRUE);
  52. $this->assertEqual($logging, TRUE, 'Email delivery is being logged.');
  53. }
  54. /**
  55. * Tests logging mail with maillog module.
  56. */
  57. public function testLogging() {
  58. $langcode = language_default('language');
  59. // This is automatically assigned by Simpletest.
  60. $sender = 'simpletest@example.com';
  61. // Send an email.
  62. $to_email = 'to_test@example.com';
  63. $reply_email = 'reply_test@example.com';
  64. $params = array();
  65. drupal_mail('smtp_tests', 'smtp_basic_test', $to_email, $langcode, $params);
  66. // The SMTP module controls the 'from' address but defaults to using the
  67. // site's system email address.
  68. $from_email = variable_get('site_mail', '');
  69. // Compare the maillog db entry with the sent mail.
  70. $logged_email = $this->getLatestMaillogEntry();
  71. $this->assertTrue(!empty($logged_email), 'The test email was captured.');
  72. $this->assertEqual($to_email, $logged_email['header_to']);//, 'Email "to" address is correct.');
  73. $this->assertEqual($from_email, $logged_email['header_from']);//, 'Email "from" address is correct.');
  74. $this->assertEqual($from_email, $logged_email['header_all']['From']);//, 'Email "from" header is correct.');
  75. $this->assertEqual($sender, $logged_email['header_all']['Sender']);//, 'Email "sender" header is correct.');
  76. $this->assertEqual($sender, $logged_email['header_all']['Return-Path']);//, 'Email "return-path" header is correct.');
  77. $this->assertEqual('Drupal', $logged_email['header_all']['X-Mailer']);//, 'Email "x-mailer" header is correct.');
  78. $this->assertEqual(t('Test email subject'), $logged_email['subject']);//, 'Email subject is correct.');
  79. $this->assertEqual(t('Test email body.') . "\n", $logged_email['body']);//, 'Email body is correct.');
  80. }
  81. /**
  82. * Confirm the queue works.
  83. */
  84. public function testQueue() {
  85. // Turn on the queue.
  86. variable_set('smtp_queue', TRUE);
  87. // Send a test message.
  88. $langcode = language_default('language');
  89. $sender = 'simpletest@example.com';
  90. $to_email = 'to_test@example.com';
  91. $reply_email = 'reply_test@example.com';
  92. $params = array();
  93. drupal_mail('smtp_tests', 'smtp_basic_test', $to_email, $langcode, $params);
  94. // Check the queue for messages.
  95. $queue_count = $this->getQueueCount();
  96. $this->assertEqual($queue_count, 1, 'An email was found in the send queue.');
  97. }
  98. /**
  99. * Confirm the queue works.
  100. */
  101. public function testFailQueue() {
  102. // Turn on the queue failover.
  103. variable_set('smtp_queue_fail', TRUE);
  104. // Make sure the queue is disabled.
  105. variable_set('smtp_queue', FALSE);
  106. // Turn on email delivery.
  107. variable_set('smtp_deliver', TRUE);
  108. // Set some fake values for the delivery, it should fail and then cause the
  109. // email to go in to the queue.
  110. variable_set('smtp_from', 'drupal@example.com');
  111. variable_set('smtp_fromname', 'Drupal Simpletest');
  112. variable_set('smtp_host', 'smtp.gmail.com');
  113. variable_set('smtp_hostbackup', '');
  114. variable_set('smtp_password', 'THIS WILL NOT WORK!');
  115. variable_set('smtp_port', '465');
  116. variable_set('smtp_protocol', 'ssl');
  117. variable_set('smtp_username', 'hello@example.com');
  118. // Send a test message.
  119. $langcode = language_default('language');
  120. $sender = 'simpletest@example.com';
  121. $to_email = 'to_test@example.com';
  122. $reply_email = 'reply_test@example.com';
  123. $params = array();
  124. drupal_mail('smtp_tests', 'smtp_basic_test', $to_email, $langcode, $params);
  125. // Check the queue for messages.
  126. $queue_count = $this->getQueueCount('smtp_failure_queue');
  127. $this->assertEqual($queue_count, 1, 'An email was found in the failure queue.');
  128. $queue_count = $this->getQueueCount();
  129. $this->assertEqual($queue_count, 0, 'An email was not found in the regular email queue.');
  130. // Run the queue so that messages can be moved to the normal email queue.
  131. drupal_cron_run();
  132. // Check the queue for messages.
  133. $queue_count = $this->getQueueCount();
  134. $this->assertEqual($queue_count, 1, 'An email was found in the regular email queue.');
  135. $queue_count = $this->getQueueCount('smtp_failure_queue');
  136. $this->assertEqual($queue_count, 0, 'An email was not found in the failure queue.');
  137. }
  138. /**
  139. * Gets the latest Maillog entry.
  140. *
  141. * @return array
  142. * Maillog entry.
  143. */
  144. protected function getLatestMaillogEntry() {
  145. $query = 'SELECT idmaillog, header_from, header_to, header_reply_to, header_all, subject, body FROM {maillog} ORDER BY idmaillog DESC';
  146. $result = db_query_range($query, 0, 1);
  147. if ($maillog = $result->fetchAssoc()) {
  148. // Unserialize values.
  149. $maillog['header_all'] = unserialize($maillog['header_all']);
  150. }
  151. return $maillog;
  152. }
  153. /**
  154. * Get the number of emails in a specific queue.
  155. *
  156. * @param string $queue
  157. * The name of the queue to add the emails to.
  158. *
  159. * @return int
  160. * The number of messages found in the requested queue.
  161. */
  162. protected function getQueueCount($queue = 'smtp_send_queue') {
  163. return db_query("SELECT count('name') FROM {queue} WHERE name = :queue",
  164. array(':queue' => $queue))
  165. ->fetchField();
  166. }
  167. }