DrupalWebTestCase::assertMailString

  1. drupal
    1. 8
    2. 7
Versions
7 protected DrupalWebTestCase::assertMailString($field_name, $string, $email_depth)

Asserts that the most recently sent e-mail message has the string in it.

Parameters

$field_name Name of field or message property to assert: subject, body, id, ...

$string String to search for.

$email_depth Number of emails to search for string, starting with most recent.

Return value

TRUE on pass, FALSE on fail.

Code

modules/simpletest/drupal_web_test_case.php, line 3332

<?php
protected function assertMailString($field_name, $string, $email_depth) {
  $mails = $this->drupalGetMails();
  $string_found = FALSE;
  for ($i = sizeof($mails) -1; $i >= sizeof($mails) - $email_depth && $i >= 0; $i--) {
    $mail = $mails[$i];
    // Normalize whitespace, as we don't know what the mail system might have
    // done. Any run of whitespace becomes a single space.
    $normalized_mail = preg_replace('/\s+/', ' ', $mail[$field_name]);
    $normalized_string = preg_replace('/\s+/', ' ', $string);
    $string_found = (FALSE !== strpos($normalized_mail, $normalized_string));
    if ($string_found) {
      break;
    }
  }
  return $this->assertTrue($string_found, t('Expected text found in @field of email message: "@expected".', array('@field' => $field_name, '@expected' => $string)));
}
?>