MailCaptureTest.php 3.6 KB

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