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