Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\Test;
|
Chris@0
|
4
|
Chris@0
|
5 /**
|
Chris@0
|
6 * Provides methods for testing emails sent during test runs.
|
Chris@0
|
7 */
|
Chris@0
|
8 trait AssertMailTrait {
|
Chris@0
|
9
|
Chris@0
|
10 /**
|
Chris@0
|
11 * Gets an array containing all emails sent during this test case.
|
Chris@0
|
12 *
|
Chris@0
|
13 * @param array $filter
|
Chris@0
|
14 * An array containing key/value pairs used to filter the emails that are
|
Chris@0
|
15 * returned.
|
Chris@0
|
16 *
|
Chris@0
|
17 * @return array
|
Chris@0
|
18 * An array containing email messages captured during the current test.
|
Chris@0
|
19 */
|
Chris@0
|
20 protected function getMails(array $filter = []) {
|
Chris@0
|
21 $captured_emails = $this->container->get('state')->get('system.test_mail_collector', []);
|
Chris@0
|
22 $filtered_emails = [];
|
Chris@0
|
23
|
Chris@0
|
24 foreach ($captured_emails as $message) {
|
Chris@0
|
25 foreach ($filter as $key => $value) {
|
Chris@0
|
26 if (!isset($message[$key]) || $message[$key] != $value) {
|
Chris@0
|
27 continue 2;
|
Chris@0
|
28 }
|
Chris@0
|
29 }
|
Chris@0
|
30 $filtered_emails[] = $message;
|
Chris@0
|
31 }
|
Chris@0
|
32
|
Chris@0
|
33 return $filtered_emails;
|
Chris@0
|
34 }
|
Chris@0
|
35
|
Chris@0
|
36 /**
|
Chris@0
|
37 * Asserts that the most recently sent email message has the given value.
|
Chris@0
|
38 *
|
Chris@0
|
39 * The field in $name must have the content described in $value.
|
Chris@0
|
40 *
|
Chris@0
|
41 * @param string $name
|
Chris@0
|
42 * Name of field or message property to assert. Examples: subject, body,
|
Chris@0
|
43 * id, ...
|
Chris@0
|
44 * @param string $value
|
Chris@0
|
45 * Value of the field to assert.
|
Chris@0
|
46 * @param string $message
|
Chris@0
|
47 * (optional) A message to display with the assertion. Do not translate
|
Chris@17
|
48 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
|
Chris@0
|
49 * variables in the message text, not t(). If left blank, a default message
|
Chris@0
|
50 * will be displayed.
|
Chris@0
|
51 * @param string $group
|
Chris@0
|
52 * (optional) The group this message is in, which is displayed in a column
|
Chris@0
|
53 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@0
|
54 * translate this string. Defaults to 'Email'; most tests do not override
|
Chris@0
|
55 * this default.
|
Chris@0
|
56 *
|
Chris@0
|
57 * @return bool
|
Chris@0
|
58 * TRUE on pass, FALSE on fail.
|
Chris@0
|
59 */
|
Chris@0
|
60 protected function assertMail($name, $value = '', $message = '', $group = 'Email') {
|
Chris@0
|
61 $captured_emails = $this->container->get('state')->get('system.test_mail_collector') ?: [];
|
Chris@0
|
62 $email = end($captured_emails);
|
Chris@0
|
63 return $this->assertTrue($email && isset($email[$name]) && $email[$name] == $value, $message, $group);
|
Chris@0
|
64 }
|
Chris@0
|
65
|
Chris@0
|
66 /**
|
Chris@0
|
67 * Asserts that the most recently sent email message has the string in it.
|
Chris@0
|
68 *
|
Chris@0
|
69 * @param string $field_name
|
Chris@0
|
70 * Name of field or message property to assert: subject, body, id, ...
|
Chris@0
|
71 * @param string $string
|
Chris@0
|
72 * String to search for.
|
Chris@0
|
73 * @param int $email_depth
|
Chris@0
|
74 * Number of emails to search for string, starting with most recent.
|
Chris@0
|
75 * @param string $message
|
Chris@0
|
76 * (optional) A message to display with the assertion. Do not translate
|
Chris@17
|
77 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
|
Chris@0
|
78 * variables in the message text, not t(). If left blank, a default message
|
Chris@0
|
79 * will be displayed.
|
Chris@0
|
80 * @param string $group
|
Chris@0
|
81 * (optional) The group this message is in, which is displayed in a column
|
Chris@0
|
82 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@0
|
83 * translate this string. Defaults to 'Other'; most tests do not override
|
Chris@0
|
84 * this default.
|
Chris@0
|
85 *
|
Chris@0
|
86 * @return bool
|
Chris@0
|
87 * TRUE on pass, FALSE on fail.
|
Chris@0
|
88 */
|
Chris@0
|
89 protected function assertMailString($field_name, $string, $email_depth, $message = '', $group = 'Other') {
|
Chris@0
|
90 $mails = $this->getMails();
|
Chris@0
|
91 $string_found = FALSE;
|
Chris@0
|
92 // Cast MarkupInterface objects to string.
|
Chris@0
|
93 $string = (string) $string;
|
Chris@0
|
94 for ($i = count($mails) - 1; $i >= count($mails) - $email_depth && $i >= 0; $i--) {
|
Chris@0
|
95 $mail = $mails[$i];
|
Chris@0
|
96 // Normalize whitespace, as we don't know what the mail system might have
|
Chris@0
|
97 // done. Any run of whitespace becomes a single space.
|
Chris@0
|
98 $normalized_mail = preg_replace('/\s+/', ' ', $mail[$field_name]);
|
Chris@0
|
99 $normalized_string = preg_replace('/\s+/', ' ', $string);
|
Chris@0
|
100 $string_found = (FALSE !== strpos($normalized_mail, $normalized_string));
|
Chris@0
|
101 if ($string_found) {
|
Chris@0
|
102 break;
|
Chris@0
|
103 }
|
Chris@0
|
104 }
|
Chris@0
|
105 if (!$message) {
|
Chris@0
|
106 $message = format_string('Expected text found in @field of email message: "@expected".', ['@field' => $field_name, '@expected' => $string]);
|
Chris@0
|
107 }
|
Chris@0
|
108 return $this->assertTrue($string_found, $message, $group);
|
Chris@0
|
109 }
|
Chris@0
|
110
|
Chris@0
|
111 /**
|
Chris@0
|
112 * Asserts that the most recently sent email message has the pattern in it.
|
Chris@0
|
113 *
|
Chris@0
|
114 * @param string $field_name
|
Chris@0
|
115 * Name of field or message property to assert: subject, body, id, ...
|
Chris@0
|
116 * @param string $regex
|
Chris@0
|
117 * Pattern to search for.
|
Chris@0
|
118 * @param string $message
|
Chris@0
|
119 * (optional) A message to display with the assertion. Do not translate
|
Chris@17
|
120 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
|
Chris@0
|
121 * variables in the message text, not t(). If left blank, a default message
|
Chris@0
|
122 * will be displayed.
|
Chris@0
|
123 * @param string $group
|
Chris@0
|
124 * (optional) The group this message is in, which is displayed in a column
|
Chris@0
|
125 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@0
|
126 * translate this string. Defaults to 'Other'; most tests do not override
|
Chris@0
|
127 * this default.
|
Chris@0
|
128 *
|
Chris@0
|
129 * @return bool
|
Chris@0
|
130 * TRUE on pass, FALSE on fail.
|
Chris@0
|
131 */
|
Chris@0
|
132 protected function assertMailPattern($field_name, $regex, $message = '', $group = 'Other') {
|
Chris@0
|
133 $mails = $this->getMails();
|
Chris@0
|
134 $mail = end($mails);
|
Chris@0
|
135 $regex_found = preg_match("/$regex/", $mail[$field_name]);
|
Chris@0
|
136 if (!$message) {
|
Chris@0
|
137 $message = format_string('Expected text found in @field of email message: "@expected".', ['@field' => $field_name, '@expected' => $regex]);
|
Chris@0
|
138 }
|
Chris@0
|
139 return $this->assertTrue($regex_found, $message, $group);
|
Chris@0
|
140 }
|
Chris@0
|
141
|
Chris@0
|
142 /**
|
Chris@0
|
143 * Outputs to verbose the most recent $count emails sent.
|
Chris@0
|
144 *
|
Chris@0
|
145 * @param int $count
|
Chris@0
|
146 * Optional number of emails to output.
|
Chris@0
|
147 */
|
Chris@0
|
148 protected function verboseEmail($count = 1) {
|
Chris@0
|
149 $mails = $this->getMails();
|
Chris@0
|
150 for ($i = count($mails) - 1; $i >= count($mails) - $count && $i >= 0; $i--) {
|
Chris@0
|
151 $mail = $mails[$i];
|
Chris@0
|
152 $this->verbose('Email:<pre>' . print_r($mail, TRUE) . '</pre>');
|
Chris@0
|
153 }
|
Chris@0
|
154 }
|
Chris@0
|
155
|
Chris@0
|
156 }
|