3 namespace Drupal\Tests\simpletest\Functional;
5 use Drupal\Tests\BrowserTestBase;
6 use Drupal\Core\Test\AssertMailTrait;
9 * Tests the SimpleTest email capturing logic, the assertMail assertion and the
10 * drupalGetMails function.
14 class MailCaptureTest extends BrowserTestBase {
16 getMails as drupalGetMails;
19 * Test to see if the wrapper function is executed correctly.
21 public function testMailSend() {
23 $subject = $this->randomString(64);
24 $body = $this->randomString(128);
26 'id' => 'drupal_mail_test',
27 'headers' => ['Content-type' => 'text/html'],
28 'subject' => $subject,
29 'to' => 'foobar@example.com',
33 // Before we send the email, drupalGetMails should return an empty array.
34 $captured_emails = $this->drupalGetMails();
35 $this->assertEqual(count($captured_emails), 0, 'The captured emails queue is empty.', 'Email');
38 \Drupal::service('plugin.manager.mail')->getInstance(['module' => 'simpletest', 'key' => 'drupal_mail_test'])->mail($message);
40 // Ensure that there is one email in the captured emails array.
41 $captured_emails = $this->drupalGetMails();
42 $this->assertEqual(count($captured_emails), 1, 'One email was captured.', 'Email');
44 // Assert that the email was sent by iterating over the message properties
45 // and ensuring that they are captured intact.
46 foreach ($message as $field => $value) {
47 $this->assertMail($field, $value, format_string('The email was sent and the value for property @field is intact.', ['@field' => $field]), 'Email');
50 // Send additional emails so more than one email is captured.
51 for ($index = 0; $index < 5; $index++) {
53 'id' => 'drupal_mail_test_' . $index,
54 'headers' => ['Content-type' => 'text/html'],
55 'subject' => $this->randomString(64),
56 'to' => $this->randomMachineName(32) . '@example.com',
57 'body' => $this->randomString(512),
59 \Drupal::service('plugin.manager.mail')->getInstance(['module' => 'drupal_mail_test', 'key' => $index])->mail($message);
62 // There should now be 6 emails captured.
63 $captured_emails = $this->drupalGetMails();
64 $this->assertEqual(count($captured_emails), 6, 'All emails were captured.', 'Email');
66 // Test different ways of getting filtered emails via drupalGetMails().
67 $captured_emails = $this->drupalGetMails(['id' => 'drupal_mail_test']);
68 $this->assertEqual(count($captured_emails), 1, 'Only one email is returned when filtering by id.', 'Email');
69 $captured_emails = $this->drupalGetMails(['id' => 'drupal_mail_test', 'subject' => $subject]);
70 $this->assertEqual(count($captured_emails), 1, 'Only one email is returned when filtering by id and subject.', 'Email');
71 $captured_emails = $this->drupalGetMails(['id' => 'drupal_mail_test', 'subject' => $subject, 'from' => 'this_was_not_used@example.com']);
72 $this->assertEqual(count($captured_emails), 0, 'No emails are returned when querying with an unused from address.', 'Email');
74 // Send the last email again, so we can confirm that the
75 // drupalGetMails-filter correctly returns all emails with a given
77 \Drupal::service('plugin.manager.mail')->getInstance(['module' => 'drupal_mail_test', 'key' => $index])->mail($message);
78 $captured_emails = $this->drupalGetMails(['id' => 'drupal_mail_test_4']);
79 $this->assertEqual(count($captured_emails), 2, 'All emails with the same id are returned when filtering by id.', 'Email');