3 namespace Drupal\KernelTests\Core\Test;
5 use Drupal\Core\Test\AssertMailTrait;
6 use Drupal\KernelTests\KernelTestBase;
9 * Tests \Drupal\Core\Test\AssertMailTrait works.
13 * @coversDefaultClass \Drupal\Core\Test\AssertMailTrait
15 class AssertMailTraitTest extends KernelTestBase {
19 * Tests that the maintenance theme initializes the theme and its base themes.
21 public function testAssertMailTrait() {
22 /* @var \Drupal\Core\Mail\MailManagerInterface $mail_service */
23 $mail_service = \Drupal::service('plugin.manager.mail');
26 $subject = $this->randomString(64);
27 $body = $this->randomString(128);
29 'id' => 'drupal_mail_test',
30 'headers' => ['Content-type' => 'text/html'],
31 'subject' => $subject,
32 'to' => 'foobar@example.com',
36 // Before we send the email, \Drupal\Core\Test\AssertMailTrait::getMails()
37 // should return an empty array.
38 $captured_emails = $this->getMails();
39 $this->assertCount(0, $captured_emails, 'The captured emails queue is empty.');
42 $mail_service->getInstance(['module' => 'simpletest', 'key' => 'drupal_mail_test'])->mail($message);
44 // Ensure that there is one email in the captured emails array.
45 $captured_emails = $this->getMails();
46 $this->assertEquals(count($captured_emails), 1, 'One email was captured.');
48 // Assert that the email was sent by iterating over the message properties
49 // and ensuring that they are captured intact.
50 foreach ($message as $field => $value) {
51 $this->assertMail($field, $value, "The email was sent and the value for property $field is intact.");
54 // Send additional emails so more than one email is captured.
55 for ($index = 0; $index < 5; $index++) {
57 'id' => 'drupal_mail_test_' . $index,
58 'headers' => ['Content-type' => 'text/html'],
59 'subject' => $this->randomString(64),
60 'to' => $this->randomMachineName(32) . '@example.com',
61 'body' => $this->randomString(512),
63 $mail_service->getInstance(['module' => 'drupal_mail_test', 'key' => $index])->mail($message);
66 // There should now be 6 emails captured.
67 $captured_emails = $this->getMails();
68 $this->assertCount(6, $captured_emails, 'All emails were captured.');
70 // Test different ways of getting filtered emails via
71 // \Drupal\Core\Test\AssertMailTrait::getMails().
72 $captured_emails = $this->getMails(['id' => 'drupal_mail_test']);
73 $this->assertCount(1, $captured_emails, 'Only one email is returned when filtering by id.');
74 $captured_emails = $this->getMails(['id' => 'drupal_mail_test', 'subject' => $subject]);
75 $this->assertCount(1, $captured_emails, 'Only one email is returned when filtering by id and subject.');
76 $captured_emails = $this->getMails([
77 'id' => 'drupal_mail_test',
78 'subject' => $subject,
79 'from' => 'this_was_not_used@example.com',
81 $this->assertCount(0, $captured_emails, 'No emails are returned when querying with an unused from address.');
83 // Send the last email again, so we can confirm that
84 // \Drupal\Core\Test\AssertMailTrait::getMails() filters correctly returns
85 // all emails with a given property/value.
86 $mail_service->getInstance(['module' => 'drupal_mail_test', 'key' => $index])->mail($message);
87 $captured_emails = $this->getMails(['id' => 'drupal_mail_test_4']);
88 $this->assertCount(2, $captured_emails, 'All emails with the same id are returned when filtering by id.');