'SMTP unit tests', 'description' => 'Test the SMTP module.', 'group' => 'SMTP', ); } /** * {@inheritdoc} */ function setUp(array $modules = array()) { // Requirements. $modules[] = 'smtp'; // Some extra logic for fully testing the module. $modules[] = 'smtp_tests'; // This module is used to log all emails so that the delivery can be // confirmed. $modules[] = 'maillog'; parent::setUp($modules); // Take over the email system. variable_set('mail_system', array('default-system' => 'SmtpMailSystem')); // Turn on the mail module. variable_set('smtp_on', TRUE); // Do not actually deliver the emails. variable_set('smtp_deliver', FALSE); // Use Maillog to log all emails. variable_set('maillog_log', TRUE); } /** * Confirm that SMTP has taken over the 'mail_system' variable. */ function testSetup() { $enabled = variable_get('mail_system', array()); $should_be = array( 'default-system' => 'SmtpMailSystem', ); $this->assertEqual($enabled, $should_be, 'SMTP is controlling mail delivery.'); $delivery = variable_get('smtp_on', TRUE); $this->assertEqual($delivery, TRUE, 'SMTP is enabled.'); $delivery = variable_get('smtp_deliver', FALSE); $this->assertEqual($delivery, FALSE, 'Email delivery is disabled.'); $logging = variable_get('maillog_log', TRUE); $this->assertEqual($logging, TRUE, 'Email delivery is being logged.'); } /** * Tests logging mail with maillog module. */ public function testLogging() { $langcode = language_default('language'); // This is automatically assigned by Simpletest. $sender = 'simpletest@example.com'; // Send an email. $to_email = 'to_test@example.com'; $reply_email = 'reply_test@example.com'; $params = array(); drupal_mail('smtp_tests', 'smtp_basic_test', $to_email, $langcode, $params); // The SMTP module controls the 'from' address but defaults to using the // site's system email address. $from_email = variable_get('site_mail', ''); // Compare the maillog db entry with the sent mail. $logged_email = $this->getLatestMaillogEntry(); $this->assertTrue(!empty($logged_email), 'The test email was captured.'); $this->assertEqual($to_email, $logged_email['header_to']);//, 'Email "to" address is correct.'); $this->assertEqual($from_email, $logged_email['header_from']);//, 'Email "from" address is correct.'); $this->assertEqual($from_email, $logged_email['header_all']['From']);//, 'Email "from" header is correct.'); $this->assertEqual($sender, $logged_email['header_all']['Sender']);//, 'Email "sender" header is correct.'); $this->assertEqual($sender, $logged_email['header_all']['Return-Path']);//, 'Email "return-path" header is correct.'); $this->assertEqual('Drupal', $logged_email['header_all']['X-Mailer']);//, 'Email "x-mailer" header is correct.'); $this->assertEqual(t('Test email subject'), $logged_email['subject']);//, 'Email subject is correct.'); $this->assertEqual(t('Test email body.') . "\n", $logged_email['body']);//, 'Email body is correct.'); } /** * Gets the latest Maillog entry. * * @return array * Maillog entry. */ protected function getLatestMaillogEntry() { $query = 'SELECT idmaillog, header_from, header_to, header_reply_to, header_all, subject, body FROM {maillog} ORDER BY idmaillog DESC'; $result = db_query_range($query, 0, 1); if ($maillog = $result->fetchAssoc()) { // Unserialize values. $maillog['header_all'] = unserialize($maillog['header_all']); } return $maillog; } }