Merge pull request #10393 from JMAConsulting/exportBatch
[civicrm-core.git] / tests / phpunit / api / v3 / MailingTest.php
CommitLineData
6a488035 1<?php
8891a36e 2/*
6a488035
TO
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
6a488035
TO
26
27/**
28 * Test APIv3 civicrm_mailing_* functions
29 *
6c6e6187 30 * @package CiviCRM
acb109b7 31 * @group headless
6a488035
TO
32 */
33class api_v3_MailingTest extends CiviUnitTestCase {
34 protected $_groupID;
35 protected $_email;
c43d01f3 36 protected $_apiversion = 3;
37 protected $_params = array();
e37b4b04 38 protected $_entity = 'Mailing';
161ae41b 39 protected $_contactID;
430ae6dd 40
d8e9212d
TO
41 /**
42 * APIv3 result from creating an example footer
43 * @var array
44 */
45 protected $footer;
46
00be9182 47 public function setUp() {
6a488035 48 parent::setUp();
7ada2376 49 $this->useTransaction();
97b7d4a0 50 CRM_Mailing_BAO_MailingJob::$mailsProcessed = 0; // DGW
161ae41b 51 $this->_contactID = $this->individualCreate();
fadb804f 52 $this->_groupID = $this->groupCreate();
ef170fe0 53 $this->_email = 'test@test.test';
c43d01f3 54 $this->_params = array(
51ae62c4 55 'subject' => 'Hello {contact.display_name}',
b4fb9733
ML
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>",
c43d01f3 58 'name' => 'mailing name',
161ae41b 59 'created_id' => $this->_contactID,
b0a31714
TO
60 'header_id' => '',
61 'footer_id' => '',
c43d01f3 62 );
d8e9212d
TO
63
64 $this->footer = civicrm_api3('MailingComponent', 'create', array(
7824ac72
SL
65 'name' => 'test domain footer',
66 'component_type' => 'footer',
d8e9212d
TO
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 ));
6a488035
TO
70 }
71
00be9182 72 public function tearDown() {
97b7d4a0
TO
73 CRM_Mailing_BAO_MailingJob::$mailsProcessed = 0; // DGW
74 parent::tearDown();
6a488035
TO
75 }
76
77 /**
fe482240 78 * Test civicrm_mailing_create.
6a488035
TO
79 */
80 public function testMailerCreateSuccess() {
5f445749 81 $result = $this->callAPIAndDocument('mailing', 'create', $this->_params + array('scheduled_date' => 'now'), __FUNCTION__, __FILE__);
c43d01f3 82 $jobs = $this->callAPISuccess('mailing_job', 'get', array('mailing_id' => $result['id']));
6a488035 83 $this->assertEquals(1, $jobs['count']);
ef170fe0 84 unset($this->_params['created_id']); // return isn't working on this in getAndCheck so lets not check it for now
e37b4b04 85 $this->getAndCheck($this->_params, $result['id'], 'mailing');
6a488035
TO
86 }
87
11bb7484 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
c442f1b6 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
703875d8
TO
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
6bc3944a
TO
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
21eb0c57
TO
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'));
92915c55
TO
165 $contactIDs['a'] = $this->individualCreate(array(
166 'email' => 'include.me@example.org',
167 'first_name' => 'Includer',
5396af74 168 'last_name' => 'Person',
92915c55
TO
169 ));
170 $contactIDs['b'] = $this->individualCreate(array(
171 'email' => 'exclude.me@example.org',
5396af74 172 'last_name' => 'Excluder',
92915c55
TO
173 ));
174 $this->callAPISuccess('GroupContact', 'create', array(
175 'group_id' => $groupIDs['a'],
5396af74 176 'contact_id' => $contactIDs['a'],
92915c55
TO
177 ));
178 $this->callAPISuccess('GroupContact', 'create', array(
179 'group_id' => $groupIDs['b'],
5396af74 180 'contact_id' => $contactIDs['b'],
92915c55 181 ));
21eb0c57
TO
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();
5f445749 190 $createParams['api.mailing_job.create'] = 1;
21eb0c57
TO
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);
39010e60
EM
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);
21eb0c57
TO
198
199 // ** Pass 2: Update without any changes to groups[include]
39010e60
EM
200 $nullOpParams = $createParams;
201 $nullOpParams['id'] = $createResult['id'];
7575b840 202 $updateParams['api.mailing_job.create'] = 1;
39010e60
EM
203 unset($nullOpParams['groups']['include']);
204 $this->callAPISuccess('Mailing', 'create', $nullOpParams);
21eb0c57
TO
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);
39010e60
EM
208 $getRecipient2 = $this->callAPISuccess('MailingRecipients', 'get', array('mailing_id' => $createResult['id']));
209 $getRecip2_ids = array_values(CRM_Utils_Array::collect('contact_id', $getRecipient2['values']));
7575b840 210 $this->assertEquals(array($contactIDs['a']), $getRecip2_ids);
21eb0c57
TO
211
212 // ** Pass 3: Update with different groups[include]
213 $updateParams = $createParams;
214 $updateParams['id'] = $createResult['id'];
215 $updateParams['groups']['include'] = array($groupIDs['b']);
7575b840 216 $updateParams['api.mailing_job.create'] = 1;
21eb0c57
TO
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);
39010e60
EM
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);
21eb0c57
TO
224 }
225
ef643544 226 public function testMailerPreview() {
51ae62c4 227 // BEGIN SAMPLE DATA
6c6e6187 228 $contactID = $this->individualCreate();
ef643544 229 $displayName = $this->callAPISuccess('contact', 'get', array('id' => $contactID));
230 $displayName = $displayName['values'][$contactID]['display_name'];
51ae62c4
TO
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
7811a84b 240
6c6e6187 241 $maxIDs = array(
51ae62c4
TO
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'),
39010e60 245 'recipient' => CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_recipients'),
51ae62c4
TO
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'
39010e60 251 $this->assertDBQuery($maxIDs['recipient'], 'SELECT MAX(id) FROM civicrm_mailing_recipients'); // 'Preview should not create any mailing_recipient records'
7811a84b 252
51ae62c4
TO
253 $previewResult = $result['values'][$result['id']]['api.Mailing.preview'];
254 $this->assertEquals("Hello $displayName", $previewResult['values']['subject']);
21b09c13
TO
255 $this->assertContains("This is $displayName", $previewResult['values']['body_text']);
256 $this->assertContains("<p>This is $displayName.</p>", $previewResult['values']['body_html']);
ef643544 257 }
7811a84b 258
fddd185d
TO
259 public function testMailerPreviewRecipients() {
260 // BEGIN SAMPLE DATA
161ae41b
TO
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'));
39010e60 263 $contactIDs['include_me'] = $this->individualCreate(array(
92915c55
TO
264 'email' => 'include.me@example.org',
265 'first_name' => 'Includer',
5396af74 266 'last_name' => 'Person',
92915c55 267 ));
39010e60 268 $contactIDs['exclude_me'] = $this->individualCreate(array(
92915c55 269 'email' => 'exclude.me@example.org',
39010e60 270 'last_name' => 'Excluder',
92915c55
TO
271 ));
272 $this->callAPISuccess('GroupContact', 'create', array(
273 'group_id' => $groupIDs['inc'],
5396af74 274 'contact_id' => $contactIDs['include_me'],
92915c55
TO
275 ));
276 $this->callAPISuccess('GroupContact', 'create', array(
277 'group_id' => $groupIDs['inc'],
5396af74 278 'contact_id' => $contactIDs['exclude_me'],
92915c55
TO
279 ));
280 $this->callAPISuccess('GroupContact', 'create', array(
281 'group_id' => $groupIDs['exc'],
5396af74 282 'contact_id' => $contactIDs['exclude_me'],
92915c55 283 ));
fddd185d
TO
284
285 $params = $this->_params;
161ae41b
TO
286 $params['groups']['include'] = array($groupIDs['inc']);
287 $params['groups']['exclude'] = array($groupIDs['exc']);
fddd185d
TO
288 $params['mailings']['include'] = array();
289 $params['mailings']['exclude'] = array();
b73e0c53 290 $params['options']['force_rollback'] = 1;
5f445749 291 $params['api.mailing_job.create'] = 1;
b73e0c53
TO
292 $params['api.MailingRecipients.get'] = array(
293 'mailing_id' => '$value.id',
294 'api.contact.getvalue' => array(
295 'return' => 'display_name',
296 ),
297 'api.email.getvalue' => array(
298 'return' => 'email',
299 ),
300 );
fddd185d
TO
301 // END SAMPLE DATA
302
6c6e6187 303 $maxIDs = array(
fddd185d
TO
304 'mailing' => CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing'),
305 'job' => CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_job'),
306 'group' => CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_group'),
307 );
b73e0c53 308 $create = $this->callAPIAndDocument('Mailing', 'create', $params, __FUNCTION__, __FILE__);
fddd185d
TO
309 $this->assertDBQuery($maxIDs['mailing'], 'SELECT MAX(id) FROM civicrm_mailing'); // 'Preview should not create any mailing records'
310 $this->assertDBQuery($maxIDs['job'], 'SELECT MAX(id) FROM civicrm_mailing_job'); // 'Preview should not create any mailing_job record'
311 $this->assertDBQuery($maxIDs['group'], 'SELECT MAX(id) FROM civicrm_mailing_group'); // 'Preview should not create any mailing_group records'
312
b73e0c53 313 $preview = $create['values'][$create['id']]['api.MailingRecipients.get'];
fddd185d 314 $previewIds = array_values(CRM_Utils_Array::collect('contact_id', $preview['values']));
39010e60 315 $this->assertEquals(array((string) $contactIDs['include_me']), $previewIds);
b73e0c53
TO
316 $previewEmails = array_values(CRM_Utils_Array::collect('api.email.getvalue', $preview['values']));
317 $this->assertEquals(array('include.me@example.org'), $previewEmails);
318 $previewNames = array_values(CRM_Utils_Array::collect('api.contact.getvalue', $preview['values']));
6c6e6187 319 $this->assertTrue((bool) preg_match('/Includer Person/', $previewNames[0]), "Name 'Includer Person' should appear in '" . $previewNames[0] . '"');
fddd185d
TO
320 }
321
b20e7428
AS
322 public function testMailerPreviewRecipientsDeduplicate() {
323 // BEGIN SAMPLE DATA
324 $groupIDs['grp'] = $this->groupCreate(array('name' => 'Example group', 'title' => 'Example group'));
325 $contactIDs['include_me'] = $this->individualCreate(array(
326 'email' => 'include.me@example.org',
327 'first_name' => 'Includer',
328 'last_name' => 'Person',
329 ));
330 $contactIDs['include_me_duplicate'] = $this->individualCreate(array(
331 'email' => 'include.me@example.org',
332 'first_name' => 'IncluderDuplicate',
333 'last_name' => 'Person',
334 ));
335 $this->callAPISuccess('GroupContact', 'create', array(
336 'group_id' => $groupIDs['grp'],
337 'contact_id' => $contactIDs['include_me'],
338 ));
339 $this->callAPISuccess('GroupContact', 'create', array(
340 'group_id' => $groupIDs['grp'],
341 'contact_id' => $contactIDs['include_me_duplicate'],
342 ));
343
344 $params = $this->_params;
345 $params['groups']['include'] = array($groupIDs['grp']);
346 $params['mailings']['include'] = array();
347 $params['options']['force_rollback'] = 1;
348 $params['dedupe_email'] = 1;
349 $params['api.mailing_job.create'] = 1;
350 $params['api.MailingRecipients.get'] = array(
351 'mailing_id' => '$value.id',
352 'api.contact.getvalue' => array(
353 'return' => 'display_name',
354 ),
355 'api.email.getvalue' => array(
356 'return' => 'email',
357 ),
358 );
359 // END SAMPLE DATA
360
3c27d467 361 $create = $this->callAPISuccess('Mailing', 'create', $params);
b20e7428
AS
362
363 $preview = $create['values'][$create['id']]['api.MailingRecipients.get'];
364 $this->assertEquals(1, $preview['count']);
365 $previewEmails = array_values(CRM_Utils_Array::collect('api.email.getvalue', $preview['values']));
366 $this->assertEquals(array('include.me@example.org'), $previewEmails);
367 }
368
39010e60
EM
369 /**
370 *
371 */
7ada2376 372 public function testMailerSendTest_email() {
92915c55
TO
373 $contactIDs['alice'] = $this->individualCreate(array(
374 'email' => 'alice@example.org',
375 'first_name' => 'Alice',
5396af74 376 'last_name' => 'Person',
92915c55 377 ));
7811a84b 378
ef643544 379 $mail = $this->callAPISuccess('mailing', 'create', $this->_params);
7811a84b 380
736a042c 381 $params = array('mailing_id' => $mail['id'], 'test_email' => 'alice@example.org', 'test_group' => NULL);
ef643544 382 $deliveredInfo = $this->callAPISuccess($this->_entity, 'send_test', $params);
383 $this->assertEquals(1, $deliveredInfo['count'], "in line " . __LINE__); // verify mail has been sent to user by count
736a042c
TO
384
385 $deliveredContacts = array_values(CRM_Utils_Array::collect('contact_id', $deliveredInfo['values']));
386 $this->assertEquals(array($contactIDs['alice']), $deliveredContacts);
387
388 $deliveredEmails = array_values(CRM_Utils_Array::collect('email', $deliveredInfo['values']));
389 $this->assertEquals(array('alice@example.org'), $deliveredEmails);
ef643544 390 }
7811a84b 391
39010e60
EM
392 /**
393 *
394 */
7ada2376
TO
395 public function testMailerSendTest_group() {
396 // BEGIN SAMPLE DATA
161ae41b 397 $groupIDs['inc'] = $this->groupCreate(array('name' => 'Example include group', 'title' => 'Example include group'));
92915c55
TO
398 $contactIDs['alice'] = $this->individualCreate(array(
399 'email' => 'alice@example.org',
400 'first_name' => 'Alice',
5396af74 401 'last_name' => 'Person',
92915c55
TO
402 ));
403 $contactIDs['bob'] = $this->individualCreate(array(
404 'email' => 'bob@example.org',
405 'first_name' => 'Bob',
5396af74 406 'last_name' => 'Person',
92915c55
TO
407 ));
408 $contactIDs['carol'] = $this->individualCreate(array(
409 'email' => 'carol@example.org',
410 'first_name' => 'Carol',
5396af74 411 'last_name' => 'Person',
92915c55
TO
412 ));
413 $this->callAPISuccess('GroupContact', 'create', array(
414 'group_id' => $groupIDs['inc'],
5396af74 415 'contact_id' => $contactIDs['alice'],
92915c55
TO
416 ));
417 $this->callAPISuccess('GroupContact', 'create', array(
418 'group_id' => $groupIDs['inc'],
5396af74 419 'contact_id' => $contactIDs['bob'],
92915c55
TO
420 ));
421 $this->callAPISuccess('GroupContact', 'create', array(
422 'group_id' => $groupIDs['inc'],
5396af74 423 'contact_id' => $contactIDs['carol'],
92915c55 424 ));
7ada2376
TO
425 // END SAMPLE DATA
426
427 $mail = $this->callAPISuccess('mailing', 'create', $this->_params);
428 $deliveredInfo = $this->callAPISuccess($this->_entity, 'send_test', array(
429 'mailing_id' => $mail['id'],
430 'test_email' => NULL,
161ae41b 431 'test_group' => $groupIDs['inc'],
7ada2376
TO
432 ));
433 $this->assertEquals(3, $deliveredInfo['count'], "in line " . __LINE__); // verify mail has been sent to user by count
736a042c 434
7ada2376 435 $deliveredContacts = array_values(CRM_Utils_Array::collect('contact_id', $deliveredInfo['values']));
161ae41b 436 $this->assertEquals(array($contactIDs['alice'], $contactIDs['bob'], $contactIDs['carol']), $deliveredContacts);
736a042c 437
7ada2376
TO
438 $deliveredEmails = array_values(CRM_Utils_Array::collect('email', $deliveredInfo['values']));
439 $this->assertEquals(array('alice@example.org', 'bob@example.org', 'carol@example.org'), $deliveredEmails);
440 }
441
ad4f6a9c
EM
442 /**
443 * @return array
444 */
6818346a
TO
445 public function submitProvider() {
446 $cases = array(); // $useLogin, $params, $expectedFailure, $expectedJobCount
447 $cases[] = array(
448 TRUE, //useLogin
d78cc635 449 array(), // createParams
6818346a
TO
450 array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
451 FALSE, // expectedFailure
452 1, // expectedJobCount
453 );
454 $cases[] = array(
455 FALSE, //useLogin
d78cc635 456 array(), // createParams
6818346a
TO
457 array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
458 "/Failed to determine current user/", // expectedFailure
459 0, // expectedJobCount
460 );
461 $cases[] = array(
462 TRUE, //useLogin
d78cc635 463 array(), // createParams
6818346a
TO
464 array('scheduled_date' => '2014-12-13 10:00:00'),
465 FALSE, // expectedFailure
466 1, // expectedJobCount
467 );
468 $cases[] = array(
469 TRUE, //useLogin
d78cc635 470 array(), // createParams
6818346a
TO
471 array(),
472 "/Missing parameter scheduled_date and.or approval_date/", // expectedFailure
473 0, // expectedJobCount
474 );
d78cc635
TO
475 $cases[] = array(
476 TRUE, //useLogin
477 array('name' => ''), // createParams
478 array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
21b09c13 479 "/Mailing cannot be sent. There are missing or invalid fields \\(name\\)./", // expectedFailure
d78cc635
TO
480 0, // expectedJobCount
481 );
21b09c13
TO
482 $cases[] = array(
483 TRUE, //useLogin
484 array('body_html' => '', 'body_text' => ''), // createParams
485 array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
486 "/Mailing cannot be sent. There are missing or invalid fields \\(body\\)./", // expectedFailure
487 0, // expectedJobCount
488 );
489 $cases[] = array(
490 TRUE, //useLogin
491 array('body_html' => 'Oops, did I leave my tokens at home?'), // createParams
492 array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
493 "/Mailing cannot be sent. There are missing or invalid fields \\(.*body_html.*optOut.*\\)./", // expectedFailure
494 0, // expectedJobCount
495 );
496 $cases[] = array(
497 TRUE, //useLogin
498 array('body_text' => 'Oops, did I leave my tokens at home?'), // createParams
499 array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
500 "/Mailing cannot be sent. There are missing or invalid fields \\(.*body_text.*optOut.*\\)./", // expectedFailure
501 0, // expectedJobCount
502 );
d8e9212d
TO
503 $cases[] = array(
504 TRUE, //useLogin
505 array('body_text' => 'Look ma, magic tokens in the text!', 'footer_id' => '%FOOTER%'), // createParams
506 array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
507 FALSE, // expectedFailure
508 1, // expectedJobCount
509 );
510 $cases[] = array(
511 TRUE, //useLogin
512 array('body_html' => '<p>Look ma, magic tokens in the markup!</p>', 'footer_id' => '%FOOTER%'), // createParams
513 array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'),
514 FALSE, // expectedFailure
515 1, // expectedJobCount
516 );
6818346a
TO
517 return $cases;
518 }
519
520 /**
521 * @param bool $useLogin
d78cc635
TO
522 * @param array $createParams
523 * @param array $submitParams
6818346a
TO
524 * @param null|string $expectedFailure
525 * @param int $expectedJobCount
526 * @dataProvider submitProvider
527 */
d78cc635 528 public function testMailerSubmit($useLogin, $createParams, $submitParams, $expectedFailure, $expectedJobCount) {
6818346a
TO
529 if ($useLogin) {
530 $this->createLoggedInUser();
531 }
532
d8e9212d
TO
533 if (isset($createParams['footer_id']) && $createParams['footer_id'] == '%FOOTER%') {
534 $createParams['footer_id'] = $this->footer['id'];
535 }
536
d78cc635 537 $id = $this->createDraftMailing($createParams);
6818346a 538
d78cc635 539 $submitParams['id'] = $id;
6818346a 540 if ($expectedFailure) {
d78cc635 541 $submitResult = $this->callAPIFailure('mailing', 'submit', $submitParams);
6818346a
TO
542 $this->assertRegExp($expectedFailure, $submitResult['error_message']);
543 }
544 else {
d78cc635 545 $submitResult = $this->callAPIAndDocument('mailing', 'submit', $submitParams, __FUNCTION__, __FILE__);
6818346a
TO
546 $this->assertTrue(is_numeric($submitResult['id']));
547 $this->assertTrue(is_numeric($submitResult['values'][$id]['scheduled_id']));
d78cc635 548 $this->assertEquals($submitParams['scheduled_date'], $submitResult['values'][$id]['scheduled_date']);
6818346a
TO
549 }
550 $this->assertDBQuery($expectedJobCount, 'SELECT count(*) FROM civicrm_mailing_job WHERE mailing_id = %1', array(
21dfd5f5 551 1 => array($id, 'Integer'),
6818346a
TO
552 ));
553 }
554
9629f523
JP
555 /**
556 * Test unsubscribe list contains correct groups
557 * when include = 'previous mailing'
558 */
559 public function testUnsubscribeGroupList() {
560 // Create set of groups and add a contact to both of them.
561 $groupID2 = $this->groupCreate(array('name' => 'Test group 2', 'title' => 'group title 2'));
562 $groupID3 = $this->groupCreate(array('name' => 'Test group 3', 'title' => 'group title 3'));
563 $contactId = $this->individualCreate();
564 foreach (array($groupID2, $groupID3) as $grp) {
565 $params = array(
566 'contact_id' => $contactId,
567 'group_id' => $grp,
568 );
569 $this->callAPISuccess('GroupContact', 'create', $params);
570 }
571
572 //Send mail to groupID3
573 $mail = $this->callAPISuccess('mailing', 'create', $this->_params);
574 $params = array('mailing_id' => $mail['id'], 'test_email' => NULL, 'test_group' => $groupID3);
575 $this->callAPISuccess($this->_entity, 'send_test', $params);
576
577 $mgParams = array(
578 'mailing_id' => $mail['id'],
579 'entity_table' => 'civicrm_group',
580 'entity_id' => $groupID3,
581 'group_type' => 'Include',
582 );
583 $mailingGroup = $this->callAPISuccess('MailingGroup', 'create', $mgParams);
584
585 //Include previous mail in the mailing group.
586 $mail2 = $this->callAPISuccess('mailing', 'create', $this->_params);
587 $params = array('mailing_id' => $mail2['id'], 'test_email' => NULL, 'test_group' => $groupID3);
588 $this->callAPISuccess($this->_entity, 'send_test', $params);
589
590 $mgParams = array(
591 'mailing_id' => $mail2['id'],
592 'entity_table' => 'civicrm_mailing',
593 'entity_id' => $mail['id'],
594 'group_type' => 'Include',
595 );
596 $mailingGroup = $this->callAPISuccess('MailingGroup', 'create', $mgParams);
597 //CRM-20431 - Delete group id that matches first mailing id.
598 $this->callAPISuccess('Group', 'delete', array('id' => $this->_groupID));
599 $jobId = CRM_Core_DAO::getFieldValue('CRM_Mailing_DAO_MailingJob', $mail2['id'], 'id', 'mailing_id');
600 $hash = CRM_Core_DAO::getFieldValue('CRM_Mailing_Event_DAO_Queue', $jobId, 'hash', 'job_id');
601 $queueId = CRM_Core_DAO::getFieldValue('CRM_Mailing_Event_DAO_Queue', $jobId, 'id', 'job_id');
602
603 $group = CRM_Mailing_Event_BAO_Unsubscribe::unsub_from_mailing($jobId, $queueId, $hash, TRUE);
604 //Assert only one group returns in the unsubscribe list.
605 $this->assertCount(1, $group);
606 $this->assertEquals($groupID3, key($group));
607 }
608
39010e60
EM
609 /**
610 *
611 */
ef643544 612 public function testMailerStats() {
613 $result = $this->groupContactCreate($this->_groupID, 100);
614 $this->assertEquals(100, $result['added']); //verify if 100 contacts are added for group
7811a84b 615
616 //Create and send test mail first and change the mail job to live,
617 //because stats api only works on live mail
ef643544 618 $mail = $this->callAPISuccess('mailing', 'create', $this->_params);
7811a84b 619 $params = array('mailing_id' => $mail['id'], 'test_email' => NULL, 'test_group' => $this->_groupID);
ef643544 620 $deliveredInfo = $this->callAPISuccess($this->_entity, 'send_test', $params);
92915c55 621 $deliveredIds = implode(',', array_keys($deliveredInfo['values']));
7811a84b 622
623 //Change the test mail into live
624 $sql = "UPDATE civicrm_mailing_job SET is_test = 0 WHERE mailing_id = {$mail['id']}";
625 CRM_Core_DAO::executeQuery($sql);
626
ef643544 627 foreach (array('bounce', 'unsubscribe', 'opened') as $type) {
628 $sql = "CREATE TEMPORARY TABLE mail_{$type}_temp
629(event_queue_id int, time_stamp datetime, delivered_id int)
630SELECT event_queue_id, time_stamp, id
631 FROM civicrm_mailing_event_delivered
632 WHERE id IN ($deliveredIds)
633 ORDER BY RAND() LIMIT 0,20;";
ef643544 634 CRM_Core_DAO::executeQuery($sql);
7811a84b 635
ef643544 636 $sql = "DELETE FROM civicrm_mailing_event_delivered WHERE id IN (SELECT delivered_id FROM mail_{$type}_temp);";
637 CRM_Core_DAO::executeQuery($sql);
7811a84b 638
ef643544 639 if ($type == 'unsubscribe') {
640 $sql = "INSERT INTO civicrm_mailing_event_{$type} (event_queue_id, time_stamp, org_unsubscribe)
641SELECT event_queue_id, time_stamp, 1 FROM mail_{$type}_temp";
642 }
643 else {
644 $sql = "INSERT INTO civicrm_mailing_event_{$type} (event_queue_id, time_stamp)
645SELECT event_queue_id, time_stamp FROM mail_{$type}_temp";
646 }
647 CRM_Core_DAO::executeQuery($sql);
648 }
7811a84b 649
ef643544 650 $result = $this->callAPISuccess('mailing', 'stats', array('mailing_id' => $mail['id']));
651 $expectedResult = array(
652 'Delivered' => 80, //since among 100 mails 20 has been bounced
653 'Bounces' => 20,
654 'Opened' => 20,
655 'Unique Clicks' => 0,
21dfd5f5 656 'Unsubscribers' => 20,
ef643544 657 );
ef643544 658 $this->checkArrayEquals($expectedResult, $result['values'][$mail['id']]);
659 }
92915c55 660
c43d01f3 661 /**
fe482240 662 * Test civicrm_mailing_delete.
c43d01f3 663 */
664 public function testMailerDeleteSuccess() {
e37b4b04 665 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
ad4f6a9c 666 $this->callAPIAndDocument($this->_entity, 'delete', array('id' => $result['id']), __FUNCTION__, __FILE__);
e37b4b04 667 $this->assertAPIDeleted($this->_entity, $result['id']);
c43d01f3 668 }
6a488035 669
1d280e03
CW
670 /**
671 * Test Mailing.gettokens.
672 */
673 public function testMailGetTokens() {
9950a1c9 674 $description = "Demonstrates fetching tokens for one or more entities (in this case \"Contact\" and \"Mailing\").
1d280e03 675 Optionally pass sequential=1 to have output ready-formatted for the select2 widget.";
9950a1c9
CW
676 $result = $this->callAPIAndDocument($this->_entity, 'gettokens', array('entity' => array('Contact', 'Mailing')), __FUNCTION__, __FILE__, $description);
677 $this->assertContains('Contact Type', $result['values']);
678
679 // Check that passing "sequential" correctly outputs a hierarchical array
680 $result = $this->callAPISuccess($this->_entity, 'gettokens', array('entity' => 'contact', 'sequential' => 1));
681 $this->assertArrayHasKey('text', $result['values'][0]);
682 $this->assertArrayHasKey('id', $result['values'][0]['children'][0]);
1d280e03
CW
683 }
684
00dc999a
TO
685 public function testClone() {
686 // BEGIN SAMPLE DATA
687 $groupIDs['inc'] = $this->groupCreate(array('name' => 'Example include group', 'title' => 'Example include group'));
688 $contactIDs['include_me'] = $this->individualCreate(array(
689 'email' => 'include.me@example.org',
690 'first_name' => 'Includer',
691 'last_name' => 'Person',
692 ));
693 $this->callAPISuccess('GroupContact', 'create', array(
694 'group_id' => $groupIDs['inc'],
695 'contact_id' => $contactIDs['include_me'],
696 ));
697
698 $params = $this->_params;
699 $params['groups']['include'] = array($groupIDs['inc']);
700 $params['groups']['exclude'] = array();
701 $params['mailings']['include'] = array();
702 $params['mailings']['exclude'] = array();
703 // END SAMPLE DATA
704
705 $create = $this->callAPISuccess('Mailing', 'create', $params);
706 $createId = $create['id'];
707 $this->createLoggedInUser();
708 $clone = $this->callAPIAndDocument('Mailing', 'clone', array('id' => $create['id']), __FUNCTION__, __FILE__);
709 $cloneId = $clone['id'];
710
711 $this->assertNotEquals($createId, $cloneId, 'Create and clone should return different records');
712 $this->assertTrue(is_numeric($cloneId));
713
714 $this->assertNotEmpty($clone['values'][$cloneId]['subject']);
715 $this->assertEquals($params['subject'], $clone['values'][$cloneId]['subject'], "Cloned subject should match");
716
717 // created_id is special - populated based on current user (ie the cloner).
718 $this->assertNotEmpty($clone['values'][$cloneId]['created_id']);
719 $this->assertNotEquals($create['values'][$createId]['created_id'], $clone['values'][$cloneId]['created_id'], 'Clone should be created by a different person');
720
721 // Target groups+mailings are special.
722 $cloneGroups = $this->callAPISuccess('MailingGroup', 'get', array('mailing_id' => $cloneId, 'sequential' => 1));
723 $this->assertEquals(1, $cloneGroups['count']);
724 $this->assertEquals($cloneGroups['values'][0]['group_type'], 'Include');
725 $this->assertEquals($cloneGroups['values'][0]['entity_table'], 'civicrm_group');
726 $this->assertEquals($cloneGroups['values'][0]['entity_id'], $groupIDs['inc']);
727 }
728
6a488035
TO
729 //@ todo tests below here are all failure tests which are not hugely useful - need success tests
730
731 //------------ civicrm_mailing_event_bounce methods------------
732
733 /**
734 * Test civicrm_mailing_event_bounce with wrong params.
e37b4b04 735 * Note that tests like this are slightly better than no test but an
736 * api function cannot be considered supported / 'part of the api' without a
737 * success test
6a488035
TO
738 */
739 public function testMailerBounceWrongParams() {
740 $params = array(
741 'job_id' => 'Wrong ID',
742 'event_queue_id' => 'Wrong ID',
743 'hash' => 'Wrong Hash',
744 'body' => 'Body...',
6a488035
TO
745 'time_stamp' => '20111109212100',
746 );
ad4f6a9c 747 $this->callAPIFailure('mailing_event', 'bounce', $params,
30d44db2 748 'Queue event could not be found'
749 );
6a488035
TO
750 }
751
752 //----------- civicrm_mailing_event_confirm methods -----------
753
754 /**
755 * Test civicrm_mailing_event_confirm with wrong params.
e37b4b04 756 * Note that tests like this are slightly better than no test but an
757 * api function cannot be considered supported / 'part of the api' without a
758 * success test
6a488035
TO
759 */
760 public function testMailerConfirmWrongParams() {
761 $params = array(
762 'contact_id' => 'Wrong ID',
763 'subscribe_id' => 'Wrong ID',
764 'hash' => 'Wrong Hash',
765 'event_subscribe_id' => '123',
766 'time_stamp' => '20111111010101',
ef170fe0 767 );
ad4f6a9c 768 $this->callAPIFailure('mailing_event', 'confirm', $params,
4f94e3fa 769 'contact_id is not a valid integer'
e37b4b04 770 );
6a488035
TO
771 }
772
773 //---------- civicrm_mailing_event_reply methods -----------
774
775 /**
776 * Test civicrm_mailing_event_reply with wrong params.
e37b4b04 777 *
778 * Note that tests like this are slightly better than no test but an
779 * api function cannot be considered supported / 'part of the api' without a
780 * success test
6a488035
TO
781 */
782 public function testMailerReplyWrongParams() {
783 $params = array(
784 'job_id' => 'Wrong ID',
785 'event_queue_id' => 'Wrong ID',
786 'hash' => 'Wrong Hash',
787 'bodyTxt' => 'Body...',
788 'replyTo' => $this->_email,
789 'time_stamp' => '20111111010101',
ef170fe0 790 );
ad4f6a9c 791 $this->callAPIFailure('mailing_event', 'reply', $params,
e37b4b04 792 'Queue event could not be found'
30d44db2 793 );
6a488035
TO
794 }
795
796
797 //----------- civicrm_mailing_event_forward methods ----------
798
799 /**
800 * Test civicrm_mailing_event_forward with wrong params.
e37b4b04 801 * Note that tests like this are slightly better than no test but an
802 * api function cannot be considered supported / 'part of the api' without a
803 * success test
6a488035
TO
804 */
805 public function testMailerForwardWrongParams() {
806 $params = array(
807 'job_id' => 'Wrong ID',
808 'event_queue_id' => 'Wrong ID',
809 'hash' => 'Wrong Hash',
810 'email' => $this->_email,
811 'time_stamp' => '20111111010101',
ef170fe0 812 );
ad4f6a9c 813 $this->callAPIFailure('mailing_event', 'forward', $params,
e37b4b04 814 'Queue event could not be found'
815 );
6a488035
TO
816 }
817
6818346a 818 /**
d78cc635
TO
819 * @param array $params
820 * Extra parameters for the draft mailing.
6818346a
TO
821 * @return array|int
822 */
d78cc635
TO
823 public function createDraftMailing($params = array()) {
824 $createParams = array_merge($this->_params, $params);
6818346a
TO
825 $createResult = $this->callAPISuccess('mailing', 'create', $createParams, __FUNCTION__, __FILE__);
826 $this->assertTrue(is_numeric($createResult['id']));
827 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_mailing_job WHERE mailing_id = %1', array(
21dfd5f5 828 1 => array($createResult['id'], 'Integer'),
6818346a
TO
829 ));
830 return $createResult['id'];
831 }
832
b4fb9733
ML
833 /**
834 * Test to make sure that if the event queue hashes have been archived,
835 * we can still have working click-trough URLs working (CRM-17959).
836 */
837 public function testUrlWithMissingTrackingHash() {
3c27d467 838 $mail = $this->callAPISuccess('mailing', 'create', $this->_params + array('scheduled_date' => 'now'), __FUNCTION__, __FILE__);
b4fb9733
ML
839 $jobs = $this->callAPISuccess('mailing_job', 'get', array('mailing_id' => $mail['id']));
840 $this->assertEquals(1, $jobs['count']);
841
842 $params = array('mailing_id' => $mail['id'], 'test_email' => 'alice@example.org', 'test_group' => NULL);
843 $deliveredInfo = $this->callAPISuccess($this->_entity, 'send_test', $params);
844
845 $sql = "SELECT turl.id as url_id, turl.url, q.id as queue_id
846 FROM civicrm_mailing_trackable_url as turl
847 INNER JOIN civicrm_mailing_job as j ON turl.mailing_id = j.mailing_id
848 INNER JOIN civicrm_mailing_event_queue q ON j.id = q.job_id
849 ORDER BY turl.id DESC LIMIT 1";
850
851 $dao = CRM_Core_DAO::executeQuery($sql);
852 $this->assertTrue($dao->fetch());
853
854 $url = CRM_Mailing_Event_BAO_TrackableURLOpen::track($dao->queue_id, $dao->url_id);
855 $this->assertContains('https://civicrm.org', $url);
856
857 // Now delete the event queue hashes and see if the tracking still works.
858 CRM_Core_DAO::executeQuery('DELETE FROM civicrm_mailing_event_queue');
859
860 $url = CRM_Mailing_Event_BAO_TrackableURLOpen::track($dao->queue_id, $dao->url_id);
861 $this->assertContains('https://civicrm.org', $url);
862 }
6a488035 863
3c70eacb 864 /**
865 * Test Trackable URL with unicode character
866 */
867 public function testTrackableURLWithUnicodeSign() {
868 $unicodeURL = "https://civiƄcrm.org";
869 $this->_params['body_text'] = str_replace("https://civicrm.org", $unicodeURL, $this->_params['body_text']);
870 $this->_params['body_html'] = str_replace("https://civicrm.org", $unicodeURL, $this->_params['body_html']);
871
3c27d467 872 $mail = $this->callAPISuccess('mailing', 'create', $this->_params + array('scheduled_date' => 'now'));
3c70eacb 873
874 $params = array('mailing_id' => $mail['id'], 'test_email' => 'alice@example.org', 'test_group' => NULL);
3c27d467 875 $this->callAPISuccess($this->_entity, 'send_test', $params);
3c70eacb 876
877 $sql = "SELECT turl.id as url_id, turl.url, q.id as queue_id
878 FROM civicrm_mailing_trackable_url as turl
879 INNER JOIN civicrm_mailing_job as j ON turl.mailing_id = j.mailing_id
880 INNER JOIN civicrm_mailing_event_queue q ON j.id = q.job_id
881 ORDER BY turl.id DESC LIMIT 1";
882
883 $dao = CRM_Core_DAO::executeQuery($sql);
884 $this->assertTrue($dao->fetch());
885
886 $url = CRM_Mailing_Event_BAO_TrackableURLOpen::track($dao->queue_id, $dao->url_id);
887 $this->assertContains($unicodeURL, $url);
888
889 // Now delete the event queue hashes and see if the tracking still works.
890 CRM_Core_DAO::executeQuery('DELETE FROM civicrm_mailing_event_queue');
891
892 $url = CRM_Mailing_Event_BAO_TrackableURLOpen::track($dao->queue_id, $dao->url_id);
893 $this->assertContains($unicodeURL, $url);
894 }
895
c26f9b8c
SL
896 /**
897 * CRM-20892 : Test if Mail.create API throws error on update,
898 * if modified_date less then the date when the mail was last updated/created
899 */
900 public function testModifiedDateMismatchOnMailingUpdate() {
901 $mail = $this->callAPISuccess('mailing', 'create', $this->_params + array('modified_date' => 'now'));
902 try {
903 $this->callAPISuccess('mailing', 'create', $this->_params + array('id' => $mail['id'], 'modified_date' => '2 seconds ago'));
904 }
905 catch (Exception $e) {
906 $this->assertRegExp("/Failure in api call for mailing create: Mailing has not been saved, Content maybe out of date, please refresh the page and try again/", $e->getMessage());
907 }
908 }
909
6a488035 910}