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