3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 Start writing to db now or wait until start() is called
67 public function __construct(&$unit_test, $startImmediately = TRUE) {
68 $this->_ut
= $unit_test;
70 // Check if running under webtests or not
71 if (is_subclass_of($unit_test, 'CiviSeleniumTestCase')) {
72 $this->_webtest
= TRUE;
75 if ($startImmediately) {
81 * Start writing emails to db instead of current option
83 public function start() {
84 if ($this->_webtest
) {
85 // Change outbound mail setting
86 $this->_ut
->openCiviPage('admin/setting/smtp', "reset=1", "_qf_Smtp_next");
88 // First remember the current setting
89 $this->_outBound_option
= $this->getSelectedOutboundOption();
91 $this->_ut
->click('xpath=//input[@name="outBound_option" and @value="' . CRM_Mailing_Config
::OUTBOUND_OPTION_REDIRECT_TO_DB
. '"]');
92 $this->_ut
->clickLink("_qf_Smtp_next");
94 // Is there supposed to be a status message displayed when outbound email settings are changed?
100 // save current setting for outbound option, then change it
101 $mailingBackend = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MAILING_PREFERENCES_NAME
,
105 $this->_outBound_option
= $mailingBackend['outBound_option'];
106 $mailingBackend['outBound_option'] = CRM_Mailing_Config
::OUTBOUND_OPTION_REDIRECT_TO_DB
;
108 CRM_Core_BAO_Setting
::setItem($mailingBackend,
109 CRM_Core_BAO_Setting
::MAILING_PREFERENCES_NAME
,
113 $mailingBackend = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MAILING_PREFERENCES_NAME
,
119 public function stop() {
120 if ($this->_webtest
) {
121 if ($this->_outBound_option
!= CRM_Mailing_Config
::OUTBOUND_OPTION_REDIRECT_TO_DB
) {
122 // Change outbound mail setting
123 $this->_ut
->openCiviPage('admin/setting/smtp', "reset=1", "_qf_Smtp_next");
124 $this->_ut
->click('xpath=//input[@name="outBound_option" and @value="' . $this->_outBound_option
. '"]');
125 // There will be a warning when switching from test to live mode
126 if ($this->_outBound_option
!= CRM_Mailing_Config
::OUTBOUND_OPTION_DISABLED
) {
127 $this->_ut
->getAlert();
129 $this->_ut
->clickLink("_qf_Smtp_next");
134 $mailingBackend = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MAILING_PREFERENCES_NAME
,
138 $mailingBackend['outBound_option'] = $this->_outBound_option
;
140 CRM_Core_BAO_Setting
::setItem($mailingBackend,
141 CRM_Core_BAO_Setting
::MAILING_PREFERENCES_NAME
,
148 * @param string $type
150 * @return ezcMail|string
152 public function getMostRecentEmail($type = 'raw') {
155 if ($this->_webtest
) {
156 // I don't understand but for some reason we have to load the page twice for a recent mailing to appear.
157 $this->_ut
->openCiviPage('mailing/browse/archived', 'reset=1');
158 $this->_ut
->openCiviPage('mailing/browse/archived', 'reset=1', 'css=td.crm-mailing-name');
160 // We can't fetch mailing headers from webtest so we'll only try if the format is raw
161 if ($this->_webtest
&& $type == 'raw') {
162 // 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.
163 $this->_ut
->clickLink('xpath=//tr[contains(@id, "crm-mailing_")]//a[text()="Report"]');
165 // Also not sure how robust this is, but there isn't a good
166 // identifier for this link either.
167 $this->_ut
->waitForElementPresent('xpath=//a[contains(text(), "View complete message")]');
168 $this->_ut
->clickAjaxLink('xpath=//a[contains(text(), "View complete message")]');
169 $msg = $this->_ut
->getText('css=.ui-dialog-content.crm-ajax-container');
172 $dao = CRM_Core_DAO
::executeQuery('SELECT headers, body FROM civicrm_mailing_spool ORDER BY id DESC LIMIT 1');
174 $msg = $dao->headers
. "\n\n" . $dao->body
;
184 $msg = $this->convertToEzc($msg);
191 * @param string $type 'raw'|'ezc'
194 * @return array(ezcMail)|array(string)
196 public function getAllMessages($type = 'raw') {
199 if ($this->_webtest
) {
200 throw new Exception("Not implementated: getAllMessages for WebTest");
203 $dao = CRM_Core_DAO
::executeQuery('SELECT headers, body FROM civicrm_mailing_spool ORDER BY id');
204 while ($dao->fetch()) {
205 $msgs[] = $dao->headers
. "\n\n" . $dao->body
;
215 foreach ($msgs as $i => $msg) {
216 $msgs[$i] = $this->convertToEzc($msg);
227 public function getSelectedOutboundOption() {
228 $selectedOption = CRM_Mailing_Config
::OUTBOUND_OPTION_MAIL
;
229 // Is there a better way to do this? How do you get the currently selected value of a radio button in selenium?
230 for ($i = 0; $i <= 5; $i++
) {
231 if ($i != CRM_Mailing_Config
::OUTBOUND_OPTION_MOCK
) {
232 if ($this->_ut
->getValue('xpath=//input[@name="outBound_option" and @value="' . $i . '"]') == "on") {
233 $selectedOption = $i;
238 return $selectedOption;
242 * Utility functions (previously part of CiviUnitTestCase)
243 * Included for backward compatibility with existing tests.
247 * Check contents of mail log
249 * @param array $strings strings that should be included
250 * @param array $absentStrings 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 foreach ($strings as $string) {
258 $this->_ut
->assertContains($string, $mail, "$string . not found in $mail $prefix");
260 foreach ($absentStrings as $string) {
261 $this->_ut
->assertEmpty(strstr($mail, $string), "$string incorrectly found in $mail $prefix");
268 * Check that mail log is empty
270 public function assertMailLogEmpty($prefix = '') {
271 $mail = $this->getMostRecentEmail('raw');
272 $this->_ut
->assertEmpty($mail, 'mail sent when it should not have been ' . $prefix);
276 * Assert that $expectedRecipients (and no else) have received emails
278 * @param array $expectedRecipients array($msgPos => array($recipPos => $emailAddr))
280 public function assertRecipients($expectedRecipients) {
281 $recipients = array();
282 foreach ($this->getAllMessages('ezc') as $message) {
283 $recipients[] = CRM_Utils_Array
::collect('email', $message->to
);
286 sort($expectedRecipients);
287 $this->_ut
->assertEquals(
290 "Incorrect recipients: " . print_r(array('expected' => $expectedRecipients, 'actual' => $recipients), TRUE)
295 * Remove any sent messages from the log
297 public function clearMessages() {
298 if ($this->_webtest
) {
299 throw new Exception("Not implementated: clearMessages for WebTest");
302 CRM_Core_DAO
::executeQuery('DELETE FROM civicrm_mailing_spool ORDER BY id DESC LIMIT 1');
307 * @param string $msg email header and body
310 private function convertToEzc($msg) {
311 $set = new ezcMailVariableSet($msg);
312 $parser = new ezcMailParser();
313 $mail = $parser->parseMail($set);
314 $this->_ut
->assertNotEmpty($mail, 'Cannot parse mail');