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