--- mailhandler.test
+++ mailhandler.test
@@ -0,0 +1,311 @@
+<?php
+// $Id$
+
+/**
+ * @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' => 'Admin interface',
+      'description' => 'Verify mailhandler administrative functionality.',
+      'group' => 'Mailhandler',
+    );
+  }
+
+  function setUp(){
+    // Enable the exmaple 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';
+    $this->drupalPost(NULL, $mailbox, 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';
+    
+    $this->drupalPost(NULL, $clonebox, 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']);
+
+    // Try to clone a missing mailbox.
+    // @todo: assert it is not working
+    $this->drupalGet('admin/content/mailhandler/clone/' . $mailbox['mid']);
+    
+    // Try to delete a missing mailbox.
+    // @todo: assert it is not working
+    $this->drupalGet('admin/content/mailhandler/delete/' . $mailbox['mid']);
+    $this->drupalPost(NULL, array(), t('Delete'));
+  }
+
+  /**
+   * 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.
+   */
+  function testMailhandlerDefaultFilterFormats() {
+
+    // 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 = db_result(db_query(
+      "SELECT mh.format FROM {mailhandler} mh WHERE mail = '%s'", $mailbox['mail']
+    ));
+
+    $this->assertEqual($format, FILTER_FORMAT_DEFAULT, t('Default filter format in 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');
+    
+    // Change settings
+    $settings = array(
+      'mailhandler_default_type'     => 'page',
+      'mailhandler_max_retrieval'    => '5',
+      'mailhandler_default_encoding' => 'UTF-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']);
+  }
+
+
+
+  /**
+   * 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' => FILTER_FORMAT_DEFAULT,
+      'authentication' => '',
+    );
+    drupal_write_record('mailhandler', $mailbox);
+    return $mailbox;
+  }
+
+}
+
+
+


