Drupal Test

Email Testing

Using the \AKlump\DrupalTest\EndToEndTestCase tests, you can:

Requirements

  1. Using these methods, requires the mailparse PHP extension; learn more.

Test Implementation

  1. In setUpBeforeClass indicate the email handler to use. At this time there are two handlers, AKlump\DrupalTest\Utilities\UnixMail and \AKlump\DrupalTest\Utilities\MailhogMail, and you may write your own by implementing \AKlump\DrupalTest\Utilities\EmailHandlerInterface.

    use AKlump\DrupalTest\Utilities\UnixMail;
    ...
    public static function setUpBeforeClass() {
      static::setEmailHandler(new UnixMail());
    }
    
  2. Do something in a test like this example, which waits for a password reset email and then visits the contained URL.

    public function testWelcomeEmailContainsPasswordResetUrl() {
      $email = $this->waitForEmail();
    
      // ::waitForEmail always returns an array, we just want the first email.
      $email = reset($email);
    
      $body = $email->getMessageBody('text');
      $this->assertSame(1, preg_match('/(http:\/\/.+\/user\/reset.+)\n/', $body, $matches));
    
      $reset_pass_url = $matches[1];
      $this->loadPageByUrl($reset_pass_url);
    }
    

Asserting Emails

waitForEmail will return an array of PhpMimeMailParser\Parserinstances, which makes it easy to assert against parts of each email. To learn more about that class click here.

Parser
decodeContentTransfer ($encodedString, $encodingType)
decodeHeader ($input)
decodeSingleHeader ($input)
getAddresses ($name)
getAttachments ($include_inline = true)
getAttachmentStream (&$part)
getData ()
getEmbeddedData ($contentId)
getHeader ($name)
getHeaders ()
getHeadersRaw ()
getInlineParts ($type = 'text')
getMessageBody ($type = 'text')
getPart ($type, $parts)
getPartBody (&$part)
getPartBodyFromFile (&$part)
getPartBodyFromText (&$part)
getPartCharset ($part)
getPartComplete (&$part)
getPartFromFile (&$part)
getPartFromText (&$part)
getPartHeader (&$part)
getPartHeaderFromFile (&$part)
getPartHeaderFromText (&$part)
getParts ()
getRawHeader ($name)
parse ()
partIdIsChildOfAnAttachment ($checkPartId)
partIdIsChildOfPart ($partId, $parentPartId)

On the Drupal Side of Things

Reroute Email Module

  1. Install the reroute email module.
  2. Route all email so that your EmailHandlerInterface can retrieve it.
  3. You can determine the email address used by your handler with \AKlump\DrupalTest\Utilities\EmailHandlerInterface::getInboxAddress. For example, you could do this temporarily and then read the console output:

      public function setUp() {
        $this->setEmailHandler(new UnixMail());
        echo $this->emailHandler->getInboxAddress(); die;
      }
    

MailHog

When using Lando this is the strategy to use; use MailHog and \AKlump\DrupalTest\Utilities\MailhogMail.

Sending Test Emails with Bash

echo "MESSAGE" | mail -s "SUBJECT" "USER@HOST"

Observation Mode: View Email Contents

When implementing waitForEmail if observation mode is enabled you will see the emails as observerPopups.