Merge pull request #13906 from agh1/whygroupcontribamt
[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 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' => "<link href='https://fonts.googleapis.com/css?family=Roboto+Condensed:400,700|Zilla+Slab:500,700' rel='stylesheet' type='text/css'><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,
60 'header_id' => '',
61 'footer_id' => '',
62 );
63
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}.',
69 ));
70 }
71
72 public function tearDown() {
73 CRM_Mailing_BAO_MailingJob::$mailsProcessed = 0; // DGW
74 parent::tearDown();
75 }
76
77 /**
78 * Test civicrm_mailing_create.
79 */
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');
86 }
87
88 /**
89 * Tes that the parameter _skip_evil_bao_auto_schedule_ is respected & prevents jobs being created.
90 */
91 public function testSkipAutoSchedule() {
92 $this->callAPISuccess('Mailing', 'create', array_merge($this->_params, [
93 '_skip_evil_bao_auto_schedule_' => TRUE,
94 'scheduled_date' => 'now'
95 ]));
96 $this->callAPISuccessGetCount('Mailing', [], 1);
97 $this->callAPISuccessGetCount('MailingJob', [], 0);
98 }
99
100 /**
101 * Create a completed mailing (e.g when importing from a provider).
102 */
103 public function testMailerCreateCompleted() {
104 $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';
105 $this->_params['is_completed'] = 1;
106 $result = $this->callAPIAndDocument('mailing', 'create', $this->_params + array('scheduled_date' => 'now'), __FUNCTION__, __FILE__);
107 $jobs = $this->callAPISuccess('mailing_job', 'get', array('mailing_id' => $result['id']));
108 $this->assertEquals(1, $jobs['count']);
109 $this->assertEquals('Complete', $jobs['values'][$jobs['id']]['status']);
110 unset($this->_params['created_id']); // return isn't working on this in getAndCheck so lets not check it for now
111 $this->getAndCheck($this->_params, $result['id'], 'mailing');
112 }
113
114 /**
115 * Per CRM-20316 the mailing should still create without created_id (not mandatory).
116 */
117 public function testMailerCreateSuccessNoCreatedID() {
118 unset($this->_params['created_id']);
119 $result = $this->callAPIAndDocument('mailing', 'create', $this->_params + array('scheduled_date' => 'now'), __FUNCTION__, __FILE__);
120 $this->getAndCheck($this->_params, $result['id'], 'mailing');
121 }
122
123 /**
124 *
125 */
126 public function testTemplateTypeOptions() {
127 $types = $this->callAPISuccess('Mailing', 'getoptions', array('field' => 'template_type'));
128 $this->assertTrue(isset($types['values']['traditional']));
129 }
130
131 /**
132 * The `template_options` field should be treated a JSON object.
133 *
134 * This test will create, read, and update the field.
135 */
136 public function testMailerCreateTemplateOptions() {
137 // 1. Create mailing with template_options.
138 $params = $this->_params;
139 $params['template_options'] = json_encode(array('foo' => 'bar_1'));
140 $createResult = $this->callAPISuccess('mailing', 'create', $params);
141 $id = $createResult['id'];
142 $this->assertDBQuery('{"foo":"bar_1"}', 'SELECT template_options FROM civicrm_mailing WHERE id = %1', array(
143 1 => array($id, 'Int'),
144 ));
145 $this->assertEquals('bar_1', $createResult['values'][$id]['template_options']['foo']);
146
147 // 2. Get mailing with template_options.
148 $getResult = $this->callAPISuccess('mailing', 'get', array(
149 'id' => $id,
150 ));
151 $this->assertEquals('bar_1', $getResult['values'][$id]['template_options']['foo']);
152 $getValueResult = $this->callAPISuccess('mailing', 'getvalue', array(
153 'id' => $id,
154 'return' => 'template_options',
155 ));
156 $this->assertEquals('bar_1', $getValueResult['foo']);
157
158 // 3. Update mailing with template_options.
159 $updateResult = $this->callAPISuccess('mailing', 'create', array(
160 'id' => $id,
161 'template_options' => array('foo' => 'bar_2'),
162 ));
163 $this->assertDBQuery('{"foo":"bar_2"}', 'SELECT template_options FROM civicrm_mailing WHERE id = %1', array(
164 1 => array($id, 'Int'),
165 ));
166 $this->assertEquals('bar_2', $updateResult['values'][$id]['template_options']['foo']);
167 }
168
169 /**
170 * The Mailing.create API supports magic properties "groups[include,enclude]" and "mailings[include,exclude]".
171 * Make sure these work
172 */
173 public function testMagicGroups_create_update() {
174 // BEGIN SAMPLE DATA
175 $groupIDs['a'] = $this->groupCreate(array('name' => 'Example include group', 'title' => 'Example include group'));
176 $groupIDs['b'] = $this->groupCreate(array('name' => 'Example exclude group', 'title' => 'Example exclude group'));
177 $contactIDs['a'] = $this->individualCreate(array(
178 'email' => 'include.me@example.org',
179 'first_name' => 'Includer',
180 'last_name' => 'Person',
181 ));
182 $contactIDs['b'] = $this->individualCreate(array(
183 'email' => 'exclude.me@example.org',
184 'last_name' => 'Excluder',
185 ));
186 $this->callAPISuccess('GroupContact', 'create', array(
187 'group_id' => $groupIDs['a'],
188 'contact_id' => $contactIDs['a'],
189 ));
190 $this->callAPISuccess('GroupContact', 'create', array(
191 'group_id' => $groupIDs['b'],
192 'contact_id' => $contactIDs['b'],
193 ));
194 // END SAMPLE DATA
195
196 // ** Pass 1: Create
197 $createParams = $this->_params;
198 $createParams['groups']['include'] = [$groupIDs['a']];
199 $createParams['groups']['exclude'] = [];
200 $createParams['mailings']['include'] = [];
201 $createParams['mailings']['exclude'] = [];
202 $createParams['scheduled_date'] = 'now';
203 $createResult = $this->callAPISuccess('Mailing', 'create', $createParams);
204 $getGroup1 = $this->callAPISuccess('MailingGroup', 'get', array('mailing_id' => $createResult['id']));
205 $getGroup1_ids = array_values(CRM_Utils_Array::collect('entity_id', $getGroup1['values']));
206 $this->assertEquals(array($groupIDs['a']), $getGroup1_ids);
207 $getRecipient1 = $this->callAPISuccess('MailingRecipients', 'get', array('mailing_id' => $createResult['id']));
208 $getRecipient1_ids = array_values(CRM_Utils_Array::collect('contact_id', $getRecipient1['values']));
209 $this->assertEquals(array($contactIDs['a']), $getRecipient1_ids);
210
211 // ** Pass 2: Update without any changes to groups[include]
212 $nullOpParams = $createParams;
213 $nullOpParams['id'] = $createResult['id'];
214 $updateParams['api.mailing_job.create'] = 1;
215 unset($nullOpParams['groups']['include']);
216 $this->callAPISuccess('Mailing', 'create', $nullOpParams);
217 $getGroup2 = $this->callAPISuccess('MailingGroup', 'get', array('mailing_id' => $createResult['id']));
218 $getGroup2_ids = array_values(CRM_Utils_Array::collect('entity_id', $getGroup2['values']));
219 $this->assertEquals(array($groupIDs['a']), $getGroup2_ids);
220 $getRecipient2 = $this->callAPISuccess('MailingRecipients', 'get', array('mailing_id' => $createResult['id']));
221 $getRecip2_ids = array_values(CRM_Utils_Array::collect('contact_id', $getRecipient2['values']));
222 $this->assertEquals(array($contactIDs['a']), $getRecip2_ids);
223
224 // ** Pass 3: Update with different groups[include]
225 $updateParams = $createParams;
226 $updateParams['id'] = $createResult['id'];
227 $updateParams['groups']['include'] = array($groupIDs['b']);
228 $updateParams['scheduled_date'] = 'now';
229 $this->callAPISuccess('Mailing', 'create', $updateParams);
230 $getGroup3 = $this->callAPISuccess('MailingGroup', 'get', array('mailing_id' => $createResult['id']));
231 $getGroup3_ids = array_values(CRM_Utils_Array::collect('entity_id', $getGroup3['values']));
232 $this->assertEquals(array($groupIDs['b']), $getGroup3_ids);
233 $getRecipient3 = $this->callAPISuccess('MailingRecipients', 'get', array('mailing_id' => $createResult['id']));
234 $getRecipient3_ids = array_values(CRM_Utils_Array::collect('contact_id', $getRecipient3['values']));
235 $this->assertEquals(array($contactIDs['b']), $getRecipient3_ids);
236 }
237
238 public function testMailerPreview() {
239 // BEGIN SAMPLE DATA
240 $contactID = $this->individualCreate();
241 $displayName = $this->callAPISuccess('contact', 'get', array('id' => $contactID));
242 $displayName = $displayName['values'][$contactID]['display_name'];
243 $this->assertTrue(!empty($displayName));
244
245 $params = $this->_params;
246 $params['api.Mailing.preview'] = array(
247 'id' => '$value.id',
248 'contact_id' => $contactID,
249 );
250 $params['options']['force_rollback'] = 1;
251 // END SAMPLE DATA
252
253 $maxIDs = array(
254 'mailing' => CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing'),
255 'job' => CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_job'),
256 'group' => CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_group'),
257 'recipient' => CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_recipients'),
258 );
259 $result = $this->callAPISuccess('mailing', 'create', $params);
260 $this->assertDBQuery($maxIDs['mailing'], 'SELECT MAX(id) FROM civicrm_mailing'); // 'Preview should not create any mailing records'
261 $this->assertDBQuery($maxIDs['job'], 'SELECT MAX(id) FROM civicrm_mailing_job'); // 'Preview should not create any mailing_job record'
262 $this->assertDBQuery($maxIDs['group'], 'SELECT MAX(id) FROM civicrm_mailing_group'); // 'Preview should not create any mailing_group records'
263 $this->assertDBQuery($maxIDs['recipient'], 'SELECT MAX(id) FROM civicrm_mailing_recipients'); // 'Preview should not create any mailing_recipient records'
264
265 $previewResult = $result['values'][$result['id']]['api.Mailing.preview'];
266 $this->assertEquals("Hello $displayName", $previewResult['values']['subject']);
267 $this->assertContains("This is $displayName", $previewResult['values']['body_text']);
268 $this->assertContains("<p>This is $displayName.</p>", $previewResult['values']['body_html']);
269 }
270
271 public function testMailerPreviewRecipients() {
272 // BEGIN SAMPLE DATA
273 $groupIDs['inc'] = $this->groupCreate(array('name' => 'Example include group', 'title' => 'Example include group'));
274 $groupIDs['exc'] = $this->groupCreate(array('name' => 'Example exclude group', 'title' => 'Example exclude group'));
275 $contactIDs['include_me'] = $this->individualCreate(array(
276 'email' => 'include.me@example.org',
277 'first_name' => 'Includer',
278 'last_name' => 'Person',
279 ));
280 $contactIDs['exclude_me'] = $this->individualCreate(array(
281 'email' => 'exclude.me@example.org',
282 'last_name' => 'Excluder',
283 ));
284 $this->callAPISuccess('GroupContact', 'create', array(
285 'group_id' => $groupIDs['inc'],
286 'contact_id' => $contactIDs['include_me'],
287 ));
288 $this->callAPISuccess('GroupContact', 'create', array(
289 'group_id' => $groupIDs['inc'],
290 'contact_id' => $contactIDs['exclude_me'],
291 ));
292 $this->callAPISuccess('GroupContact', 'create', array(
293 'group_id' => $groupIDs['exc'],
294 'contact_id' => $contactIDs['exclude_me'],
295 ));
296
297 $params = $this->_params;
298 $params['groups']['include'] = array($groupIDs['inc']);
299 $params['groups']['exclude'] = array($groupIDs['exc']);
300 $params['mailings']['include'] = array();
301 $params['mailings']['exclude'] = array();
302 $params['options']['force_rollback'] = 1;
303 $params['api.MailingRecipients.get'] = array(
304 'mailing_id' => '$value.id',
305 'api.contact.getvalue' => array(
306 'return' => 'display_name',
307 ),
308 'api.email.getvalue' => array(
309 'return' => 'email',
310 ),
311 );
312 // END SAMPLE DATA
313
314 $maxIDs = array(
315 'mailing' => CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing'),
316 'group' => CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_group'),
317 );
318 $create = $this->callAPIAndDocument('Mailing', 'create', $params, __FUNCTION__, __FILE__);
319 $this->assertDBQuery($maxIDs['mailing'], 'SELECT MAX(id) FROM civicrm_mailing'); // 'Preview should not create any mailing records'
320 $this->assertDBQuery($maxIDs['group'], 'SELECT MAX(id) FROM civicrm_mailing_group'); // 'Preview should not create any mailing_group records'
321
322 $preview = $create['values'][$create['id']]['api.MailingRecipients.get'];
323 $previewIds = array_values(CRM_Utils_Array::collect('contact_id', $preview['values']));
324 $this->assertEquals(array((string) $contactIDs['include_me']), $previewIds);
325 $previewEmails = array_values(CRM_Utils_Array::collect('api.email.getvalue', $preview['values']));
326 $this->assertEquals(array('include.me@example.org'), $previewEmails);
327 $previewNames = array_values(CRM_Utils_Array::collect('api.contact.getvalue', $preview['values']));
328 $this->assertTrue((bool) preg_match('/Includer Person/', $previewNames[0]), "Name 'Includer Person' should appear in '" . $previewNames[0] . '"');
329 }
330
331 /**
332 * Test if Mailing recipients include duplicate OR on_hold emails
333 */
334 public function testMailerPreviewRecipientsDeduplicateAndOnholdEmails() {
335 // BEGIN SAMPLE DATA
336 $groupIDs['grp'] = $this->groupCreate(array('name' => 'Example group', 'title' => 'Example group'));
337 $contactIDs['include_me'] = $this->individualCreate(array(
338 'email' => 'include.me@example.org',
339 'first_name' => 'Includer',
340 'last_name' => 'Person',
341 ));
342 $contactIDs['include_me_duplicate'] = $this->individualCreate(array(
343 'email' => 'include.me@example.org',
344 'first_name' => 'IncluderDuplicate',
345 'last_name' => 'Person',
346 ));
347
348 $contactIDs['include_me_onhold'] = $this->individualCreate(array(
349 'email' => 'onholdinclude.me@example.org',
350 'first_name' => 'Onhold',
351 'last_name' => 'Person',
352 ));
353 $emailId = $this->callAPISuccessGetValue('Email', array(
354 'return' => 'id',
355 'contact_id' => $contactIDs['include_me_onhold'],
356 ));
357 $this->callAPISuccess('Email', 'create', array(
358 'id' => $emailId,
359 'on_hold' => 1,
360 ));
361
362 $this->callAPISuccess('GroupContact', 'create', array(
363 'group_id' => $groupIDs['grp'],
364 'contact_id' => $contactIDs['include_me'],
365 ));
366 $this->callAPISuccess('GroupContact', 'create', array(
367 'group_id' => $groupIDs['grp'],
368 'contact_id' => $contactIDs['include_me_duplicate'],
369 ));
370 $this->callAPISuccess('GroupContact', 'create', array(
371 'group_id' => $groupIDs['grp'],
372 'contact_id' => $contactIDs['include_me_onhold'],
373 ));
374
375 $params = $this->_params;
376 $params['groups']['include'] = array($groupIDs['grp']);
377 $params['mailings']['include'] = array();
378 $params['options']['force_rollback'] = 1;
379 $params['dedupe_email'] = 1;
380 $params['api.MailingRecipients.get'] = array(
381 'mailing_id' => '$value.id',
382 'api.contact.getvalue' => array(
383 'return' => 'display_name',
384 ),
385 'api.email.getvalue' => array(
386 'return' => 'email',
387 ),
388 );
389 // END SAMPLE DATA
390
391 $create = $this->callAPISuccess('Mailing', 'create', $params);
392
393 //Recipient should not contain duplicate or on_hold emails.
394 $preview = $create['values'][$create['id']]['api.MailingRecipients.get'];
395 $this->assertEquals(1, $preview['count']);
396 $previewEmails = array_values(CRM_Utils_Array::collect('api.email.getvalue', $preview['values']));
397 $this->assertEquals(array('include.me@example.org'), $previewEmails);
398 }
399
400 /**
401 * Test sending a test mailing.
402 */
403 public function testMailerSendTest_email() {
404 $contactIDs['alice'] = $this->individualCreate(array(
405 'email' => 'alice@example.org',
406 'first_name' => 'Alice',
407 'last_name' => 'Person',
408 ));
409
410 $mail = $this->callAPISuccess('mailing', 'create', $this->_params);
411
412 $params = array('mailing_id' => $mail['id'], 'test_email' => 'ALicE@example.org', 'test_group' => NULL);
413 // Per https://lab.civicrm.org/dev/core/issues/229 ensure this is not passed through!
414 // Per https://lab.civicrm.org/dev/mail/issues/32 test non-lowercase email
415 $params['id'] = $mail['id'];
416 $deliveredInfo = $this->callAPISuccess($this->_entity, 'send_test', $params);
417 $this->assertEquals(1, $deliveredInfo['count']); // verify mail has been sent to user by count
418
419 $deliveredContacts = array_values(CRM_Utils_Array::collect('contact_id', $deliveredInfo['values']));
420 $this->assertEquals(array($contactIDs['alice']), $deliveredContacts);
421
422 $deliveredEmails = array_values(CRM_Utils_Array::collect('email', $deliveredInfo['values']));
423 $this->assertEquals(array('alice@example.org'), $deliveredEmails);
424 }
425
426 /**
427 *
428 */
429 public function testMailerSendTest_group() {
430 // BEGIN SAMPLE DATA
431 $groupIDs['inc'] = $this->groupCreate(array('name' => 'Example include group', 'title' => 'Example include group'));
432 $contactIDs['alice'] = $this->individualCreate(array(
433 'email' => 'alice@example.org',
434 'first_name' => 'Alice',
435 'last_name' => 'Person',
436 ));
437 $contactIDs['bob'] = $this->individualCreate(array(
438 'email' => 'bob@example.org',
439 'first_name' => 'Bob',
440 'last_name' => 'Person',
441 ));
442 $contactIDs['carol'] = $this->individualCreate(array(
443 'email' => 'carol@example.org',
444 'first_name' => 'Carol',
445 'last_name' => 'Person',
446 ));
447 $this->callAPISuccess('GroupContact', 'create', array(
448 'group_id' => $groupIDs['inc'],
449 'contact_id' => $contactIDs['alice'],
450 ));
451 $this->callAPISuccess('GroupContact', 'create', array(
452 'group_id' => $groupIDs['inc'],
453 'contact_id' => $contactIDs['bob'],
454 ));
455 $this->callAPISuccess('GroupContact', 'create', array(
456 'group_id' => $groupIDs['inc'],
457 'contact_id' => $contactIDs['carol'],
458 ));
459 // END SAMPLE DATA
460
461 $mail = $this->callAPISuccess('mailing', 'create', $this->_params);
462 $deliveredInfo = $this->callAPISuccess($this->_entity, 'send_test', array(
463 'mailing_id' => $mail['id'],
464 'test_email' => NULL,
465 'test_group' => $groupIDs['inc'],
466 ));
467 $this->assertEquals(3, $deliveredInfo['count'], "in line " . __LINE__); // verify mail has been sent to user by count
468
469 $deliveredContacts = array_values(CRM_Utils_Array::collect('contact_id', $deliveredInfo['values']));
470 $this->assertEquals(array($contactIDs['alice'], $contactIDs['bob'], $contactIDs['carol']), $deliveredContacts);
471
472 $deliveredEmails = array_values(CRM_Utils_Array::collect('email', $deliveredInfo['values']));
473 $this->assertEquals(array('alice@example.org', 'bob@example.org', 'carol@example.org'), $deliveredEmails);
474 }
475
476 /**
477 * @return array
478 */
479 public function submitProvider() {
480 $cases = array(); // $useLogin, $params, $expectedFailure, $expectedJobCount
481 $cases[] = array(
482 TRUE, //useLogin
483 array(), // createParams
484 array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
485 FALSE, // expectedFailure
486 1, // expectedJobCount
487 );
488 $cases[] = array(
489 FALSE, //useLogin
490 array(), // createParams
491 array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
492 "/Failed to determine current user/", // expectedFailure
493 0, // expectedJobCount
494 );
495 $cases[] = array(
496 TRUE, //useLogin
497 array(), // createParams
498 array('scheduled_date' => '2014-12-13 10:00:00'),
499 FALSE, // expectedFailure
500 1, // expectedJobCount
501 );
502 $cases[] = array(
503 TRUE, //useLogin
504 array(), // createParams
505 array(),
506 "/Missing parameter scheduled_date and.or approval_date/", // expectedFailure
507 0, // expectedJobCount
508 );
509 $cases[] = array(
510 TRUE, //useLogin
511 array('name' => ''), // createParams
512 array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
513 "/Mailing cannot be sent. There are missing or invalid fields \\(name\\)./", // expectedFailure
514 0, // expectedJobCount
515 );
516 $cases[] = array(
517 TRUE, //useLogin
518 array('body_html' => '', 'body_text' => ''), // createParams
519 array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
520 "/Mailing cannot be sent. There are missing or invalid fields \\(body\\)./", // expectedFailure
521 0, // expectedJobCount
522 );
523 $cases[] = array(
524 TRUE, //useLogin
525 array('body_html' => 'Oops, did I leave my tokens at home?'), // createParams
526 array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
527 "/Mailing cannot be sent. There are missing or invalid fields \\(.*body_html.*optOut.*\\)./", // expectedFailure
528 0, // expectedJobCount
529 );
530 $cases[] = array(
531 TRUE, //useLogin
532 array('body_text' => 'Oops, did I leave my tokens at home?'), // createParams
533 array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
534 "/Mailing cannot be sent. There are missing or invalid fields \\(.*body_text.*optOut.*\\)./", // expectedFailure
535 0, // expectedJobCount
536 );
537 $cases[] = array(
538 TRUE, //useLogin
539 array('body_text' => 'Look ma, magic tokens in the text!', 'footer_id' => '%FOOTER%'), // createParams
540 array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
541 FALSE, // expectedFailure
542 1, // expectedJobCount
543 );
544 $cases[] = array(
545 TRUE, //useLogin
546 array('body_html' => '<p>Look ma, magic tokens in the markup!</p>', 'footer_id' => '%FOOTER%'), // createParams
547 array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
548 FALSE, // expectedFailure
549 1, // expectedJobCount
550 );
551 return $cases;
552 }
553
554 /**
555 * @param bool $useLogin
556 * @param array $createParams
557 * @param array $submitParams
558 * @param null|string $expectedFailure
559 * @param int $expectedJobCount
560 * @dataProvider submitProvider
561 */
562 public function testMailerSubmit($useLogin, $createParams, $submitParams, $expectedFailure, $expectedJobCount) {
563 if ($useLogin) {
564 $this->createLoggedInUser();
565 }
566
567 if (isset($createParams['footer_id']) && $createParams['footer_id'] == '%FOOTER%') {
568 $createParams['footer_id'] = $this->footer['id'];
569 }
570
571 $id = $this->createDraftMailing($createParams);
572
573 $submitParams['id'] = $id;
574 if ($expectedFailure) {
575 $submitResult = $this->callAPIFailure('mailing', 'submit', $submitParams);
576 $this->assertRegExp($expectedFailure, $submitResult['error_message']);
577 }
578 else {
579 $submitResult = $this->callAPIAndDocument('mailing', 'submit', $submitParams, __FUNCTION__, __FILE__);
580 $this->assertTrue(is_numeric($submitResult['id']));
581 $this->assertTrue(is_numeric($submitResult['values'][$id]['scheduled_id']));
582 $this->assertEquals($submitParams['scheduled_date'], $submitResult['values'][$id]['scheduled_date']);
583 }
584 $this->assertDBQuery($expectedJobCount, 'SELECT count(*) FROM civicrm_mailing_job WHERE mailing_id = %1', array(
585 1 => array($id, 'Integer'),
586 ));
587 }
588
589 /**
590 * Test unsubscribe list contains correct groups
591 * when include = 'previous mailing'
592 */
593 public function testUnsubscribeGroupList() {
594 // Create set of groups and add a contact to both of them.
595 $groupID2 = $this->groupCreate(array('name' => 'Test group 2', 'title' => 'group title 2'));
596 $groupID3 = $this->groupCreate(array('name' => 'Test group 3', 'title' => 'group title 3'));
597 $contactId = $this->individualCreate();
598 foreach (array($groupID2, $groupID3) as $grp) {
599 $params = array(
600 'contact_id' => $contactId,
601 'group_id' => $grp,
602 );
603 $this->callAPISuccess('GroupContact', 'create', $params);
604 }
605
606 //Send mail to groupID3
607 $mail = $this->callAPISuccess('mailing', 'create', $this->_params);
608 $params = array('mailing_id' => $mail['id'], 'test_email' => NULL, 'test_group' => $groupID3);
609 $this->callAPISuccess($this->_entity, 'send_test', $params);
610
611 $mgParams = array(
612 'mailing_id' => $mail['id'],
613 'entity_table' => 'civicrm_group',
614 'entity_id' => $groupID3,
615 'group_type' => 'Include',
616 );
617 $mailingGroup = $this->callAPISuccess('MailingGroup', 'create', $mgParams);
618
619 //Include previous mail in the mailing group.
620 $mail2 = $this->callAPISuccess('mailing', 'create', $this->_params);
621 $params = array('mailing_id' => $mail2['id'], 'test_email' => NULL, 'test_group' => $groupID3);
622 $this->callAPISuccess($this->_entity, 'send_test', $params);
623
624 $mgParams = array(
625 'mailing_id' => $mail2['id'],
626 'entity_table' => 'civicrm_mailing',
627 'entity_id' => $mail['id'],
628 'group_type' => 'Include',
629 );
630 $mailingGroup = $this->callAPISuccess('MailingGroup', 'create', $mgParams);
631 //CRM-20431 - Delete group id that matches first mailing id.
632 $this->callAPISuccess('Group', 'delete', array('id' => $this->_groupID));
633 $jobId = CRM_Core_DAO::getFieldValue('CRM_Mailing_DAO_MailingJob', $mail2['id'], 'id', 'mailing_id');
634 $hash = CRM_Core_DAO::getFieldValue('CRM_Mailing_Event_DAO_Queue', $jobId, 'hash', 'job_id');
635 $queueId = CRM_Core_DAO::getFieldValue('CRM_Mailing_Event_DAO_Queue', $jobId, 'id', 'job_id');
636
637 $group = CRM_Mailing_Event_BAO_Unsubscribe::unsub_from_mailing($jobId, $queueId, $hash, TRUE);
638 //Assert only one group returns in the unsubscribe list.
639 $this->assertCount(1, $group);
640 $this->assertEquals($groupID3, key($group));
641 }
642
643 /**
644 *
645 */
646 public function testMailerStats() {
647 $result = $this->groupContactCreate($this->_groupID, 100);
648 $this->assertEquals(100, $result['added']); //verify if 100 contacts are added for group
649
650 //Create and send test mail first and change the mail job to live,
651 //because stats api only works on live mail
652 $mail = $this->callAPISuccess('mailing', 'create', $this->_params);
653 $params = array('mailing_id' => $mail['id'], 'test_email' => NULL, 'test_group' => $this->_groupID);
654 $deliveredInfo = $this->callAPISuccess($this->_entity, 'send_test', $params);
655 $deliveredIds = implode(',', array_keys($deliveredInfo['values']));
656
657 //Change the test mail into live
658 $sql = "UPDATE civicrm_mailing_job SET is_test = 0 WHERE mailing_id = {$mail['id']}";
659 CRM_Core_DAO::executeQuery($sql);
660
661 foreach (array('bounce', 'unsubscribe', 'opened') as $type) {
662 $sql = "CREATE TEMPORARY TABLE mail_{$type}_temp
663 (event_queue_id int, time_stamp datetime, delivered_id int)
664 SELECT event_queue_id, time_stamp, id
665 FROM civicrm_mailing_event_delivered
666 WHERE id IN ($deliveredIds)
667 ORDER BY RAND() LIMIT 0,20;";
668 CRM_Core_DAO::executeQuery($sql);
669
670 $sql = "DELETE FROM civicrm_mailing_event_delivered WHERE id IN (SELECT delivered_id FROM mail_{$type}_temp);";
671 CRM_Core_DAO::executeQuery($sql);
672
673 if ($type == 'unsubscribe') {
674 $sql = "INSERT INTO civicrm_mailing_event_{$type} (event_queue_id, time_stamp, org_unsubscribe)
675 SELECT event_queue_id, time_stamp, 1 FROM mail_{$type}_temp";
676 }
677 else {
678 $sql = "INSERT INTO civicrm_mailing_event_{$type} (event_queue_id, time_stamp)
679 SELECT event_queue_id, time_stamp FROM mail_{$type}_temp";
680 }
681 CRM_Core_DAO::executeQuery($sql);
682 }
683
684 $result = $this->callAPISuccess('mailing', 'stats', array('mailing_id' => $mail['id']));
685 $expectedResult = array(
686 'Delivered' => 80, //since among 100 mails 20 has been bounced
687 'Bounces' => 20,
688 'Opened' => 20,
689 'Unique Clicks' => 0,
690 'Unsubscribers' => 20,
691 'delivered_rate' => '80%',
692 'opened_rate' => '25%',
693 'clickthrough_rate' => '0%',
694 );
695 $this->checkArrayEquals($expectedResult, $result['values'][$mail['id']]);
696 }
697
698 /**
699 * Test civicrm_mailing_delete.
700 */
701 public function testMailerDeleteSuccess() {
702 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
703 $this->callAPIAndDocument($this->_entity, 'delete', array('id' => $result['id']), __FUNCTION__, __FILE__);
704 $this->assertAPIDeleted($this->_entity, $result['id']);
705 }
706
707 /**
708 * Test Mailing.gettokens.
709 */
710 public function testMailGetTokens() {
711 $description = "Demonstrates fetching tokens for one or more entities (in this case \"Contact\" and \"Mailing\").
712 Optionally pass sequential=1 to have output ready-formatted for the select2 widget.";
713 $result = $this->callAPIAndDocument($this->_entity, 'gettokens', array('entity' => array('Contact', 'Mailing')), __FUNCTION__, __FILE__, $description);
714 $this->assertContains('Contact Type', $result['values']);
715
716 // Check that passing "sequential" correctly outputs a hierarchical array
717 $result = $this->callAPISuccess($this->_entity, 'gettokens', array('entity' => 'contact', 'sequential' => 1));
718 $this->assertArrayHasKey('text', $result['values'][0]);
719 $this->assertArrayHasKey('id', $result['values'][0]['children'][0]);
720 }
721
722 public function testClone() {
723 // BEGIN SAMPLE DATA
724 $groupIDs['inc'] = $this->groupCreate(array('name' => 'Example include group', 'title' => 'Example include group'));
725 $contactIDs['include_me'] = $this->individualCreate(array(
726 'email' => 'include.me@example.org',
727 'first_name' => 'Includer',
728 'last_name' => 'Person',
729 ));
730 $this->callAPISuccess('GroupContact', 'create', array(
731 'group_id' => $groupIDs['inc'],
732 'contact_id' => $contactIDs['include_me'],
733 ));
734
735 $params = $this->_params;
736 $params['groups']['include'] = array($groupIDs['inc']);
737 $params['groups']['exclude'] = array();
738 $params['mailings']['include'] = array();
739 $params['mailings']['exclude'] = array();
740 // END SAMPLE DATA
741
742 $create = $this->callAPISuccess('Mailing', 'create', $params);
743 $created = $this->callAPISuccess('Mailing', 'get', []);
744 $createId = $create['id'];
745 $this->createLoggedInUser();
746 $clone = $this->callAPIAndDocument('Mailing', 'clone', array('id' => $create['id']), __FUNCTION__, __FILE__);
747 $cloneId = $clone['id'];
748
749 $this->assertNotEquals($createId, $cloneId, 'Create and clone should return different records');
750 $this->assertTrue(is_numeric($cloneId));
751
752 $this->assertNotEmpty($clone['values'][$cloneId]['subject']);
753 $this->assertEquals($params['subject'], $clone['values'][$cloneId]['subject'], "Cloned subject should match");
754
755 // created_id is special - populated based on current user (ie the cloner).
756 $this->assertNotEmpty($clone['values'][$cloneId]['created_id']);
757 $this->assertNotEquals($create['values'][$createId]['created_id'], $clone['values'][$cloneId]['created_id'], 'Clone should be created by a different person');
758
759 // Target groups+mailings are special.
760 $cloneGroups = $this->callAPISuccess('MailingGroup', 'get', array('mailing_id' => $cloneId, 'sequential' => 1));
761 $this->assertEquals(1, $cloneGroups['count']);
762 $this->assertEquals($cloneGroups['values'][0]['group_type'], 'Include');
763 $this->assertEquals($cloneGroups['values'][0]['entity_table'], 'civicrm_group');
764 $this->assertEquals($cloneGroups['values'][0]['entity_id'], $groupIDs['inc']);
765 }
766
767 //@ todo tests below here are all failure tests which are not hugely useful - need success tests
768
769 //------------ civicrm_mailing_event_bounce methods------------
770
771 /**
772 * Test civicrm_mailing_event_bounce with wrong params.
773 * Note that tests like this are slightly better than no test but an
774 * api function cannot be considered supported / 'part of the api' without a
775 * success test
776 */
777 public function testMailerBounceWrongParams() {
778 $params = array(
779 'job_id' => 'Wrong ID',
780 'event_queue_id' => 'Wrong ID',
781 'hash' => 'Wrong Hash',
782 'body' => 'Body...',
783 'time_stamp' => '20111109212100',
784 );
785 $this->callAPIFailure('mailing_event', 'bounce', $params,
786 'Queue event could not be found'
787 );
788 }
789
790 //----------- civicrm_mailing_event_confirm methods -----------
791
792 /**
793 * Test civicrm_mailing_event_confirm with wrong params.
794 * Note that tests like this are slightly better than no test but an
795 * api function cannot be considered supported / 'part of the api' without a
796 * success test
797 */
798 public function testMailerConfirmWrongParams() {
799 $params = array(
800 'contact_id' => 'Wrong ID',
801 'subscribe_id' => 'Wrong ID',
802 'hash' => 'Wrong Hash',
803 'event_subscribe_id' => '123',
804 'time_stamp' => '20111111010101',
805 );
806 $this->callAPIFailure('mailing_event', 'confirm', $params,
807 'contact_id is not a valid integer'
808 );
809 }
810
811 //---------- civicrm_mailing_event_reply methods -----------
812
813 /**
814 * Test civicrm_mailing_event_reply with wrong params.
815 *
816 * Note that tests like this are slightly better than no test but an
817 * api function cannot be considered supported / 'part of the api' without a
818 * success test
819 */
820 public function testMailerReplyWrongParams() {
821 $params = array(
822 'job_id' => 'Wrong ID',
823 'event_queue_id' => 'Wrong ID',
824 'hash' => 'Wrong Hash',
825 'bodyTxt' => 'Body...',
826 'replyTo' => $this->_email,
827 'time_stamp' => '20111111010101',
828 );
829 $this->callAPIFailure('mailing_event', 'reply', $params,
830 'Queue event could not be found'
831 );
832 }
833
834
835 //----------- civicrm_mailing_event_forward methods ----------
836
837 /**
838 * Test civicrm_mailing_event_forward with wrong params.
839 * Note that tests like this are slightly better than no test but an
840 * api function cannot be considered supported / 'part of the api' without a
841 * success test
842 */
843 public function testMailerForwardWrongParams() {
844 $params = array(
845 'job_id' => 'Wrong ID',
846 'event_queue_id' => 'Wrong ID',
847 'hash' => 'Wrong Hash',
848 'email' => $this->_email,
849 'time_stamp' => '20111111010101',
850 );
851 $this->callAPIFailure('mailing_event', 'forward', $params,
852 'Queue event could not be found'
853 );
854 }
855
856 /**
857 * @param array $params
858 * Extra parameters for the draft mailing.
859 * @return array|int
860 */
861 public function createDraftMailing($params = array()) {
862 $createParams = array_merge($this->_params, $params);
863 $createResult = $this->callAPISuccess('mailing', 'create', $createParams, __FUNCTION__, __FILE__);
864 $this->assertTrue(is_numeric($createResult['id']));
865 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_mailing_job WHERE mailing_id = %1', array(
866 1 => array($createResult['id'], 'Integer'),
867 ));
868 return $createResult['id'];
869 }
870
871 /**
872 * Test to make sure that if the event queue hashes have been archived,
873 * we can still have working click-trough URLs working (CRM-17959).
874 */
875 public function testUrlWithMissingTrackingHash() {
876 $mail = $this->callAPISuccess('mailing', 'create', $this->_params + array('scheduled_date' => 'now'), __FUNCTION__, __FILE__);
877 $jobs = $this->callAPISuccess('mailing_job', 'get', array('mailing_id' => $mail['id']));
878 $this->assertEquals(1, $jobs['count']);
879
880 $params = array('mailing_id' => $mail['id'], 'test_email' => 'alice@example.org', 'test_group' => NULL);
881 $deliveredInfo = $this->callAPISuccess($this->_entity, 'send_test', $params);
882
883 $sql = "SELECT turl.id as url_id, turl.url, q.id as queue_id
884 FROM civicrm_mailing_trackable_url as turl
885 INNER JOIN civicrm_mailing_job as j ON turl.mailing_id = j.mailing_id
886 INNER JOIN civicrm_mailing_event_queue q ON j.id = q.job_id
887 ORDER BY turl.id DESC LIMIT 1";
888
889 $dao = CRM_Core_DAO::executeQuery($sql);
890 $this->assertTrue($dao->fetch());
891
892 $url = CRM_Mailing_Event_BAO_TrackableURLOpen::track($dao->queue_id, $dao->url_id);
893 $this->assertContains('https://civicrm.org', $url);
894
895 // Now delete the event queue hashes and see if the tracking still works.
896 CRM_Core_DAO::executeQuery('DELETE FROM civicrm_mailing_event_queue');
897
898 $url = CRM_Mailing_Event_BAO_TrackableURLOpen::track($dao->queue_id, $dao->url_id);
899 $this->assertContains('https://civicrm.org', $url);
900
901 // Ensure that Google CSS link is not tracked.
902 $sql = "SELECT id FROM civicrm_mailing_trackable_url where url = 'https://fonts.googleapis.com/css?family=Roboto+Condensed:400,700|Zilla+Slab:500,700'";
903 $this->assertEquals([], CRM_Core_DAO::executeQuery($sql)->fetchAll());
904 }
905
906 /**
907 * Test Trackable URL with unicode character
908 */
909 public function testTrackableURLWithUnicodeSign() {
910 $unicodeURL = "https://civiƄcrm.org";
911 $this->_params['body_text'] = str_replace("https://civicrm.org", $unicodeURL, $this->_params['body_text']);
912 $this->_params['body_html'] = str_replace("https://civicrm.org", $unicodeURL, $this->_params['body_html']);
913
914 $mail = $this->callAPISuccess('mailing', 'create', $this->_params + array('scheduled_date' => 'now'));
915
916 $params = array('mailing_id' => $mail['id'], 'test_email' => 'alice@example.org', 'test_group' => NULL);
917 $this->callAPISuccess($this->_entity, 'send_test', $params);
918
919 $sql = "SELECT turl.id as url_id, turl.url, q.id as queue_id
920 FROM civicrm_mailing_trackable_url as turl
921 INNER JOIN civicrm_mailing_job as j ON turl.mailing_id = j.mailing_id
922 INNER JOIN civicrm_mailing_event_queue q ON j.id = q.job_id
923 ORDER BY turl.id DESC LIMIT 1";
924
925 $dao = CRM_Core_DAO::executeQuery($sql);
926 $this->assertTrue($dao->fetch());
927
928 $url = CRM_Mailing_Event_BAO_TrackableURLOpen::track($dao->queue_id, $dao->url_id);
929 $this->assertContains($unicodeURL, $url);
930
931 // Now delete the event queue hashes and see if the tracking still works.
932 CRM_Core_DAO::executeQuery('DELETE FROM civicrm_mailing_event_queue');
933
934 $url = CRM_Mailing_Event_BAO_TrackableURLOpen::track($dao->queue_id, $dao->url_id);
935 $this->assertContains($unicodeURL, $url);
936 }
937
938 /**
939 * CRM-20892 : Test if Mail.create API throws error on update,
940 * if modified_date less then the date when the mail was last updated/created
941 */
942 public function testModifiedDateMismatchOnMailingUpdate() {
943 $mail = $this->callAPISuccess('mailing', 'create', $this->_params + array('modified_date' => 'now'));
944 try {
945 $this->callAPISuccess('mailing', 'create', $this->_params + array('id' => $mail['id'], 'modified_date' => '2 seconds ago'));
946 }
947 catch (Exception $e) {
948 $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());
949 }
950 }
951
952 }