AssertMailTraitTest.php 3.6 KB

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