You are here

mandrill_hooks.test in Mandrill 7.2

Test class and methods for the Mandrill Test module.

File

tests/mandrill_hooks.test
View source
<?php

/**
 * @file
 * Test class and methods for the Mandrill Test module.
 */
class MandrillHooksTestCase extends DrupalWebTestCase {

  /**
   * Returns info displayed in the test interface.
   *
   * @return array
   *   Formatted as specified by simpletest.
   */
  public static function getInfo() {

    // Note: getInfo() strings are not translated with t().
    return array(
      'name' => 'Mandrill Hooks Tests',
      'description' => 'Tests Mandrill hook functionality.',
      'group' => 'Mandrill',
    );
  }

  /**
   * Pre-test setup function.
   *
   * Enables dependencies.
   * Sets the mandrill_api_key variable to the test key.
   */
  protected function setUp() {

    // Use a profile that contains required modules:
    $prof = drupal_get_profile();
    $this->profile = $prof;

    // Enable modules required for the test.
    $enabled_modules = array(
      'libraries',
      'mandrill',
      'mandrill_test',
      'entity',
    );
    parent::setUp($enabled_modules);
    variable_set('mandrill_api_classname', 'DrupalMandrillTest');
    variable_set('mandrill_api_key', 'MANDRILL_TEST_API_KEY');
  }

  /**
   * Post-test function.
   *
   * Sets test mode to FALSE.
   */
  protected function tearDown() {
    parent::tearDown();
    variable_del('mandrill_api_classname');
    variable_del('mandrill_api_key');
    variable_del('mandrill_test_mailsend_result');
  }

  /**
   * Tests implementing hook_mandrill_mail_alter().
   */
  public function testMailAlterHook() {

    /**
     * @see MandrillMailSystem::mail() for use example.
     */
    $message = $this
      ->getMessageTestData();
    $mandrill_params = array(
      'message' => $message,
      'function' => 'mandrill_sender_plain',
      'args' => array(),
    );

    /**
     * Perform alterations on the message.
     * @see mandrill_test_mandrill_mail_alter()
     */
    drupal_alter('mandrill_mail', $mandrill_params, $message);

    // Test altered values.
    $this
      ->assertEqual($mandrill_params['message']['subject'], 'Altered Test Subject', 'Tested altered mail subject.');
    $this
      ->assertEqual($mandrill_params['message']['html'], '<p>Altered mail content</p>', 'Tested altered mail content.');

    // Test unaltered values.
    $this
      ->assertEqual($mandrill_params['message']['from_email'], $message['from_email'], 'Tested unaltered from email.');
    $this
      ->assertEqual($mandrill_params['message']['from_name'], $message['from_name'], 'Tested unaltered from name.');
  }

  /**
   * Tests implementing hook_mandrill_valid_attachment_types_alter().
   */
  public function testValidAttachmentTypesAlterHook() {
    $types = array(
      'image/png',
      'image/jpeg',
      'image/gif',
    );

    /**
     * Perform alterations on the attachment types array.
     * @see mandrill_test_mandrill_valid_attachment_types_alter()
     */
    drupal_alter('mandrill_valid_attachment_types', $types);
    $this
      ->assertTrue(in_array('application/pdf', $types), 'Tested altered attachment types.');
  }

  /**
   * Tests implementing hook_mandrill_mailsend_result().
   */
  public function testMailsendResultHook() {
    $mail_system = new MandrillMailSystem();
    $message = $this
      ->getMessageTestData();
    $message['to'] = 'Recipient One <recipient.one@example.com>';
    $mail_system
      ->mail($message);
    $mailsend_result = variable_get('mandrill_test_mailsend_result', NULL);
    $this
      ->assertNotNull($mailsend_result, 'Tested mailsend_result hook triggered.');
    if (!is_null($mailsend_result)) {
      $this
        ->assertEqual($mailsend_result['email'], 'recipient.one@example.com', 'Tested expected mailsend_result hook result');
    }
  }

  /**
   * Gets message data used in tests.
   */
  protected function getMessageTestData() {
    $message = array(
      'id' => 1,
      'module' => NULL,
      'body' => '<p>Mail content</p>',
      'subject' => 'Mail Subject',
      'from_email' => 'sender@example.com',
      'from_name' => 'Test Sender',
    );
    return $message;
  }

}

Classes

Namesort descending Description
MandrillHooksTestCase @file Test class and methods for the Mandrill Test module.