home | career | drupal | java | mac | mysql | perl | php | scala | uml | unix

Drupal example source code file (mail.test)

This example Drupal source code file (mail.test) is included in the DevDaily.com "Drupal Source Code Warehouse" project. The intent of this project is to help you "Learn Drupal by Example".

PHP - Drupal tags/keywords

array, extends, function, language, mail, mailtestcase, message, php, private, public, return, sent_message, static, system

The mail.test Drupal example source code

<?php
// $Id: mail.test,v 1.5 2010/08/05 23:53:38 webchick Exp $

/**
 * Test the Drupal mailing system.
 */
class MailTestCase extends DrupalWebTestCase implements MailSystemInterface {
  /**
   * The most recent message that was sent through the test case.
   *
   * We take advantage here of the fact that static variables are shared among
   * all instance of the same class.
   */
  private static $sent_message;

  public static function getInfo() {
    return array(
      'name' => 'Mail system',
      'description' => 'Performs tests on the pluggable mailing framework.',
      'group' => 'System',
    );
  }

  function setUp() {
    parent::setUp();

    // Set MailTestCase (i.e. this class) as the SMTP library
    variable_set('mail_system', array('default-system' => 'MailTestCase'));
  }

  /**
   * Assert that the pluggable mail system is functional.
   */
  function testPluggableFramework() {
    global $language;

    // Use MailTestCase for sending a message.
    $message = drupal_mail('simpletest', 'mail_test', 'testing@drupal.org', $language);

    // Assert whether the message was sent through the send function.
    $this->assertEqual(self::$sent_message['to'], 'testing@drupal.org', t('Pluggable mail system is extendable.'));
  }

  /**
   * Concatenate and wrap the e-mail body for plain-text mails.
   *
   * @see DefaultMailSystem
   */
  public function format(array $message) {
    // Join the body array into one string.
    $message['body'] = implode("\n\n", $message['body']);
    // Convert any HTML to plain-text.
    $message['body'] = drupal_html_to_text($message['body']);
    // Wrap the mail body for sending.
    $message['body'] = drupal_wrap_mail($message['body']);
    return $message;
  }

  /**
   * Send function that is called through the mail system.
   */
  public function mail(array $message) {
    self::$sent_message = $message;
  }
}

Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal mail.test source code file:

new blog posts

"Drupal" is a registered trademark of Dries Buytaert.

my drupal tutorials and examples  

Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

Beginning in 2016, a portion of the proceeds from pages under the '/drupal-code-examples/' URI will be donated to charity.