Merge pull request #14119 from eileenmcnaughton/recur_meta
[civicrm-core.git] / tests / phpunit / api / v3 / MailingTest.php
1 <?php
2 /*
3 * File for the TestMailing class
4 *
5 * (PHP 5)
6 *
7 * @package CiviCRM
8 *
9 * This file is part of CiviCRM
10 *
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.
15 *
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.
20 *
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/>.
24 */
25
26
27 /**
28 * Test APIv3 civicrm_mailing_* functions
29 *
30 * @package CiviCRM
31 * @group headless
32 */
33 class api_v3_MailingTest extends CiviUnitTestCase {
34 protected $_groupID;
35 protected $_email;
36 protected $_apiversion = 3;
37 protected $_params = array();
38 protected $_entity = 'Mailing';
39 protected $_contactID;
40
41 /**
42 * APIv3 result from creating an example footer
43 * @var array
44 */
45 protected $footer;
46
47 public function setUp() {
48 parent::setUp();
49 $this->useTransaction();
50 // DGW
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,
61 'header_id' => '',
62 'footer_id' => '',
63 );
64
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}.',
70 ));
71 }
72
73 public function tearDown() {
74 // DGW
75 CRM_Mailing_BAO_MailingJob::$mailsProcessed = 0;
76 parent::tearDown();
77 }
78
79 /**
80 * Test civicrm_mailing_create.
81 */
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');
89 }
90
91 /**
92 * Tes that the parameter _skip_evil_bao_auto_schedule_ is respected & prevents jobs being created.
93 */
94 public function testSkipAutoSchedule() {
95 $this->callAPISuccess('Mailing', 'create', array_merge($this->_params, [
96 '_skip_evil_bao_auto_schedule_' => TRUE,
97 'scheduled_date' => 'now',
98 ]));
99 $this->callAPISuccessGetCount('Mailing', [], 1);
100 $this->callAPISuccessGetCount('MailingJob', [], 0);
101 }
102
103 /**
104 * Create a completed mailing (e.g when importing from a provider).
105 */
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');
116 }
117
118 /**
119 * Per CRM-20316 the mailing should still create without created_id (not mandatory).
120 */
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');
125 }
126
127 /**
128 *
129 */
130 public function testTemplateTypeOptions() {
131 $types = $this->callAPISuccess('Mailing', 'getoptions', array('field' => 'template_type'));
132 $this->assertTrue(isset($types['values']['traditional']));
133 }
134
135 /**
136 * The `template_options` field should be treated a JSON object.
137 *
138 * This test will create, read, and update the field.
139 */
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'),
148 ));
149 $this->assertEquals('bar_1', $createResult['values'][$id]['template_options']['foo']);
150
151 // 2. Get mailing with template_options.
152 $getResult = $this->callAPISuccess('mailing', 'get', array(
153 'id' => $id,
154 ));
155 $this->assertEquals('bar_1', $getResult['values'][$id]['template_options']['foo']);
156 $getValueResult = $this->callAPISuccess('mailing', 'getvalue', array(
157 'id' => $id,
158 'return' => 'template_options',
159 ));
160 $this->assertEquals('bar_1', $getValueResult['foo']);
161
162 // 3. Update mailing with template_options.
163 $updateResult = $this->callAPISuccess('mailing', 'create', array(
164 'id' => $id,
165 'template_options' => array('foo' => 'bar_2'),
166 ));
167 $this->assertDBQuery('{"foo":"bar_2"}', 'SELECT template_options FROM civicrm_mailing WHERE id = %1', array(
168 1 => array($id, 'Int'),
169 ));
170 $this->assertEquals('bar_2', $updateResult['values'][$id]['template_options']['foo']);
171 }
172
173 /**
174 * The Mailing.create API supports magic properties "groups[include,enclude]" and "mailings[include,exclude]".
175 * Make sure these work
176 */
177 public function testMagicGroups_create_update() {
178 // BEGIN SAMPLE DATA
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',
185 ));
186 $contactIDs['b'] = $this->individualCreate(array(
187 'email' => 'exclude.me@example.org',
188 'last_name' => 'Excluder',
189 ));
190 $this->callAPISuccess('GroupContact', 'create', array(
191 'group_id' => $groupIDs['a'],
192 'contact_id' => $contactIDs['a'],
193 ));
194 $this->callAPISuccess('GroupContact', 'create', array(
195 'group_id' => $groupIDs['b'],
196 'contact_id' => $contactIDs['b'],
197 ));
198 // END SAMPLE DATA
199
200 // ** Pass 1: Create
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);
214
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);
227
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);
240 }
241
242 public function testMailerPreview() {
243 // BEGIN SAMPLE DATA
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));
248
249 $params = $this->_params;
250 $params['api.Mailing.preview'] = array(
251 'id' => '$value.id',
252 'contact_id' => $contactID,
253 );
254 $params['options']['force_rollback'] = 1;
255 // END SAMPLE DATA
256
257 $maxIDs = array(
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'),
262 );
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;amp;reset=1&amp;jid=&amp;qid=&amp;h=">Forward this email with no protocol</a>', $previewResult['values']['body_html']);
278 $this->assertNotContains("http://http://", $previewResult['values']['body_html']);
279 }
280
281 public function testMailerPreviewRecipients() {
282 // BEGIN SAMPLE DATA
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',
289 ));
290 $contactIDs['exclude_me'] = $this->individualCreate(array(
291 'email' => 'exclude.me@example.org',
292 'last_name' => 'Excluder',
293 ));
294 $this->callAPISuccess('GroupContact', 'create', array(
295 'group_id' => $groupIDs['inc'],
296 'contact_id' => $contactIDs['include_me'],
297 ));
298 $this->callAPISuccess('GroupContact', 'create', array(
299 'group_id' => $groupIDs['inc'],
300 'contact_id' => $contactIDs['exclude_me'],
301 ));
302 $this->callAPISuccess('GroupContact', 'create', array(
303 'group_id' => $groupIDs['exc'],
304 'contact_id' => $contactIDs['exclude_me'],
305 ));
306
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',
317 ),
318 'api.email.getvalue' => array(
319 'return' => 'email',
320 ),
321 );
322 // END SAMPLE DATA
323
324 $maxIDs = 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'),
327 );
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');
333
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] . '"');
341 }
342
343 /**
344 * Test if Mailing recipients include duplicate OR on_hold emails
345 */
346 public function testMailerPreviewRecipientsDeduplicateAndOnholdEmails() {
347 // BEGIN SAMPLE DATA
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',
353 ));
354 $contactIDs['include_me_duplicate'] = $this->individualCreate(array(
355 'email' => 'include.me@example.org',
356 'first_name' => 'IncluderDuplicate',
357 'last_name' => 'Person',
358 ));
359
360 $contactIDs['include_me_onhold'] = $this->individualCreate(array(
361 'email' => 'onholdinclude.me@example.org',
362 'first_name' => 'Onhold',
363 'last_name' => 'Person',
364 ));
365 $emailId = $this->callAPISuccessGetValue('Email', array(
366 'return' => 'id',
367 'contact_id' => $contactIDs['include_me_onhold'],
368 ));
369 $this->callAPISuccess('Email', 'create', array(
370 'id' => $emailId,
371 'on_hold' => 1,
372 ));
373
374 $this->callAPISuccess('GroupContact', 'create', array(
375 'group_id' => $groupIDs['grp'],
376 'contact_id' => $contactIDs['include_me'],
377 ));
378 $this->callAPISuccess('GroupContact', 'create', array(
379 'group_id' => $groupIDs['grp'],
380 'contact_id' => $contactIDs['include_me_duplicate'],
381 ));
382 $this->callAPISuccess('GroupContact', 'create', array(
383 'group_id' => $groupIDs['grp'],
384 'contact_id' => $contactIDs['include_me_onhold'],
385 ));
386
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',
396 ),
397 'api.email.getvalue' => array(
398 'return' => 'email',
399 ),
400 );
401 // END SAMPLE DATA
402
403 $create = $this->callAPISuccess('Mailing', 'create', $params);
404
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);
410 }
411
412 /**
413 * Test sending a test mailing.
414 */
415 public function testMailerSendTest_email() {
416 $contactIDs['alice'] = $this->individualCreate(array(
417 'email' => 'alice@example.org',
418 'first_name' => 'Alice',
419 'last_name' => 'Person',
420 ));
421
422 $mail = $this->callAPISuccess('mailing', 'create', $this->_params);
423
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']);
431
432 $deliveredContacts = array_values(CRM_Utils_Array::collect('contact_id', $deliveredInfo['values']));
433 $this->assertEquals(array($contactIDs['alice']), $deliveredContacts);
434
435 $deliveredEmails = array_values(CRM_Utils_Array::collect('email', $deliveredInfo['values']));
436 $this->assertEquals(array('alice@example.org'), $deliveredEmails);
437 }
438
439 /**
440 *
441 */
442 public function testMailerSendTest_group() {
443 // BEGIN SAMPLE DATA
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',
449 ));
450 $contactIDs['bob'] = $this->individualCreate(array(
451 'email' => 'bob@example.org',
452 'first_name' => 'Bob',
453 'last_name' => 'Person',
454 ));
455 $contactIDs['carol'] = $this->individualCreate(array(
456 'email' => 'carol@example.org',
457 'first_name' => 'Carol',
458 'last_name' => 'Person',
459 ));
460 $this->callAPISuccess('GroupContact', 'create', array(
461 'group_id' => $groupIDs['inc'],
462 'contact_id' => $contactIDs['alice'],
463 ));
464 $this->callAPISuccess('GroupContact', 'create', array(
465 'group_id' => $groupIDs['inc'],
466 'contact_id' => $contactIDs['bob'],
467 ));
468 $this->callAPISuccess('GroupContact', 'create', array(
469 'group_id' => $groupIDs['inc'],
470 'contact_id' => $contactIDs['carol'],
471 ));
472 // END SAMPLE DATA
473
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'],
479 ));
480 // verify mail has been sent to user by count
481 $this->assertEquals(3, $deliveredInfo['count'], "in line " . __LINE__);
482
483 $deliveredContacts = array_values(CRM_Utils_Array::collect('contact_id', $deliveredInfo['values']));
484 $this->assertEquals(array($contactIDs['alice'], $contactIDs['bob'], $contactIDs['carol']), $deliveredContacts);
485
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);
488 }
489
490 /**
491 * @return array
492 */
493 public function submitProvider() {
494 // $useLogin, $params, $expectedFailure, $expectedJobCount
495 $cases = array();
496 $cases[] = array(
497 //useLogin
498 TRUE,
499 // createParams
500 array(),
501 array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
502 // expectedFailure
503 FALSE,
504 // expectedJobCount
505 1,
506 );
507 $cases[] = array(
508 //useLogin
509 FALSE,
510 // createParams
511 array(),
512 array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
513 // expectedFailure
514 "/Failed to determine current user/",
515 // expectedJobCount
516 0,
517 );
518 $cases[] = array(
519 //useLogin
520 TRUE,
521 // createParams
522 array(),
523 array('scheduled_date' => '2014-12-13 10:00:00'),
524 // expectedFailure
525 FALSE,
526 // expectedJobCount
527 1,
528 );
529 $cases[] = array(
530 //useLogin
531 TRUE,
532 // createParams
533 array(),
534 array(),
535 // expectedFailure
536 "/Missing parameter scheduled_date and.or approval_date/",
537 // expectedJobCount
538 0,
539 );
540 $cases[] = array(
541 //useLogin
542 TRUE,
543 // createParams
544 array('name' => ''),
545 array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
546 // expectedFailure
547 "/Mailing cannot be sent. There are missing or invalid fields \\(name\\)./",
548 // expectedJobCount
549 0,
550 );
551 $cases[] = array(
552 //useLogin
553 TRUE,
554 // createParams
555 array('body_html' => '', 'body_text' => ''),
556 array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
557 // expectedFailure
558 "/Mailing cannot be sent. There are missing or invalid fields \\(body\\)./",
559 // expectedJobCount
560 0,
561 );
562 $cases[] = array(
563 //useLogin
564 TRUE,
565 // createParams
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'),
568 // expectedFailure
569 "/Mailing cannot be sent. There are missing or invalid fields \\(.*body_html.*optOut.*\\)./",
570 // expectedJobCount
571 0,
572 );
573 $cases[] = array(
574 //useLogin
575 TRUE,
576 // createParams
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'),
579 // expectedFailure
580 "/Mailing cannot be sent. There are missing or invalid fields \\(.*body_text.*optOut.*\\)./",
581 // expectedJobCount
582 0,
583 );
584 $cases[] = array(
585 //useLogin
586 TRUE,
587 // createParams
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'),
590 // expectedFailure
591 FALSE,
592 // expectedJobCount
593 1,
594 );
595 $cases[] = array(
596 //useLogin
597 TRUE,
598 // createParams
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'),
601 // expectedFailure
602 FALSE,
603 // expectedJobCount
604 1,
605 );
606 return $cases;
607 }
608
609 /**
610 * @param bool $useLogin
611 * @param array $createParams
612 * @param array $submitParams
613 * @param null|string $expectedFailure
614 * @param int $expectedJobCount
615 * @dataProvider submitProvider
616 */
617 public function testMailerSubmit($useLogin, $createParams, $submitParams, $expectedFailure, $expectedJobCount) {
618 if ($useLogin) {
619 $this->createLoggedInUser();
620 }
621
622 if (isset($createParams['footer_id']) && $createParams['footer_id'] == '%FOOTER%') {
623 $createParams['footer_id'] = $this->footer['id'];
624 }
625
626 $id = $this->createDraftMailing($createParams);
627
628 $submitParams['id'] = $id;
629 if ($expectedFailure) {
630 $submitResult = $this->callAPIFailure('mailing', 'submit', $submitParams);
631 $this->assertRegExp($expectedFailure, $submitResult['error_message']);
632 }
633 else {
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']);
638 }
639 $this->assertDBQuery($expectedJobCount, 'SELECT count(*) FROM civicrm_mailing_job WHERE mailing_id = %1', array(
640 1 => array($id, 'Integer'),
641 ));
642 }
643
644 /**
645 * Test unsubscribe list contains correct groups
646 * when include = 'previous mailing'
647 */
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) {
654 $params = array(
655 'contact_id' => $contactId,
656 'group_id' => $grp,
657 );
658 $this->callAPISuccess('GroupContact', 'create', $params);
659 }
660
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);
665
666 $mgParams = array(
667 'mailing_id' => $mail['id'],
668 'entity_table' => 'civicrm_group',
669 'entity_id' => $groupID3,
670 'group_type' => 'Include',
671 );
672 $mailingGroup = $this->callAPISuccess('MailingGroup', 'create', $mgParams);
673
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);
678
679 $mgParams = array(
680 'mailing_id' => $mail2['id'],
681 'entity_table' => 'civicrm_mailing',
682 'entity_id' => $mail['id'],
683 'group_type' => 'Include',
684 );
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');
691
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));
696 }
697
698 /**
699 *
700 */
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']);
705
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']));
712
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);
716
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);
725
726 $sql = "DELETE FROM civicrm_mailing_event_delivered WHERE id IN (SELECT delivered_id FROM mail_{$type}_temp);";
727 CRM_Core_DAO::executeQuery($sql);
728
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";
732 }
733 else {
734 $sql = "INSERT INTO civicrm_mailing_event_{$type} (event_queue_id, time_stamp)
735 SELECT event_queue_id, time_stamp FROM mail_{$type}_temp";
736 }
737 CRM_Core_DAO::executeQuery($sql);
738 }
739
740 $result = $this->callAPISuccess('mailing', 'stats', array('mailing_id' => $mail['id']));
741 $expectedResult = array(
742 //since among 100 mails 20 has been bounced
743 'Delivered' => 80,
744 'Bounces' => 20,
745 'Opened' => 20,
746 'Unique Clicks' => 0,
747 'Unsubscribers' => 20,
748 'delivered_rate' => '80%',
749 'opened_rate' => '25%',
750 'clickthrough_rate' => '0%',
751 );
752 $this->checkArrayEquals($expectedResult, $result['values'][$mail['id']]);
753 }
754
755 /**
756 * Test civicrm_mailing_delete.
757 */
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']);
762 }
763
764 /**
765 * Test Mailing.gettokens.
766 */
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']);
772
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]);
777 }
778
779 public function testClone() {
780 // BEGIN SAMPLE DATA
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',
786 ));
787 $this->callAPISuccess('GroupContact', 'create', array(
788 'group_id' => $groupIDs['inc'],
789 'contact_id' => $contactIDs['include_me'],
790 ));
791
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();
797 // END SAMPLE DATA
798
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'];
805
806 $this->assertNotEquals($createId, $cloneId, 'Create and clone should return different records');
807 $this->assertTrue(is_numeric($cloneId));
808
809 $this->assertNotEmpty($clone['values'][$cloneId]['subject']);
810 $this->assertEquals($params['subject'], $clone['values'][$cloneId]['subject'], "Cloned subject should match");
811
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');
815
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']);
822 }
823
824 //@ todo tests below here are all failure tests which are not hugely useful - need success tests
825
826 //------------ civicrm_mailing_event_bounce methods------------
827
828 /**
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
832 * success test
833 */
834 public function testMailerBounceWrongParams() {
835 $params = array(
836 'job_id' => 'Wrong ID',
837 'event_queue_id' => 'Wrong ID',
838 'hash' => 'Wrong Hash',
839 'body' => 'Body...',
840 'time_stamp' => '20111109212100',
841 );
842 $this->callAPIFailure('mailing_event', 'bounce', $params,
843 'Queue event could not be found'
844 );
845 }
846
847 //----------- civicrm_mailing_event_confirm methods -----------
848
849 /**
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
853 * success test
854 */
855 public function testMailerConfirmWrongParams() {
856 $params = array(
857 'contact_id' => 'Wrong ID',
858 'subscribe_id' => 'Wrong ID',
859 'hash' => 'Wrong Hash',
860 'event_subscribe_id' => '123',
861 'time_stamp' => '20111111010101',
862 );
863 $this->callAPIFailure('mailing_event', 'confirm', $params,
864 'contact_id is not a valid integer'
865 );
866 }
867
868 //---------- civicrm_mailing_event_reply methods -----------
869
870 /**
871 * Test civicrm_mailing_event_reply with wrong params.
872 *
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
875 * success test
876 */
877 public function testMailerReplyWrongParams() {
878 $params = array(
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',
885 );
886 $this->callAPIFailure('mailing_event', 'reply', $params,
887 'Queue event could not be found'
888 );
889 }
890
891 //----------- civicrm_mailing_event_forward methods ----------
892
893 /**
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
897 * success test
898 */
899 public function testMailerForwardWrongParams() {
900 $params = array(
901 'job_id' => 'Wrong ID',
902 'event_queue_id' => 'Wrong ID',
903 'hash' => 'Wrong Hash',
904 'email' => $this->_email,
905 'time_stamp' => '20111111010101',
906 );
907 $this->callAPIFailure('mailing_event', 'forward', $params,
908 'Queue event could not be found'
909 );
910 }
911
912 /**
913 * @param array $params
914 * Extra parameters for the draft mailing.
915 * @return array|int
916 */
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'),
923 ));
924 return $createResult['id'];
925 }
926
927 /**
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).
930 */
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']);
935
936 $params = array('mailing_id' => $mail['id'], 'test_email' => 'alice@example.org', 'test_group' => NULL);
937 $deliveredInfo = $this->callAPISuccess($this->_entity, 'send_test', $params);
938
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";
944
945 $dao = CRM_Core_DAO::executeQuery($sql);
946 $this->assertTrue($dao->fetch());
947
948 $url = CRM_Mailing_Event_BAO_TrackableURLOpen::track($dao->queue_id, $dao->url_id);
949 $this->assertContains('https://civicrm.org', $url);
950
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');
953
954 $url = CRM_Mailing_Event_BAO_TrackableURLOpen::track($dao->queue_id, $dao->url_id);
955 $this->assertContains('https://civicrm.org', $url);
956
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());
960 }
961
962 /**
963 * Test Trackable URL with unicode character
964 */
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']);
969
970 $mail = $this->callAPISuccess('mailing', 'create', $this->_params + array('scheduled_date' => 'now'));
971
972 $params = array('mailing_id' => $mail['id'], 'test_email' => 'alice@example.org', 'test_group' => NULL);
973 $this->callAPISuccess($this->_entity, 'send_test', $params);
974
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";
980
981 $dao = CRM_Core_DAO::executeQuery($sql);
982 $this->assertTrue($dao->fetch());
983
984 $url = CRM_Mailing_Event_BAO_TrackableURLOpen::track($dao->queue_id, $dao->url_id);
985 $this->assertContains($unicodeURL, $url);
986
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');
989
990 $url = CRM_Mailing_Event_BAO_TrackableURLOpen::track($dao->queue_id, $dao->url_id);
991 $this->assertContains($unicodeURL, $url);
992 }
993
994 /**
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
997 */
998 public function testModifiedDateMismatchOnMailingUpdate() {
999 $mail = $this->callAPISuccess('mailing', 'create', $this->_params + array('modified_date' => 'now'));
1000 try {
1001 $this->callAPISuccess('mailing', 'create', $this->_params + array('id' => $mail['id'], 'modified_date' => '2 seconds ago'));
1002 }
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());
1005 }
1006 }
1007
1008 }