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 * Create a completed mailing (e.g when importing from a provider).
91 public function testMailerCreateCompleted() {
92 $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';
93 $this->_params
['is_completed'] = 1;
94 $result = $this->callAPIAndDocument('mailing', 'create', $this->_params +
array('scheduled_date' => 'now'), __FUNCTION__
, __FILE__
);
95 $jobs = $this->callAPISuccess('mailing_job', 'get', array('mailing_id' => $result['id']));
96 $this->assertEquals(1, $jobs['count']);
97 $this->assertEquals('Complete', $jobs['values'][$jobs['id']]['status']);
98 unset($this->_params
['created_id']); // return isn't working on this in getAndCheck so lets not check it for now
99 $this->getAndCheck($this->_params
, $result['id'], 'mailing');
103 * Per CRM-20316 the mailing should still create without created_id (not mandatory).
105 public function testMailerCreateSuccessNoCreatedID() {
106 unset($this->_params
['created_id']);
107 $result = $this->callAPIAndDocument('mailing', 'create', $this->_params +
array('scheduled_date' => 'now'), __FUNCTION__
, __FILE__
);
108 $this->getAndCheck($this->_params
, $result['id'], 'mailing');
114 public function testTemplateTypeOptions() {
115 $types = $this->callAPISuccess('Mailing', 'getoptions', array('field' => 'template_type'));
116 $this->assertTrue(isset($types['values']['traditional']));
120 * The `template_options` field should be treated a JSON object.
122 * This test will create, read, and update the field.
124 public function testMailerCreateTemplateOptions() {
125 // 1. Create mailing with template_options.
126 $params = $this->_params
;
127 $params['template_options'] = json_encode(array('foo' => 'bar_1'));
128 $createResult = $this->callAPISuccess('mailing', 'create', $params);
129 $id = $createResult['id'];
130 $this->assertDBQuery('{"foo":"bar_1"}', 'SELECT template_options FROM civicrm_mailing WHERE id = %1', array(
131 1 => array($id, 'Int'),
133 $this->assertEquals('bar_1', $createResult['values'][$id]['template_options']['foo']);
135 // 2. Get mailing with template_options.
136 $getResult = $this->callAPISuccess('mailing', 'get', array(
139 $this->assertEquals('bar_1', $getResult['values'][$id]['template_options']['foo']);
140 $getValueResult = $this->callAPISuccess('mailing', 'getvalue', array(
142 'return' => 'template_options',
144 $this->assertEquals('bar_1', $getValueResult['foo']);
146 // 3. Update mailing with template_options.
147 $updateResult = $this->callAPISuccess('mailing', 'create', array(
149 'template_options' => array('foo' => 'bar_2'),
151 $this->assertDBQuery('{"foo":"bar_2"}', 'SELECT template_options FROM civicrm_mailing WHERE id = %1', array(
152 1 => array($id, 'Int'),
154 $this->assertEquals('bar_2', $updateResult['values'][$id]['template_options']['foo']);
158 * The Mailing.create API supports magic properties "groups[include,enclude]" and "mailings[include,exclude]".
159 * Make sure these work
161 public function testMagicGroups_create_update() {
163 $groupIDs['a'] = $this->groupCreate(array('name' => 'Example include group', 'title' => 'Example include group'));
164 $groupIDs['b'] = $this->groupCreate(array('name' => 'Example exclude group', 'title' => 'Example exclude group'));
165 $contactIDs['a'] = $this->individualCreate(array(
166 'email' => 'include.me@example.org',
167 'first_name' => 'Includer',
168 'last_name' => 'Person',
170 $contactIDs['b'] = $this->individualCreate(array(
171 'email' => 'exclude.me@example.org',
172 'last_name' => 'Excluder',
174 $this->callAPISuccess('GroupContact', 'create', array(
175 'group_id' => $groupIDs['a'],
176 'contact_id' => $contactIDs['a'],
178 $this->callAPISuccess('GroupContact', 'create', array(
179 'group_id' => $groupIDs['b'],
180 'contact_id' => $contactIDs['b'],
185 $createParams = $this->_params
;
186 $createParams['groups']['include'] = array($groupIDs['a']);
187 $createParams['groups']['exclude'] = array();
188 $createParams['mailings']['include'] = array();
189 $createParams['mailings']['exclude'] = array();
190 $createParams['api.mailing_job.create'] = 1;
191 $createResult = $this->callAPISuccess('Mailing', 'create', $createParams);
192 $getGroup1 = $this->callAPISuccess('MailingGroup', 'get', array('mailing_id' => $createResult['id']));
193 $getGroup1_ids = array_values(CRM_Utils_Array
::collect('entity_id', $getGroup1['values']));
194 $this->assertEquals(array($groupIDs['a']), $getGroup1_ids);
195 $getRecipient1 = $this->callAPISuccess('MailingRecipients', 'get', array('mailing_id' => $createResult['id']));
196 $getRecipient1_ids = array_values(CRM_Utils_Array
::collect('contact_id', $getRecipient1['values']));
197 $this->assertEquals(array($contactIDs['a']), $getRecipient1_ids);
199 // ** Pass 2: Update without any changes to groups[include]
200 $nullOpParams = $createParams;
201 $nullOpParams['id'] = $createResult['id'];
202 $updateParams['api.mailing_job.create'] = 1;
203 unset($nullOpParams['groups']['include']);
204 $this->callAPISuccess('Mailing', 'create', $nullOpParams);
205 $getGroup2 = $this->callAPISuccess('MailingGroup', 'get', array('mailing_id' => $createResult['id']));
206 $getGroup2_ids = array_values(CRM_Utils_Array
::collect('entity_id', $getGroup2['values']));
207 $this->assertEquals(array($groupIDs['a']), $getGroup2_ids);
208 $getRecipient2 = $this->callAPISuccess('MailingRecipients', 'get', array('mailing_id' => $createResult['id']));
209 $getRecip2_ids = array_values(CRM_Utils_Array
::collect('contact_id', $getRecipient2['values']));
210 $this->assertEquals(array($contactIDs['a']), $getRecip2_ids);
212 // ** Pass 3: Update with different groups[include]
213 $updateParams = $createParams;
214 $updateParams['id'] = $createResult['id'];
215 $updateParams['groups']['include'] = array($groupIDs['b']);
216 $updateParams['api.mailing_job.create'] = 1;
217 $this->callAPISuccess('Mailing', 'create', $updateParams);
218 $getGroup3 = $this->callAPISuccess('MailingGroup', 'get', array('mailing_id' => $createResult['id']));
219 $getGroup3_ids = array_values(CRM_Utils_Array
::collect('entity_id', $getGroup3['values']));
220 $this->assertEquals(array($groupIDs['b']), $getGroup3_ids);
221 $getRecipient3 = $this->callAPISuccess('MailingRecipients', 'get', array('mailing_id' => $createResult['id']));
222 $getRecipient3_ids = array_values(CRM_Utils_Array
::collect('contact_id', $getRecipient3['values']));
223 $this->assertEquals(array($contactIDs['b']), $getRecipient3_ids);
226 public function testMailerPreview() {
228 $contactID = $this->individualCreate();
229 $displayName = $this->callAPISuccess('contact', 'get', array('id' => $contactID));
230 $displayName = $displayName['values'][$contactID]['display_name'];
231 $this->assertTrue(!empty($displayName));
233 $params = $this->_params
;
234 $params['api.Mailing.preview'] = array(
236 'contact_id' => $contactID,
238 $params['options']['force_rollback'] = 1;
242 'mailing' => CRM_Core_DAO
::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing'),
243 'job' => CRM_Core_DAO
::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_job'),
244 'group' => CRM_Core_DAO
::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_group'),
245 'recipient' => CRM_Core_DAO
::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_recipients'),
247 $result = $this->callAPISuccess('mailing', 'create', $params);
248 $this->assertDBQuery($maxIDs['mailing'], 'SELECT MAX(id) FROM civicrm_mailing'); // 'Preview should not create any mailing records'
249 $this->assertDBQuery($maxIDs['job'], 'SELECT MAX(id) FROM civicrm_mailing_job'); // 'Preview should not create any mailing_job record'
250 $this->assertDBQuery($maxIDs['group'], 'SELECT MAX(id) FROM civicrm_mailing_group'); // 'Preview should not create any mailing_group records'
251 $this->assertDBQuery($maxIDs['recipient'], 'SELECT MAX(id) FROM civicrm_mailing_recipients'); // 'Preview should not create any mailing_recipient records'
253 $previewResult = $result['values'][$result['id']]['api.Mailing.preview'];
254 $this->assertEquals("Hello $displayName", $previewResult['values']['subject']);
255 $this->assertContains("This is $displayName", $previewResult['values']['body_text']);
256 $this->assertContains("<p>This is $displayName.</p>", $previewResult['values']['body_html']);
259 public function testMailerPreviewRecipients() {
261 $groupIDs['inc'] = $this->groupCreate(array('name' => 'Example include group', 'title' => 'Example include group'));
262 $groupIDs['exc'] = $this->groupCreate(array('name' => 'Example exclude group', 'title' => 'Example exclude group'));
263 $contactIDs['include_me'] = $this->individualCreate(array(
264 'email' => 'include.me@example.org',
265 'first_name' => 'Includer',
266 'last_name' => 'Person',
268 $contactIDs['exclude_me'] = $this->individualCreate(array(
269 'email' => 'exclude.me@example.org',
270 'last_name' => 'Excluder',
272 $this->callAPISuccess('GroupContact', 'create', array(
273 'group_id' => $groupIDs['inc'],
274 'contact_id' => $contactIDs['include_me'],
276 $this->callAPISuccess('GroupContact', 'create', array(
277 'group_id' => $groupIDs['inc'],
278 'contact_id' => $contactIDs['exclude_me'],
280 $this->callAPISuccess('GroupContact', 'create', array(
281 'group_id' => $groupIDs['exc'],
282 'contact_id' => $contactIDs['exclude_me'],
285 $params = $this->_params
;
286 $params['groups']['include'] = array($groupIDs['inc']);
287 $params['groups']['exclude'] = array($groupIDs['exc']);
288 $params['mailings']['include'] = array();
289 $params['mailings']['exclude'] = array();
290 $params['options']['force_rollback'] = 1;
291 $params['api.mailing_job.create'] = 1;
292 $params['api.MailingRecipients.get'] = array(
293 'mailing_id' => '$value.id',
294 'api.contact.getvalue' => array(
295 'return' => 'display_name',
297 'api.email.getvalue' => array(
304 'mailing' => CRM_Core_DAO
::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing'),
305 'job' => CRM_Core_DAO
::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_job'),
306 'group' => CRM_Core_DAO
::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_group'),
308 $create = $this->callAPIAndDocument('Mailing', 'create', $params, __FUNCTION__
, __FILE__
);
309 $this->assertDBQuery($maxIDs['mailing'], 'SELECT MAX(id) FROM civicrm_mailing'); // 'Preview should not create any mailing records'
310 $this->assertDBQuery($maxIDs['job'], 'SELECT MAX(id) FROM civicrm_mailing_job'); // 'Preview should not create any mailing_job record'
311 $this->assertDBQuery($maxIDs['group'], 'SELECT MAX(id) FROM civicrm_mailing_group'); // 'Preview should not create any mailing_group records'
313 $preview = $create['values'][$create['id']]['api.MailingRecipients.get'];
314 $previewIds = array_values(CRM_Utils_Array
::collect('contact_id', $preview['values']));
315 $this->assertEquals(array((string) $contactIDs['include_me']), $previewIds);
316 $previewEmails = array_values(CRM_Utils_Array
::collect('api.email.getvalue', $preview['values']));
317 $this->assertEquals(array('include.me@example.org'), $previewEmails);
318 $previewNames = array_values(CRM_Utils_Array
::collect('api.contact.getvalue', $preview['values']));
319 $this->assertTrue((bool) preg_match('/Includer Person/', $previewNames[0]), "Name 'Includer Person' should appear in '" . $previewNames[0] . '"');
322 public function testMailerPreviewRecipientsDeduplicate() {
324 $groupIDs['grp'] = $this->groupCreate(array('name' => 'Example group', 'title' => 'Example group'));
325 $contactIDs['include_me'] = $this->individualCreate(array(
326 'email' => 'include.me@example.org',
327 'first_name' => 'Includer',
328 'last_name' => 'Person',
330 $contactIDs['include_me_duplicate'] = $this->individualCreate(array(
331 'email' => 'include.me@example.org',
332 'first_name' => 'IncluderDuplicate',
333 'last_name' => 'Person',
335 $this->callAPISuccess('GroupContact', 'create', array(
336 'group_id' => $groupIDs['grp'],
337 'contact_id' => $contactIDs['include_me'],
339 $this->callAPISuccess('GroupContact', 'create', array(
340 'group_id' => $groupIDs['grp'],
341 'contact_id' => $contactIDs['include_me_duplicate'],
344 $params = $this->_params
;
345 $params['groups']['include'] = array($groupIDs['grp']);
346 $params['mailings']['include'] = array();
347 $params['options']['force_rollback'] = 1;
348 $params['dedupe_email'] = 1;
349 $params['api.mailing_job.create'] = 1;
350 $params['api.MailingRecipients.get'] = array(
351 'mailing_id' => '$value.id',
352 'api.contact.getvalue' => array(
353 'return' => 'display_name',
355 'api.email.getvalue' => array(
361 $create = $this->callAPISuccess('Mailing', 'create', $params);
363 $preview = $create['values'][$create['id']]['api.MailingRecipients.get'];
364 $this->assertEquals(1, $preview['count']);
365 $previewEmails = array_values(CRM_Utils_Array
::collect('api.email.getvalue', $preview['values']));
366 $this->assertEquals(array('include.me@example.org'), $previewEmails);
372 public function testMailerSendTest_email() {
373 $contactIDs['alice'] = $this->individualCreate(array(
374 'email' => 'alice@example.org',
375 'first_name' => 'Alice',
376 'last_name' => 'Person',
379 $mail = $this->callAPISuccess('mailing', 'create', $this->_params
);
381 $params = array('mailing_id' => $mail['id'], 'test_email' => 'alice@example.org', 'test_group' => NULL);
382 $deliveredInfo = $this->callAPISuccess($this->_entity
, 'send_test', $params);
383 $this->assertEquals(1, $deliveredInfo['count'], "in line " . __LINE__
); // verify mail has been sent to user by count
385 $deliveredContacts = array_values(CRM_Utils_Array
::collect('contact_id', $deliveredInfo['values']));
386 $this->assertEquals(array($contactIDs['alice']), $deliveredContacts);
388 $deliveredEmails = array_values(CRM_Utils_Array
::collect('email', $deliveredInfo['values']));
389 $this->assertEquals(array('alice@example.org'), $deliveredEmails);
395 public function testMailerSendTest_group() {
397 $groupIDs['inc'] = $this->groupCreate(array('name' => 'Example include group', 'title' => 'Example include group'));
398 $contactIDs['alice'] = $this->individualCreate(array(
399 'email' => 'alice@example.org',
400 'first_name' => 'Alice',
401 'last_name' => 'Person',
403 $contactIDs['bob'] = $this->individualCreate(array(
404 'email' => 'bob@example.org',
405 'first_name' => 'Bob',
406 'last_name' => 'Person',
408 $contactIDs['carol'] = $this->individualCreate(array(
409 'email' => 'carol@example.org',
410 'first_name' => 'Carol',
411 'last_name' => 'Person',
413 $this->callAPISuccess('GroupContact', 'create', array(
414 'group_id' => $groupIDs['inc'],
415 'contact_id' => $contactIDs['alice'],
417 $this->callAPISuccess('GroupContact', 'create', array(
418 'group_id' => $groupIDs['inc'],
419 'contact_id' => $contactIDs['bob'],
421 $this->callAPISuccess('GroupContact', 'create', array(
422 'group_id' => $groupIDs['inc'],
423 'contact_id' => $contactIDs['carol'],
427 $mail = $this->callAPISuccess('mailing', 'create', $this->_params
);
428 $deliveredInfo = $this->callAPISuccess($this->_entity
, 'send_test', array(
429 'mailing_id' => $mail['id'],
430 'test_email' => NULL,
431 'test_group' => $groupIDs['inc'],
433 $this->assertEquals(3, $deliveredInfo['count'], "in line " . __LINE__
); // verify mail has been sent to user by count
435 $deliveredContacts = array_values(CRM_Utils_Array
::collect('contact_id', $deliveredInfo['values']));
436 $this->assertEquals(array($contactIDs['alice'], $contactIDs['bob'], $contactIDs['carol']), $deliveredContacts);
438 $deliveredEmails = array_values(CRM_Utils_Array
::collect('email', $deliveredInfo['values']));
439 $this->assertEquals(array('alice@example.org', 'bob@example.org', 'carol@example.org'), $deliveredEmails);
445 public function submitProvider() {
446 $cases = array(); // $useLogin, $params, $expectedFailure, $expectedJobCount
449 array(), // createParams
450 array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
451 FALSE, // expectedFailure
452 1, // expectedJobCount
456 array(), // createParams
457 array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
458 "/Failed to determine current user/", // expectedFailure
459 0, // expectedJobCount
463 array(), // createParams
464 array('scheduled_date' => '2014-12-13 10:00:00'),
465 FALSE, // expectedFailure
466 1, // expectedJobCount
470 array(), // createParams
472 "/Missing parameter scheduled_date and.or approval_date/", // expectedFailure
473 0, // expectedJobCount
477 array('name' => ''), // 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 \\(name\\)./", // expectedFailure
480 0, // expectedJobCount
484 array('body_html' => '', 'body_text' => ''), // 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\\)./", // expectedFailure
487 0, // expectedJobCount
491 array('body_html' => 'Oops, did I leave my tokens at home?'), // createParams
492 array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
493 "/Mailing cannot be sent. There are missing or invalid fields \\(.*body_html.*optOut.*\\)./", // expectedFailure
494 0, // expectedJobCount
498 array('body_text' => 'Oops, did I leave my tokens at home?'), // createParams
499 array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
500 "/Mailing cannot be sent. There are missing or invalid fields \\(.*body_text.*optOut.*\\)./", // expectedFailure
501 0, // expectedJobCount
505 array('body_text' => 'Look ma, magic tokens in the text!', 'footer_id' => '%FOOTER%'), // createParams
506 array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
507 FALSE, // expectedFailure
508 1, // expectedJobCount
512 array('body_html' => '<p>Look ma, magic tokens in the markup!</p>', 'footer_id' => '%FOOTER%'), // createParams
513 array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
514 FALSE, // expectedFailure
515 1, // expectedJobCount
521 * @param bool $useLogin
522 * @param array $createParams
523 * @param array $submitParams
524 * @param null|string $expectedFailure
525 * @param int $expectedJobCount
526 * @dataProvider submitProvider
528 public function testMailerSubmit($useLogin, $createParams, $submitParams, $expectedFailure, $expectedJobCount) {
530 $this->createLoggedInUser();
533 if (isset($createParams['footer_id']) && $createParams['footer_id'] == '%FOOTER%') {
534 $createParams['footer_id'] = $this->footer
['id'];
537 $id = $this->createDraftMailing($createParams);
539 $submitParams['id'] = $id;
540 if ($expectedFailure) {
541 $submitResult = $this->callAPIFailure('mailing', 'submit', $submitParams);
542 $this->assertRegExp($expectedFailure, $submitResult['error_message']);
545 $submitResult = $this->callAPIAndDocument('mailing', 'submit', $submitParams, __FUNCTION__
, __FILE__
);
546 $this->assertTrue(is_numeric($submitResult['id']));
547 $this->assertTrue(is_numeric($submitResult['values'][$id]['scheduled_id']));
548 $this->assertEquals($submitParams['scheduled_date'], $submitResult['values'][$id]['scheduled_date']);
550 $this->assertDBQuery($expectedJobCount, 'SELECT count(*) FROM civicrm_mailing_job WHERE mailing_id = %1', array(
551 1 => array($id, 'Integer'),
556 * Test unsubscribe list contains correct groups
557 * when include = 'previous mailing'
559 public function testUnsubscribeGroupList() {
560 // Create set of groups and add a contact to both of them.
561 $groupID2 = $this->groupCreate(array('name' => 'Test group 2', 'title' => 'group title 2'));
562 $groupID3 = $this->groupCreate(array('name' => 'Test group 3', 'title' => 'group title 3'));
563 $contactId = $this->individualCreate();
564 foreach (array($groupID2, $groupID3) as $grp) {
566 'contact_id' => $contactId,
569 $this->callAPISuccess('GroupContact', 'create', $params);
572 //Send mail to groupID3
573 $mail = $this->callAPISuccess('mailing', 'create', $this->_params
);
574 $params = array('mailing_id' => $mail['id'], 'test_email' => NULL, 'test_group' => $groupID3);
575 $this->callAPISuccess($this->_entity
, 'send_test', $params);
578 'mailing_id' => $mail['id'],
579 'entity_table' => 'civicrm_group',
580 'entity_id' => $groupID3,
581 'group_type' => 'Include',
583 $mailingGroup = $this->callAPISuccess('MailingGroup', 'create', $mgParams);
585 //Include previous mail in the mailing group.
586 $mail2 = $this->callAPISuccess('mailing', 'create', $this->_params
);
587 $params = array('mailing_id' => $mail2['id'], 'test_email' => NULL, 'test_group' => $groupID3);
588 $this->callAPISuccess($this->_entity
, 'send_test', $params);
591 'mailing_id' => $mail2['id'],
592 'entity_table' => 'civicrm_mailing',
593 'entity_id' => $mail['id'],
594 'group_type' => 'Include',
596 $mailingGroup = $this->callAPISuccess('MailingGroup', 'create', $mgParams);
597 //CRM-20431 - Delete group id that matches first mailing id.
598 $this->callAPISuccess('Group', 'delete', array('id' => $this->_groupID
));
599 $jobId = CRM_Core_DAO
::getFieldValue('CRM_Mailing_DAO_MailingJob', $mail2['id'], 'id', 'mailing_id');
600 $hash = CRM_Core_DAO
::getFieldValue('CRM_Mailing_Event_DAO_Queue', $jobId, 'hash', 'job_id');
601 $queueId = CRM_Core_DAO
::getFieldValue('CRM_Mailing_Event_DAO_Queue', $jobId, 'id', 'job_id');
603 $group = CRM_Mailing_Event_BAO_Unsubscribe
::unsub_from_mailing($jobId, $queueId, $hash, TRUE);
604 //Assert only one group returns in the unsubscribe list.
605 $this->assertCount(1, $group);
606 $this->assertEquals($groupID3, key($group));
612 public function testMailerStats() {
613 $result = $this->groupContactCreate($this->_groupID
, 100);
614 $this->assertEquals(100, $result['added']); //verify if 100 contacts are added for group
616 //Create and send test mail first and change the mail job to live,
617 //because stats api only works on live mail
618 $mail = $this->callAPISuccess('mailing', 'create', $this->_params
);
619 $params = array('mailing_id' => $mail['id'], 'test_email' => NULL, 'test_group' => $this->_groupID
);
620 $deliveredInfo = $this->callAPISuccess($this->_entity
, 'send_test', $params);
621 $deliveredIds = implode(',', array_keys($deliveredInfo['values']));
623 //Change the test mail into live
624 $sql = "UPDATE civicrm_mailing_job SET is_test = 0 WHERE mailing_id = {$mail['id']}";
625 CRM_Core_DAO
::executeQuery($sql);
627 foreach (array('bounce', 'unsubscribe', 'opened') as $type) {
628 $sql = "CREATE TEMPORARY TABLE mail_{$type}_temp
629 (event_queue_id int, time_stamp datetime, delivered_id int)
630 SELECT event_queue_id, time_stamp, id
631 FROM civicrm_mailing_event_delivered
632 WHERE id IN ($deliveredIds)
633 ORDER BY RAND() LIMIT 0,20;";
634 CRM_Core_DAO
::executeQuery($sql);
636 $sql = "DELETE FROM civicrm_mailing_event_delivered WHERE id IN (SELECT delivered_id FROM mail_{$type}_temp);";
637 CRM_Core_DAO
::executeQuery($sql);
639 if ($type == 'unsubscribe') {
640 $sql = "INSERT INTO civicrm_mailing_event_{$type} (event_queue_id, time_stamp, org_unsubscribe)
641 SELECT event_queue_id, time_stamp, 1 FROM mail_{$type}_temp";
644 $sql = "INSERT INTO civicrm_mailing_event_{$type} (event_queue_id, time_stamp)
645 SELECT event_queue_id, time_stamp FROM mail_{$type}_temp";
647 CRM_Core_DAO
::executeQuery($sql);
650 $result = $this->callAPISuccess('mailing', 'stats', array('mailing_id' => $mail['id']));
651 $expectedResult = array(
652 'Delivered' => 80, //since among 100 mails 20 has been bounced
655 'Unique Clicks' => 0,
656 'Unsubscribers' => 20,
658 $this->checkArrayEquals($expectedResult, $result['values'][$mail['id']]);
662 * Test civicrm_mailing_delete.
664 public function testMailerDeleteSuccess() {
665 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
666 $this->callAPIAndDocument($this->_entity
, 'delete', array('id' => $result['id']), __FUNCTION__
, __FILE__
);
667 $this->assertAPIDeleted($this->_entity
, $result['id']);
671 * Test Mailing.gettokens.
673 public function testMailGetTokens() {
674 $description = "Demonstrates fetching tokens for one or more entities (in this case \"Contact\" and \"Mailing\").
675 Optionally pass sequential=1 to have output ready-formatted for the select2 widget.";
676 $result = $this->callAPIAndDocument($this->_entity
, 'gettokens', array('entity' => array('Contact', 'Mailing')), __FUNCTION__
, __FILE__
, $description);
677 $this->assertContains('Contact Type', $result['values']);
679 // Check that passing "sequential" correctly outputs a hierarchical array
680 $result = $this->callAPISuccess($this->_entity
, 'gettokens', array('entity' => 'contact', 'sequential' => 1));
681 $this->assertArrayHasKey('text', $result['values'][0]);
682 $this->assertArrayHasKey('id', $result['values'][0]['children'][0]);
685 public function testClone() {
687 $groupIDs['inc'] = $this->groupCreate(array('name' => 'Example include group', 'title' => 'Example include group'));
688 $contactIDs['include_me'] = $this->individualCreate(array(
689 'email' => 'include.me@example.org',
690 'first_name' => 'Includer',
691 'last_name' => 'Person',
693 $this->callAPISuccess('GroupContact', 'create', array(
694 'group_id' => $groupIDs['inc'],
695 'contact_id' => $contactIDs['include_me'],
698 $params = $this->_params
;
699 $params['groups']['include'] = array($groupIDs['inc']);
700 $params['groups']['exclude'] = array();
701 $params['mailings']['include'] = array();
702 $params['mailings']['exclude'] = array();
705 $create = $this->callAPISuccess('Mailing', 'create', $params);
706 $createId = $create['id'];
707 $this->createLoggedInUser();
708 $clone = $this->callAPIAndDocument('Mailing', 'clone', array('id' => $create['id']), __FUNCTION__
, __FILE__
);
709 $cloneId = $clone['id'];
711 $this->assertNotEquals($createId, $cloneId, 'Create and clone should return different records');
712 $this->assertTrue(is_numeric($cloneId));
714 $this->assertNotEmpty($clone['values'][$cloneId]['subject']);
715 $this->assertEquals($params['subject'], $clone['values'][$cloneId]['subject'], "Cloned subject should match");
717 // created_id is special - populated based on current user (ie the cloner).
718 $this->assertNotEmpty($clone['values'][$cloneId]['created_id']);
719 $this->assertNotEquals($create['values'][$createId]['created_id'], $clone['values'][$cloneId]['created_id'], 'Clone should be created by a different person');
721 // Target groups+mailings are special.
722 $cloneGroups = $this->callAPISuccess('MailingGroup', 'get', array('mailing_id' => $cloneId, 'sequential' => 1));
723 $this->assertEquals(1, $cloneGroups['count']);
724 $this->assertEquals($cloneGroups['values'][0]['group_type'], 'Include');
725 $this->assertEquals($cloneGroups['values'][0]['entity_table'], 'civicrm_group');
726 $this->assertEquals($cloneGroups['values'][0]['entity_id'], $groupIDs['inc']);
729 //@ todo tests below here are all failure tests which are not hugely useful - need success tests
731 //------------ civicrm_mailing_event_bounce methods------------
734 * Test civicrm_mailing_event_bounce with wrong params.
735 * Note that tests like this are slightly better than no test but an
736 * api function cannot be considered supported / 'part of the api' without a
739 public function testMailerBounceWrongParams() {
741 'job_id' => 'Wrong ID',
742 'event_queue_id' => 'Wrong ID',
743 'hash' => 'Wrong Hash',
745 'time_stamp' => '20111109212100',
747 $this->callAPIFailure('mailing_event', 'bounce', $params,
748 'Queue event could not be found'
752 //----------- civicrm_mailing_event_confirm methods -----------
755 * Test civicrm_mailing_event_confirm with wrong params.
756 * Note that tests like this are slightly better than no test but an
757 * api function cannot be considered supported / 'part of the api' without a
760 public function testMailerConfirmWrongParams() {
762 'contact_id' => 'Wrong ID',
763 'subscribe_id' => 'Wrong ID',
764 'hash' => 'Wrong Hash',
765 'event_subscribe_id' => '123',
766 'time_stamp' => '20111111010101',
768 $this->callAPIFailure('mailing_event', 'confirm', $params,
769 'contact_id is not a valid integer'
773 //---------- civicrm_mailing_event_reply methods -----------
776 * Test civicrm_mailing_event_reply with wrong params.
778 * Note that tests like this are slightly better than no test but an
779 * api function cannot be considered supported / 'part of the api' without a
782 public function testMailerReplyWrongParams() {
784 'job_id' => 'Wrong ID',
785 'event_queue_id' => 'Wrong ID',
786 'hash' => 'Wrong Hash',
787 'bodyTxt' => 'Body...',
788 'replyTo' => $this->_email
,
789 'time_stamp' => '20111111010101',
791 $this->callAPIFailure('mailing_event', 'reply', $params,
792 'Queue event could not be found'
797 //----------- civicrm_mailing_event_forward methods ----------
800 * Test civicrm_mailing_event_forward with wrong params.
801 * Note that tests like this are slightly better than no test but an
802 * api function cannot be considered supported / 'part of the api' without a
805 public function testMailerForwardWrongParams() {
807 'job_id' => 'Wrong ID',
808 'event_queue_id' => 'Wrong ID',
809 'hash' => 'Wrong Hash',
810 'email' => $this->_email
,
811 'time_stamp' => '20111111010101',
813 $this->callAPIFailure('mailing_event', 'forward', $params,
814 'Queue event could not be found'
819 * @param array $params
820 * Extra parameters for the draft mailing.
823 public function createDraftMailing($params = array()) {
824 $createParams = array_merge($this->_params
, $params);
825 $createResult = $this->callAPISuccess('mailing', 'create', $createParams, __FUNCTION__
, __FILE__
);
826 $this->assertTrue(is_numeric($createResult['id']));
827 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_mailing_job WHERE mailing_id = %1', array(
828 1 => array($createResult['id'], 'Integer'),
830 return $createResult['id'];
834 * Test to make sure that if the event queue hashes have been archived,
835 * we can still have working click-trough URLs working (CRM-17959).
837 public function testUrlWithMissingTrackingHash() {
838 $mail = $this->callAPISuccess('mailing', 'create', $this->_params +
array('scheduled_date' => 'now'), __FUNCTION__
, __FILE__
);
839 $jobs = $this->callAPISuccess('mailing_job', 'get', array('mailing_id' => $mail['id']));
840 $this->assertEquals(1, $jobs['count']);
842 $params = array('mailing_id' => $mail['id'], 'test_email' => 'alice@example.org', 'test_group' => NULL);
843 $deliveredInfo = $this->callAPISuccess($this->_entity
, 'send_test', $params);
845 $sql = "SELECT turl.id as url_id, turl.url, q.id as queue_id
846 FROM civicrm_mailing_trackable_url as turl
847 INNER JOIN civicrm_mailing_job as j ON turl.mailing_id = j.mailing_id
848 INNER JOIN civicrm_mailing_event_queue q ON j.id = q.job_id
849 ORDER BY turl.id DESC LIMIT 1";
851 $dao = CRM_Core_DAO
::executeQuery($sql);
852 $this->assertTrue($dao->fetch());
854 $url = CRM_Mailing_Event_BAO_TrackableURLOpen
::track($dao->queue_id
, $dao->url_id
);
855 $this->assertContains('https://civicrm.org', $url);
857 // Now delete the event queue hashes and see if the tracking still works.
858 CRM_Core_DAO
::executeQuery('DELETE FROM civicrm_mailing_event_queue');
860 $url = CRM_Mailing_Event_BAO_TrackableURLOpen
::track($dao->queue_id
, $dao->url_id
);
861 $this->assertContains('https://civicrm.org', $url);
865 * Test Trackable URL with unicode character
867 public function testTrackableURLWithUnicodeSign() {
868 $unicodeURL = "https://civiĆcrm.org";
869 $this->_params
['body_text'] = str_replace("https://civicrm.org", $unicodeURL, $this->_params
['body_text']);
870 $this->_params
['body_html'] = str_replace("https://civicrm.org", $unicodeURL, $this->_params
['body_html']);
872 $mail = $this->callAPISuccess('mailing', 'create', $this->_params +
array('scheduled_date' => 'now'));
874 $params = array('mailing_id' => $mail['id'], 'test_email' => 'alice@example.org', 'test_group' => NULL);
875 $this->callAPISuccess($this->_entity
, 'send_test', $params);
877 $sql = "SELECT turl.id as url_id, turl.url, q.id as queue_id
878 FROM civicrm_mailing_trackable_url as turl
879 INNER JOIN civicrm_mailing_job as j ON turl.mailing_id = j.mailing_id
880 INNER JOIN civicrm_mailing_event_queue q ON j.id = q.job_id
881 ORDER BY turl.id DESC LIMIT 1";
883 $dao = CRM_Core_DAO
::executeQuery($sql);
884 $this->assertTrue($dao->fetch());
886 $url = CRM_Mailing_Event_BAO_TrackableURLOpen
::track($dao->queue_id
, $dao->url_id
);
887 $this->assertContains($unicodeURL, $url);
889 // Now delete the event queue hashes and see if the tracking still works.
890 CRM_Core_DAO
::executeQuery('DELETE FROM civicrm_mailing_event_queue');
892 $url = CRM_Mailing_Event_BAO_TrackableURLOpen
::track($dao->queue_id
, $dao->url_id
);
893 $this->assertContains($unicodeURL, $url);
897 * CRM-20892 : Test if Mail.create API throws error on update,
898 * if modified_date less then the date when the mail was last updated/created
900 public function testModifiedDateMismatchOnMailingUpdate() {
901 $mail = $this->callAPISuccess('mailing', 'create', $this->_params +
array('modified_date' => 'now'));
903 $this->callAPISuccess('mailing', 'create', $this->_params +
array('id' => $mail['id'], 'modified_date' => '2 seconds ago'));
905 catch (Exception
$e) {
906 $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());