You are here

protected function EmailContext::findSubjectAndBody in Open Social 8

Same name and namespace in other branches
  1. 8.2 tests/behat/features/bootstrap/EmailContext.php \EmailContext::findSubjectAndBody()

Find an email with the given subject and body.

Parameters

string $subject: The subject of the email.

array $body: Text that should be in the email.

Return value

bool Email was found or not.

4 calls to EmailContext::findSubjectAndBody()
EmailContext::iShouldHaveAnEmailWithTitleAndBody in tests/behat/features/bootstrap/EmailContext.php
I read an email.
EmailContext::iShouldHaveAnEmailWithTitleAndBodyMulti in tests/behat/features/bootstrap/EmailContext.php
I read an email with multiple content.
EmailContext::iShouldNotHaveAnEmailWithTitleAndBody in tests/behat/features/bootstrap/EmailContext.php
I do not have an email.
EmailContext::iShouldNotHaveAnEmailWithTitleAndBodyMulti in tests/behat/features/bootstrap/EmailContext.php
I do not have an email with multiple content.

File

tests/behat/features/bootstrap/EmailContext.php, line 111

Class

EmailContext

Code

protected function findSubjectAndBody($subject, $body) {
  $finder = $this
    ->getSpooledEmails();
  $found_email = FALSE;
  if ($finder) {

    /** @var File $file */
    foreach ($finder as $file) {

      /** @var Swift_Message $email */
      $email = $this
        ->getEmailContent($file);
      $email_subject = $email
        ->getSubject();
      $email_body = $email
        ->getBody();

      // Make it a traversable HTML doc.
      $doc = new DOMDocument();
      $doc
        ->loadHTML($email_body);
      $xpath = new DOMXPath($doc);

      // Find the post header and email content in the HTML file.
      $content = $xpath
        ->evaluate('string(//*[contains(@class,"postheader")])');
      $content .= $xpath
        ->evaluate('string(//*[contains(@class,"main")])');
      $content_found = 0;
      foreach ($body as $string) {
        if (strpos($content, $string)) {
          $content_found++;
        }
      }
      if ($email_subject == $subject && $content_found === count($body)) {
        $found_email = TRUE;
      }
    }
  }
  return $found_email;
}