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