AssertMailTrait.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace Drupal\Core\Test;
  3. /**
  4. * Provides methods for testing emails sent during test runs.
  5. */
  6. trait AssertMailTrait {
  7. /**
  8. * Gets an array containing all emails sent during this test case.
  9. *
  10. * @param array $filter
  11. * An array containing key/value pairs used to filter the emails that are
  12. * returned.
  13. *
  14. * @return array
  15. * An array containing email messages captured during the current test.
  16. */
  17. protected function getMails(array $filter = []) {
  18. $captured_emails = $this->container->get('state')->get('system.test_mail_collector', []);
  19. $filtered_emails = [];
  20. foreach ($captured_emails as $message) {
  21. foreach ($filter as $key => $value) {
  22. if (!isset($message[$key]) || $message[$key] != $value) {
  23. continue 2;
  24. }
  25. }
  26. $filtered_emails[] = $message;
  27. }
  28. return $filtered_emails;
  29. }
  30. /**
  31. * Asserts that the most recently sent email message has the given value.
  32. *
  33. * The field in $name must have the content described in $value.
  34. *
  35. * @param string $name
  36. * Name of field or message property to assert. Examples: subject, body,
  37. * id, ...
  38. * @param string $value
  39. * Value of the field to assert.
  40. * @param string $message
  41. * (optional) A message to display with the assertion. Do not translate
  42. * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
  43. * variables in the message text, not t(). If left blank, a default message
  44. * will be displayed.
  45. * @param string $group
  46. * (optional) The group this message is in, which is displayed in a column
  47. * in test output. Use 'Debug' to indicate this is debugging output. Do not
  48. * translate this string. Defaults to 'Email'; most tests do not override
  49. * this default.
  50. *
  51. * @return bool
  52. * TRUE on pass, FALSE on fail.
  53. */
  54. protected function assertMail($name, $value = '', $message = '', $group = 'Email') {
  55. $captured_emails = $this->container->get('state')->get('system.test_mail_collector') ?: [];
  56. $email = end($captured_emails);
  57. return $this->assertTrue($email && isset($email[$name]) && $email[$name] == $value, $message, $group);
  58. }
  59. /**
  60. * Asserts that the most recently sent email message has the string in it.
  61. *
  62. * @param string $field_name
  63. * Name of field or message property to assert: subject, body, id, ...
  64. * @param string $string
  65. * String to search for.
  66. * @param int $email_depth
  67. * Number of emails to search for string, starting with most recent.
  68. * @param string $message
  69. * (optional) A message to display with the assertion. Do not translate
  70. * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
  71. * variables in the message text, not t(). If left blank, a default message
  72. * will be displayed.
  73. * @param string $group
  74. * (optional) The group this message is in, which is displayed in a column
  75. * in test output. Use 'Debug' to indicate this is debugging output. Do not
  76. * translate this string. Defaults to 'Other'; most tests do not override
  77. * this default.
  78. *
  79. * @return bool
  80. * TRUE on pass, FALSE on fail.
  81. */
  82. protected function assertMailString($field_name, $string, $email_depth, $message = '', $group = 'Other') {
  83. $mails = $this->getMails();
  84. $string_found = FALSE;
  85. // Cast MarkupInterface objects to string.
  86. $string = (string) $string;
  87. for ($i = count($mails) - 1; $i >= count($mails) - $email_depth && $i >= 0; $i--) {
  88. $mail = $mails[$i];
  89. // Normalize whitespace, as we don't know what the mail system might have
  90. // done. Any run of whitespace becomes a single space.
  91. $normalized_mail = preg_replace('/\s+/', ' ', $mail[$field_name]);
  92. $normalized_string = preg_replace('/\s+/', ' ', $string);
  93. $string_found = (FALSE !== strpos($normalized_mail, $normalized_string));
  94. if ($string_found) {
  95. break;
  96. }
  97. }
  98. if (!$message) {
  99. $message = format_string('Expected text found in @field of email message: "@expected".', ['@field' => $field_name, '@expected' => $string]);
  100. }
  101. return $this->assertTrue($string_found, $message, $group);
  102. }
  103. /**
  104. * Asserts that the most recently sent email message has the pattern in it.
  105. *
  106. * @param string $field_name
  107. * Name of field or message property to assert: subject, body, id, ...
  108. * @param string $regex
  109. * Pattern to search for.
  110. * @param string $message
  111. * (optional) A message to display with the assertion. Do not translate
  112. * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
  113. * variables in the message text, not t(). If left blank, a default message
  114. * will be displayed.
  115. * @param string $group
  116. * (optional) The group this message is in, which is displayed in a column
  117. * in test output. Use 'Debug' to indicate this is debugging output. Do not
  118. * translate this string. Defaults to 'Other'; most tests do not override
  119. * this default.
  120. *
  121. * @return bool
  122. * TRUE on pass, FALSE on fail.
  123. */
  124. protected function assertMailPattern($field_name, $regex, $message = '', $group = 'Other') {
  125. $mails = $this->getMails();
  126. $mail = end($mails);
  127. $regex_found = preg_match("/$regex/", $mail[$field_name]);
  128. if (!$message) {
  129. $message = format_string('Expected text found in @field of email message: "@expected".', ['@field' => $field_name, '@expected' => $regex]);
  130. }
  131. return $this->assertTrue($regex_found, $message, $group);
  132. }
  133. /**
  134. * Outputs to verbose the most recent $count emails sent.
  135. *
  136. * @param int $count
  137. * Optional number of emails to output.
  138. */
  139. protected function verboseEmail($count = 1) {
  140. $mails = $this->getMails();
  141. for ($i = count($mails) - 1; $i >= count($mails) - $count && $i >= 0; $i--) {
  142. $mail = $mails[$i];
  143. $this->verbose('Email:<pre>' . print_r($mail, TRUE) . '</pre>');
  144. }
  145. }
  146. }