You are here

function MailhandlerTestCase::testMailhandlerListAddEditDelete in Mailhandler 6

Verify the functionality of the administrative interface.

File

./mailhandler.test, line 41
Simpletest case for mailhandler module.

Class

MailhandlerTestCase
Functionality tests for mailhandler module.

Code

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.'));
}