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