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