You are here

mailhandler.test in Mailhandler 6

Simpletest case for mailhandler module.

Verify mailhandler module functionality.

File

mailhandler.test
View source
<?php

/**
 * @file
 * Simpletest case for mailhandler module.
 *
 * Verify mailhandler module functionality.
 */

/**
 * Functionality tests for mailhandler module.
 */
class MailhandlerTestCase extends DrupalWebTestCase {

  /**
   * Current test temp directory.
   *
   * @var string
   */
  protected $tempdir;
  public static function getInfo() {
    return array(
      'name' => 'Interface functionality',
      'description' => 'Verify mailhandler administrative functionality for adding, editing, cloning and deleting mailboxes.',
      'group' => 'Mailhandler',
    );
  }
  function setUp() {

    // Enable the mailhandler module.
    parent::setUp('mailhandler');

    // Use the current testcase temp directory to store the testing maildir.
    $this->tempdir = variable_get('file_directory_temp', 'sites/default/files');
  }

  /**
   * Verify the functionality of the administrative interface.
   */
  function testMailhandlerListAddEditDelete() {

    // Create and login user.
    $account = $this
      ->drupalCreateUser(array(
      'administer mailhandler',
      'administer filters',
    ));
    $this
      ->drupalLogin($account);

    // Verify Mailhandler list page shows 'No mailboxes' message.
    $this
      ->drupalGet('admin/content/mailhandler');
    $this
      ->assertText(t('No mailboxes have been defined.'));

    // Create a mailbox using the Mailbox Add form. Put a value for every form
    // field and try to avoid using defaults, to make sure they are all saved
    // and populated again in the form.
    $mailbox = array(
      'mail' => $this
        ->randomName() . '@simpletest.tld',
      'mailto' => $this
        ->randomName() . '@simpletest.tld',
      'folder' => $this->tempdir,
      'imap' => 1,
      'domain' => $this
        ->randomName() . '.simpletest.tld',
      'port' => rand(1, 1000),
      'name' => $this
        ->randomName(),
      'pass' => $this
        ->randomName(),
      'extraimap' => $this
        ->randomName(),
      'mime' => 'TEXT/PLAIN,TEXT/HTML',
      'security' => 1,
      'replies' => 0,
      'fromheader' => $this
        ->randomName(),
      'commands' => $this
        ->randomName(),
      'sigseparator' => $this
        ->randomName(),
      'delete_after_read' => 0,
      'enabled' => 0,
      'format' => '2',
      'authentication' => 'mailhandler_default',
    );
    $this
      ->drupalPost('admin/content/mailhandler/add', $mailbox, t('Save mailbox'));
    $this
      ->assertRaw(t('Mailbox %mailbox added.', array(
      '%mailbox' => $mailbox['mail'],
    )));
    $mailbox['mid'] = db_result(db_query("SELECT mh.mid FROM {mailhandler} mh WHERE mail = '%s'", $mailbox['mail']));

    // Edit the mailbox and verify the fields.
    $this
      ->drupalGet('admin/content/mailhandler/edit/' . $mailbox['mid']);
    $this
      ->assertMailboxFields($mailbox);

    // Change the email and save the form.
    $mailbox['mail'] = $this
      ->randomName() . '@simpletest.tld';

    // 'mid' is not a valid form field, submit a valid $edit array for Post.
    $edit = $mailbox;
    unset($edit['mid']);
    $this
      ->drupalPost(NULL, $edit, t('Save mailbox'));
    $this
      ->assertRaw(t('Mailbox %mailbox updated.', array(
      '%mailbox' => $mailbox['mail'],
    )));

    // Clone this mailbox, fields must be copied from original mailbox.
    $this
      ->drupalGet('admin/content/mailhandler/clone/' . $mailbox['mid']);
    $this
      ->assertMailboxFields($mailbox);

    // Change new mailbox mail and save.
    $clonebox = $mailbox;
    $clonebox['mail'] = $this
      ->randomName() . '@simpletest.tld';

    // 'mid' is not a valid form field, submit a valid $edit array for Post.
    $edit = $clonebox;
    unset($edit['mid']);
    $this
      ->drupalPost(NULL, $edit, t('Save mailbox'));
    $this
      ->assertRaw(t('Mailbox %mailbox added.', array(
      '%mailbox' => $clonebox['mail'],
    )));
    $clonebox['mid'] = db_result(db_query("SELECT mh.mid FROM {mailhandler} mh WHERE mail = '%s'", $clonebox['mail']));

    // Edit the cloned mailbox and verify the fields.
    $this
      ->drupalGet('admin/content/mailhandler/edit/' . $clonebox['mid']);
    $this
      ->assertMailboxFields($clonebox);

    // Verify Mailhandler list page shows two defined mailboxes.
    $this
      ->drupalGet('admin/content/mailhandler');
    $this
      ->assertLinkByHref('mailto:' . $mailbox['mail']);
    $this
      ->assertLinkByHref('mailto:' . $clonebox['mail']);

    // Remove this mailbox. Verify CSRF protection with confirm form.
    $this
      ->drupalGet('admin/content/mailhandler/delete/' . $mailbox['mid']);
    $this
      ->assertRaw(t('Do you wish to delete mailbox %mailbox?', array(
      '%mailbox' => $mailbox['mail'],
    )));
    $this
      ->drupalPost(NULL, array(), t('Delete'));
    $this
      ->assertRaw(t('Mailbox %mailbox deleted', array(
      '%mailbox' => $mailbox['mail'],
    )));

    // Remove the cloned mailbox. Verify CSRF protection with confirm form.
    $this
      ->drupalGet('admin/content/mailhandler/delete/' . $clonebox['mid']);
    $this
      ->assertRaw(t('Do you wish to delete mailbox %mailbox?', array(
      '%mailbox' => $clonebox['mail'],
    )));
    $this
      ->drupalPost(NULL, array(), t('Delete'));
    $this
      ->assertRaw(t('Mailbox %mailbox deleted', array(
      '%mailbox' => $clonebox['mail'],
    )));

    // Verify Mailhandler list page shows 'No mailboxes' message.
    $this
      ->drupalGet('admin/content/mailhandler');
    $this
      ->assertText(t('No mailboxes have been defined.'));

    // Try to edit a missing mailbox.
    // @todo: assert it is not working
    $this
      ->drupalGet('admin/content/mailhandler/edit/' . $mailbox['mid']);
    $this
      ->assertResponse(404, t('Edit missing mailbox returns Not found.'));

    // Try to clone a missing mailbox.
    // @todo: assert it is not working
    $this
      ->drupalGet('admin/content/mailhandler/clone/' . $mailbox['mid']);
    $this
      ->assertResponse(404, t('Clone missing mailbox returns Not found.'));

    // Try to delete a missing mailbox.
    // @todo: assert it is not working
    $this
      ->drupalGet('admin/content/mailhandler/delete/' . $mailbox['mid']);
    $this
      ->assertResponse(404, t('Delete missing mailbox returns Not found.'));
  }

  /**
   * Verify the mailbox settings are shown in the edit form.
   *
   * @param $mailbox
   *   array with mailbox settings to verify in the edit form.
   */
  function assertMailboxFields($mailbox) {

    // Do not use a foreach with $mailbox to make sure all fields have their
    // value. Any missing value will fail here.
    $this
      ->assertFieldByName('mail', $mailbox['mail']);
    $this
      ->assertFieldByName('mailto', $mailbox['mailto']);
    $this
      ->assertFieldByName('folder', $mailbox['folder']);
    $this
      ->assertFieldByName('imap', $mailbox['imap']);
    $this
      ->assertFieldByName('imap', $mailbox['imap']);
    $this
      ->assertFieldByName('domain', $mailbox['domain']);
    $this
      ->assertFieldByName('port', $mailbox['port']);
    $this
      ->assertFieldByName('name', $mailbox['name']);
    $this
      ->assertFieldByName('pass', $mailbox['pass']);
    $this
      ->assertFieldByName('extraimap', $mailbox['extraimap']);
    $this
      ->assertFieldByName('mime', $mailbox['mime']);
    $this
      ->assertFieldByName('security', $mailbox['security']);
    $this
      ->assertFieldByName('replies', $mailbox['replies']);
    $this
      ->assertFieldByName('fromheader', $mailbox['fromheader']);
    $this
      ->assertFieldByName('commands', $mailbox['commands']);
    $this
      ->assertFieldByName('sigseparator', $mailbox['sigseparator']);
    $this
      ->assertFieldByName('delete_after_read', $mailbox['delete_after_read']);
    $this
      ->assertFieldByName('enabled', $mailbox['enabled']);
    $this
      ->assertFieldByName('format', $mailbox['format']);
    $this
      ->assertFieldByName('authentication', $mailbox['authentication']);
  }

  /**
   * Verify the functionality of the administrative interface when the user has
   * no privileges to edit filter formats. Even if default format for all the
   * mailboxes is FILTER_FORMAT_DEFAULT, it should not end up using this format,
   * but any available format, preventing the filtering operation from working
   * when this Drupal setting is changed.
   */
  function testMailhandlerDefaultFilterFormat() {

    // Create and login user.
    $account = $this
      ->drupalCreateUser(array(
      'administer mailhandler',
    ));
    $this
      ->drupalLogin($account);

    // Create a mailbox using the Mailbox Add form. Put a value for every form
    // field and try to avoid using defaults, to make sure they are all saved
    // and populated again in the form. The email format will not be available
    // as this user has not 'administer filters' privileges.
    $mailbox = array(
      'mail' => $this
        ->randomName() . '@simpletest.tld',
      'mailto' => $this
        ->randomName() . '@simpletest.tld',
      'folder' => variable_get('file_directory_temp', 'sites/default/files'),
      'imap' => 1,
      'domain' => $this
        ->randomName() . '.simpletest.tld',
      'port' => rand(1, 1000),
      'name' => $this
        ->randomName(),
      'pass' => $this
        ->randomName(),
      'extraimap' => $this
        ->randomName(),
      'mime' => 'TEXT/PLAIN,TEXT/HTML',
      'security' => 1,
      'replies' => 0,
      'fromheader' => $this
        ->randomName(),
      'commands' => $this
        ->randomName(),
      'sigseparator' => $this
        ->randomName(),
      'delete_after_read' => 0,
      'enabled' => 0,
      'authentication' => 'mailhandler_default',
    );
    $this
      ->drupalPost('admin/content/mailhandler/add', $mailbox, t('Save mailbox'));
    $this
      ->assertRaw(t('Mailbox %mailbox added.', array(
      '%mailbox' => $mailbox['mail'],
    )));

    // Format for the mailbox must be set to any available for this user, so no
    // no longer will hang the FILTER_FORMAT_DEFAULT value.
    $format = db_result(db_query("SELECT mh.format FROM {mailhandler} mh WHERE mail = '%s'", $mailbox['mail']));
    $this
      ->assertNotEqual($format, FILTER_FORMAT_DEFAULT, t('Default filter format is not use.'));
  }

  /**
   * Verify the functionality of the administrative interface when the user has
   * no privileges to edit filter formats.
   */
  function testMailhandlerSettings() {

    // Create and login user.
    $account = $this
      ->drupalCreateUser(array(
      'administer mailhandler',
    ));
    $this
      ->drupalLogin($account);

    // Verify default settings
    $this
      ->drupalGet('admin/settings/mailhandler');
    $this
      ->assertfieldByName('mailhandler_default_type', 'story');
    $this
      ->assertfieldByName('mailhandler_max_retrieval', '0');
    $this
      ->assertfieldByName('mailhandler_default_encoding', 'UTF-8');
    $this
      ->assertfieldByName('mailhandler_watchdog_level', '6');

    // Change settings
    $settings = array(
      'mailhandler_default_type' => 'page',
      'mailhandler_max_retrieval' => '5',
      'mailhandler_default_encoding' => 'UTF-1',
      'mailhandler_watchdog_level' => '1',
    );
    $this
      ->drupalPost(NULL, $settings, t('Save configuration'));
    $this
      ->assertText(t('The configuration options have been saved.'));
    $this
      ->assertMailhandlerSettings($settings);

    // Cron maximun validation: empty
    $settings['mailhandler_max_retrieval'] = '';
    $this
      ->drupalPost(NULL, $settings, t('Save configuration'));
    $this
      ->assertText(t('The configuration options have been saved.'));
    $this
      ->assertMailhandlerSettings($settings);

    // Cron maximun validation: empty
    $settings['mailhandler_max_retrieval'] = '-1';
    $this
      ->drupalPost(NULL, $settings, t('Save configuration'));
    $this
      ->assertNoText(t('The configuration options have been saved.'));
    $this
      ->assertText(t('The value should a positive integer, or zero.'));
  }

  // Verify mailhandler settings.
  function assertMailhandlerSettings($settings) {
    $this
      ->assertfieldByName('mailhandler_default_type', $settings['mailhandler_default_type']);
    $this
      ->assertfieldByName('mailhandler_max_retrieval', $settings['mailhandler_max_retrieval']);
    $this
      ->assertfieldByName('mailhandler_default_encoding', $settings['mailhandler_default_encoding']);
    $this
      ->assertfieldByName('mailhandler_watchdog_level', $settings['mailhandler_watchdog_level']);
  }

  /**
   * Helper functions.
   */

  /**
   * Create a mailbox in the database.
   *
   * @param $mailbox
   *  associative array with mailbox settings.
   * @return array
   *  updated mailbox with mid.
   *
   */
  function drupalCreateMailbox($mailbox = array()) {

    // Populate defaults array.
    $mailbox += array(
      'mail' => $this
        ->randomName() . '@simpletest.tld',
      'mailto' => '',
      'folder' => $this->tempdir,
      'imap' => 0,
      'domain' => '',
      'port' => '',
      'name' => '',
      'pass' => '',
      'extraimap' => '',
      'mime' => 'TEXT/HTML,TEXT/PLAIN',
      'security' => 0,
      'replies' => 1,
      'fromheader' => '',
      'commands' => '',
      'sigseparator' => '',
      'delete_after_read' => 1,
      'enabled' => 1,
      'format' => variable_get('filter_default_format', 1),
      'authentication' => '',
    );
    drupal_write_record('mailhandler', $mailbox);
    return $mailbox;
  }

}

/**
 * Processors tests for mailhandler module.
 */
class MailhandlerProcessingTestCase extends DrupalWebTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Email processing',
      'description' => 'Verify mailhandler processing operations.',
      'group' => 'Mailhandler',
    );
  }
  function setUp() {

    // Enable the mailhandler module.
    parent::setUp('mailhandler');
  }

  /**
   * Verify the body parsing functionality.
   */
  function testMailhandlerBodyParse() {

    // Incomplete test.
  }

  /**
   * Verify the commands parsing functionality.
   */
  function testMailhandlerCommandsParse() {

    // Initialise a node object
    $node = new stdClass();
    $node->type = 'dummy';
    $commands = array(
      'ping: 23',
      'pong: 42',
    );
    mailhandler_node_process_message_commands(&$node, $commands);
    $this
      ->pass("<pre>" . print_r($node, 1) . "</pre>");
  }

}

Classes

Namesort descending Description
MailhandlerProcessingTestCase Processors tests for mailhandler module.
MailhandlerTestCase Functionality tests for mailhandler module.