You are here

protected function BmTestBase::assertMailStringCount in Backup and Migrate 7.3

Confirm the most recently sent e-mail(s) contain the expected string.

Parameters

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

string $string: String to search for.

int $expected: Number of times $string should occur in the field.

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

Return value

bool TRUE on pass, FALSE on fail.

See also

DrupalWebTestCase::assertMailString()

1 call to BmTestBase::assertMailStringCount()
BmTestEmail::testAddEmailDestination in tests/BmTestEmail.test
Confirm the whole email process.

File

tests/BmTestBase.test, line 275
Shared functionality to make the rest of the tests simpler.

Class

BmTestBase
Base class for testing a module's custom tags.

Code

protected function assertMailStringCount($field_name, $string, $expected, $email_depth) {
  $mails = $this
    ->drupalGetMails();
  $number_of_mails = count($mails);
  $string_count = 0;
  for ($i = $number_of_mails - 1; $i >= $number_of_mails - $email_depth && $i >= 0; $i--) {
    $mail = $mails[$i];

    // Normalize whitespace, as it isn't eacy to 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_count += substr_count($normalized_mail, $normalized_string);
  }
  $mail_count = min($number_of_mails, $email_depth);
  $message = format_plural($mail_count, 'Text "@needle" was found @stringcount times in field "@field" of the most recent email message.', 'Text "@needle" was found @stringcount times in field "@field" of the @mailcount most recent email messages.', array(
    '@needle' => $string,
    '@stringcount' => $string_count,
    '@field' => $field_name,
    '@mailcount' => $mail_count,
  ));
  return $this
    ->assertTrue($string_count === $expected, $message);
}