Merge pull request #17473 from eileenmcnaughton/anet
[civicrm-core.git] / tests / phpunit / CiviTest / CiviMailUtils.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Mail utils for use during unit testing to allow retrieval
14 * and examination of 'sent' emails.
15 *
16 * Basic usage:
17 *
18 * $mut = new CiviMailUtils( $this, true ); //true automatically starts spooling
19 * ... do stuff ...
20 * $msg = $mut->getMostRecentEmail( 'raw' ); // or 'ezc' to get an ezc mail object
21 * ... assert stuff about $msg ...
22 * $mut->stop();
23 *
24 *
25 * @package CiviCRM
26 */
27
28 /**
29 * Class CiviMailUtils
30 */
31 class CiviMailUtils extends PHPUnit\Framework\TestCase {
32
33 /**
34 * Current outbound email option
35 * @var mixed
36 */
37 protected $_outBound_option = NULL;
38
39 /**
40 * Is this a webtest
41 * @var bool
42 */
43 protected $_webtest = FALSE;
44
45 /**
46 * Constructor.
47 *
48 * @param CiviSeleniumTestCase|CiviUnitTestCase $unit_test The currently running test
49 * @param bool $startImmediately
50 * Start writing to db now or wait until start() is called.
51 */
52 public function __construct(&$unit_test, $startImmediately = TRUE) {
53 $this->_ut = $unit_test;
54
55 // Check if running under webtests or not
56 if (is_subclass_of($unit_test, 'CiviSeleniumTestCase')) {
57 $this->_webtest = TRUE;
58 }
59
60 if ($startImmediately) {
61 $this->start();
62 }
63 }
64
65 /**
66 * Start writing emails to db instead of current option.
67 */
68 public function start() {
69 if ($this->_webtest) {
70 // Change outbound mail setting
71 $this->_ut->openCiviPage('admin/setting/smtp', "reset=1", "_qf_Smtp_next");
72
73 // First remember the current setting
74 $this->_outBound_option = $this->getSelectedOutboundOption();
75
76 $this->_ut->click('xpath=//input[@name="outBound_option" and @value="' . CRM_Mailing_Config::OUTBOUND_OPTION_REDIRECT_TO_DB . '"]');
77 $this->_ut->clickLink("_qf_Smtp_next");
78
79 // Is there supposed to be a status message displayed when outbound email settings are changed?
80 // assert something?
81
82 }
83 else {
84
85 // save current setting for outbound option, then change it
86 $mailingBackend = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME,
87 'mailing_backend'
88 );
89
90 $this->_outBound_option = $mailingBackend['outBound_option'];
91 $mailingBackend['outBound_option'] = CRM_Mailing_Config::OUTBOUND_OPTION_REDIRECT_TO_DB;
92
93 Civi::settings()->set('mailing_backend', $mailingBackend);
94
95 $mailingBackend = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME,
96 'mailing_backend'
97 );
98 }
99 }
100
101 public function stop() {
102 if ($this->_webtest) {
103 if ($this->_outBound_option != CRM_Mailing_Config::OUTBOUND_OPTION_REDIRECT_TO_DB) {
104 // Change outbound mail setting
105 $this->_ut->openCiviPage('admin/setting/smtp', "reset=1", "_qf_Smtp_next");
106 $this->_ut->click('xpath=//input[@name="outBound_option" and @value="' . $this->_outBound_option . '"]');
107 // There will be a warning when switching from test to live mode
108 if ($this->_outBound_option != CRM_Mailing_Config::OUTBOUND_OPTION_DISABLED) {
109 $this->_ut->getAlert();
110 }
111 $this->_ut->clickLink("_qf_Smtp_next");
112 }
113 }
114 else {
115
116 $mailingBackend = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME,
117 'mailing_backend'
118 );
119
120 $mailingBackend['outBound_option'] = $this->_outBound_option;
121
122 Civi::settings()->set('mailing_backend', $mailingBackend);
123 }
124 }
125
126 /**
127 * @param string $type
128 *
129 * @return ezcMail|string
130 */
131 public function getMostRecentEmail($type = 'raw') {
132 $msg = '';
133
134 if ($this->_webtest) {
135 // I don't understand but for some reason we have to load the page twice for a recent mailing to appear.
136 $this->_ut->openCiviPage('mailing/browse/archived', 'reset=1');
137 $this->_ut->openCiviPage('mailing/browse/archived', 'reset=1', 'css=td.crm-mailing-name');
138 }
139 // We can't fetch mailing headers from webtest so we'll only try if the format is raw
140 if ($this->_webtest && $type == 'raw') {
141 // This should select the first "Report" link in the table, which is sorted by Completion Date descending, so in theory is the most recent email. Not sure of a more robust way at the moment.
142 $this->_ut->clickLink('xpath=//tr[contains(@id, "crm-mailing_")]//a[text()="Report"]');
143
144 // Also not sure how robust this is, but there isn't a good
145 // identifier for this link either.
146 $this->_ut->waitForElementPresent('xpath=//a[contains(text(), "View complete message")]');
147 $this->_ut->clickAjaxLink('xpath=//a[contains(text(), "View complete message")]');
148 $msg = $this->_ut->getText('css=.ui-dialog-content.crm-ajax-container');
149 }
150 else {
151 $dao = CRM_Core_DAO::executeQuery('SELECT headers, body FROM civicrm_mailing_spool ORDER BY id DESC LIMIT 1');
152 if ($dao->fetch()) {
153 $msg = $dao->headers . "\n\n" . $dao->body;
154 }
155 }
156
157 switch ($type) {
158 case 'raw':
159 // nothing to do
160 break;
161
162 case 'ezc':
163 $msg = $this->convertToEzc($msg);
164 break;
165 }
166 return $msg;
167 }
168
169 /**
170 * @param string $type
171 * 'raw'|'ezc'.
172 *
173 * @throws CRM_Core_Exception
174 *
175 * @return array(ezcMail)|array(string)
176 */
177 public function getAllMessages($type = 'raw') {
178 $msgs = [];
179
180 if ($this->_webtest) {
181 throw new CRM_Core_Exception('Not implemented: getAllMessages for WebTest');
182 }
183 $dao = CRM_Core_DAO::executeQuery('SELECT headers, body FROM civicrm_mailing_spool ORDER BY id');
184 while ($dao->fetch()) {
185 $msgs[] = $dao->headers . "\n\n" . $dao->body;
186 }
187
188 switch ($type) {
189 case 'raw':
190 // nothing to do
191 break;
192
193 case 'ezc':
194 foreach ($msgs as $i => $msg) {
195 $msgs[$i] = $this->convertToEzc($msg);
196 }
197 break;
198 }
199
200 return $msgs;
201 }
202
203 /**
204 * @return int
205 */
206 public function getSelectedOutboundOption() {
207 $selectedOption = CRM_Mailing_Config::OUTBOUND_OPTION_MAIL;
208 // Is there a better way to do this? How do you get the currently selected value of a radio button in selenium?
209 for ($i = 0; $i <= 5; $i++) {
210 if ($i != CRM_Mailing_Config::OUTBOUND_OPTION_MOCK) {
211 if ($this->_ut->getValue('xpath=//input[@name="outBound_option" and @value="' . $i . '"]') == "on") {
212 $selectedOption = $i;
213 break;
214 }
215 }
216 }
217 return $selectedOption;
218 }
219
220 /*
221 * Utility functions (previously part of CiviUnitTestCase)
222 * Included for backward compatibility with existing tests.
223 */
224
225 /**
226 * Check contents of mail log.
227 *
228 * @param array $strings
229 * Strings that should be included.
230 * @param array $absentStrings
231 * Strings that should not be included.
232 * @param string $prefix
233 *
234 * @return \ezcMail|string
235 */
236 public function checkMailLog($strings, $absentStrings = [], $prefix = '') {
237 $mail = $this->getMostRecentEmail('raw');
238 return $this->checkMailForStrings($strings, $absentStrings, $prefix, $mail);
239 }
240
241 /**
242 * Check contents of mail log.
243 *
244 * @param array $strings
245 * Strings that should be included.
246 * @param array $absentStrings
247 * Strings that should not be included.
248 * @param string $prefix
249 *
250 * @return \ezcMail|string
251 */
252 public function checkAllMailLog($strings, $absentStrings = [], $prefix = '') {
253 $mails = $this->getAllMessages('raw');
254 $mail = implode(',', $mails);
255 return $this->checkMailForStrings($strings, $absentStrings, $prefix, $mail);
256 }
257
258 /**
259 * Check that mail log is empty.
260 * @param string $prefix
261 */
262 public function assertMailLogEmpty($prefix = '') {
263 $mail = $this->getMostRecentEmail('raw');
264 $this->_ut->assertEmpty($mail, 'mail sent when it should not have been ' . $prefix);
265 }
266
267 /**
268 * Assert that $expectedRecipients (and no else) have received emails
269 *
270 * @param array $expectedRecipients
271 * Array($msgPos => array($recipPos => $emailAddr)).
272 */
273 public function assertRecipients($expectedRecipients) {
274 $recipients = [];
275 foreach ($this->getAllMessages('ezc') as $message) {
276 $recipients[] = CRM_Utils_Array::collect('email', $message->to);
277 }
278 $cmp = function($a, $b) {
279 if ($a[0] == $b[0]) {
280 return 0;
281 }
282 return ($a[0] < $b[0]) ? 1 : -1;
283 };
284 usort($recipients, $cmp);
285 usort($expectedRecipients, $cmp);
286 $this->_ut->assertEquals(
287 $expectedRecipients,
288 $recipients,
289 "Incorrect recipients: " . print_r(array('expected' => $expectedRecipients, 'actual' => $recipients), TRUE)
290 );
291 }
292
293 /**
294 * Assert that $expectedSubjects (and no other subjects) were sent.
295 *
296 * @param array $expectedSubjects
297 * Array(string $subj).
298 */
299 public function assertSubjects($expectedSubjects) {
300 $subjects = [];
301 foreach ($this->getAllMessages('ezc') as $message) {
302 /** @var ezcMail $message */
303 $subjects[] = $message->subject;
304 }
305 sort($subjects);
306 sort($expectedSubjects);
307 $this->_ut->assertEquals(
308 $expectedSubjects,
309 $subjects,
310 "Incorrect subjects: " . print_r(array('expected' => $expectedSubjects, 'actual' => $subjects), TRUE)
311 );
312 }
313
314 /**
315 * Remove any sent messages from the log.
316 *
317 * @param int $limit
318 * How many recent messages to remove, defaults to 0 (all).
319 *
320 * @throws \CRM_Core_Exception
321 */
322 public function clearMessages($limit = 0) {
323 if ($this->_webtest) {
324 throw new \CRM_Core_Exception("Not implemented: clearMessages for WebTest");
325 }
326 else {
327 $sql = 'DELETE FROM civicrm_mailing_spool ORDER BY id DESC';
328 if ($limit) {
329 $sql .= ' LIMIT ' . $limit;
330 }
331 CRM_Core_DAO::executeQuery($sql);
332 }
333 }
334
335 /**
336 * @param string $msg
337 * Email header and body.
338 * @return ezcMail
339 */
340 private function convertToEzc($msg) {
341 $set = new ezcMailVariableSet($msg);
342 $parser = new ezcMailParser();
343 $mail = $parser->parseMail($set);
344 $this->_ut->assertNotEmpty($mail, 'Cannot parse mail');
345 return $mail[0];
346 }
347
348 /**
349 * @param $strings
350 * @param $absentStrings
351 * @param $prefix
352 * @param $mail
353 * @return mixed
354 */
355 public function checkMailForStrings($strings, $absentStrings, $prefix, $mail) {
356 foreach ($strings as $string) {
357 $this->_ut->assertContains($string, $mail, "$string . not found in $mail $prefix");
358 }
359 foreach ($absentStrings as $string) {
360 $this->_ut->assertEmpty(strstr($mail, $string), "$string incorrectly found in $mail $prefix");
361 }
362 return $mail;
363 }
364
365 }