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/>.
28 * Test APIv3 civicrm_mailing_* functions
33 class api_v3_MailingTest
extends CiviUnitTestCase
{
36 protected $_apiversion = 3;
37 protected $_params = array();
38 protected $_entity = 'Mailing';
39 protected $_contactID;
42 * APIv3 result from creating an example footer
47 public function setUp() {
49 $this->useTransaction();
50 CRM_Mailing_BAO_MailingJob
::$mailsProcessed = 0; // DGW
51 $this->_contactID
= $this->individualCreate();
52 $this->_groupID
= $this->groupCreate();
53 $this->_email
= 'test@test.test';
54 $this->_params
= array(
55 'subject' => 'Hello {contact.display_name}',
56 'body_text' => "This is {contact.display_name}.\nhttps://civicrm.org\n{domain.address}{action.optOutUrl}",
57 'body_html' => "<p>This is {contact.display_name}.</p><p><a href='https://civicrm.org/'>CiviCRM.org</a></p><p>{domain.address}{action.optOutUrl}</p>",
58 'name' => 'mailing name',
59 'created_id' => $this->_contactID
,
64 $this->footer
= civicrm_api3('MailingComponent', 'create', array(
65 'name' => 'test domain footer',
66 'component_type' => 'footer',
67 'body_html' => '<p>From {domain.address}. To opt out, go to {action.optOutUrl}.</p>',
68 'body_text' => 'From {domain.address}. To opt out, go to {action.optOutUrl}.',
72 public function tearDown() {
73 CRM_Mailing_BAO_MailingJob
::$mailsProcessed = 0; // DGW
78 * Test civicrm_mailing_create.
80 public function testMailerCreateSuccess() {
81 $result = $this->callAPIAndDocument('mailing', 'create', $this->_params +
array('scheduled_date' => 'now'), __FUNCTION__
, __FILE__
);
82 $jobs = $this->callAPISuccess('mailing_job', 'get', array('mailing_id' => $result['id']));
83 $this->assertEquals(1, $jobs['count']);
84 unset($this->_params
['created_id']); // return isn't working on this in getAndCheck so lets not check it for now
85 $this->getAndCheck($this->_params
, $result['id'], 'mailing');
89 * Per CRM-20316 the mailing should still create without created_id (not mandatory).
91 public function testMailerCreateSuccessNoCreatedID() {
92 unset($this->_params
['created_id']);
93 $result = $this->callAPIAndDocument('mailing', 'create', $this->_params +
array('scheduled_date' => 'now'), __FUNCTION__
, __FILE__
);
94 $this->getAndCheck($this->_params
, $result['id'], 'mailing');
100 public function testTemplateTypeOptions() {
101 $types = $this->callAPISuccess('Mailing', 'getoptions', array('field' => 'template_type'));
102 $this->assertTrue(isset($types['values']['traditional']));
106 * The `template_options` field should be treated a JSON object.
108 * This test will create, read, and update the field.
110 public function testMailerCreateTemplateOptions() {
111 // 1. Create mailing with template_options.
112 $params = $this->_params
;
113 $params['template_options'] = json_encode(array('foo' => 'bar_1'));
114 $createResult = $this->callAPISuccess('mailing', 'create', $params);
115 $id = $createResult['id'];
116 $this->assertDBQuery('{"foo":"bar_1"}', 'SELECT template_options FROM civicrm_mailing WHERE id = %1', array(
117 1 => array($id, 'Int'),
119 $this->assertEquals('bar_1', $createResult['values'][$id]['template_options']['foo']);
121 // 2. Get mailing with template_options.
122 $getResult = $this->callAPISuccess('mailing', 'get', array(
125 $this->assertEquals('bar_1', $getResult['values'][$id]['template_options']['foo']);
126 $getValueResult = $this->callAPISuccess('mailing', 'getvalue', array(
128 'return' => 'template_options',
130 $this->assertEquals('bar_1', $getValueResult['foo']);
132 // 3. Update mailing with template_options.
133 $updateResult = $this->callAPISuccess('mailing', 'create', array(
135 'template_options' => array('foo' => 'bar_2'),
137 $this->assertDBQuery('{"foo":"bar_2"}', 'SELECT template_options FROM civicrm_mailing WHERE id = %1', array(
138 1 => array($id, 'Int'),
140 $this->assertEquals('bar_2', $updateResult['values'][$id]['template_options']['foo']);
144 * The Mailing.create API supports magic properties "groups[include,enclude]" and "mailings[include,exclude]".
145 * Make sure these work
147 public function testMagicGroups_create_update() {
149 $groupIDs['a'] = $this->groupCreate(array('name' => 'Example include group', 'title' => 'Example include group'));
150 $groupIDs['b'] = $this->groupCreate(array('name' => 'Example exclude group', 'title' => 'Example exclude group'));
151 $contactIDs['a'] = $this->individualCreate(array(
152 'email' => 'include.me@example.org',
153 'first_name' => 'Includer',
154 'last_name' => 'Person',
156 $contactIDs['b'] = $this->individualCreate(array(
157 'email' => 'exclude.me@example.org',
158 'last_name' => 'Excluder',
160 $this->callAPISuccess('GroupContact', 'create', array(
161 'group_id' => $groupIDs['a'],
162 'contact_id' => $contactIDs['a'],
164 $this->callAPISuccess('GroupContact', 'create', array(
165 'group_id' => $groupIDs['b'],
166 'contact_id' => $contactIDs['b'],
171 $createParams = $this->_params
;
172 $createParams['groups']['include'] = array($groupIDs['a']);
173 $createParams['groups']['exclude'] = array();
174 $createParams['mailings']['include'] = array();
175 $createParams['mailings']['exclude'] = array();
176 $createParams['api.mailing_job.create'] = 1;
177 $createResult = $this->callAPISuccess('Mailing', 'create', $createParams);
178 $getGroup1 = $this->callAPISuccess('MailingGroup', 'get', array('mailing_id' => $createResult['id']));
179 $getGroup1_ids = array_values(CRM_Utils_Array
::collect('entity_id', $getGroup1['values']));
180 $this->assertEquals(array($groupIDs['a']), $getGroup1_ids);
181 $getRecipient1 = $this->callAPISuccess('MailingRecipients', 'get', array('mailing_id' => $createResult['id']));
182 $getRecipient1_ids = array_values(CRM_Utils_Array
::collect('contact_id', $getRecipient1['values']));
183 $this->assertEquals(array($contactIDs['a']), $getRecipient1_ids);
185 // ** Pass 2: Update without any changes to groups[include]
186 $nullOpParams = $createParams;
187 $nullOpParams['id'] = $createResult['id'];
188 $updateParams['api.mailing_job.create'] = 1;
189 unset($nullOpParams['groups']['include']);
190 $this->callAPISuccess('Mailing', 'create', $nullOpParams);
191 $getGroup2 = $this->callAPISuccess('MailingGroup', 'get', array('mailing_id' => $createResult['id']));
192 $getGroup2_ids = array_values(CRM_Utils_Array
::collect('entity_id', $getGroup2['values']));
193 $this->assertEquals(array($groupIDs['a']), $getGroup2_ids);
194 $getRecipient2 = $this->callAPISuccess('MailingRecipients', 'get', array('mailing_id' => $createResult['id']));
195 $getRecip2_ids = array_values(CRM_Utils_Array
::collect('contact_id', $getRecipient2['values']));
196 $this->assertEquals(array($contactIDs['a']), $getRecip2_ids);
198 // ** Pass 3: Update with different groups[include]
199 $updateParams = $createParams;
200 $updateParams['id'] = $createResult['id'];
201 $updateParams['groups']['include'] = array($groupIDs['b']);
202 $updateParams['api.mailing_job.create'] = 1;
203 $this->callAPISuccess('Mailing', 'create', $updateParams);
204 $getGroup3 = $this->callAPISuccess('MailingGroup', 'get', array('mailing_id' => $createResult['id']));
205 $getGroup3_ids = array_values(CRM_Utils_Array
::collect('entity_id', $getGroup3['values']));
206 $this->assertEquals(array($groupIDs['b']), $getGroup3_ids);
207 $getRecipient3 = $this->callAPISuccess('MailingRecipients', 'get', array('mailing_id' => $createResult['id']));
208 $getRecipient3_ids = array_values(CRM_Utils_Array
::collect('contact_id', $getRecipient3['values']));
209 $this->assertEquals(array($contactIDs['b']), $getRecipient3_ids);
212 public function testMailerPreview() {
214 $contactID = $this->individualCreate();
215 $displayName = $this->callAPISuccess('contact', 'get', array('id' => $contactID));
216 $displayName = $displayName['values'][$contactID]['display_name'];
217 $this->assertTrue(!empty($displayName));
219 $params = $this->_params
;
220 $params['api.Mailing.preview'] = array(
222 'contact_id' => $contactID,
224 $params['options']['force_rollback'] = 1;
228 'mailing' => CRM_Core_DAO
::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing'),
229 'job' => CRM_Core_DAO
::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_job'),
230 'group' => CRM_Core_DAO
::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_group'),
231 'recipient' => CRM_Core_DAO
::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_recipients'),
233 $result = $this->callAPISuccess('mailing', 'create', $params);
234 $this->assertDBQuery($maxIDs['mailing'], 'SELECT MAX(id) FROM civicrm_mailing'); // 'Preview should not create any mailing records'
235 $this->assertDBQuery($maxIDs['job'], 'SELECT MAX(id) FROM civicrm_mailing_job'); // 'Preview should not create any mailing_job record'
236 $this->assertDBQuery($maxIDs['group'], 'SELECT MAX(id) FROM civicrm_mailing_group'); // 'Preview should not create any mailing_group records'
237 $this->assertDBQuery($maxIDs['recipient'], 'SELECT MAX(id) FROM civicrm_mailing_recipients'); // 'Preview should not create any mailing_recipient records'
239 $previewResult = $result['values'][$result['id']]['api.Mailing.preview'];
240 $this->assertEquals("Hello $displayName", $previewResult['values']['subject']);
241 $this->assertContains("This is $displayName", $previewResult['values']['body_text']);
242 $this->assertContains("<p>This is $displayName.</p>", $previewResult['values']['body_html']);
245 public function testMailerPreviewRecipients() {
247 $groupIDs['inc'] = $this->groupCreate(array('name' => 'Example include group', 'title' => 'Example include group'));
248 $groupIDs['exc'] = $this->groupCreate(array('name' => 'Example exclude group', 'title' => 'Example exclude group'));
249 $contactIDs['include_me'] = $this->individualCreate(array(
250 'email' => 'include.me@example.org',
251 'first_name' => 'Includer',
252 'last_name' => 'Person',
254 $contactIDs['exclude_me'] = $this->individualCreate(array(
255 'email' => 'exclude.me@example.org',
256 'last_name' => 'Excluder',
258 $this->callAPISuccess('GroupContact', 'create', array(
259 'group_id' => $groupIDs['inc'],
260 'contact_id' => $contactIDs['include_me'],
262 $this->callAPISuccess('GroupContact', 'create', array(
263 'group_id' => $groupIDs['inc'],
264 'contact_id' => $contactIDs['exclude_me'],
266 $this->callAPISuccess('GroupContact', 'create', array(
267 'group_id' => $groupIDs['exc'],
268 'contact_id' => $contactIDs['exclude_me'],
271 $params = $this->_params
;
272 $params['groups']['include'] = array($groupIDs['inc']);
273 $params['groups']['exclude'] = array($groupIDs['exc']);
274 $params['mailings']['include'] = array();
275 $params['mailings']['exclude'] = array();
276 $params['options']['force_rollback'] = 1;
277 $params['api.mailing_job.create'] = 1;
278 $params['api.MailingRecipients.get'] = array(
279 'mailing_id' => '$value.id',
280 'api.contact.getvalue' => array(
281 'return' => 'display_name',
283 'api.email.getvalue' => array(
290 'mailing' => CRM_Core_DAO
::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing'),
291 'job' => CRM_Core_DAO
::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_job'),
292 'group' => CRM_Core_DAO
::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_group'),
294 $create = $this->callAPIAndDocument('Mailing', 'create', $params, __FUNCTION__
, __FILE__
);
295 $this->assertDBQuery($maxIDs['mailing'], 'SELECT MAX(id) FROM civicrm_mailing'); // 'Preview should not create any mailing records'
296 $this->assertDBQuery($maxIDs['job'], 'SELECT MAX(id) FROM civicrm_mailing_job'); // 'Preview should not create any mailing_job record'
297 $this->assertDBQuery($maxIDs['group'], 'SELECT MAX(id) FROM civicrm_mailing_group'); // 'Preview should not create any mailing_group records'
299 $preview = $create['values'][$create['id']]['api.MailingRecipients.get'];
300 $previewIds = array_values(CRM_Utils_Array
::collect('contact_id', $preview['values']));
301 $this->assertEquals(array((string) $contactIDs['include_me']), $previewIds);
302 $previewEmails = array_values(CRM_Utils_Array
::collect('api.email.getvalue', $preview['values']));
303 $this->assertEquals(array('include.me@example.org'), $previewEmails);
304 $previewNames = array_values(CRM_Utils_Array
::collect('api.contact.getvalue', $preview['values']));
305 $this->assertTrue((bool) preg_match('/Includer Person/', $previewNames[0]), "Name 'Includer Person' should appear in '" . $previewNames[0] . '"');
308 public function testMailerPreviewRecipientsDeduplicate() {
310 $groupIDs['grp'] = $this->groupCreate(array('name' => 'Example group', 'title' => 'Example group'));
311 $contactIDs['include_me'] = $this->individualCreate(array(
312 'email' => 'include.me@example.org',
313 'first_name' => 'Includer',
314 'last_name' => 'Person',
316 $contactIDs['include_me_duplicate'] = $this->individualCreate(array(
317 'email' => 'include.me@example.org',
318 'first_name' => 'IncluderDuplicate',
319 'last_name' => 'Person',
321 $this->callAPISuccess('GroupContact', 'create', array(
322 'group_id' => $groupIDs['grp'],
323 'contact_id' => $contactIDs['include_me'],
325 $this->callAPISuccess('GroupContact', 'create', array(
326 'group_id' => $groupIDs['grp'],
327 'contact_id' => $contactIDs['include_me_duplicate'],
330 $params = $this->_params
;
331 $params['groups']['include'] = array($groupIDs['grp']);
332 $params['mailings']['include'] = array();
333 $params['options']['force_rollback'] = 1;
334 $params['dedupe_email'] = 1;
335 $params['api.mailing_job.create'] = 1;
336 $params['api.MailingRecipients.get'] = array(
337 'mailing_id' => '$value.id',
338 'api.contact.getvalue' => array(
339 'return' => 'display_name',
341 'api.email.getvalue' => array(
347 $create = $this->callAPISuccess('Mailing', 'create', $params);
349 $preview = $create['values'][$create['id']]['api.MailingRecipients.get'];
350 $this->assertEquals(1, $preview['count']);
351 $previewEmails = array_values(CRM_Utils_Array
::collect('api.email.getvalue', $preview['values']));
352 $this->assertEquals(array('include.me@example.org'), $previewEmails);
358 public function testMailerSendTest_email() {
359 $contactIDs['alice'] = $this->individualCreate(array(
360 'email' => 'alice@example.org',
361 'first_name' => 'Alice',
362 'last_name' => 'Person',
365 $mail = $this->callAPISuccess('mailing', 'create', $this->_params
);
367 $params = array('mailing_id' => $mail['id'], 'test_email' => 'alice@example.org', 'test_group' => NULL);
368 $deliveredInfo = $this->callAPISuccess($this->_entity
, 'send_test', $params);
369 $this->assertEquals(1, $deliveredInfo['count'], "in line " . __LINE__
); // verify mail has been sent to user by count
371 $deliveredContacts = array_values(CRM_Utils_Array
::collect('contact_id', $deliveredInfo['values']));
372 $this->assertEquals(array($contactIDs['alice']), $deliveredContacts);
374 $deliveredEmails = array_values(CRM_Utils_Array
::collect('email', $deliveredInfo['values']));
375 $this->assertEquals(array('alice@example.org'), $deliveredEmails);
381 public function testMailerSendTest_group() {
383 $groupIDs['inc'] = $this->groupCreate(array('name' => 'Example include group', 'title' => 'Example include group'));
384 $contactIDs['alice'] = $this->individualCreate(array(
385 'email' => 'alice@example.org',
386 'first_name' => 'Alice',
387 'last_name' => 'Person',
389 $contactIDs['bob'] = $this->individualCreate(array(
390 'email' => 'bob@example.org',
391 'first_name' => 'Bob',
392 'last_name' => 'Person',
394 $contactIDs['carol'] = $this->individualCreate(array(
395 'email' => 'carol@example.org',
396 'first_name' => 'Carol',
397 'last_name' => 'Person',
399 $this->callAPISuccess('GroupContact', 'create', array(
400 'group_id' => $groupIDs['inc'],
401 'contact_id' => $contactIDs['alice'],
403 $this->callAPISuccess('GroupContact', 'create', array(
404 'group_id' => $groupIDs['inc'],
405 'contact_id' => $contactIDs['bob'],
407 $this->callAPISuccess('GroupContact', 'create', array(
408 'group_id' => $groupIDs['inc'],
409 'contact_id' => $contactIDs['carol'],
413 $mail = $this->callAPISuccess('mailing', 'create', $this->_params
);
414 $deliveredInfo = $this->callAPISuccess($this->_entity
, 'send_test', array(
415 'mailing_id' => $mail['id'],
416 'test_email' => NULL,
417 'test_group' => $groupIDs['inc'],
419 $this->assertEquals(3, $deliveredInfo['count'], "in line " . __LINE__
); // verify mail has been sent to user by count
421 $deliveredContacts = array_values(CRM_Utils_Array
::collect('contact_id', $deliveredInfo['values']));
422 $this->assertEquals(array($contactIDs['alice'], $contactIDs['bob'], $contactIDs['carol']), $deliveredContacts);
424 $deliveredEmails = array_values(CRM_Utils_Array
::collect('email', $deliveredInfo['values']));
425 $this->assertEquals(array('alice@example.org', 'bob@example.org', 'carol@example.org'), $deliveredEmails);
431 public function submitProvider() {
432 $cases = array(); // $useLogin, $params, $expectedFailure, $expectedJobCount
435 array(), // createParams
436 array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
437 FALSE, // expectedFailure
438 1, // expectedJobCount
442 array(), // createParams
443 array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
444 "/Failed to determine current user/", // expectedFailure
445 0, // expectedJobCount
449 array(), // createParams
450 array('scheduled_date' => '2014-12-13 10:00:00'),
451 FALSE, // expectedFailure
452 1, // expectedJobCount
456 array(), // createParams
458 "/Missing parameter scheduled_date and.or approval_date/", // expectedFailure
459 0, // expectedJobCount
463 array('name' => ''), // createParams
464 array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
465 "/Mailing cannot be sent. There are missing or invalid fields \\(name\\)./", // expectedFailure
466 0, // expectedJobCount
470 array('body_html' => '', 'body_text' => ''), // createParams
471 array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
472 "/Mailing cannot be sent. There are missing or invalid fields \\(body\\)./", // expectedFailure
473 0, // expectedJobCount
477 array('body_html' => 'Oops, did I leave my tokens at home?'), // createParams
478 array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
479 "/Mailing cannot be sent. There are missing or invalid fields \\(.*body_html.*optOut.*\\)./", // expectedFailure
480 0, // expectedJobCount
484 array('body_text' => 'Oops, did I leave my tokens at home?'), // createParams
485 array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
486 "/Mailing cannot be sent. There are missing or invalid fields \\(.*body_text.*optOut.*\\)./", // expectedFailure
487 0, // expectedJobCount
491 array('body_text' => 'Look ma, magic tokens in the text!', 'footer_id' => '%FOOTER%'), // createParams
492 array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
493 FALSE, // expectedFailure
494 1, // expectedJobCount
498 array('body_html' => '<p>Look ma, magic tokens in the markup!</p>', 'footer_id' => '%FOOTER%'), // createParams
499 array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
500 FALSE, // expectedFailure
501 1, // expectedJobCount
507 * @param bool $useLogin
508 * @param array $createParams
509 * @param array $submitParams
510 * @param null|string $expectedFailure
511 * @param int $expectedJobCount
512 * @dataProvider submitProvider
514 public function testMailerSubmit($useLogin, $createParams, $submitParams, $expectedFailure, $expectedJobCount) {
516 $this->createLoggedInUser();
519 if (isset($createParams['footer_id']) && $createParams['footer_id'] == '%FOOTER%') {
520 $createParams['footer_id'] = $this->footer
['id'];
523 $id = $this->createDraftMailing($createParams);
525 $submitParams['id'] = $id;
526 if ($expectedFailure) {
527 $submitResult = $this->callAPIFailure('mailing', 'submit', $submitParams);
528 $this->assertRegExp($expectedFailure, $submitResult['error_message']);
531 $submitResult = $this->callAPIAndDocument('mailing', 'submit', $submitParams, __FUNCTION__
, __FILE__
);
532 $this->assertTrue(is_numeric($submitResult['id']));
533 $this->assertTrue(is_numeric($submitResult['values'][$id]['scheduled_id']));
534 $this->assertEquals($submitParams['scheduled_date'], $submitResult['values'][$id]['scheduled_date']);
536 $this->assertDBQuery($expectedJobCount, 'SELECT count(*) FROM civicrm_mailing_job WHERE mailing_id = %1', array(
537 1 => array($id, 'Integer'),
542 * Test unsubscribe list contains correct groups
543 * when include = 'previous mailing'
545 public function testUnsubscribeGroupList() {
546 // Create set of groups and add a contact to both of them.
547 $groupID2 = $this->groupCreate(array('name' => 'Test group 2', 'title' => 'group title 2'));
548 $groupID3 = $this->groupCreate(array('name' => 'Test group 3', 'title' => 'group title 3'));
549 $contactId = $this->individualCreate();
550 foreach (array($groupID2, $groupID3) as $grp) {
552 'contact_id' => $contactId,
555 $this->callAPISuccess('GroupContact', 'create', $params);
558 //Send mail to groupID3
559 $mail = $this->callAPISuccess('mailing', 'create', $this->_params
);
560 $params = array('mailing_id' => $mail['id'], 'test_email' => NULL, 'test_group' => $groupID3);
561 $this->callAPISuccess($this->_entity
, 'send_test', $params);
564 'mailing_id' => $mail['id'],
565 'entity_table' => 'civicrm_group',
566 'entity_id' => $groupID3,
567 'group_type' => 'Include',
569 $mailingGroup = $this->callAPISuccess('MailingGroup', 'create', $mgParams);
571 //Include previous mail in the mailing group.
572 $mail2 = $this->callAPISuccess('mailing', 'create', $this->_params
);
573 $params = array('mailing_id' => $mail2['id'], 'test_email' => NULL, 'test_group' => $groupID3);
574 $this->callAPISuccess($this->_entity
, 'send_test', $params);
577 'mailing_id' => $mail2['id'],
578 'entity_table' => 'civicrm_mailing',
579 'entity_id' => $mail['id'],
580 'group_type' => 'Include',
582 $mailingGroup = $this->callAPISuccess('MailingGroup', 'create', $mgParams);
583 //CRM-20431 - Delete group id that matches first mailing id.
584 $this->callAPISuccess('Group', 'delete', array('id' => $this->_groupID
));
585 $jobId = CRM_Core_DAO
::getFieldValue('CRM_Mailing_DAO_MailingJob', $mail2['id'], 'id', 'mailing_id');
586 $hash = CRM_Core_DAO
::getFieldValue('CRM_Mailing_Event_DAO_Queue', $jobId, 'hash', 'job_id');
587 $queueId = CRM_Core_DAO
::getFieldValue('CRM_Mailing_Event_DAO_Queue', $jobId, 'id', 'job_id');
589 $group = CRM_Mailing_Event_BAO_Unsubscribe
::unsub_from_mailing($jobId, $queueId, $hash, TRUE);
590 //Assert only one group returns in the unsubscribe list.
591 $this->assertCount(1, $group);
592 $this->assertEquals($groupID3, key($group));
598 public function testMailerStats() {
599 $result = $this->groupContactCreate($this->_groupID
, 100);
600 $this->assertEquals(100, $result['added']); //verify if 100 contacts are added for group
602 //Create and send test mail first and change the mail job to live,
603 //because stats api only works on live mail
604 $mail = $this->callAPISuccess('mailing', 'create', $this->_params
);
605 $params = array('mailing_id' => $mail['id'], 'test_email' => NULL, 'test_group' => $this->_groupID
);
606 $deliveredInfo = $this->callAPISuccess($this->_entity
, 'send_test', $params);
607 $deliveredIds = implode(',', array_keys($deliveredInfo['values']));
609 //Change the test mail into live
610 $sql = "UPDATE civicrm_mailing_job SET is_test = 0 WHERE mailing_id = {$mail['id']}";
611 CRM_Core_DAO
::executeQuery($sql);
613 foreach (array('bounce', 'unsubscribe', 'opened') as $type) {
614 $sql = "CREATE TEMPORARY TABLE mail_{$type}_temp
615 (event_queue_id int, time_stamp datetime, delivered_id int)
616 SELECT event_queue_id, time_stamp, id
617 FROM civicrm_mailing_event_delivered
618 WHERE id IN ($deliveredIds)
619 ORDER BY RAND() LIMIT 0,20;";
620 CRM_Core_DAO
::executeQuery($sql);
622 $sql = "DELETE FROM civicrm_mailing_event_delivered WHERE id IN (SELECT delivered_id FROM mail_{$type}_temp);";
623 CRM_Core_DAO
::executeQuery($sql);
625 if ($type == 'unsubscribe') {
626 $sql = "INSERT INTO civicrm_mailing_event_{$type} (event_queue_id, time_stamp, org_unsubscribe)
627 SELECT event_queue_id, time_stamp, 1 FROM mail_{$type}_temp";
630 $sql = "INSERT INTO civicrm_mailing_event_{$type} (event_queue_id, time_stamp)
631 SELECT event_queue_id, time_stamp FROM mail_{$type}_temp";
633 CRM_Core_DAO
::executeQuery($sql);
636 $result = $this->callAPISuccess('mailing', 'stats', array('mailing_id' => $mail['id']));
637 $expectedResult = array(
638 'Delivered' => 80, //since among 100 mails 20 has been bounced
641 'Unique Clicks' => 0,
642 'Unsubscribers' => 20,
644 $this->checkArrayEquals($expectedResult, $result['values'][$mail['id']]);
648 * Test civicrm_mailing_delete.
650 public function testMailerDeleteSuccess() {
651 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
652 $this->callAPIAndDocument($this->_entity
, 'delete', array('id' => $result['id']), __FUNCTION__
, __FILE__
);
653 $this->assertAPIDeleted($this->_entity
, $result['id']);
657 * Test Mailing.gettokens.
659 public function testMailGetTokens() {
660 $description = "Demonstrates fetching tokens for one or more entities (in this case \"Contact\" and \"Mailing\").
661 Optionally pass sequential=1 to have output ready-formatted for the select2 widget.";
662 $result = $this->callAPIAndDocument($this->_entity
, 'gettokens', array('entity' => array('Contact', 'Mailing')), __FUNCTION__
, __FILE__
, $description);
663 $this->assertContains('Contact Type', $result['values']);
665 // Check that passing "sequential" correctly outputs a hierarchical array
666 $result = $this->callAPISuccess($this->_entity
, 'gettokens', array('entity' => 'contact', 'sequential' => 1));
667 $this->assertArrayHasKey('text', $result['values'][0]);
668 $this->assertArrayHasKey('id', $result['values'][0]['children'][0]);
671 public function testClone() {
673 $groupIDs['inc'] = $this->groupCreate(array('name' => 'Example include group', 'title' => 'Example include group'));
674 $contactIDs['include_me'] = $this->individualCreate(array(
675 'email' => 'include.me@example.org',
676 'first_name' => 'Includer',
677 'last_name' => 'Person',
679 $this->callAPISuccess('GroupContact', 'create', array(
680 'group_id' => $groupIDs['inc'],
681 'contact_id' => $contactIDs['include_me'],
684 $params = $this->_params
;
685 $params['groups']['include'] = array($groupIDs['inc']);
686 $params['groups']['exclude'] = array();
687 $params['mailings']['include'] = array();
688 $params['mailings']['exclude'] = array();
691 $create = $this->callAPISuccess('Mailing', 'create', $params);
692 $createId = $create['id'];
693 $this->createLoggedInUser();
694 $clone = $this->callAPIAndDocument('Mailing', 'clone', array('id' => $create['id']), __FUNCTION__
, __FILE__
);
695 $cloneId = $clone['id'];
697 $this->assertNotEquals($createId, $cloneId, 'Create and clone should return different records');
698 $this->assertTrue(is_numeric($cloneId));
700 $this->assertNotEmpty($clone['values'][$cloneId]['subject']);
701 $this->assertEquals($params['subject'], $clone['values'][$cloneId]['subject'], "Cloned subject should match");
703 // created_id is special - populated based on current user (ie the cloner).
704 $this->assertNotEmpty($clone['values'][$cloneId]['created_id']);
705 $this->assertNotEquals($create['values'][$createId]['created_id'], $clone['values'][$cloneId]['created_id'], 'Clone should be created by a different person');
707 // Target groups+mailings are special.
708 $cloneGroups = $this->callAPISuccess('MailingGroup', 'get', array('mailing_id' => $cloneId, 'sequential' => 1));
709 $this->assertEquals(1, $cloneGroups['count']);
710 $this->assertEquals($cloneGroups['values'][0]['group_type'], 'Include');
711 $this->assertEquals($cloneGroups['values'][0]['entity_table'], 'civicrm_group');
712 $this->assertEquals($cloneGroups['values'][0]['entity_id'], $groupIDs['inc']);
715 //@ todo tests below here are all failure tests which are not hugely useful - need success tests
717 //------------ civicrm_mailing_event_bounce methods------------
720 * Test civicrm_mailing_event_bounce with wrong params.
721 * Note that tests like this are slightly better than no test but an
722 * api function cannot be considered supported / 'part of the api' without a
725 public function testMailerBounceWrongParams() {
727 'job_id' => 'Wrong ID',
728 'event_queue_id' => 'Wrong ID',
729 'hash' => 'Wrong Hash',
731 'time_stamp' => '20111109212100',
733 $this->callAPIFailure('mailing_event', 'bounce', $params,
734 'Queue event could not be found'
738 //----------- civicrm_mailing_event_confirm methods -----------
741 * Test civicrm_mailing_event_confirm with wrong params.
742 * Note that tests like this are slightly better than no test but an
743 * api function cannot be considered supported / 'part of the api' without a
746 public function testMailerConfirmWrongParams() {
748 'contact_id' => 'Wrong ID',
749 'subscribe_id' => 'Wrong ID',
750 'hash' => 'Wrong Hash',
751 'event_subscribe_id' => '123',
752 'time_stamp' => '20111111010101',
754 $this->callAPIFailure('mailing_event', 'confirm', $params,
755 'contact_id is not a valid integer'
759 //---------- civicrm_mailing_event_reply methods -----------
762 * Test civicrm_mailing_event_reply with wrong params.
764 * Note that tests like this are slightly better than no test but an
765 * api function cannot be considered supported / 'part of the api' without a
768 public function testMailerReplyWrongParams() {
770 'job_id' => 'Wrong ID',
771 'event_queue_id' => 'Wrong ID',
772 'hash' => 'Wrong Hash',
773 'bodyTxt' => 'Body...',
774 'replyTo' => $this->_email
,
775 'time_stamp' => '20111111010101',
777 $this->callAPIFailure('mailing_event', 'reply', $params,
778 'Queue event could not be found'
783 //----------- civicrm_mailing_event_forward methods ----------
786 * Test civicrm_mailing_event_forward with wrong params.
787 * Note that tests like this are slightly better than no test but an
788 * api function cannot be considered supported / 'part of the api' without a
791 public function testMailerForwardWrongParams() {
793 'job_id' => 'Wrong ID',
794 'event_queue_id' => 'Wrong ID',
795 'hash' => 'Wrong Hash',
796 'email' => $this->_email
,
797 'time_stamp' => '20111111010101',
799 $this->callAPIFailure('mailing_event', 'forward', $params,
800 'Queue event could not be found'
805 * @param array $params
806 * Extra parameters for the draft mailing.
809 public function createDraftMailing($params = array()) {
810 $createParams = array_merge($this->_params
, $params);
811 $createResult = $this->callAPISuccess('mailing', 'create', $createParams, __FUNCTION__
, __FILE__
);
812 $this->assertTrue(is_numeric($createResult['id']));
813 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_mailing_job WHERE mailing_id = %1', array(
814 1 => array($createResult['id'], 'Integer'),
816 return $createResult['id'];
820 * Test to make sure that if the event queue hashes have been archived,
821 * we can still have working click-trough URLs working (CRM-17959).
823 public function testUrlWithMissingTrackingHash() {
824 $mail = $this->callAPISuccess('mailing', 'create', $this->_params +
array('scheduled_date' => 'now'), __FUNCTION__
, __FILE__
);
825 $jobs = $this->callAPISuccess('mailing_job', 'get', array('mailing_id' => $mail['id']));
826 $this->assertEquals(1, $jobs['count']);
828 $params = array('mailing_id' => $mail['id'], 'test_email' => 'alice@example.org', 'test_group' => NULL);
829 $deliveredInfo = $this->callAPISuccess($this->_entity
, 'send_test', $params);
831 $sql = "SELECT turl.id as url_id, turl.url, q.id as queue_id
832 FROM civicrm_mailing_trackable_url as turl
833 INNER JOIN civicrm_mailing_job as j ON turl.mailing_id = j.mailing_id
834 INNER JOIN civicrm_mailing_event_queue q ON j.id = q.job_id
835 ORDER BY turl.id DESC LIMIT 1";
837 $dao = CRM_Core_DAO
::executeQuery($sql);
838 $this->assertTrue($dao->fetch());
840 $url = CRM_Mailing_Event_BAO_TrackableURLOpen
::track($dao->queue_id
, $dao->url_id
);
841 $this->assertContains('https://civicrm.org', $url);
843 // Now delete the event queue hashes and see if the tracking still works.
844 CRM_Core_DAO
::executeQuery('DELETE FROM civicrm_mailing_event_queue');
846 $url = CRM_Mailing_Event_BAO_TrackableURLOpen
::track($dao->queue_id
, $dao->url_id
);
847 $this->assertContains('https://civicrm.org', $url);
851 * Test Trackable URL with unicode character
853 public function testTrackableURLWithUnicodeSign() {
854 $unicodeURL = "https://civiĆcrm.org";
855 $this->_params
['body_text'] = str_replace("https://civicrm.org", $unicodeURL, $this->_params
['body_text']);
856 $this->_params
['body_html'] = str_replace("https://civicrm.org", $unicodeURL, $this->_params
['body_html']);
858 $mail = $this->callAPISuccess('mailing', 'create', $this->_params +
array('scheduled_date' => 'now'));
860 $params = array('mailing_id' => $mail['id'], 'test_email' => 'alice@example.org', 'test_group' => NULL);
861 $this->callAPISuccess($this->_entity
, 'send_test', $params);
863 $sql = "SELECT turl.id as url_id, turl.url, q.id as queue_id
864 FROM civicrm_mailing_trackable_url as turl
865 INNER JOIN civicrm_mailing_job as j ON turl.mailing_id = j.mailing_id
866 INNER JOIN civicrm_mailing_event_queue q ON j.id = q.job_id
867 ORDER BY turl.id DESC LIMIT 1";
869 $dao = CRM_Core_DAO
::executeQuery($sql);
870 $this->assertTrue($dao->fetch());
872 $url = CRM_Mailing_Event_BAO_TrackableURLOpen
::track($dao->queue_id
, $dao->url_id
);
873 $this->assertContains($unicodeURL, $url);
875 // Now delete the event queue hashes and see if the tracking still works.
876 CRM_Core_DAO
::executeQuery('DELETE FROM civicrm_mailing_event_queue');
878 $url = CRM_Mailing_Event_BAO_TrackableURLOpen
::track($dao->queue_id
, $dao->url_id
);
879 $this->assertContains($unicodeURL, $url);