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();
51 CRM_Mailing_BAO_MailingJob
::$mailsProcessed = 0;
52 $this->_contactID
= $this->individualCreate();
53 $this->_groupID
= $this->groupCreate();
54 $this->_email
= 'test@test.test';
55 $this->_params
= array(
56 'subject' => 'Hello {contact.display_name}',
57 'body_text' => "This is {contact.display_name}.\nhttps://civicrm.org\n{domain.address}{action.optOutUrl}",
58 'body_html' => "<link href='https://fonts.googleapis.com/css?family=Roboto+Condensed:400,700|Zilla+Slab:500,700' rel='stylesheet' type='text/css'><p><a href=\"http://{action.forward}\">Forward this email</a><a href=\"{action.forward}\">Forward this email with no protocol</a></p<p>This is {contact.display_name}.</p><p><a href='https://civicrm.org/'>CiviCRM.org</a></p><p>{domain.address}{action.optOutUrl}</p>",
59 'name' => 'mailing name',
60 'created_id' => $this->_contactID
,
65 $this->footer
= civicrm_api3('MailingComponent', 'create', array(
66 'name' => 'test domain footer',
67 'component_type' => 'footer',
68 'body_html' => '<p>From {domain.address}. To opt out, go to {action.optOutUrl}.</p>',
69 'body_text' => 'From {domain.address}. To opt out, go to {action.optOutUrl}.',
73 public function tearDown() {
75 CRM_Mailing_BAO_MailingJob
::$mailsProcessed = 0;
80 * Test civicrm_mailing_create.
82 public function testMailerCreateSuccess() {
83 $result = $this->callAPIAndDocument('mailing', 'create', $this->_params +
array('scheduled_date' => 'now'), __FUNCTION__
, __FILE__
);
84 $jobs = $this->callAPISuccess('mailing_job', 'get', array('mailing_id' => $result['id']));
85 $this->assertEquals(1, $jobs['count']);
86 // return isn't working on this in getAndCheck so lets not check it for now
87 unset($this->_params
['created_id']);
88 $this->getAndCheck($this->_params
, $result['id'], 'mailing');
92 * Tes that the parameter _skip_evil_bao_auto_schedule_ is respected & prevents jobs being created.
94 public function testSkipAutoSchedule() {
95 $this->callAPISuccess('Mailing', 'create', array_merge($this->_params
, [
96 '_skip_evil_bao_auto_schedule_' => TRUE,
97 'scheduled_date' => 'now',
99 $this->callAPISuccessGetCount('Mailing', [], 1);
100 $this->callAPISuccessGetCount('MailingJob', [], 0);
104 * Create a completed mailing (e.g when importing from a provider).
106 public function testMailerCreateCompleted() {
107 $this->_params
['body_html'] = 'I am completed so it does not matter if there is an opt out link since I have already been sent by another system';
108 $this->_params
['is_completed'] = 1;
109 $result = $this->callAPIAndDocument('mailing', 'create', $this->_params +
array('scheduled_date' => 'now'), __FUNCTION__
, __FILE__
);
110 $jobs = $this->callAPISuccess('mailing_job', 'get', array('mailing_id' => $result['id']));
111 $this->assertEquals(1, $jobs['count']);
112 $this->assertEquals('Complete', $jobs['values'][$jobs['id']]['status']);
113 // return isn't working on this in getAndCheck so lets not check it for now
114 unset($this->_params
['created_id']);
115 $this->getAndCheck($this->_params
, $result['id'], 'mailing');
119 * Per CRM-20316 the mailing should still create without created_id (not mandatory).
121 public function testMailerCreateSuccessNoCreatedID() {
122 unset($this->_params
['created_id']);
123 $result = $this->callAPIAndDocument('mailing', 'create', $this->_params +
array('scheduled_date' => 'now'), __FUNCTION__
, __FILE__
);
124 $this->getAndCheck($this->_params
, $result['id'], 'mailing');
130 public function testTemplateTypeOptions() {
131 $types = $this->callAPISuccess('Mailing', 'getoptions', array('field' => 'template_type'));
132 $this->assertTrue(isset($types['values']['traditional']));
136 * The `template_options` field should be treated a JSON object.
138 * This test will create, read, and update the field.
140 public function testMailerCreateTemplateOptions() {
141 // 1. Create mailing with template_options.
142 $params = $this->_params
;
143 $params['template_options'] = json_encode(array('foo' => 'bar_1'));
144 $createResult = $this->callAPISuccess('mailing', 'create', $params);
145 $id = $createResult['id'];
146 $this->assertDBQuery('{"foo":"bar_1"}', 'SELECT template_options FROM civicrm_mailing WHERE id = %1', array(
147 1 => array($id, 'Int'),
149 $this->assertEquals('bar_1', $createResult['values'][$id]['template_options']['foo']);
151 // 2. Get mailing with template_options.
152 $getResult = $this->callAPISuccess('mailing', 'get', array(
155 $this->assertEquals('bar_1', $getResult['values'][$id]['template_options']['foo']);
156 $getValueResult = $this->callAPISuccess('mailing', 'getvalue', array(
158 'return' => 'template_options',
160 $this->assertEquals('bar_1', $getValueResult['foo']);
162 // 3. Update mailing with template_options.
163 $updateResult = $this->callAPISuccess('mailing', 'create', array(
165 'template_options' => array('foo' => 'bar_2'),
167 $this->assertDBQuery('{"foo":"bar_2"}', 'SELECT template_options FROM civicrm_mailing WHERE id = %1', array(
168 1 => array($id, 'Int'),
170 $this->assertEquals('bar_2', $updateResult['values'][$id]['template_options']['foo']);
174 * The Mailing.create API supports magic properties "groups[include,enclude]" and "mailings[include,exclude]".
175 * Make sure these work
177 public function testMagicGroups_create_update() {
179 $groupIDs['a'] = $this->groupCreate(array('name' => 'Example include group', 'title' => 'Example include group'));
180 $groupIDs['b'] = $this->groupCreate(array('name' => 'Example exclude group', 'title' => 'Example exclude group'));
181 $contactIDs['a'] = $this->individualCreate(array(
182 'email' => 'include.me@example.org',
183 'first_name' => 'Includer',
184 'last_name' => 'Person',
186 $contactIDs['b'] = $this->individualCreate(array(
187 'email' => 'exclude.me@example.org',
188 'last_name' => 'Excluder',
190 $this->callAPISuccess('GroupContact', 'create', array(
191 'group_id' => $groupIDs['a'],
192 'contact_id' => $contactIDs['a'],
194 $this->callAPISuccess('GroupContact', 'create', array(
195 'group_id' => $groupIDs['b'],
196 'contact_id' => $contactIDs['b'],
201 $createParams = $this->_params
;
202 $createParams['groups']['include'] = [$groupIDs['a']];
203 $createParams['groups']['exclude'] = [];
204 $createParams['mailings']['include'] = [];
205 $createParams['mailings']['exclude'] = [];
206 $createParams['scheduled_date'] = 'now';
207 $createResult = $this->callAPISuccess('Mailing', 'create', $createParams);
208 $getGroup1 = $this->callAPISuccess('MailingGroup', 'get', array('mailing_id' => $createResult['id']));
209 $getGroup1_ids = array_values(CRM_Utils_Array
::collect('entity_id', $getGroup1['values']));
210 $this->assertEquals(array($groupIDs['a']), $getGroup1_ids);
211 $getRecipient1 = $this->callAPISuccess('MailingRecipients', 'get', array('mailing_id' => $createResult['id']));
212 $getRecipient1_ids = array_values(CRM_Utils_Array
::collect('contact_id', $getRecipient1['values']));
213 $this->assertEquals(array($contactIDs['a']), $getRecipient1_ids);
215 // ** Pass 2: Update without any changes to groups[include]
216 $nullOpParams = $createParams;
217 $nullOpParams['id'] = $createResult['id'];
218 $updateParams['api.mailing_job.create'] = 1;
219 unset($nullOpParams['groups']['include']);
220 $this->callAPISuccess('Mailing', 'create', $nullOpParams);
221 $getGroup2 = $this->callAPISuccess('MailingGroup', 'get', array('mailing_id' => $createResult['id']));
222 $getGroup2_ids = array_values(CRM_Utils_Array
::collect('entity_id', $getGroup2['values']));
223 $this->assertEquals(array($groupIDs['a']), $getGroup2_ids);
224 $getRecipient2 = $this->callAPISuccess('MailingRecipients', 'get', array('mailing_id' => $createResult['id']));
225 $getRecip2_ids = array_values(CRM_Utils_Array
::collect('contact_id', $getRecipient2['values']));
226 $this->assertEquals(array($contactIDs['a']), $getRecip2_ids);
228 // ** Pass 3: Update with different groups[include]
229 $updateParams = $createParams;
230 $updateParams['id'] = $createResult['id'];
231 $updateParams['groups']['include'] = array($groupIDs['b']);
232 $updateParams['scheduled_date'] = 'now';
233 $this->callAPISuccess('Mailing', 'create', $updateParams);
234 $getGroup3 = $this->callAPISuccess('MailingGroup', 'get', array('mailing_id' => $createResult['id']));
235 $getGroup3_ids = array_values(CRM_Utils_Array
::collect('entity_id', $getGroup3['values']));
236 $this->assertEquals(array($groupIDs['b']), $getGroup3_ids);
237 $getRecipient3 = $this->callAPISuccess('MailingRecipients', 'get', array('mailing_id' => $createResult['id']));
238 $getRecipient3_ids = array_values(CRM_Utils_Array
::collect('contact_id', $getRecipient3['values']));
239 $this->assertEquals(array($contactIDs['b']), $getRecipient3_ids);
242 public function testMailerPreview() {
244 $contactID = $this->individualCreate();
245 $displayName = $this->callAPISuccess('contact', 'get', array('id' => $contactID));
246 $displayName = $displayName['values'][$contactID]['display_name'];
247 $this->assertTrue(!empty($displayName));
249 $params = $this->_params
;
250 $params['api.Mailing.preview'] = array(
252 'contact_id' => $contactID,
254 $params['options']['force_rollback'] = 1;
258 'mailing' => CRM_Core_DAO
::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing'),
259 'job' => CRM_Core_DAO
::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_job'),
260 'group' => CRM_Core_DAO
::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_group'),
261 'recipient' => CRM_Core_DAO
::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_recipients'),
263 $result = $this->callAPISuccess('mailing', 'create', $params);
264 // 'Preview should not create any mailing records'
265 $this->assertDBQuery($maxIDs['mailing'], 'SELECT MAX(id) FROM civicrm_mailing');
266 // 'Preview should not create any mailing_job record'
267 $this->assertDBQuery($maxIDs['job'], 'SELECT MAX(id) FROM civicrm_mailing_job');
268 // 'Preview should not create any mailing_group records'
269 $this->assertDBQuery($maxIDs['group'], 'SELECT MAX(id) FROM civicrm_mailing_group');
270 // 'Preview should not create any mailing_recipient records'
271 $this->assertDBQuery($maxIDs['recipient'], 'SELECT MAX(id) FROM civicrm_mailing_recipients');
272 $baseurl = CRM_Utils_System
::baseCMSURL();
273 $previewResult = $result['values'][$result['id']]['api.Mailing.preview'];
274 $this->assertEquals("Hello $displayName", $previewResult['values']['subject']);
275 $this->assertContains("This is $displayName", $previewResult['values']['body_text']);
276 $this->assertContains("<p>This is $displayName.</p>", $previewResult['values']['body_html']);
277 $this->assertContains('<a href="' . $baseurl . 'index.php?q=civicrm/mailing/forward&amp;reset=1&jid=&qid=&h=">Forward this email with no protocol</a>', $previewResult['values']['body_html']);
278 $this->assertNotContains("http://http://", $previewResult['values']['body_html']);
281 public function testMailerPreviewRecipients() {
283 $groupIDs['inc'] = $this->groupCreate(array('name' => 'Example include group', 'title' => 'Example include group'));
284 $groupIDs['exc'] = $this->groupCreate(array('name' => 'Example exclude group', 'title' => 'Example exclude group'));
285 $contactIDs['include_me'] = $this->individualCreate(array(
286 'email' => 'include.me@example.org',
287 'first_name' => 'Includer',
288 'last_name' => 'Person',
290 $contactIDs['exclude_me'] = $this->individualCreate(array(
291 'email' => 'exclude.me@example.org',
292 'last_name' => 'Excluder',
294 $this->callAPISuccess('GroupContact', 'create', array(
295 'group_id' => $groupIDs['inc'],
296 'contact_id' => $contactIDs['include_me'],
298 $this->callAPISuccess('GroupContact', 'create', array(
299 'group_id' => $groupIDs['inc'],
300 'contact_id' => $contactIDs['exclude_me'],
302 $this->callAPISuccess('GroupContact', 'create', array(
303 'group_id' => $groupIDs['exc'],
304 'contact_id' => $contactIDs['exclude_me'],
307 $params = $this->_params
;
308 $params['groups']['include'] = array($groupIDs['inc']);
309 $params['groups']['exclude'] = array($groupIDs['exc']);
310 $params['mailings']['include'] = array();
311 $params['mailings']['exclude'] = array();
312 $params['options']['force_rollback'] = 1;
313 $params['api.MailingRecipients.get'] = array(
314 'mailing_id' => '$value.id',
315 'api.contact.getvalue' => array(
316 'return' => 'display_name',
318 'api.email.getvalue' => array(
325 'mailing' => CRM_Core_DAO
::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing'),
326 'group' => CRM_Core_DAO
::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_group'),
328 $create = $this->callAPIAndDocument('Mailing', 'create', $params, __FUNCTION__
, __FILE__
);
329 // 'Preview should not create any mailing records'
330 $this->assertDBQuery($maxIDs['mailing'], 'SELECT MAX(id) FROM civicrm_mailing');
331 // 'Preview should not create any mailing_group records'
332 $this->assertDBQuery($maxIDs['group'], 'SELECT MAX(id) FROM civicrm_mailing_group');
334 $preview = $create['values'][$create['id']]['api.MailingRecipients.get'];
335 $previewIds = array_values(CRM_Utils_Array
::collect('contact_id', $preview['values']));
336 $this->assertEquals(array((string) $contactIDs['include_me']), $previewIds);
337 $previewEmails = array_values(CRM_Utils_Array
::collect('api.email.getvalue', $preview['values']));
338 $this->assertEquals(array('include.me@example.org'), $previewEmails);
339 $previewNames = array_values(CRM_Utils_Array
::collect('api.contact.getvalue', $preview['values']));
340 $this->assertTrue((bool) preg_match('/Includer Person/', $previewNames[0]), "Name 'Includer Person' should appear in '" . $previewNames[0] . '"');
344 * Test if Mailing recipients include duplicate OR on_hold emails
346 public function testMailerPreviewRecipientsDeduplicateAndOnholdEmails() {
348 $groupIDs['grp'] = $this->groupCreate(array('name' => 'Example group', 'title' => 'Example group'));
349 $contactIDs['include_me'] = $this->individualCreate(array(
350 'email' => 'include.me@example.org',
351 'first_name' => 'Includer',
352 'last_name' => 'Person',
354 $contactIDs['include_me_duplicate'] = $this->individualCreate(array(
355 'email' => 'include.me@example.org',
356 'first_name' => 'IncluderDuplicate',
357 'last_name' => 'Person',
360 $contactIDs['include_me_onhold'] = $this->individualCreate(array(
361 'email' => 'onholdinclude.me@example.org',
362 'first_name' => 'Onhold',
363 'last_name' => 'Person',
365 $emailId = $this->callAPISuccessGetValue('Email', array(
367 'contact_id' => $contactIDs['include_me_onhold'],
369 $this->callAPISuccess('Email', 'create', array(
374 $this->callAPISuccess('GroupContact', 'create', array(
375 'group_id' => $groupIDs['grp'],
376 'contact_id' => $contactIDs['include_me'],
378 $this->callAPISuccess('GroupContact', 'create', array(
379 'group_id' => $groupIDs['grp'],
380 'contact_id' => $contactIDs['include_me_duplicate'],
382 $this->callAPISuccess('GroupContact', 'create', array(
383 'group_id' => $groupIDs['grp'],
384 'contact_id' => $contactIDs['include_me_onhold'],
387 $params = $this->_params
;
388 $params['groups']['include'] = array($groupIDs['grp']);
389 $params['mailings']['include'] = array();
390 $params['options']['force_rollback'] = 1;
391 $params['dedupe_email'] = 1;
392 $params['api.MailingRecipients.get'] = array(
393 'mailing_id' => '$value.id',
394 'api.contact.getvalue' => array(
395 'return' => 'display_name',
397 'api.email.getvalue' => array(
403 $create = $this->callAPISuccess('Mailing', 'create', $params);
405 //Recipient should not contain duplicate or on_hold emails.
406 $preview = $create['values'][$create['id']]['api.MailingRecipients.get'];
407 $this->assertEquals(1, $preview['count']);
408 $previewEmails = array_values(CRM_Utils_Array
::collect('api.email.getvalue', $preview['values']));
409 $this->assertEquals(array('include.me@example.org'), $previewEmails);
413 * Test sending a test mailing.
415 public function testMailerSendTest_email() {
416 $contactIDs['alice'] = $this->individualCreate(array(
417 'email' => 'alice@example.org',
418 'first_name' => 'Alice',
419 'last_name' => 'Person',
422 $mail = $this->callAPISuccess('mailing', 'create', $this->_params
);
424 $params = array('mailing_id' => $mail['id'], 'test_email' => 'ALicE@example.org', 'test_group' => NULL);
425 // Per https://lab.civicrm.org/dev/core/issues/229 ensure this is not passed through!
426 // Per https://lab.civicrm.org/dev/mail/issues/32 test non-lowercase email
427 $params['id'] = $mail['id'];
428 $deliveredInfo = $this->callAPISuccess($this->_entity
, 'send_test', $params);
429 // verify mail has been sent to user by count
430 $this->assertEquals(1, $deliveredInfo['count']);
432 $deliveredContacts = array_values(CRM_Utils_Array
::collect('contact_id', $deliveredInfo['values']));
433 $this->assertEquals(array($contactIDs['alice']), $deliveredContacts);
435 $deliveredEmails = array_values(CRM_Utils_Array
::collect('email', $deliveredInfo['values']));
436 $this->assertEquals(array('alice@example.org'), $deliveredEmails);
442 public function testMailerSendTest_group() {
444 $groupIDs['inc'] = $this->groupCreate(array('name' => 'Example include group', 'title' => 'Example include group'));
445 $contactIDs['alice'] = $this->individualCreate(array(
446 'email' => 'alice@example.org',
447 'first_name' => 'Alice',
448 'last_name' => 'Person',
450 $contactIDs['bob'] = $this->individualCreate(array(
451 'email' => 'bob@example.org',
452 'first_name' => 'Bob',
453 'last_name' => 'Person',
455 $contactIDs['carol'] = $this->individualCreate(array(
456 'email' => 'carol@example.org',
457 'first_name' => 'Carol',
458 'last_name' => 'Person',
460 $this->callAPISuccess('GroupContact', 'create', array(
461 'group_id' => $groupIDs['inc'],
462 'contact_id' => $contactIDs['alice'],
464 $this->callAPISuccess('GroupContact', 'create', array(
465 'group_id' => $groupIDs['inc'],
466 'contact_id' => $contactIDs['bob'],
468 $this->callAPISuccess('GroupContact', 'create', array(
469 'group_id' => $groupIDs['inc'],
470 'contact_id' => $contactIDs['carol'],
474 $mail = $this->callAPISuccess('mailing', 'create', $this->_params
);
475 $deliveredInfo = $this->callAPISuccess($this->_entity
, 'send_test', array(
476 'mailing_id' => $mail['id'],
477 'test_email' => NULL,
478 'test_group' => $groupIDs['inc'],
480 // verify mail has been sent to user by count
481 $this->assertEquals(3, $deliveredInfo['count'], "in line " . __LINE__
);
483 $deliveredContacts = array_values(CRM_Utils_Array
::collect('contact_id', $deliveredInfo['values']));
484 $this->assertEquals(array($contactIDs['alice'], $contactIDs['bob'], $contactIDs['carol']), $deliveredContacts);
486 $deliveredEmails = array_values(CRM_Utils_Array
::collect('email', $deliveredInfo['values']));
487 $this->assertEquals(array('alice@example.org', 'bob@example.org', 'carol@example.org'), $deliveredEmails);
493 public function submitProvider() {
494 // $useLogin, $params, $expectedFailure, $expectedJobCount
501 array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
512 array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
514 "/Failed to determine current user/",
523 array('scheduled_date' => '2014-12-13 10:00:00'),
536 "/Missing parameter scheduled_date and.or approval_date/",
545 array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
547 "/Mailing cannot be sent. There are missing or invalid fields \\(name\\)./",
555 array('body_html' => '', 'body_text' => ''),
556 array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
558 "/Mailing cannot be sent. There are missing or invalid fields \\(body\\)./",
566 array('body_html' => 'Oops, did I leave my tokens at home?'),
567 array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
569 "/Mailing cannot be sent. There are missing or invalid fields \\(.*body_html.*optOut.*\\)./",
577 array('body_text' => 'Oops, did I leave my tokens at home?'),
578 array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
580 "/Mailing cannot be sent. There are missing or invalid fields \\(.*body_text.*optOut.*\\)./",
588 array('body_text' => 'Look ma, magic tokens in the text!', 'footer_id' => '%FOOTER%'),
589 array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
599 array('body_html' => '<p>Look ma, magic tokens in the markup!</p>', 'footer_id' => '%FOOTER%'),
600 array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
610 * @param bool $useLogin
611 * @param array $createParams
612 * @param array $submitParams
613 * @param null|string $expectedFailure
614 * @param int $expectedJobCount
615 * @dataProvider submitProvider
617 public function testMailerSubmit($useLogin, $createParams, $submitParams, $expectedFailure, $expectedJobCount) {
619 $this->createLoggedInUser();
622 if (isset($createParams['footer_id']) && $createParams['footer_id'] == '%FOOTER%') {
623 $createParams['footer_id'] = $this->footer
['id'];
626 $id = $this->createDraftMailing($createParams);
628 $submitParams['id'] = $id;
629 if ($expectedFailure) {
630 $submitResult = $this->callAPIFailure('mailing', 'submit', $submitParams);
631 $this->assertRegExp($expectedFailure, $submitResult['error_message']);
634 $submitResult = $this->callAPIAndDocument('mailing', 'submit', $submitParams, __FUNCTION__
, __FILE__
);
635 $this->assertTrue(is_numeric($submitResult['id']));
636 $this->assertTrue(is_numeric($submitResult['values'][$id]['scheduled_id']));
637 $this->assertEquals($submitParams['scheduled_date'], $submitResult['values'][$id]['scheduled_date']);
639 $this->assertDBQuery($expectedJobCount, 'SELECT count(*) FROM civicrm_mailing_job WHERE mailing_id = %1', array(
640 1 => array($id, 'Integer'),
645 * Test unsubscribe list contains correct groups
646 * when include = 'previous mailing'
648 public function testUnsubscribeGroupList() {
649 // Create set of groups and add a contact to both of them.
650 $groupID2 = $this->groupCreate(array('name' => 'Test group 2', 'title' => 'group title 2'));
651 $groupID3 = $this->groupCreate(array('name' => 'Test group 3', 'title' => 'group title 3'));
652 $contactId = $this->individualCreate();
653 foreach (array($groupID2, $groupID3) as $grp) {
655 'contact_id' => $contactId,
658 $this->callAPISuccess('GroupContact', 'create', $params);
661 //Send mail to groupID3
662 $mail = $this->callAPISuccess('mailing', 'create', $this->_params
);
663 $params = array('mailing_id' => $mail['id'], 'test_email' => NULL, 'test_group' => $groupID3);
664 $this->callAPISuccess($this->_entity
, 'send_test', $params);
667 'mailing_id' => $mail['id'],
668 'entity_table' => 'civicrm_group',
669 'entity_id' => $groupID3,
670 'group_type' => 'Include',
672 $mailingGroup = $this->callAPISuccess('MailingGroup', 'create', $mgParams);
674 //Include previous mail in the mailing group.
675 $mail2 = $this->callAPISuccess('mailing', 'create', $this->_params
);
676 $params = array('mailing_id' => $mail2['id'], 'test_email' => NULL, 'test_group' => $groupID3);
677 $this->callAPISuccess($this->_entity
, 'send_test', $params);
680 'mailing_id' => $mail2['id'],
681 'entity_table' => 'civicrm_mailing',
682 'entity_id' => $mail['id'],
683 'group_type' => 'Include',
685 $mailingGroup = $this->callAPISuccess('MailingGroup', 'create', $mgParams);
686 //CRM-20431 - Delete group id that matches first mailing id.
687 $this->callAPISuccess('Group', 'delete', array('id' => $this->_groupID
));
688 $jobId = CRM_Core_DAO
::getFieldValue('CRM_Mailing_DAO_MailingJob', $mail2['id'], 'id', 'mailing_id');
689 $hash = CRM_Core_DAO
::getFieldValue('CRM_Mailing_Event_DAO_Queue', $jobId, 'hash', 'job_id');
690 $queueId = CRM_Core_DAO
::getFieldValue('CRM_Mailing_Event_DAO_Queue', $jobId, 'id', 'job_id');
692 $group = CRM_Mailing_Event_BAO_Unsubscribe
::unsub_from_mailing($jobId, $queueId, $hash, TRUE);
693 //Assert only one group returns in the unsubscribe list.
694 $this->assertCount(1, $group);
695 $this->assertEquals($groupID3, key($group));
701 public function testMailerStats() {
702 $result = $this->groupContactCreate($this->_groupID
, 100);
703 //verify if 100 contacts are added for group
704 $this->assertEquals(100, $result['added']);
706 //Create and send test mail first and change the mail job to live,
707 //because stats api only works on live mail
708 $mail = $this->callAPISuccess('mailing', 'create', $this->_params
);
709 $params = array('mailing_id' => $mail['id'], 'test_email' => NULL, 'test_group' => $this->_groupID
);
710 $deliveredInfo = $this->callAPISuccess($this->_entity
, 'send_test', $params);
711 $deliveredIds = implode(',', array_keys($deliveredInfo['values']));
713 //Change the test mail into live
714 $sql = "UPDATE civicrm_mailing_job SET is_test = 0 WHERE mailing_id = {$mail['id']}";
715 CRM_Core_DAO
::executeQuery($sql);
717 foreach (array('bounce', 'unsubscribe', 'opened') as $type) {
718 $sql = "CREATE TEMPORARY TABLE mail_{$type}_temp
719 (event_queue_id int, time_stamp datetime, delivered_id int)
720 SELECT event_queue_id, time_stamp, id
721 FROM civicrm_mailing_event_delivered
722 WHERE id IN ($deliveredIds)
723 ORDER BY RAND() LIMIT 0,20;";
724 CRM_Core_DAO
::executeQuery($sql);
726 $sql = "DELETE FROM civicrm_mailing_event_delivered WHERE id IN (SELECT delivered_id FROM mail_{$type}_temp);";
727 CRM_Core_DAO
::executeQuery($sql);
729 if ($type == 'unsubscribe') {
730 $sql = "INSERT INTO civicrm_mailing_event_{$type} (event_queue_id, time_stamp, org_unsubscribe)
731 SELECT event_queue_id, time_stamp, 1 FROM mail_{$type}_temp";
734 $sql = "INSERT INTO civicrm_mailing_event_{$type} (event_queue_id, time_stamp)
735 SELECT event_queue_id, time_stamp FROM mail_{$type}_temp";
737 CRM_Core_DAO
::executeQuery($sql);
740 $result = $this->callAPISuccess('mailing', 'stats', array('mailing_id' => $mail['id']));
741 $expectedResult = array(
742 //since among 100 mails 20 has been bounced
746 'Unique Clicks' => 0,
747 'Unsubscribers' => 20,
748 'delivered_rate' => '80%',
749 'opened_rate' => '25%',
750 'clickthrough_rate' => '0%',
752 $this->checkArrayEquals($expectedResult, $result['values'][$mail['id']]);
756 * Test civicrm_mailing_delete.
758 public function testMailerDeleteSuccess() {
759 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
760 $this->callAPIAndDocument($this->_entity
, 'delete', array('id' => $result['id']), __FUNCTION__
, __FILE__
);
761 $this->assertAPIDeleted($this->_entity
, $result['id']);
765 * Test Mailing.gettokens.
767 public function testMailGetTokens() {
768 $description = "Demonstrates fetching tokens for one or more entities (in this case \"Contact\" and \"Mailing\").
769 Optionally pass sequential=1 to have output ready-formatted for the select2 widget.";
770 $result = $this->callAPIAndDocument($this->_entity
, 'gettokens', array('entity' => array('Contact', 'Mailing')), __FUNCTION__
, __FILE__
, $description);
771 $this->assertContains('Contact Type', $result['values']);
773 // Check that passing "sequential" correctly outputs a hierarchical array
774 $result = $this->callAPISuccess($this->_entity
, 'gettokens', array('entity' => 'contact', 'sequential' => 1));
775 $this->assertArrayHasKey('text', $result['values'][0]);
776 $this->assertArrayHasKey('id', $result['values'][0]['children'][0]);
779 public function testClone() {
781 $groupIDs['inc'] = $this->groupCreate(array('name' => 'Example include group', 'title' => 'Example include group'));
782 $contactIDs['include_me'] = $this->individualCreate(array(
783 'email' => 'include.me@example.org',
784 'first_name' => 'Includer',
785 'last_name' => 'Person',
787 $this->callAPISuccess('GroupContact', 'create', array(
788 'group_id' => $groupIDs['inc'],
789 'contact_id' => $contactIDs['include_me'],
792 $params = $this->_params
;
793 $params['groups']['include'] = array($groupIDs['inc']);
794 $params['groups']['exclude'] = array();
795 $params['mailings']['include'] = array();
796 $params['mailings']['exclude'] = array();
799 $create = $this->callAPISuccess('Mailing', 'create', $params);
800 $created = $this->callAPISuccess('Mailing', 'get', []);
801 $createId = $create['id'];
802 $this->createLoggedInUser();
803 $clone = $this->callAPIAndDocument('Mailing', 'clone', array('id' => $create['id']), __FUNCTION__
, __FILE__
);
804 $cloneId = $clone['id'];
806 $this->assertNotEquals($createId, $cloneId, 'Create and clone should return different records');
807 $this->assertTrue(is_numeric($cloneId));
809 $this->assertNotEmpty($clone['values'][$cloneId]['subject']);
810 $this->assertEquals($params['subject'], $clone['values'][$cloneId]['subject'], "Cloned subject should match");
812 // created_id is special - populated based on current user (ie the cloner).
813 $this->assertNotEmpty($clone['values'][$cloneId]['created_id']);
814 $this->assertNotEquals($create['values'][$createId]['created_id'], $clone['values'][$cloneId]['created_id'], 'Clone should be created by a different person');
816 // Target groups+mailings are special.
817 $cloneGroups = $this->callAPISuccess('MailingGroup', 'get', array('mailing_id' => $cloneId, 'sequential' => 1));
818 $this->assertEquals(1, $cloneGroups['count']);
819 $this->assertEquals($cloneGroups['values'][0]['group_type'], 'Include');
820 $this->assertEquals($cloneGroups['values'][0]['entity_table'], 'civicrm_group');
821 $this->assertEquals($cloneGroups['values'][0]['entity_id'], $groupIDs['inc']);
824 //@ todo tests below here are all failure tests which are not hugely useful - need success tests
826 //------------ civicrm_mailing_event_bounce methods------------
829 * Test civicrm_mailing_event_bounce with wrong params.
830 * Note that tests like this are slightly better than no test but an
831 * api function cannot be considered supported / 'part of the api' without a
834 public function testMailerBounceWrongParams() {
836 'job_id' => 'Wrong ID',
837 'event_queue_id' => 'Wrong ID',
838 'hash' => 'Wrong Hash',
840 'time_stamp' => '20111109212100',
842 $this->callAPIFailure('mailing_event', 'bounce', $params,
843 'Queue event could not be found'
847 //----------- civicrm_mailing_event_confirm methods -----------
850 * Test civicrm_mailing_event_confirm with wrong params.
851 * Note that tests like this are slightly better than no test but an
852 * api function cannot be considered supported / 'part of the api' without a
855 public function testMailerConfirmWrongParams() {
857 'contact_id' => 'Wrong ID',
858 'subscribe_id' => 'Wrong ID',
859 'hash' => 'Wrong Hash',
860 'event_subscribe_id' => '123',
861 'time_stamp' => '20111111010101',
863 $this->callAPIFailure('mailing_event', 'confirm', $params,
864 'contact_id is not a valid integer'
868 //---------- civicrm_mailing_event_reply methods -----------
871 * Test civicrm_mailing_event_reply with wrong params.
873 * Note that tests like this are slightly better than no test but an
874 * api function cannot be considered supported / 'part of the api' without a
877 public function testMailerReplyWrongParams() {
879 'job_id' => 'Wrong ID',
880 'event_queue_id' => 'Wrong ID',
881 'hash' => 'Wrong Hash',
882 'bodyTxt' => 'Body...',
883 'replyTo' => $this->_email
,
884 'time_stamp' => '20111111010101',
886 $this->callAPIFailure('mailing_event', 'reply', $params,
887 'Queue event could not be found'
891 //----------- civicrm_mailing_event_forward methods ----------
894 * Test civicrm_mailing_event_forward with wrong params.
895 * Note that tests like this are slightly better than no test but an
896 * api function cannot be considered supported / 'part of the api' without a
899 public function testMailerForwardWrongParams() {
901 'job_id' => 'Wrong ID',
902 'event_queue_id' => 'Wrong ID',
903 'hash' => 'Wrong Hash',
904 'email' => $this->_email
,
905 'time_stamp' => '20111111010101',
907 $this->callAPIFailure('mailing_event', 'forward', $params,
908 'Queue event could not be found'
913 * @param array $params
914 * Extra parameters for the draft mailing.
917 public function createDraftMailing($params = array()) {
918 $createParams = array_merge($this->_params
, $params);
919 $createResult = $this->callAPISuccess('mailing', 'create', $createParams, __FUNCTION__
, __FILE__
);
920 $this->assertTrue(is_numeric($createResult['id']));
921 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_mailing_job WHERE mailing_id = %1', array(
922 1 => array($createResult['id'], 'Integer'),
924 return $createResult['id'];
928 * Test to make sure that if the event queue hashes have been archived,
929 * we can still have working click-trough URLs working (CRM-17959).
931 public function testUrlWithMissingTrackingHash() {
932 $mail = $this->callAPISuccess('mailing', 'create', $this->_params +
array('scheduled_date' => 'now'), __FUNCTION__
, __FILE__
);
933 $jobs = $this->callAPISuccess('mailing_job', 'get', array('mailing_id' => $mail['id']));
934 $this->assertEquals(1, $jobs['count']);
936 $params = array('mailing_id' => $mail['id'], 'test_email' => 'alice@example.org', 'test_group' => NULL);
937 $deliveredInfo = $this->callAPISuccess($this->_entity
, 'send_test', $params);
939 $sql = "SELECT turl.id as url_id, turl.url, q.id as queue_id
940 FROM civicrm_mailing_trackable_url as turl
941 INNER JOIN civicrm_mailing_job as j ON turl.mailing_id = j.mailing_id
942 INNER JOIN civicrm_mailing_event_queue q ON j.id = q.job_id
943 ORDER BY turl.id DESC LIMIT 1";
945 $dao = CRM_Core_DAO
::executeQuery($sql);
946 $this->assertTrue($dao->fetch());
948 $url = CRM_Mailing_Event_BAO_TrackableURLOpen
::track($dao->queue_id
, $dao->url_id
);
949 $this->assertContains('https://civicrm.org', $url);
951 // Now delete the event queue hashes and see if the tracking still works.
952 CRM_Core_DAO
::executeQuery('DELETE FROM civicrm_mailing_event_queue');
954 $url = CRM_Mailing_Event_BAO_TrackableURLOpen
::track($dao->queue_id
, $dao->url_id
);
955 $this->assertContains('https://civicrm.org', $url);
957 // Ensure that Google CSS link is not tracked.
958 $sql = "SELECT id FROM civicrm_mailing_trackable_url where url = 'https://fonts.googleapis.com/css?family=Roboto+Condensed:400,700|Zilla+Slab:500,700'";
959 $this->assertEquals([], CRM_Core_DAO
::executeQuery($sql)->fetchAll());
963 * Test Trackable URL with unicode character
965 public function testTrackableURLWithUnicodeSign() {
966 $unicodeURL = "https://civiĆcrm.org";
967 $this->_params
['body_text'] = str_replace("https://civicrm.org", $unicodeURL, $this->_params
['body_text']);
968 $this->_params
['body_html'] = str_replace("https://civicrm.org", $unicodeURL, $this->_params
['body_html']);
970 $mail = $this->callAPISuccess('mailing', 'create', $this->_params +
array('scheduled_date' => 'now'));
972 $params = array('mailing_id' => $mail['id'], 'test_email' => 'alice@example.org', 'test_group' => NULL);
973 $this->callAPISuccess($this->_entity
, 'send_test', $params);
975 $sql = "SELECT turl.id as url_id, turl.url, q.id as queue_id
976 FROM civicrm_mailing_trackable_url as turl
977 INNER JOIN civicrm_mailing_job as j ON turl.mailing_id = j.mailing_id
978 INNER JOIN civicrm_mailing_event_queue q ON j.id = q.job_id
979 ORDER BY turl.id DESC LIMIT 1";
981 $dao = CRM_Core_DAO
::executeQuery($sql);
982 $this->assertTrue($dao->fetch());
984 $url = CRM_Mailing_Event_BAO_TrackableURLOpen
::track($dao->queue_id
, $dao->url_id
);
985 $this->assertContains($unicodeURL, $url);
987 // Now delete the event queue hashes and see if the tracking still works.
988 CRM_Core_DAO
::executeQuery('DELETE FROM civicrm_mailing_event_queue');
990 $url = CRM_Mailing_Event_BAO_TrackableURLOpen
::track($dao->queue_id
, $dao->url_id
);
991 $this->assertContains($unicodeURL, $url);
995 * CRM-20892 : Test if Mail.create API throws error on update,
996 * if modified_date less then the date when the mail was last updated/created
998 public function testModifiedDateMismatchOnMailingUpdate() {
999 $mail = $this->callAPISuccess('mailing', 'create', $this->_params +
array('modified_date' => 'now'));
1001 $this->callAPISuccess('mailing', 'create', $this->_params +
array('id' => $mail['id'], 'modified_date' => '2 seconds ago'));
1003 catch (Exception
$e) {
1004 $this->assertRegExp("/Failure in api call for mailing create: Mailing has not been saved, Content maybe out of date, please refresh the page and try again/", $e->getMessage());