You are here

protected function WebTestBase::assertMailString in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/simpletest/src/WebTestBase.php \Drupal\simpletest\WebTestBase::assertMailString()

Asserts that the most recently sent email 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.

$message: (optional) A message to display with the assertion. Do not translate messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed variables in the message text, not t(). If left blank, a default message will be displayed.

$group: (optional) The group this message is in, which is displayed in a column in test output. Use 'Debug' to indicate this is debugging output. Do not translate this string. Defaults to 'Other'; most tests do not override this default.

Return value

TRUE on pass, FALSE on fail.

File

core/modules/simpletest/src/WebTestBase.php, line 2832
Contains \Drupal\simpletest\WebTestBase.

Class

WebTestBase
Test case for typical Drupal tests.

Namespace

Drupal\simpletest

Code

protected function assertMailString($field_name, $string, $email_depth, $message = '', $group = 'Other') {
  $mails = $this
    ->drupalGetMails();
  $string_found = FALSE;

  // Cast MarkupInterface objects to string.
  $string = (string) $string;
  for ($i = count($mails) - 1; $i >= count($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;
    }
  }
  if (!$message) {
    $message = format_string('Expected text found in @field of email message: "@expected".', array(
      '@field' => $field_name,
      '@expected' => $string,
    ));
  }
  return $this
    ->assertTrue($string_found, $message, $group);
}