3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License along with this program; if not, contact CiviCRM LLC |
21 | at info[AT]civicrm[DOT]org. If you have questions about the |
22 | GNU Affero General Public License or the licensing of CiviCRM, |
23 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
24 +--------------------------------------------------------------------+
28 * Mail utils for use during unit testing to allow retrieval
29 * and examination of 'sent' emails.
33 * $mut = new CiviMailUtils( $this, true ); //true automatically starts spooling
35 * $msg = $mut->getMostRecentEmail( 'raw' ); // or 'ezc' to get an ezc mail object
36 * ... assert stuff about $msg ...
43 require_once 'ezc/Base/src/ezc_bootstrap.php';
44 require_once 'ezc/autoload/mail_autoload.php';
49 class CiviMailUtils
extends PHPUnit_Framework_TestCase
{
52 * @var mixed current outbound email option
54 protected $_outBound_option = NULL;
57 * @var bool is this a webtest
59 protected $_webtest = FALSE;
64 * @param CiviSeleniumTestCase|CiviUnitTestCase $unit_test The currently running test
65 * @param bool $startImmediately
66 * Start writing to db now or wait until start() is called.
68 public function __construct(&$unit_test, $startImmediately = TRUE) {
69 $this->_ut
= $unit_test;
71 // Check if running under webtests or not
72 if (is_subclass_of($unit_test, 'CiviSeleniumTestCase')) {
73 $this->_webtest
= TRUE;
76 if ($startImmediately) {
82 * Start writing emails to db instead of current option.
84 public function start() {
85 if ($this->_webtest
) {
86 // Change outbound mail setting
87 $this->_ut
->openCiviPage('admin/setting/smtp', "reset=1", "_qf_Smtp_next");
89 // First remember the current setting
90 $this->_outBound_option
= $this->getSelectedOutboundOption();
92 $this->_ut
->click('xpath=//input[@name="outBound_option" and @value="' . CRM_Mailing_Config
::OUTBOUND_OPTION_REDIRECT_TO_DB
. '"]');
93 $this->_ut
->clickLink("_qf_Smtp_next");
95 // Is there supposed to be a status message displayed when outbound email settings are changed?
101 // save current setting for outbound option, then change it
102 $mailingBackend = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MAILING_PREFERENCES_NAME
,
106 $this->_outBound_option
= $mailingBackend['outBound_option'];
107 $mailingBackend['outBound_option'] = CRM_Mailing_Config
::OUTBOUND_OPTION_REDIRECT_TO_DB
;
109 Civi
::settings()->set('mailing_backend', $mailingBackend);
111 $mailingBackend = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MAILING_PREFERENCES_NAME
,
117 public function stop() {
118 if ($this->_webtest
) {
119 if ($this->_outBound_option
!= CRM_Mailing_Config
::OUTBOUND_OPTION_REDIRECT_TO_DB
) {
120 // Change outbound mail setting
121 $this->_ut
->openCiviPage('admin/setting/smtp', "reset=1", "_qf_Smtp_next");
122 $this->_ut
->click('xpath=//input[@name="outBound_option" and @value="' . $this->_outBound_option
. '"]');
123 // There will be a warning when switching from test to live mode
124 if ($this->_outBound_option
!= CRM_Mailing_Config
::OUTBOUND_OPTION_DISABLED
) {
125 $this->_ut
->getAlert();
127 $this->_ut
->clickLink("_qf_Smtp_next");
132 $mailingBackend = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MAILING_PREFERENCES_NAME
,
136 $mailingBackend['outBound_option'] = $this->_outBound_option
;
138 Civi
::settings()->set('mailing_backend', $mailingBackend);
143 * @param string $type
145 * @return ezcMail|string
147 public function getMostRecentEmail($type = 'raw') {
150 if ($this->_webtest
) {
151 // I don't understand but for some reason we have to load the page twice for a recent mailing to appear.
152 $this->_ut
->openCiviPage('mailing/browse/archived', 'reset=1');
153 $this->_ut
->openCiviPage('mailing/browse/archived', 'reset=1', 'css=td.crm-mailing-name');
155 // We can't fetch mailing headers from webtest so we'll only try if the format is raw
156 if ($this->_webtest
&& $type == 'raw') {
157 // 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.
158 $this->_ut
->clickLink('xpath=//tr[contains(@id, "crm-mailing_")]//a[text()="Report"]');
160 // Also not sure how robust this is, but there isn't a good
161 // identifier for this link either.
162 $this->_ut
->waitForElementPresent('xpath=//a[contains(text(), "View complete message")]');
163 $this->_ut
->clickAjaxLink('xpath=//a[contains(text(), "View complete message")]');
164 $msg = $this->_ut
->getText('css=.ui-dialog-content.crm-ajax-container');
167 $dao = CRM_Core_DAO
::executeQuery('SELECT headers, body FROM civicrm_mailing_spool ORDER BY id DESC LIMIT 1');
169 $msg = $dao->headers
. "\n\n" . $dao->body
;
180 $msg = $this->convertToEzc($msg);
187 * @param string $type
191 * @return array(ezcMail)|array(string)
193 public function getAllMessages($type = 'raw') {
196 if ($this->_webtest
) {
197 throw new Exception("Not implementated: getAllMessages for WebTest");
200 $dao = CRM_Core_DAO
::executeQuery('SELECT headers, body FROM civicrm_mailing_spool ORDER BY id');
201 while ($dao->fetch()) {
202 $msgs[] = $dao->headers
. "\n\n" . $dao->body
;
213 foreach ($msgs as $i => $msg) {
214 $msgs[$i] = $this->convertToEzc($msg);
225 public function getSelectedOutboundOption() {
226 $selectedOption = CRM_Mailing_Config
::OUTBOUND_OPTION_MAIL
;
227 // Is there a better way to do this? How do you get the currently selected value of a radio button in selenium?
228 for ($i = 0; $i <= 5; $i++
) {
229 if ($i != CRM_Mailing_Config
::OUTBOUND_OPTION_MOCK
) {
230 if ($this->_ut
->getValue('xpath=//input[@name="outBound_option" and @value="' . $i . '"]') == "on") {
231 $selectedOption = $i;
236 return $selectedOption;
240 * Utility functions (previously part of CiviUnitTestCase)
241 * Included for backward compatibility with existing tests.
245 * Check contents of mail log.
247 * @param array $strings
248 * Strings that should be included.
249 * @param array $absentStrings
250 * Strings that should not be included.
251 * @param string $prefix
253 * @return \ezcMail|string
255 public function checkMailLog($strings, $absentStrings = array(), $prefix = '') {
256 $mail = $this->getMostRecentEmail('raw');
257 return $this->checkMailForStrings($strings, $absentStrings, $prefix, $mail);
261 * Check contents of mail log.
263 * @param array $strings
264 * Strings that should be included.
265 * @param array $absentStrings
266 * Strings that should not be included.
267 * @param string $prefix
269 * @return \ezcMail|string
271 public function checkAllMailLog($strings, $absentStrings = array(), $prefix = '') {
272 $mails = $this->getAllMessages('raw');
273 $mail = implode(',', $mails);
274 return $this->checkMailForStrings($strings, $absentStrings, $prefix, $mail);
278 * Check that mail log is empty.
279 * @param string $prefix
281 public function assertMailLogEmpty($prefix = '') {
282 $mail = $this->getMostRecentEmail('raw');
283 $this->_ut
->assertEmpty($mail, 'mail sent when it should not have been ' . $prefix);
287 * Assert that $expectedRecipients (and no else) have received emails
289 * @param array $expectedRecipients
290 * Array($msgPos => array($recipPos => $emailAddr)).
292 public function assertRecipients($expectedRecipients) {
293 $recipients = array();
294 foreach ($this->getAllMessages('ezc') as $message) {
295 $recipients[] = CRM_Utils_Array
::collect('email', $message->to
);
297 $cmp = function($a, $b) {
298 if ($a[0] == $b[0]) {
301 return ($a[0] < $b[0]) ?
1 : -1;
303 usort($recipients, $cmp);
304 usort($expectedRecipients, $cmp);
305 $this->_ut
->assertEquals(
308 "Incorrect recipients: " . print_r(array('expected' => $expectedRecipients, 'actual' => $recipients), TRUE)
313 * Assert that $expectedSubjects (and no other subjects) were sent.
315 * @param array $expectedSubjects
316 * Array(string $subj).
318 public function assertSubjects($expectedSubjects) {
320 foreach ($this->getAllMessages('ezc') as $message) {
321 /** @var ezcMail $message */
322 $subjects[] = $message->subject
;
325 sort($expectedSubjects);
326 $this->_ut
->assertEquals(
329 "Incorrect subjects: " . print_r(array('expected' => $expectedSubjects, 'actual' => $subjects), TRUE)
334 * Remove any sent messages from the log.
340 public function clearMessages($limit = 1) {
341 if ($this->_webtest
) {
342 throw new Exception("Not implementated: clearMessages for WebTest");
345 CRM_Core_DAO
::executeQuery('DELETE FROM civicrm_mailing_spool ORDER BY id DESC LIMIT ' . $limit);
351 * Email header and body.
354 private function convertToEzc($msg) {
355 $set = new ezcMailVariableSet($msg);
356 $parser = new ezcMailParser();
357 $mail = $parser->parseMail($set);
358 $this->_ut
->assertNotEmpty($mail, 'Cannot parse mail');
364 * @param $absentStrings
369 public function checkMailForStrings($strings, $absentStrings, $prefix, $mail) {
370 foreach ($strings as $string) {
371 $this->_ut
->assertContains($string, $mail, "$string . not found in $mail $prefix");
373 foreach ($absentStrings as $string) {
374 $this->_ut
->assertEmpty(strstr($mail, $string), "$string incorrectly found in $mail $prefix");;