You are here

protected function EmailContext::findSubjectAndBody in Open Social 8.7

Same name and namespace in other branches
  1. 8.9 tests/behat/features/bootstrap/EmailContext.php \Drupal\social\Behat\EmailContext::findSubjectAndBody()
  2. 8.3 tests/behat/features/bootstrap/EmailContext.php \Drupal\social\Behat\EmailContext::findSubjectAndBody()
  3. 8.4 tests/behat/features/bootstrap/EmailContext.php \Drupal\social\Behat\EmailContext::findSubjectAndBody()
  4. 8.5 tests/behat/features/bootstrap/EmailContext.php \Drupal\social\Behat\EmailContext::findSubjectAndBody()
  5. 8.6 tests/behat/features/bootstrap/EmailContext.php \Drupal\social\Behat\EmailContext::findSubjectAndBody()
  6. 8.8 tests/behat/features/bootstrap/EmailContext.php \Drupal\social\Behat\EmailContext::findSubjectAndBody()
  7. 10.3.x tests/behat/features/bootstrap/EmailContext.php \Drupal\social\Behat\EmailContext::findSubjectAndBody()
  8. 10.0.x tests/behat/features/bootstrap/EmailContext.php \Drupal\social\Behat\EmailContext::findSubjectAndBody()
  9. 10.1.x tests/behat/features/bootstrap/EmailContext.php \Drupal\social\Behat\EmailContext::findSubjectAndBody()
  10. 10.2.x tests/behat/features/bootstrap/EmailContext.php \Drupal\social\Behat\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.

Throws

Exception

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 124

Class

EmailContext

Namespace

Drupal\social\Behat

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;
      }
    }
  }
  else {
    throw new \Exception('There are no email messages.');
  }
  return $found_email;
}