Chris@18: randomString(64); Chris@18: $body = $this->randomString(128); Chris@18: $message = [ Chris@18: 'id' => 'drupal_mail_test', Chris@18: 'headers' => ['Content-type' => 'text/html'], Chris@18: 'subject' => $subject, Chris@18: 'to' => 'foobar@example.com', Chris@18: 'body' => $body, Chris@18: ]; Chris@18: Chris@18: // Before we send the email, drupalGetMails should return an empty array. Chris@18: $captured_emails = $this->drupalGetMails(); Chris@18: $this->assertEqual(count($captured_emails), 0, 'The captured emails queue is empty.', 'Email'); Chris@18: Chris@18: // Send the email. Chris@18: \Drupal::service('plugin.manager.mail')->getInstance(['module' => 'simpletest', 'key' => 'drupal_mail_test'])->mail($message); Chris@18: Chris@18: // Ensure that there is one email in the captured emails array. Chris@18: $captured_emails = $this->drupalGetMails(); Chris@18: $this->assertEqual(count($captured_emails), 1, 'One email was captured.', 'Email'); Chris@18: Chris@18: // Assert that the email was sent by iterating over the message properties Chris@18: // and ensuring that they are captured intact. Chris@18: foreach ($message as $field => $value) { Chris@18: $this->assertMail($field, $value, format_string('The email was sent and the value for property @field is intact.', ['@field' => $field]), 'Email'); Chris@18: } Chris@18: Chris@18: // Send additional emails so more than one email is captured. Chris@18: for ($index = 0; $index < 5; $index++) { Chris@18: $message = [ Chris@18: 'id' => 'drupal_mail_test_' . $index, Chris@18: 'headers' => ['Content-type' => 'text/html'], Chris@18: 'subject' => $this->randomString(64), Chris@18: 'to' => $this->randomMachineName(32) . '@example.com', Chris@18: 'body' => $this->randomString(512), Chris@18: ]; Chris@18: \Drupal::service('plugin.manager.mail')->getInstance(['module' => 'drupal_mail_test', 'key' => $index])->mail($message); Chris@18: } Chris@18: Chris@18: // There should now be 6 emails captured. Chris@18: $captured_emails = $this->drupalGetMails(); Chris@18: $this->assertEqual(count($captured_emails), 6, 'All emails were captured.', 'Email'); Chris@18: Chris@18: // Test different ways of getting filtered emails via drupalGetMails(). Chris@18: $captured_emails = $this->drupalGetMails(['id' => 'drupal_mail_test']); Chris@18: $this->assertEqual(count($captured_emails), 1, 'Only one email is returned when filtering by id.', 'Email'); Chris@18: $captured_emails = $this->drupalGetMails(['id' => 'drupal_mail_test', 'subject' => $subject]); Chris@18: $this->assertEqual(count($captured_emails), 1, 'Only one email is returned when filtering by id and subject.', 'Email'); Chris@18: $captured_emails = $this->drupalGetMails(['id' => 'drupal_mail_test', 'subject' => $subject, 'from' => 'this_was_not_used@example.com']); Chris@18: $this->assertEqual(count($captured_emails), 0, 'No emails are returned when querying with an unused from address.', 'Email'); Chris@18: Chris@18: // Send the last email again, so we can confirm that the Chris@18: // drupalGetMails-filter correctly returns all emails with a given Chris@18: // property/value. Chris@18: \Drupal::service('plugin.manager.mail')->getInstance(['module' => 'drupal_mail_test', 'key' => $index])->mail($message); Chris@18: $captured_emails = $this->drupalGetMails(['id' => 'drupal_mail_test_4']); Chris@18: $this->assertEqual(count($captured_emails), 2, 'All emails with the same id are returned when filtering by id.', 'Email'); Chris@18: } Chris@18: Chris@18: }