MailTest.php

Definition of Drupal\system\Tests\Mail\MailTest.

Namespace

Drupal\system\Tests\Mail

File

drupal/core/modules/system/lib/Drupal/system/Tests/Mail/MailTest.php
View source
<?php

/**
 * @file
 * Definition of Drupal\system\Tests\Mail\MailTest.
 */
namespace Drupal\system\Tests\Mail;

use Drupal\Core\Mail\MailInterface;
use Drupal\simpletest\WebTestBase;

/**
 * Defines a mail class used for testing.
 */
class MailTest extends WebTestBase implements MailInterface {

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array(
    'simpletest',
  );

  /**
   * 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' => 'Mail',
    );
  }
  function setUp() {
    parent::setUp();

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

  /**
   * Assert that the pluggable mail system is functional.
   */
  public function testPluggableFramework() {
    $language_interface = language(LANGUAGE_TYPE_INTERFACE);

    // Use MailTestCase for sending a message.
    $message = drupal_mail('simpletest', 'mail_test', 'testing@example.com', $language_interface->langcode);

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

  /**
   * Test that message sending may be canceled.
   *
   * @see simpletest_mail_alter()
   */
  public function testCancelMessage() {
    $language_interface = language(LANGUAGE_TYPE_INTERFACE);

    // Reset the class variable holding a copy of the last sent message.
    self::$sent_message = NULL;

    // Send a test message that simpletest_mail_alter should cancel.
    $message = drupal_mail('simpletest', 'cancel_test', 'cancel@example.com', $language_interface->langcode);

    // Assert that the message was not actually sent.
    $this
      ->assertNull(self::$sent_message, 'Message was canceled.');
  }

  /**
   * Concatenate and wrap the e-mail body for plain-text mails.
   *
   * @see Drupal\Core\Mail\PhpMail
   */
  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;
  }

}

Classes

Namesort descending Description
MailTest Defines a mail class used for testing.