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