3 * File for the TestMailing class
9 * This file is part of CiviCRM
11 * CiviCRM is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Affero General Public License
13 * as published by the Free Software Foundation; either version 3 of
14 * the License, or (at your option) any later version.
16 * CiviCRM is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Affero General Public License for more details.
21 * You should have received a copy of the GNU Affero General Public
22 * License along with this program. If not, see
23 * <http://www.gnu.org/licenses/>.
26 require_once 'CiviTest/CiviUnitTestCase.php';
30 * Test APIv3 civicrm_mailing_* functions
34 class api_v3_MailingTest
extends CiviUnitTestCase
{
37 protected $_apiversion = 3;
38 protected $_params = array();
39 protected $_entity = 'Mailing';
40 protected $_groupIDs; // array(string $pseudonym => int $id)
41 protected $_contactIDs; // array(string $pseudonym => int $id)
49 'description' => 'Test all Mailer methods.',
50 'group' => 'CiviCRM API Tests',
56 $this->_contactIDs
= array();
57 $this->_groupID
= $this->groupCreate();
58 $this->_groupIDs
= array();
59 $this->_email
= 'test@test.test';
60 $this->_params
= array(
62 'body_text' => "This is {contact.display_name}",
63 'name' => 'mailing name',
69 foreach ($this->_contactIDs
as $contactID) {
70 $this->contactDelete($contactID);
72 $this->groupDelete($this->_groupID
);
73 foreach ($this->_groupIDs
as $groupID) {
74 $this->groupDelete($groupID);
79 * Test civicrm_mailing_create
81 public function testMailerCreateSuccess() {
82 $result = $this->callAPIAndDocument('mailing', 'create', $this->_params
, __FUNCTION__
, __FILE__
);
83 $jobs = $this->callAPISuccess('mailing_job', 'get', array('mailing_id' => $result['id']));
84 $this->assertEquals(1, $jobs['count']);
85 unset($this->_params
['created_id']); // return isn't working on this in getAndCheck so lets not check it for now
86 $this->getAndCheck($this->_params
, $result['id'], 'mailing');
89 public function testMailerPreview() {
90 $contactID = $this->individualCreate();
91 $displayName = $this->callAPISuccess('contact', 'get', array('id' => $contactID));
92 $displayName = $displayName['values'][$contactID]['display_name'];
94 $result = $this->callAPISuccess('mailing', 'create', $this->_params
);
96 $params = array('id' => $result['id'], 'contact_id' => $contactID);
97 $result = $this->callAPISuccess('mailing', 'preview', $params);
98 $text = $result['values']['text'];
99 $this->assertEquals("This is $displayName", $text); // verify the text returned is correct, with replaced token
100 $this->deleteMailing($result['id']);
103 public function testMailerPreviewRecipients() {
105 $this->groupIDs
['inc'] = $this->groupCreate(array('name' => 'Example include group', 'title' => 'Example include group'));
106 $this->groupIDs
['exc'] = $this->groupCreate(array('name' => 'Example exclude group', 'title' => 'Example exclude group'));
107 $this->contactIDs
['includeme'] = $this->individualCreate(array('include.me@example.org'));
108 $this->contactIDs
['excludeme'] = $this->individualCreate(array('exclude.me@example.org'));
109 $this->callAPISuccess('GroupContact', 'create', array('group_id' => $this->groupIDs
['inc'], 'contact_id' => $this->contactIDs
['includeme']));
110 $this->callAPISuccess('GroupContact', 'create', array('group_id' => $this->groupIDs
['inc'], 'contact_id' => $this->contactIDs
['excludeme']));
111 $this->callAPISuccess('GroupContact', 'create', array('group_id' => $this->groupIDs
['exc'], 'contact_id' => $this->contactIDs
['excludeme']));
113 $params = $this->_params
;
114 $params['groups']['include'] = array($this->groupIDs
['inc']);
115 $params['groups']['exclude'] = array($this->groupIDs
['exc']);
116 $params['mailings']['include'] = array();
117 $params['mailings']['exclude'] = array();
121 'mailing' => CRM_Core_DAO
::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing'),
122 'job' => CRM_Core_DAO
::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_job'),
123 'group' => CRM_Core_DAO
::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_group'),
125 $preview = $this->callAPIAndDocument('Mailing', 'preview_recipients', $params, __FUNCTION__
, __FILE__
);
126 $this->assertDBQuery($maxIDs['mailing'], 'SELECT MAX(id) FROM civicrm_mailing'); // 'Preview should not create any mailing records'
127 $this->assertDBQuery($maxIDs['job'], 'SELECT MAX(id) FROM civicrm_mailing_job'); // 'Preview should not create any mailing_job record'
128 $this->assertDBQuery($maxIDs['group'], 'SELECT MAX(id) FROM civicrm_mailing_group'); // 'Preview should not create any mailing_group records'
130 $previewIds = array_values(CRM_Utils_Array
::collect('contact_id', $preview['values']));
131 $this->assertEquals(array((string)$this->contactIDs
['includeme']), $previewIds);
134 public function testMailerSendTestMail() {
135 $contactID = $this->individualCreate();
136 $result = $this->callAPISuccess('contact', 'get', array('id' => $contactID));
137 $email = $result['values'][$contactID]['email'];
139 $mail = $this->callAPISuccess('mailing', 'create', $this->_params
);
141 $params = array('mailing_id' => $mail['id'], 'test_email' => $email, 'test_group' => NULL);
142 $deliveredInfo = $this->callAPISuccess($this->_entity
, 'send_test', $params);
143 $this->assertEquals(1, $deliveredInfo['count'], "in line " . __LINE__
); // verify mail has been sent to user by count
144 $this->assertEquals($contactID, $deliveredInfo['values'][$deliveredInfo['id']]['contact_id'], "in line " . __LINE__
); //verify the contact_id of the recipient
145 $this->deleteMailing($mail['id']);
148 public function testMailerStats() {
149 $result = $this->groupContactCreate($this->_groupID
, 100);
150 $this->assertEquals(100, $result['added']); //verify if 100 contacts are added for group
152 //Create and send test mail first and change the mail job to live,
153 //because stats api only works on live mail
154 $mail = $this->callAPISuccess('mailing', 'create', $this->_params
);
155 $params = array('mailing_id' => $mail['id'], 'test_email' => NULL, 'test_group' => $this->_groupID
);
156 $deliveredInfo = $this->callAPISuccess($this->_entity
, 'send_test', $params);
157 $deliveredIds = implode(',', array_keys($deliveredInfo['values']));
159 //Change the test mail into live
160 $sql = "UPDATE civicrm_mailing_job SET is_test = 0 WHERE mailing_id = {$mail['id']}";
161 CRM_Core_DAO
::executeQuery($sql);
163 foreach (array('bounce', 'unsubscribe', 'opened') as $type) {
164 $sql = "CREATE TEMPORARY TABLE mail_{$type}_temp
165 (event_queue_id int, time_stamp datetime, delivered_id int)
166 SELECT event_queue_id, time_stamp, id
167 FROM civicrm_mailing_event_delivered
168 WHERE id IN ($deliveredIds)
169 ORDER BY RAND() LIMIT 0,20;";
170 CRM_Core_DAO
::executeQuery($sql);
172 $sql = "DELETE FROM civicrm_mailing_event_delivered WHERE id IN (SELECT delivered_id FROM mail_{$type}_temp);";
173 CRM_Core_DAO
::executeQuery($sql);
175 if ($type == 'unsubscribe') {
176 $sql = "INSERT INTO civicrm_mailing_event_{$type} (event_queue_id, time_stamp, org_unsubscribe)
177 SELECT event_queue_id, time_stamp, 1 FROM mail_{$type}_temp";
180 $sql = "INSERT INTO civicrm_mailing_event_{$type} (event_queue_id, time_stamp)
181 SELECT event_queue_id, time_stamp FROM mail_{$type}_temp";
183 CRM_Core_DAO
::executeQuery($sql);
186 $result = $this->callAPISuccess('mailing', 'stats', array('mailing_id' => $mail['id']));
187 $expectedResult = array(
188 'Delivered' => 80, //since among 100 mails 20 has been bounced
191 'Unique Clicks' => 0,
192 'Unsubscribers' => 20
194 $this->checkArrayEquals($expectedResult, $result['values'][$mail['id']]);
195 $this->deleteMailing($mail['id']);
198 * Test civicrm_mailing_delete
200 public function testMailerDeleteSuccess() {
201 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
202 $jobs = $this->callAPIAndDocument($this->_entity
, 'delete', array('id' => $result['id']), __FUNCTION__
, __FILE__
);
203 $this->assertAPIDeleted($this->_entity
, $result['id']);
206 //@ todo tests below here are all failure tests which are not hugely useful - need success tests
208 //------------ civicrm_mailing_event_bounce methods------------
211 * Test civicrm_mailing_event_bounce with wrong params.
212 * Note that tests like this are slightly better than no test but an
213 * api function cannot be considered supported / 'part of the api' without a
216 public function testMailerBounceWrongParams() {
218 'job_id' => 'Wrong ID',
219 'event_queue_id' => 'Wrong ID',
220 'hash' => 'Wrong Hash',
222 'time_stamp' => '20111109212100',
224 $result = $this->callAPIFailure('mailing_event', 'bounce', $params,
225 'Queue event could not be found'
229 //----------- civicrm_mailing_event_confirm methods -----------
232 * Test civicrm_mailing_event_confirm with wrong params.
233 * Note that tests like this are slightly better than no test but an
234 * api function cannot be considered supported / 'part of the api' without a
237 public function testMailerConfirmWrongParams() {
239 'contact_id' => 'Wrong ID',
240 'subscribe_id' => 'Wrong ID',
241 'hash' => 'Wrong Hash',
242 'event_subscribe_id' => '123',
243 'time_stamp' => '20111111010101',
245 $result = $this->callAPIFailure('mailing_event', 'confirm', $params,
246 'Confirmation failed'
250 //---------- civicrm_mailing_event_reply methods -----------
253 * Test civicrm_mailing_event_reply with wrong params.
255 * Note that tests like this are slightly better than no test but an
256 * api function cannot be considered supported / 'part of the api' without a
259 public function testMailerReplyWrongParams() {
261 'job_id' => 'Wrong ID',
262 'event_queue_id' => 'Wrong ID',
263 'hash' => 'Wrong Hash',
264 'bodyTxt' => 'Body...',
265 'replyTo' => $this->_email
,
266 'time_stamp' => '20111111010101',
268 $result = $this->callAPIFailure('mailing_event', 'reply', $params,
269 'Queue event could not be found'
274 //----------- civicrm_mailing_event_forward methods ----------
277 * Test civicrm_mailing_event_forward with wrong params.
278 * Note that tests like this are slightly better than no test but an
279 * api function cannot be considered supported / 'part of the api' without a
282 public function testMailerForwardWrongParams() {
284 'job_id' => 'Wrong ID',
285 'event_queue_id' => 'Wrong ID',
286 'hash' => 'Wrong Hash',
287 'email' => $this->_email
,
288 'time_stamp' => '20111111010101',
290 $result = $this->callAPIFailure('mailing_event', 'forward', $params,
291 'Queue event could not be found'
295 //----------- civicrm_mailing_create ----------