diff -u b/core/lib/Drupal/Core/Test/AssertMailTrait.php b/core/lib/Drupal/Core/Test/AssertMailTrait.php --- b/core/lib/Drupal/Core/Test/AssertMailTrait.php +++ b/core/lib/Drupal/Core/Test/AssertMailTrait.php @@ -1,4 +1,5 @@ container->get('state')->get('system.test_mail_collector', []); $filtered_emails = []; @@ -42,23 +43,23 @@ * * The field in $name must have the content described in $value. * - * @param $name + * @param string $name * Name of field or message property to assert. Examples: subject, body, * id, ... - * @param $value + * @param string $value * Value of the field to assert. - * @param $message + * @param string $message * (optional) A message to display with the assertion. Do not translate * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed * variables in the message text, not t(). If left blank, a default message * will be displayed. - * @param $group + * @param string $group * (optional) The group this message is in, which is displayed in a column * in test output. Use 'Debug' to indicate this is debugging output. Do not * translate this string. Defaults to 'Email'; most tests do not override * this default. * - * @return + * @return bool * TRUE on pass, FALSE on fail. */ protected function assertMail($name, $value = '', $message = '', $group = 'Email') { @@ -70,24 +71,24 @@ /** * Asserts that the most recently sent email message has the string in it. * - * @param $field_name + * @param string $field_name * Name of field or message property to assert: subject, body, id, ... - * @param $string + * @param string $string * String to search for. - * @param $email_depth + * @param int $email_depth * Number of emails to search for string, starting with most recent. - * @param $message + * @param string $message * (optional) A message to display with the assertion. Do not translate * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed * variables in the message text, not t(). If left blank, a default message * will be displayed. - * @param $group + * @param string $group * (optional) The group this message is in, which is displayed in a column * in test output. Use 'Debug' to indicate this is debugging output. Do not * translate this string. Defaults to 'Other'; most tests do not override * this default. * - * @return + * @return bool * TRUE on pass, FALSE on fail. */ protected function assertMailString($field_name, $string, $email_depth, $message = '', $group = 'Other') { @@ -95,7 +96,7 @@ $string_found = FALSE; // Cast MarkupInterface objects to string. $string = (string) $string; - for ($i = count($mails) -1; $i >= count($mails) - $email_depth && $i >= 0; $i--) { + for ($i = count($mails) - 1; $i >= count($mails) - $email_depth && $i >= 0; $i--) { $mail = $mails[$i]; // Normalize whitespace, as we don't know what the mail system might have // done. Any run of whitespace becomes a single space. @@ -115,22 +116,22 @@ /** * Asserts that the most recently sent email message has the pattern in it. * - * @param $field_name + * @param string $field_name * Name of field or message property to assert: subject, body, id, ... - * @param $regex + * @param string $regex * Pattern to search for. - * @param $message + * @param string $message * (optional) A message to display with the assertion. Do not translate * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed * variables in the message text, not t(). If left blank, a default message * will be displayed. - * @param $group + * @param string $group * (optional) The group this message is in, which is displayed in a column * in test output. Use 'Debug' to indicate this is debugging output. Do not * translate this string. Defaults to 'Other'; most tests do not override * this default. * - * @return + * @return bool * TRUE on pass, FALSE on fail. */ protected function assertMailPattern($field_name, $regex, $message = '', $group = 'Other') { @@ -149,13 +150,12 @@ - * @param $count + * @param int $count * Optional number of emails to output. */ protected function verboseEmail($count = 1) { $mails = $this->getMails(); - for ($i = count($mails) -1; $i >= count($mails) - $count && $i >= 0; $i--) { + for ($i = count($mails) - 1; $i >= count($mails) - $count && $i >= 0; $i--) { $mail = $mails[$i]; $this->verbose('Email:
' . print_r($mail, TRUE) . '
'); } } } - only in patch2: unchanged: --- /dev/null +++ b/core/tests/Drupal/KernelTests/Core/Test/AssertMailTraitTest.php @@ -0,0 +1,91 @@ +randomString(64); + $body = $this->randomString(128); + $message = [ + 'id' => 'drupal_mail_test', + 'headers' => ['Content-type' => 'text/html'], + 'subject' => $subject, + 'to' => 'foobar@example.com', + 'body' => $body, + ]; + + // Before we send the email, drupalGetMails should return an empty array. + $captured_emails = $this->getMails(); + $this->assertCount(0, $captured_emails, 'The captured emails queue is empty.'); + + // Send the email. + \Drupal::service('plugin.manager.mail')->getInstance(['module' => 'simpletest', 'key' => 'drupal_mail_test'])->mail($message); + + // Ensure that there is one email in the captured emails array. + $captured_emails = $this->getMails(); + $this->assertEquals(count($captured_emails), 1, 'One email was captured.'); + + // Assert that the email was sent by iterating over the message properties + // and ensuring that they are captured intact. + foreach ($message as $field => $value) { + $this->assertMail($field, $value, sprintf('The email was sent and the value for property %s is intact.', $field)); + } + + // Send additional emails so more than one email is captured. + for ($index = 0; $index < 5; $index++) { + $message = [ + 'id' => 'drupal_mail_test_' . $index, + 'headers' => ['Content-type' => 'text/html'], + 'subject' => $this->randomString(64), + 'to' => $this->randomMachineName(32) . '@example.com', + 'body' => $this->randomString(512), + ]; + \Drupal::service('plugin.manager.mail')->getInstance(['module' => 'drupal_mail_test', 'key' => $index])->mail($message); + } + + // There should now be 6 emails captured. + $captured_emails = $this->getMails(); + $this->assertCount(6, $captured_emails, 'All emails were captured.'); + + // Test different ways of getting filtered emails via drupalGetMails(). + $captured_emails = $this->getMails(['id' => 'drupal_mail_test']); + $this->assertCount(1, $captured_emails, 'Only one email is returned when filtering by id.'); + $captured_emails = $this->getMails(['id' => 'drupal_mail_test', 'subject' => $subject]); + $this->assertCount(1, $captured_emails, 'Only one email is returned when filtering by id and subject.'); + $captured_emails = $this->getMails([ + 'id' => 'drupal_mail_test', + 'subject' => $subject, + 'from' => 'this_was_not_used@example.com', + ]); + $this->assertCount(0, $captured_emails, 'No emails are returned when querying with an unused from address.'); + + // Send the last email again, so we can confirm that the + // drupalGetMails-filter correctly returns all emails with a given + // property/value. + \Drupal::service('plugin.manager.mail')->getInstance(['module' => 'drupal_mail_test', 'key' => $index])->mail($message); + $captured_emails = $this->getMails(['id' => 'drupal_mail_test_4']); + $this->assertCount(2, $captured_emails, 'All emails with the same id are returned when filtering by id.'); + } + +}