3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 $unit_test CiviSeleniumTestCase The currently running test
65 * @param $startImmediately bool Start writing to db now or wait until start() is called
67 function __construct(&$unit_test, $startImmediately = TRUE) {
68 // Check if running under webtests or not
69 if (is_subclass_of($unit_test, 'CiviSeleniumTestCase')) {
70 $this->_ut
= $unit_test;
71 $this->_webtest
= TRUE;
74 if ($startImmediately) {
80 * Start writing emails to db instead of current option
83 if ($this->_webtest
) {
84 // Change outbound mail setting
85 $this->_ut
->openCiviPage('admin/setting/smtp', "reset=1", "_qf_Smtp_next");
87 // First remember the current setting
88 $this->_outBound_option
= $this->getSelectedOutboundOption();
90 $this->_ut
->click('xpath=//input[@name="outBound_option" and @value="' . CRM_Mailing_Config
::OUTBOUND_OPTION_REDIRECT_TO_DB
. '"]');
91 $this->_ut
->clickLink("_qf_Smtp_next");
93 // Is there supposed to be a status message displayed when outbound email settings are changed?
99 // save current setting for outbound option, then change it
100 $mailingBackend = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MAILING_PREFERENCES_NAME
,
104 $this->_outBound_option
= $mailingBackend['outBound_option'];
105 $mailingBackend['outBound_option'] = CRM_Mailing_Config
::OUTBOUND_OPTION_REDIRECT_TO_DB
;
107 CRM_Core_BAO_Setting
::setItem($mailingBackend,
108 CRM_Core_BAO_Setting
::MAILING_PREFERENCES_NAME
,
112 $mailingBackend = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MAILING_PREFERENCES_NAME
,
119 if ($this->_webtest
) {
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 $this->_ut
->clickLink("_qf_Smtp_next");
125 // Is there supposed to be a status message displayed when outbound email settings are changed?
131 $mailingBackend = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MAILING_PREFERENCES_NAME
,
135 $mailingBackend['outBound_option'] = $this->_outBound_option
;
137 CRM_Core_BAO_Setting
::setItem($mailingBackend,
138 CRM_Core_BAO_Setting
::MAILING_PREFERENCES_NAME
,
145 * @param string $type
147 * @return ezcMail|string
149 function getMostRecentEmail($type = 'raw') {
152 if ($this->_webtest
) {
153 // I don't understand but for some reason we have to load the page twice for a recent mailing to appear.
154 $this->_ut
->openCiviPage('mailing/browse/archived', 'reset=1');
155 $this->_ut
->openCiviPage('mailing/browse/archived', 'reset=1', 'css=td.crm-mailing-name');
157 // We can't fetch mailing headers from webtest so we'll only try if the format is raw
158 if ($this->_webtest
&& $type == 'raw') {
159 // 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.
160 $this->_ut
->clickLink('xpath=//tr[contains(@id, "crm-mailing_")]//a[text()="Report"]');
162 // Also not sure how robust this is, but there isn't a good
163 // identifier for this link either.
164 $this->_ut
->waitForElementPresent('xpath=//a[contains(text(), "View complete message")]');
165 $this->_ut
->clickAjaxLink('xpath=//a[contains(text(), "View complete message")]', NULL);
166 $msg = $this->_ut
->getText('css=.ui-dialog-content.crm-ajax-container');
169 $dao = CRM_Core_DAO
::executeQuery('SELECT headers, body FROM civicrm_mailing_spool ORDER BY id DESC LIMIT 1');
171 $msg = $dao->headers
. "\n\n" . $dao->body
;
181 $msg = $this->convertToEzc($msg);
188 * @param string $type 'raw'|'ezc'
191 * @return array(ezcMail)|array(string)
193 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
;
212 foreach ($msgs as $i => $msg) {
213 $msgs[$i] = $this->convertToEzc($msg);
224 function getSelectedOutboundOption() {
225 $selectedOption = CRM_Mailing_Config
::OUTBOUND_OPTION_MAIL
;
226 // Is there a better way to do this? How do you get the currently selected value of a radio button in selenium?
227 for ($i = 0; $i <= 5; $i++
) {
228 if ($i != CRM_Mailing_Config
::OUTBOUND_OPTION_MOCK
) {
229 if ($this->_ut
->getValue('xpath=//input[@name="outBound_option" and @value="' . $i . '"]') == "on") {
230 $selectedOption = $i;
235 return $selectedOption;
239 * Utility functions (previously part of CiviUnitTestCase)
240 * Included for backward compatibility with existing tests.
244 * Check contents of mail log
246 * @param array $strings strings that should be included
247 * @param array $absentStrings strings that should not be included
248 * @param string $prefix
250 * @return \ezcMail|string
252 function checkMailLog($strings, $absentStrings = array(), $prefix = '') {
253 $mail = $this->getMostRecentEmail('raw');
254 foreach ($strings as $string) {
255 $this->_ut
->assertContains($string, $mail, "$string . not found in $mail $prefix");
257 foreach ($absentStrings as $string) {
258 $this->_ut
->assertEmpty(strstr($mail, $string), "$string incorrectly found in $mail $prefix");
265 * Check that mail log is empty
267 function assertMailLogEmpty($prefix = '') {
268 $mail = $this->getMostRecentEmail('raw');
269 $this->_ut
->assertEmpty($mail, 'mail sent when it should not have been ' . $prefix);
273 * Assert that $expectedRecipients (and no else) have received emails
275 * @param array $expectedRecipients array($msgPos => array($recipPos => $emailAddr))
277 function assertRecipients($expectedRecipients) {
278 $recipients = array();
279 foreach ($this->getAllMessages('ezc') as $message) {
280 $recipients[] = CRM_Utils_Array
::collect('email', $message->to
);
283 sort($expectedRecipients);
284 $this->_ut
->assertEquals(
287 "Incorrect recipients: " . print_r(array('expected' => $expectedRecipients, 'actual' => $recipients), TRUE)
292 * Remove any sent messages from the log
294 function clearMessages() {
295 if ($this->_webtest
) {
296 throw new Exception("Not implementated: clearMessages for WebTest");
299 CRM_Core_DAO
::executeQuery('DELETE FROM civicrm_mailing_spool ORDER BY id DESC LIMIT 1');
304 * @param string $msg email header and body
307 private function convertToEzc($msg) {
308 $set = new ezcMailVariableSet($msg);
309 $parser = new ezcMailParser();
310 $mail = $parser->parseMail($set);
311 $this->_ut
->assertNotEmpty($mail, 'Cannot parse mail');