class MailerCaptureTest

Tests capturing of emails when mailer_capture module is installed.

The mailer_capture module installs a mailer transport which intercepts any email sent during a test.

Attributes

#[Group('mailer')] #[RunTestsInSeparateProcesses]

Hierarchy

Expanded class hierarchy of MailerCaptureTest

File

core/modules/mailer/tests/src/Functional/MailerCaptureTest.php, line 21

Namespace

Drupal\Tests\mailer\Functional
View source
class MailerCaptureTest extends BrowserTestBase {
  use MailerCaptureTrait;
  
  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';
  
  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'mailer',
    'mailer_capture',
    'mailer_capture_test',
  ];
  
  /**
   * Tests collecting mail sent in the test runner.
   */
  public function testMailCaptureTestRunner() : void {
    // Create an email.
    $email = new Email();
    $email->from('admin@example.com');
    $email->subject('State machine energy a production like service.');
    $email->text('We name know environmental along agree let. Traditional interest this clearly concern discover.');
    // Before we send the email, getEmails should return an empty array.
    $capturedEmails = $this->getEmails();
    $this->assertCount(0, $capturedEmails, 'The captured emails queue is empty.');
    $mailer = $this->container
      ->get(MailerInterface::class);
    assert($mailer instanceof MailerInterface);
    $mailer->send($email->to('foobar@example.com'));
    // Ensure that there is one email in the captured emails array.
    $capturedEmails = $this->getEmails();
    $this->assertCount(1, $capturedEmails, 'One email was captured.');
    $this->assertEquals([
      new Address('admin@example.com'),
    ], $capturedEmails[0]->getFrom());
    $this->assertEquals([
      new Address('foobar@example.com'),
    ], $capturedEmails[0]->getTo());
    $this->assertEquals('State machine energy a production like service.', $capturedEmails[0]->getSubject());
    $this->assertEquals('We name know environmental along agree let. Traditional interest this clearly concern discover.', (string) $capturedEmails[0]->getTextBody());
    $this->clearCapturedMessages();
    $capturedEmails = $this->getEmails();
    $this->assertCount(0, $capturedEmails, 'The captured emails queue is empty.');
  }
  
  /**
   * Tests collecting mail sent in the child site.
   */
  public function testMailCaptureChild() : void {
    // Before we send the email, getEmails should return an empty array.
    $capturedEmails = $this->getEmails();
    $this->assertCount(0, $capturedEmails, 'The captured emails queue is empty.');
    $this->drupalGet('/mailer-capture-test/send-mail');
    $this->submitForm([], 'Send Mail');
    // Ensure that there is one email in the captured emails array.
    $capturedEmails = $this->getEmails();
    $this->assertCount(1, $capturedEmails, 'One email was captured.');
    $this->assertEquals([
      new Address('admin@localhost.localdomain'),
    ], $capturedEmails[0]->getFrom());
    $this->assertEquals([
      new Address('test@localhost.localdomain'),
    ], $capturedEmails[0]->getTo());
    $this->assertEquals('Test message', $capturedEmails[0]->getSubject());
    $this->assertEquals('Hello test runner!', (string) $capturedEmails[0]->getTextBody());
    $this->clearCapturedMessages();
    $capturedEmails = $this->getEmails();
    $this->assertCount(0, $capturedEmails, 'The captured emails queue is empty.');
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.