Security update for Core, with self-updated composer
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Test / AssertMailTraitTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Test;
4
5 use Drupal\Core\Test\AssertMailTrait;
6 use Drupal\KernelTests\KernelTestBase;
7
8 /**
9  * Tests \Drupal\Core\Test\AssertMailTrait works.
10  *
11  * @group Test
12  *
13  * @coversDefaultClass \Drupal\Core\Test\AssertMailTrait
14  */
15 class AssertMailTraitTest extends KernelTestBase {
16   use AssertMailTrait;
17
18   /**
19    * Tests that the maintenance theme initializes the theme and its base themes.
20    */
21   public function testAssertMailTrait() {
22     /* @var \Drupal\Core\Mail\MailManagerInterface $mail_service */
23     $mail_service = \Drupal::service('plugin.manager.mail');
24
25     // Create an email.
26     $subject = $this->randomString(64);
27     $body = $this->randomString(128);
28     $message = [
29       'id' => 'drupal_mail_test',
30       'headers' => ['Content-type' => 'text/html'],
31       'subject' => $subject,
32       'to' => 'foobar@example.com',
33       'body' => $body,
34     ];
35
36     // Before we send the email, \Drupal\Core\Test\AssertMailTrait::getMails()
37     // should return an empty array.
38     $captured_emails = $this->getMails();
39     $this->assertCount(0, $captured_emails, 'The captured emails queue is empty.');
40
41     // Send the email.
42     $mail_service->getInstance(['module' => 'simpletest', 'key' => 'drupal_mail_test'])->mail($message);
43
44     // Ensure that there is one email in the captured emails array.
45     $captured_emails = $this->getMails();
46     $this->assertEquals(count($captured_emails), 1, 'One email was captured.');
47
48     // Assert that the email was sent by iterating over the message properties
49     // and ensuring that they are captured intact.
50     foreach ($message as $field => $value) {
51       $this->assertMail($field, $value, "The email was sent and the value for property $field is intact.");
52     }
53
54     // Send additional emails so more than one email is captured.
55     for ($index = 0; $index < 5; $index++) {
56       $message = [
57         'id' => 'drupal_mail_test_' . $index,
58         'headers' => ['Content-type' => 'text/html'],
59         'subject' => $this->randomString(64),
60         'to' => $this->randomMachineName(32) . '@example.com',
61         'body' => $this->randomString(512),
62       ];
63       $mail_service->getInstance(['module' => 'drupal_mail_test', 'key' => $index])->mail($message);
64     }
65
66     // There should now be 6 emails captured.
67     $captured_emails = $this->getMails();
68     $this->assertCount(6, $captured_emails, 'All emails were captured.');
69
70     // Test different ways of getting filtered emails via
71     // \Drupal\Core\Test\AssertMailTrait::getMails().
72     $captured_emails = $this->getMails(['id' => 'drupal_mail_test']);
73     $this->assertCount(1, $captured_emails, 'Only one email is returned when filtering by id.');
74     $captured_emails = $this->getMails(['id' => 'drupal_mail_test', 'subject' => $subject]);
75     $this->assertCount(1, $captured_emails, 'Only one email is returned when filtering by id and subject.');
76     $captured_emails = $this->getMails([
77       'id' => 'drupal_mail_test',
78       'subject' => $subject,
79       'from' => 'this_was_not_used@example.com',
80     ]);
81     $this->assertCount(0, $captured_emails, 'No emails are returned when querying with an unused from address.');
82
83     // Send the last email again, so we can confirm that
84     // \Drupal\Core\Test\AssertMailTrait::getMails() filters correctly returns
85     // all emails with a given property/value.
86     $mail_service->getInstance(['module' => 'drupal_mail_test', 'key' => $index])->mail($message);
87     $captured_emails = $this->getMails(['id' => 'drupal_mail_test_4']);
88     $this->assertCount(2, $captured_emails, 'All emails with the same id are returned when filtering by id.');
89   }
90
91 }