| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 | <?php/** * @file * Some tests for the SMTP module. */class SmtpUnitTest extends DrupalWebTestCase {  /**   * {@inheritdoc}   */  public static function getInfo() {    return array(      'name' => '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.');  }  /**   * Confirm the queue works.   */  public function testQueue() {    // Turn on the queue.    variable_set('smtp_queue', TRUE);    // Send a test message.    $langcode = language_default('language');    $sender = 'simpletest@example.com';    $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);    // Check the queue for messages.    $queue_count = $this->getQueueCount();    $this->assertEqual($queue_count, 1, 'An email was found in the send queue.');  }  /**   * Confirm the queue works.   */  public function testFailQueue() {    // Turn on the queue failover.    variable_set('smtp_queue_fail', TRUE);    // Make sure the queue is disabled.    variable_set('smtp_queue', FALSE);    // Turn on email delivery.    variable_set('smtp_deliver', TRUE);    // Set some fake values for the delivery, it should fail and then cause the    // email to go in to the queue.    variable_set('smtp_from', 'drupal@example.com');    variable_set('smtp_fromname', 'Drupal Simpletest');    variable_set('smtp_host', 'smtp.gmail.com');    variable_set('smtp_hostbackup', '');    variable_set('smtp_password', 'THIS WILL NOT WORK!');    variable_set('smtp_port', '465');    variable_set('smtp_protocol', 'ssl');    variable_set('smtp_username', 'hello@example.com');    // Send a test message.    $langcode = language_default('language');    $sender = 'simpletest@example.com';    $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);    // Check the queue for messages.    $queue_count = $this->getQueueCount('smtp_failure_queue');    $this->assertEqual($queue_count, 1, 'An email was found in the failure queue.');    $queue_count = $this->getQueueCount();    $this->assertEqual($queue_count, 0, 'An email was not found in the regular email queue.');    // Run the queue so that messages can be moved to the normal email queue.    drupal_cron_run();    // Check the queue for messages.    $queue_count = $this->getQueueCount();    $this->assertEqual($queue_count, 1, 'An email was found in the regular email queue.');    $queue_count = $this->getQueueCount('smtp_failure_queue');    $this->assertEqual($queue_count, 0, 'An email was not found in the failure queue.');  }  /**   * 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;  }  /**   * Get the number of emails in a specific queue.   *   * @param string $queue   *   The name of the queue to add the emails to.   *   * @return int   *   The number of messages found in the requested queue.   */  protected function getQueueCount($queue = 'smtp_send_queue') {    return db_query("SELECT count('name') FROM {queue} WHERE name = :queue",      array(':queue' => $queue))      ->fetchField();  }}
 |