comparison core/tests/Drupal/FunctionalTests/MailCaptureTest.php @ 18:af1871eacc83

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