As far as I understand it, with assertMail we can test for the presence of only the complete content of fields like "subject" or "body". Is there a way to test for only a small part of the body being present? E.g. a specific link in an e-mail body or a string in the subject?

Comments

reglogge’s picture

Status: Active » Fixed

Found the solution at http://www.comm-press.de/blog/simple-test-mit-drupal (in German).

Create a function inside your test like this to parse the entire mail:

<?php
function drupal_mail_wrapper(array $message) {
  $captured_emails = variable_get('drupal_test_email_collector', array());
  $captured_emails[] = $message;
  variable_set('drupal_test_email_collector', $captured_emails);
  return TRUE;
}
?>

then you can assert any content like this:

<?php
$mails = variable_get('drupal_test_email_collector', array());

    $this->assertEqual(count($mails), 2, '2 emails found.');

    // Check original email.
    $this->assertEqual($mails[0]['id'], 'ecard_ecard-mail', 'Correct email id found.');
    $this->assertEqual($mails[0]['from'], $from_email, 'From address is correctly set.');
    $this->assertEqual($mails[0]['to'], $to_email, 'To address is correctly set.');
?>

Neat!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.