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 $_contactID;
48 'description' => 'Test all Mailer methods.',
49 'group' => 'CiviCRM API Tests',
55 $this->useTransaction();
56 CRM_Mailing_BAO_MailingJob
::$mailsProcessed = 0; // DGW
57 $this->_contactID
= $this->individualCreate();
58 $this->_groupID
= $this->groupCreate();
59 $this->_email
= 'test@test.test';
60 $this->_params
= array(
61 'subject' => 'Hello {contact.display_name}',
62 'body_text' => "This is {contact.display_name}",
63 'body_html' => "<p>This is {contact.display_name}</p>",
64 'name' => 'mailing name',
65 'created_id' => $this->_contactID
,
70 CRM_Mailing_BAO_MailingJob
::$mailsProcessed = 0; // DGW
75 * Test civicrm_mailing_create
77 public function testMailerCreateSuccess() {
78 $result = $this->callAPIAndDocument('mailing', 'create', $this->_params
, __FUNCTION__
, __FILE__
);
79 $jobs = $this->callAPISuccess('mailing_job', 'get', array('mailing_id' => $result['id']));
80 $this->assertEquals(1, $jobs['count']);
81 unset($this->_params
['created_id']); // return isn't working on this in getAndCheck so lets not check it for now
82 $this->getAndCheck($this->_params
, $result['id'], 'mailing');
86 * The Mailing.create API supports magic properties "groups[include,enclude]" and "mailings[include,exclude]".
87 * Make sure these work
89 public function testMagicGroups_create_update() {
91 $groupIDs['a'] = $this->groupCreate(array('name' => 'Example include group', 'title' => 'Example include group'));
92 $groupIDs['b'] = $this->groupCreate(array('name' => 'Example exclude group', 'title' => 'Example exclude group'));
93 $contactIDs['a'] = $this->individualCreate(array('email' => 'include.me@example.org', 'first_name' => 'Includer', 'last_name' => 'Person'));
94 $contactIDs['b'] = $this->individualCreate(array('email' => 'exclude.me@example.org', 'last_name' => 'Excluder', 'last_name' => 'Excluder'));
95 $this->callAPISuccess('GroupContact', 'create', array('group_id' => $groupIDs['a'], 'contact_id' => $contactIDs['a']));
96 $this->callAPISuccess('GroupContact', 'create', array('group_id' => $groupIDs['b'], 'contact_id' => $contactIDs['b']));
100 $createParams = $this->_params
;
101 $createParams['groups']['include'] = array($groupIDs['a']);
102 $createParams['groups']['exclude'] = array();
103 $createParams['mailings']['include'] = array();
104 $createParams['mailings']['exclude'] = array();
105 $createResult = $this->callAPISuccess('Mailing', 'create', $createParams);
106 $getGroup1 = $this->callAPISuccess('MailingGroup', 'get', array('mailing_id' => $createResult['id']));
107 $getGroup1_ids = array_values(CRM_Utils_Array
::collect('entity_id', $getGroup1['values']));
108 $this->assertEquals(array($groupIDs['a']), $getGroup1_ids);
109 $getRecip1 = $this->callAPISuccess('MailingRecipients', 'get', array('mailing_id' => $createResult['id']));
110 $getRecip1_ids = array_values(CRM_Utils_Array
::collect('contact_id', $getRecip1['values']));
111 $this->assertEquals(array($contactIDs['a']), $getRecip1_ids);
113 // ** Pass 2: Update without any changes to groups[include]
114 $nullopParams = $createParams;
115 $nullopParams['id'] = $createResult['id'];
116 $updateParams['api.mailing_job.create'] = 1;
117 unset($nullopParams['groups']['include']);
118 $this->callAPISuccess('Mailing', 'create', $nullopParams);
119 $getGroup2 = $this->callAPISuccess('MailingGroup', 'get', array('mailing_id' => $createResult['id']));
120 $getGroup2_ids = array_values(CRM_Utils_Array
::collect('entity_id', $getGroup2['values']));
121 $this->assertEquals(array($groupIDs['a']), $getGroup2_ids);
122 $getRecip2 = $this->callAPISuccess('MailingRecipients', 'get', array('mailing_id' => $createResult['id']));
123 $getRecip2_ids = array_values(CRM_Utils_Array
::collect('contact_id', $getRecip2['values']));
124 $this->assertEquals(array($contactIDs['a']), $getRecip2_ids);
126 // ** Pass 3: Update with different groups[include]
127 $updateParams = $createParams;
128 $updateParams['id'] = $createResult['id'];
129 $updateParams['groups']['include'] = array($groupIDs['b']);
130 $updateParams['api.mailing_job.create'] = 1;
131 $this->callAPISuccess('Mailing', 'create', $updateParams);
132 $getGroup3 = $this->callAPISuccess('MailingGroup', 'get', array('mailing_id' => $createResult['id']));
133 $getGroup3_ids = array_values(CRM_Utils_Array
::collect('entity_id', $getGroup3['values']));
134 $this->assertEquals(array($groupIDs['b']), $getGroup3_ids);
135 $getRecip3 = $this->callAPISuccess('MailingRecipients', 'get', array('mailing_id' => $createResult['id']));
136 $getRecip3_ids = array_values(CRM_Utils_Array
::collect('contact_id', $getRecip3['values']));
137 $this->assertEquals(array($contactIDs['b']), $getRecip3_ids);
140 public function testMailerPreview() {
142 $contactID = $this->individualCreate();
143 $displayName = $this->callAPISuccess('contact', 'get', array('id' => $contactID));
144 $displayName = $displayName['values'][$contactID]['display_name'];
145 $this->assertTrue(!empty($displayName));
147 $params = $this->_params
;
148 $params['api.Mailing.preview'] = array(
150 'contact_id' => $contactID,
152 $params['options']['force_rollback'] = 1;
156 'mailing' => CRM_Core_DAO
::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing'),
157 'job' => CRM_Core_DAO
::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_job'),
158 'group' => CRM_Core_DAO
::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_group'),
159 'recip' => CRM_Core_DAO
::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_recipients'),
161 $result = $this->callAPISuccess('mailing', 'create', $params);
162 $this->assertDBQuery($maxIDs['mailing'], 'SELECT MAX(id) FROM civicrm_mailing'); // 'Preview should not create any mailing records'
163 $this->assertDBQuery($maxIDs['job'], 'SELECT MAX(id) FROM civicrm_mailing_job'); // 'Preview should not create any mailing_job record'
164 $this->assertDBQuery($maxIDs['group'], 'SELECT MAX(id) FROM civicrm_mailing_group'); // 'Preview should not create any mailing_group records'
165 $this->assertDBQuery($maxIDs['recip'], 'SELECT MAX(id) FROM civicrm_mailing_recipients'); // 'Preview should not create any mailing_recipient records'
167 $previewResult = $result['values'][$result['id']]['api.Mailing.preview'];
168 $this->assertEquals("Hello $displayName", $previewResult['values']['subject']);
169 $this->assertEquals("This is $displayName", $previewResult['values']['body_text']);
170 $this->assertContains("<p>This is $displayName</p>", $previewResult['values']['body_html']);
173 public function testMailerPreviewRecipients() {
175 $groupIDs['inc'] = $this->groupCreate(array('name' => 'Example include group', 'title' => 'Example include group'));
176 $groupIDs['exc'] = $this->groupCreate(array('name' => 'Example exclude group', 'title' => 'Example exclude group'));
177 $contactIDs['includeme'] = $this->individualCreate(array('email' => 'include.me@example.org', 'first_name' => 'Includer', 'last_name' => 'Person'));
178 $contactIDs['excludeme'] = $this->individualCreate(array('email' => 'exclude.me@example.org', 'last_name' => 'Excluder', 'last_name' => 'Excluder'));
179 $this->callAPISuccess('GroupContact', 'create', array('group_id' => $groupIDs['inc'], 'contact_id' => $contactIDs['includeme']));
180 $this->callAPISuccess('GroupContact', 'create', array('group_id' => $groupIDs['inc'], 'contact_id' => $contactIDs['excludeme']));
181 $this->callAPISuccess('GroupContact', 'create', array('group_id' => $groupIDs['exc'], 'contact_id' => $contactIDs['excludeme']));
183 $params = $this->_params
;
184 $params['groups']['include'] = array($groupIDs['inc']);
185 $params['groups']['exclude'] = array($groupIDs['exc']);
186 $params['mailings']['include'] = array();
187 $params['mailings']['exclude'] = array();
188 $params['options']['force_rollback'] = 1;
189 $params['api.MailingRecipients.get'] = array(
190 'mailing_id' => '$value.id',
191 'api.contact.getvalue' => array(
192 'return' => 'display_name',
194 'api.email.getvalue' => array(
201 'mailing' => CRM_Core_DAO
::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing'),
202 'job' => CRM_Core_DAO
::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_job'),
203 'group' => CRM_Core_DAO
::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_group'),
205 $create = $this->callAPIAndDocument('Mailing', 'create', $params, __FUNCTION__
, __FILE__
);
206 $this->assertDBQuery($maxIDs['mailing'], 'SELECT MAX(id) FROM civicrm_mailing'); // 'Preview should not create any mailing records'
207 $this->assertDBQuery($maxIDs['job'], 'SELECT MAX(id) FROM civicrm_mailing_job'); // 'Preview should not create any mailing_job record'
208 $this->assertDBQuery($maxIDs['group'], 'SELECT MAX(id) FROM civicrm_mailing_group'); // 'Preview should not create any mailing_group records'
210 $preview = $create['values'][$create['id']]['api.MailingRecipients.get'];
211 $previewIds = array_values(CRM_Utils_Array
::collect('contact_id', $preview['values']));
212 $this->assertEquals(array((string)$contactIDs['includeme']), $previewIds);
213 $previewEmails = array_values(CRM_Utils_Array
::collect('api.email.getvalue', $preview['values']));
214 $this->assertEquals(array('include.me@example.org'), $previewEmails);
215 $previewNames = array_values(CRM_Utils_Array
::collect('api.contact.getvalue', $preview['values']));
216 $this->assertTrue((bool)preg_match('/Includer Person/', $previewNames[0]), "Name 'Includer Person' should appear in '" . $previewNames[0] . '"');
219 public function testMailerSendTest_email() {
220 $contactIDs['alice'] = $this->individualCreate(array('email' => 'alice@example.org', 'first_name' => 'Alice', 'last_name' => 'Person'));
222 $mail = $this->callAPISuccess('mailing', 'create', $this->_params
);
224 $params = array('mailing_id' => $mail['id'], 'test_email' => 'alice@example.org', 'test_group' => NULL);
225 $deliveredInfo = $this->callAPISuccess($this->_entity
, 'send_test', $params);
226 $this->assertEquals(1, $deliveredInfo['count'], "in line " . __LINE__
); // verify mail has been sent to user by count
228 $deliveredContacts = array_values(CRM_Utils_Array
::collect('contact_id', $deliveredInfo['values']));
229 $this->assertEquals(array($contactIDs['alice']), $deliveredContacts);
231 $deliveredEmails = array_values(CRM_Utils_Array
::collect('email', $deliveredInfo['values']));
232 $this->assertEquals(array('alice@example.org'), $deliveredEmails);
235 public function testMailerSendTest_group() {
237 $groupIDs['inc'] = $this->groupCreate(array('name' => 'Example include group', 'title' => 'Example include group'));
238 $contactIDs['alice'] = $this->individualCreate(array('email' => 'alice@example.org', 'first_name' => 'Alice', 'last_name' => 'Person'));
239 $contactIDs['bob'] = $this->individualCreate(array('email' => 'bob@example.org', 'first_name' => 'Bob', 'last_name' => 'Person'));
240 $contactIDs['carol'] = $this->individualCreate(array('email' => 'carol@example.org', 'first_name' => 'Carol', 'last_name' => 'Person'));
241 $this->callAPISuccess('GroupContact', 'create', array('group_id' => $groupIDs['inc'], 'contact_id' => $contactIDs['alice']));
242 $this->callAPISuccess('GroupContact', 'create', array('group_id' => $groupIDs['inc'], 'contact_id' => $contactIDs['bob']));
243 $this->callAPISuccess('GroupContact', 'create', array('group_id' => $groupIDs['inc'], 'contact_id' => $contactIDs['carol']));
246 $mail = $this->callAPISuccess('mailing', 'create', $this->_params
);
247 $deliveredInfo = $this->callAPISuccess($this->_entity
, 'send_test', array(
248 'mailing_id' => $mail['id'],
249 'test_email' => NULL,
250 'test_group' => $groupIDs['inc'],
252 $this->assertEquals(3, $deliveredInfo['count'], "in line " . __LINE__
); // verify mail has been sent to user by count
254 $deliveredContacts = array_values(CRM_Utils_Array
::collect('contact_id', $deliveredInfo['values']));
255 $this->assertEquals(array($contactIDs['alice'], $contactIDs['bob'], $contactIDs['carol']), $deliveredContacts);
257 $deliveredEmails = array_values(CRM_Utils_Array
::collect('email', $deliveredInfo['values']));
258 $this->assertEquals(array('alice@example.org', 'bob@example.org', 'carol@example.org'), $deliveredEmails);
261 public function testMailerStats() {
262 $result = $this->groupContactCreate($this->_groupID
, 100);
263 $this->assertEquals(100, $result['added']); //verify if 100 contacts are added for group
265 //Create and send test mail first and change the mail job to live,
266 //because stats api only works on live mail
267 $mail = $this->callAPISuccess('mailing', 'create', $this->_params
);
268 $params = array('mailing_id' => $mail['id'], 'test_email' => NULL, 'test_group' => $this->_groupID
);
269 $deliveredInfo = $this->callAPISuccess($this->_entity
, 'send_test', $params);
270 $deliveredIds = implode(',', array_keys($deliveredInfo['values']));
272 //Change the test mail into live
273 $sql = "UPDATE civicrm_mailing_job SET is_test = 0 WHERE mailing_id = {$mail['id']}";
274 CRM_Core_DAO
::executeQuery($sql);
276 foreach (array('bounce', 'unsubscribe', 'opened') as $type) {
277 $sql = "CREATE TEMPORARY TABLE mail_{$type}_temp
278 (event_queue_id int, time_stamp datetime, delivered_id int)
279 SELECT event_queue_id, time_stamp, id
280 FROM civicrm_mailing_event_delivered
281 WHERE id IN ($deliveredIds)
282 ORDER BY RAND() LIMIT 0,20;";
283 CRM_Core_DAO
::executeQuery($sql);
285 $sql = "DELETE FROM civicrm_mailing_event_delivered WHERE id IN (SELECT delivered_id FROM mail_{$type}_temp);";
286 CRM_Core_DAO
::executeQuery($sql);
288 if ($type == 'unsubscribe') {
289 $sql = "INSERT INTO civicrm_mailing_event_{$type} (event_queue_id, time_stamp, org_unsubscribe)
290 SELECT event_queue_id, time_stamp, 1 FROM mail_{$type}_temp";
293 $sql = "INSERT INTO civicrm_mailing_event_{$type} (event_queue_id, time_stamp)
294 SELECT event_queue_id, time_stamp FROM mail_{$type}_temp";
296 CRM_Core_DAO
::executeQuery($sql);
299 $result = $this->callAPISuccess('mailing', 'stats', array('mailing_id' => $mail['id']));
300 $expectedResult = array(
301 'Delivered' => 80, //since among 100 mails 20 has been bounced
304 'Unique Clicks' => 0,
305 'Unsubscribers' => 20
307 $this->checkArrayEquals($expectedResult, $result['values'][$mail['id']]);
310 * Test civicrm_mailing_delete
312 public function testMailerDeleteSuccess() {
313 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
314 $jobs = $this->callAPIAndDocument($this->_entity
, 'delete', array('id' => $result['id']), __FUNCTION__
, __FILE__
);
315 $this->assertAPIDeleted($this->_entity
, $result['id']);
318 //@ todo tests below here are all failure tests which are not hugely useful - need success tests
320 //------------ civicrm_mailing_event_bounce methods------------
323 * Test civicrm_mailing_event_bounce with wrong params.
324 * Note that tests like this are slightly better than no test but an
325 * api function cannot be considered supported / 'part of the api' without a
328 public function testMailerBounceWrongParams() {
330 'job_id' => 'Wrong ID',
331 'event_queue_id' => 'Wrong ID',
332 'hash' => 'Wrong Hash',
334 'time_stamp' => '20111109212100',
336 $result = $this->callAPIFailure('mailing_event', 'bounce', $params,
337 'Queue event could not be found'
341 //----------- civicrm_mailing_event_confirm methods -----------
344 * Test civicrm_mailing_event_confirm with wrong params.
345 * Note that tests like this are slightly better than no test but an
346 * api function cannot be considered supported / 'part of the api' without a
349 public function testMailerConfirmWrongParams() {
351 'contact_id' => 'Wrong ID',
352 'subscribe_id' => 'Wrong ID',
353 'hash' => 'Wrong Hash',
354 'event_subscribe_id' => '123',
355 'time_stamp' => '20111111010101',
357 $result = $this->callAPIFailure('mailing_event', 'confirm', $params,
358 'Confirmation failed'
362 //---------- civicrm_mailing_event_reply methods -----------
365 * Test civicrm_mailing_event_reply with wrong params.
367 * Note that tests like this are slightly better than no test but an
368 * api function cannot be considered supported / 'part of the api' without a
371 public function testMailerReplyWrongParams() {
373 'job_id' => 'Wrong ID',
374 'event_queue_id' => 'Wrong ID',
375 'hash' => 'Wrong Hash',
376 'bodyTxt' => 'Body...',
377 'replyTo' => $this->_email
,
378 'time_stamp' => '20111111010101',
380 $result = $this->callAPIFailure('mailing_event', 'reply', $params,
381 'Queue event could not be found'
386 //----------- civicrm_mailing_event_forward methods ----------
389 * Test civicrm_mailing_event_forward with wrong params.
390 * Note that tests like this are slightly better than no test but an
391 * api function cannot be considered supported / 'part of the api' without a
394 public function testMailerForwardWrongParams() {
396 'job_id' => 'Wrong ID',
397 'event_queue_id' => 'Wrong ID',
398 'hash' => 'Wrong Hash',
399 'email' => $this->_email
,
400 'time_stamp' => '20111111010101',
402 $result = $this->callAPIFailure('mailing_event', 'forward', $params,
403 'Queue event could not be found'
407 //----------- civicrm_mailing_create ----------