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