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 | ||
26 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
27 | ||
28 | ||
29 | /** | |
30 | * Test APIv3 civicrm_mailing_* functions | |
31 | * | |
6c6e6187 | 32 | * @package CiviCRM |
6a488035 TO |
33 | */ |
34 | class api_v3_MailingTest extends CiviUnitTestCase { | |
35 | protected $_groupID; | |
36 | protected $_email; | |
c43d01f3 | 37 | protected $_apiversion = 3; |
38 | protected $_params = array(); | |
e37b4b04 | 39 | protected $_entity = 'Mailing'; |
161ae41b | 40 | protected $_contactID; |
430ae6dd | 41 | |
00be9182 | 42 | public function setUp() { |
6a488035 | 43 | parent::setUp(); |
7ada2376 | 44 | $this->useTransaction(); |
97b7d4a0 | 45 | CRM_Mailing_BAO_MailingJob::$mailsProcessed = 0; // DGW |
161ae41b | 46 | $this->_contactID = $this->individualCreate(); |
fadb804f | 47 | $this->_groupID = $this->groupCreate(); |
ef170fe0 | 48 | $this->_email = 'test@test.test'; |
c43d01f3 | 49 | $this->_params = array( |
51ae62c4 | 50 | 'subject' => 'Hello {contact.display_name}', |
7811a84b | 51 | 'body_text' => "This is {contact.display_name}", |
51ae62c4 | 52 | 'body_html' => "<p>This is {contact.display_name}</p>", |
c43d01f3 | 53 | 'name' => 'mailing name', |
161ae41b | 54 | 'created_id' => $this->_contactID, |
c43d01f3 | 55 | ); |
6a488035 TO |
56 | } |
57 | ||
00be9182 | 58 | public function tearDown() { |
97b7d4a0 TO |
59 | CRM_Mailing_BAO_MailingJob::$mailsProcessed = 0; // DGW |
60 | parent::tearDown(); | |
6a488035 TO |
61 | } |
62 | ||
63 | /** | |
64 | * Test civicrm_mailing_create | |
65 | */ | |
66 | public function testMailerCreateSuccess() { | |
c43d01f3 | 67 | $result = $this->callAPIAndDocument('mailing', 'create', $this->_params, __FUNCTION__, __FILE__); |
68 | $jobs = $this->callAPISuccess('mailing_job', 'get', array('mailing_id' => $result['id'])); | |
6a488035 | 69 | $this->assertEquals(1, $jobs['count']); |
ef170fe0 | 70 | unset($this->_params['created_id']); // return isn't working on this in getAndCheck so lets not check it for now |
e37b4b04 | 71 | $this->getAndCheck($this->_params, $result['id'], 'mailing'); |
6a488035 TO |
72 | } |
73 | ||
21eb0c57 TO |
74 | /** |
75 | * The Mailing.create API supports magic properties "groups[include,enclude]" and "mailings[include,exclude]". | |
76 | * Make sure these work | |
77 | */ | |
78 | public function testMagicGroups_create_update() { | |
79 | // BEGIN SAMPLE DATA | |
80 | $groupIDs['a'] = $this->groupCreate(array('name' => 'Example include group', 'title' => 'Example include group')); | |
81 | $groupIDs['b'] = $this->groupCreate(array('name' => 'Example exclude group', 'title' => 'Example exclude group')); | |
92915c55 TO |
82 | $contactIDs['a'] = $this->individualCreate(array( |
83 | 'email' => 'include.me@example.org', | |
84 | 'first_name' => 'Includer', | |
85 | 'last_name' => 'Person' | |
86 | )); | |
87 | $contactIDs['b'] = $this->individualCreate(array( | |
88 | 'email' => 'exclude.me@example.org', | |
92915c55 TO |
89 | 'last_name' => 'Excluder' |
90 | )); | |
91 | $this->callAPISuccess('GroupContact', 'create', array( | |
92 | 'group_id' => $groupIDs['a'], | |
93 | 'contact_id' => $contactIDs['a'] | |
94 | )); | |
95 | $this->callAPISuccess('GroupContact', 'create', array( | |
96 | 'group_id' => $groupIDs['b'], | |
97 | 'contact_id' => $contactIDs['b'] | |
98 | )); | |
21eb0c57 TO |
99 | // END SAMPLE DATA |
100 | ||
101 | // ** Pass 1: Create | |
102 | $createParams = $this->_params; | |
103 | $createParams['groups']['include'] = array($groupIDs['a']); | |
104 | $createParams['groups']['exclude'] = array(); | |
105 | $createParams['mailings']['include'] = array(); | |
106 | $createParams['mailings']['exclude'] = array(); | |
107 | $createResult = $this->callAPISuccess('Mailing', 'create', $createParams); | |
108 | $getGroup1 = $this->callAPISuccess('MailingGroup', 'get', array('mailing_id' => $createResult['id'])); | |
109 | $getGroup1_ids = array_values(CRM_Utils_Array::collect('entity_id', $getGroup1['values'])); | |
110 | $this->assertEquals(array($groupIDs['a']), $getGroup1_ids); | |
39010e60 EM |
111 | $getRecipient1 = $this->callAPISuccess('MailingRecipients', 'get', array('mailing_id' => $createResult['id'])); |
112 | $getRecipient1_ids = array_values(CRM_Utils_Array::collect('contact_id', $getRecipient1['values'])); | |
113 | $this->assertEquals(array($contactIDs['a']), $getRecipient1_ids); | |
21eb0c57 TO |
114 | |
115 | // ** Pass 2: Update without any changes to groups[include] | |
39010e60 EM |
116 | $nullOpParams = $createParams; |
117 | $nullOpParams['id'] = $createResult['id']; | |
7575b840 | 118 | $updateParams['api.mailing_job.create'] = 1; |
39010e60 EM |
119 | unset($nullOpParams['groups']['include']); |
120 | $this->callAPISuccess('Mailing', 'create', $nullOpParams); | |
21eb0c57 TO |
121 | $getGroup2 = $this->callAPISuccess('MailingGroup', 'get', array('mailing_id' => $createResult['id'])); |
122 | $getGroup2_ids = array_values(CRM_Utils_Array::collect('entity_id', $getGroup2['values'])); | |
123 | $this->assertEquals(array($groupIDs['a']), $getGroup2_ids); | |
39010e60 EM |
124 | $getRecipient2 = $this->callAPISuccess('MailingRecipients', 'get', array('mailing_id' => $createResult['id'])); |
125 | $getRecip2_ids = array_values(CRM_Utils_Array::collect('contact_id', $getRecipient2['values'])); | |
7575b840 | 126 | $this->assertEquals(array($contactIDs['a']), $getRecip2_ids); |
21eb0c57 TO |
127 | |
128 | // ** Pass 3: Update with different groups[include] | |
129 | $updateParams = $createParams; | |
130 | $updateParams['id'] = $createResult['id']; | |
131 | $updateParams['groups']['include'] = array($groupIDs['b']); | |
7575b840 | 132 | $updateParams['api.mailing_job.create'] = 1; |
21eb0c57 TO |
133 | $this->callAPISuccess('Mailing', 'create', $updateParams); |
134 | $getGroup3 = $this->callAPISuccess('MailingGroup', 'get', array('mailing_id' => $createResult['id'])); | |
135 | $getGroup3_ids = array_values(CRM_Utils_Array::collect('entity_id', $getGroup3['values'])); | |
136 | $this->assertEquals(array($groupIDs['b']), $getGroup3_ids); | |
39010e60 EM |
137 | $getRecipient3 = $this->callAPISuccess('MailingRecipients', 'get', array('mailing_id' => $createResult['id'])); |
138 | $getRecipient3_ids = array_values(CRM_Utils_Array::collect('contact_id', $getRecipient3['values'])); | |
139 | $this->assertEquals(array($contactIDs['b']), $getRecipient3_ids); | |
21eb0c57 TO |
140 | } |
141 | ||
ef643544 | 142 | public function testMailerPreview() { |
51ae62c4 | 143 | // BEGIN SAMPLE DATA |
6c6e6187 | 144 | $contactID = $this->individualCreate(); |
ef643544 | 145 | $displayName = $this->callAPISuccess('contact', 'get', array('id' => $contactID)); |
146 | $displayName = $displayName['values'][$contactID]['display_name']; | |
51ae62c4 TO |
147 | $this->assertTrue(!empty($displayName)); |
148 | ||
149 | $params = $this->_params; | |
150 | $params['api.Mailing.preview'] = array( | |
151 | 'id' => '$value.id', | |
152 | 'contact_id' => $contactID, | |
153 | ); | |
154 | $params['options']['force_rollback'] = 1; | |
155 | // END SAMPLE DATA | |
7811a84b | 156 | |
6c6e6187 | 157 | $maxIDs = array( |
51ae62c4 TO |
158 | 'mailing' => CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing'), |
159 | 'job' => CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_job'), | |
160 | 'group' => CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_group'), | |
39010e60 | 161 | 'recipient' => CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_recipients'), |
51ae62c4 TO |
162 | ); |
163 | $result = $this->callAPISuccess('mailing', 'create', $params); | |
164 | $this->assertDBQuery($maxIDs['mailing'], 'SELECT MAX(id) FROM civicrm_mailing'); // 'Preview should not create any mailing records' | |
165 | $this->assertDBQuery($maxIDs['job'], 'SELECT MAX(id) FROM civicrm_mailing_job'); // 'Preview should not create any mailing_job record' | |
166 | $this->assertDBQuery($maxIDs['group'], 'SELECT MAX(id) FROM civicrm_mailing_group'); // 'Preview should not create any mailing_group records' | |
39010e60 | 167 | $this->assertDBQuery($maxIDs['recipient'], 'SELECT MAX(id) FROM civicrm_mailing_recipients'); // 'Preview should not create any mailing_recipient records' |
7811a84b | 168 | |
51ae62c4 TO |
169 | $previewResult = $result['values'][$result['id']]['api.Mailing.preview']; |
170 | $this->assertEquals("Hello $displayName", $previewResult['values']['subject']); | |
171 | $this->assertEquals("This is $displayName", $previewResult['values']['body_text']); | |
172 | $this->assertContains("<p>This is $displayName</p>", $previewResult['values']['body_html']); | |
ef643544 | 173 | } |
7811a84b | 174 | |
fddd185d TO |
175 | public function testMailerPreviewRecipients() { |
176 | // BEGIN SAMPLE DATA | |
161ae41b TO |
177 | $groupIDs['inc'] = $this->groupCreate(array('name' => 'Example include group', 'title' => 'Example include group')); |
178 | $groupIDs['exc'] = $this->groupCreate(array('name' => 'Example exclude group', 'title' => 'Example exclude group')); | |
39010e60 | 179 | $contactIDs['include_me'] = $this->individualCreate(array( |
92915c55 TO |
180 | 'email' => 'include.me@example.org', |
181 | 'first_name' => 'Includer', | |
182 | 'last_name' => 'Person' | |
183 | )); | |
39010e60 | 184 | $contactIDs['exclude_me'] = $this->individualCreate(array( |
92915c55 | 185 | 'email' => 'exclude.me@example.org', |
39010e60 | 186 | 'last_name' => 'Excluder', |
92915c55 TO |
187 | )); |
188 | $this->callAPISuccess('GroupContact', 'create', array( | |
189 | 'group_id' => $groupIDs['inc'], | |
39010e60 | 190 | 'contact_id' => $contactIDs['include_me'] |
92915c55 TO |
191 | )); |
192 | $this->callAPISuccess('GroupContact', 'create', array( | |
193 | 'group_id' => $groupIDs['inc'], | |
39010e60 | 194 | 'contact_id' => $contactIDs['exclude_me'] |
92915c55 TO |
195 | )); |
196 | $this->callAPISuccess('GroupContact', 'create', array( | |
197 | 'group_id' => $groupIDs['exc'], | |
39010e60 | 198 | 'contact_id' => $contactIDs['exclude_me'] |
92915c55 | 199 | )); |
fddd185d TO |
200 | |
201 | $params = $this->_params; | |
161ae41b TO |
202 | $params['groups']['include'] = array($groupIDs['inc']); |
203 | $params['groups']['exclude'] = array($groupIDs['exc']); | |
fddd185d TO |
204 | $params['mailings']['include'] = array(); |
205 | $params['mailings']['exclude'] = array(); | |
b73e0c53 TO |
206 | $params['options']['force_rollback'] = 1; |
207 | $params['api.MailingRecipients.get'] = array( | |
208 | 'mailing_id' => '$value.id', | |
209 | 'api.contact.getvalue' => array( | |
210 | 'return' => 'display_name', | |
211 | ), | |
212 | 'api.email.getvalue' => array( | |
213 | 'return' => 'email', | |
214 | ), | |
215 | ); | |
fddd185d TO |
216 | // END SAMPLE DATA |
217 | ||
6c6e6187 | 218 | $maxIDs = array( |
fddd185d TO |
219 | 'mailing' => CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing'), |
220 | 'job' => CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_job'), | |
221 | 'group' => CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_mailing_group'), | |
222 | ); | |
b73e0c53 | 223 | $create = $this->callAPIAndDocument('Mailing', 'create', $params, __FUNCTION__, __FILE__); |
fddd185d TO |
224 | $this->assertDBQuery($maxIDs['mailing'], 'SELECT MAX(id) FROM civicrm_mailing'); // 'Preview should not create any mailing records' |
225 | $this->assertDBQuery($maxIDs['job'], 'SELECT MAX(id) FROM civicrm_mailing_job'); // 'Preview should not create any mailing_job record' | |
226 | $this->assertDBQuery($maxIDs['group'], 'SELECT MAX(id) FROM civicrm_mailing_group'); // 'Preview should not create any mailing_group records' | |
227 | ||
b73e0c53 | 228 | $preview = $create['values'][$create['id']]['api.MailingRecipients.get']; |
fddd185d | 229 | $previewIds = array_values(CRM_Utils_Array::collect('contact_id', $preview['values'])); |
39010e60 | 230 | $this->assertEquals(array((string) $contactIDs['include_me']), $previewIds); |
b73e0c53 TO |
231 | $previewEmails = array_values(CRM_Utils_Array::collect('api.email.getvalue', $preview['values'])); |
232 | $this->assertEquals(array('include.me@example.org'), $previewEmails); | |
233 | $previewNames = array_values(CRM_Utils_Array::collect('api.contact.getvalue', $preview['values'])); | |
6c6e6187 | 234 | $this->assertTrue((bool) preg_match('/Includer Person/', $previewNames[0]), "Name 'Includer Person' should appear in '" . $previewNames[0] . '"'); |
fddd185d TO |
235 | } |
236 | ||
39010e60 EM |
237 | /** |
238 | * | |
239 | */ | |
7ada2376 | 240 | public function testMailerSendTest_email() { |
92915c55 TO |
241 | $contactIDs['alice'] = $this->individualCreate(array( |
242 | 'email' => 'alice@example.org', | |
243 | 'first_name' => 'Alice', | |
244 | 'last_name' => 'Person' | |
245 | )); | |
7811a84b | 246 | |
ef643544 | 247 | $mail = $this->callAPISuccess('mailing', 'create', $this->_params); |
7811a84b | 248 | |
736a042c | 249 | $params = array('mailing_id' => $mail['id'], 'test_email' => 'alice@example.org', 'test_group' => NULL); |
ef643544 | 250 | $deliveredInfo = $this->callAPISuccess($this->_entity, 'send_test', $params); |
251 | $this->assertEquals(1, $deliveredInfo['count'], "in line " . __LINE__); // verify mail has been sent to user by count | |
736a042c TO |
252 | |
253 | $deliveredContacts = array_values(CRM_Utils_Array::collect('contact_id', $deliveredInfo['values'])); | |
254 | $this->assertEquals(array($contactIDs['alice']), $deliveredContacts); | |
255 | ||
256 | $deliveredEmails = array_values(CRM_Utils_Array::collect('email', $deliveredInfo['values'])); | |
257 | $this->assertEquals(array('alice@example.org'), $deliveredEmails); | |
ef643544 | 258 | } |
7811a84b | 259 | |
39010e60 EM |
260 | /** |
261 | * | |
262 | */ | |
7ada2376 TO |
263 | public function testMailerSendTest_group() { |
264 | // BEGIN SAMPLE DATA | |
161ae41b | 265 | $groupIDs['inc'] = $this->groupCreate(array('name' => 'Example include group', 'title' => 'Example include group')); |
92915c55 TO |
266 | $contactIDs['alice'] = $this->individualCreate(array( |
267 | 'email' => 'alice@example.org', | |
268 | 'first_name' => 'Alice', | |
269 | 'last_name' => 'Person' | |
270 | )); | |
271 | $contactIDs['bob'] = $this->individualCreate(array( | |
272 | 'email' => 'bob@example.org', | |
273 | 'first_name' => 'Bob', | |
274 | 'last_name' => 'Person' | |
275 | )); | |
276 | $contactIDs['carol'] = $this->individualCreate(array( | |
277 | 'email' => 'carol@example.org', | |
278 | 'first_name' => 'Carol', | |
279 | 'last_name' => 'Person' | |
280 | )); | |
281 | $this->callAPISuccess('GroupContact', 'create', array( | |
282 | 'group_id' => $groupIDs['inc'], | |
283 | 'contact_id' => $contactIDs['alice'] | |
284 | )); | |
285 | $this->callAPISuccess('GroupContact', 'create', array( | |
286 | 'group_id' => $groupIDs['inc'], | |
287 | 'contact_id' => $contactIDs['bob'] | |
288 | )); | |
289 | $this->callAPISuccess('GroupContact', 'create', array( | |
290 | 'group_id' => $groupIDs['inc'], | |
291 | 'contact_id' => $contactIDs['carol'] | |
292 | )); | |
7ada2376 TO |
293 | // END SAMPLE DATA |
294 | ||
295 | $mail = $this->callAPISuccess('mailing', 'create', $this->_params); | |
296 | $deliveredInfo = $this->callAPISuccess($this->_entity, 'send_test', array( | |
297 | 'mailing_id' => $mail['id'], | |
298 | 'test_email' => NULL, | |
161ae41b | 299 | 'test_group' => $groupIDs['inc'], |
7ada2376 TO |
300 | )); |
301 | $this->assertEquals(3, $deliveredInfo['count'], "in line " . __LINE__); // verify mail has been sent to user by count | |
736a042c | 302 | |
7ada2376 | 303 | $deliveredContacts = array_values(CRM_Utils_Array::collect('contact_id', $deliveredInfo['values'])); |
161ae41b | 304 | $this->assertEquals(array($contactIDs['alice'], $contactIDs['bob'], $contactIDs['carol']), $deliveredContacts); |
736a042c | 305 | |
7ada2376 TO |
306 | $deliveredEmails = array_values(CRM_Utils_Array::collect('email', $deliveredInfo['values'])); |
307 | $this->assertEquals(array('alice@example.org', 'bob@example.org', 'carol@example.org'), $deliveredEmails); | |
308 | } | |
309 | ||
ad4f6a9c EM |
310 | /** |
311 | * @return array | |
312 | */ | |
6818346a TO |
313 | public function submitProvider() { |
314 | $cases = array(); // $useLogin, $params, $expectedFailure, $expectedJobCount | |
315 | $cases[] = array( | |
316 | TRUE, //useLogin | |
317 | array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'), | |
318 | FALSE, // expectedFailure | |
319 | 1, // expectedJobCount | |
320 | ); | |
321 | $cases[] = array( | |
322 | FALSE, //useLogin | |
323 | array('scheduled_date' => '2014-12-13 10:00:00', 'approval_date' => '2014-12-13 00:00:00'), | |
324 | "/Failed to determine current user/", // expectedFailure | |
325 | 0, // expectedJobCount | |
326 | ); | |
327 | $cases[] = array( | |
328 | TRUE, //useLogin | |
329 | array('scheduled_date' => '2014-12-13 10:00:00'), | |
330 | FALSE, // expectedFailure | |
331 | 1, // expectedJobCount | |
332 | ); | |
333 | $cases[] = array( | |
334 | TRUE, //useLogin | |
335 | array(), | |
336 | "/Missing parameter scheduled_date and.or approval_date/", // expectedFailure | |
337 | 0, // expectedJobCount | |
338 | ); | |
339 | return $cases; | |
340 | } | |
341 | ||
342 | /** | |
343 | * @param bool $useLogin | |
344 | * @param array $params | |
345 | * @param null|string $expectedFailure | |
346 | * @param int $expectedJobCount | |
347 | * @dataProvider submitProvider | |
348 | */ | |
349 | public function testMailerSubmit($useLogin, $params, $expectedFailure, $expectedJobCount) { | |
350 | if ($useLogin) { | |
351 | $this->createLoggedInUser(); | |
352 | } | |
353 | ||
354 | $id = $this->createDraftMailing(); | |
355 | ||
356 | $params['id'] = $id; | |
357 | if ($expectedFailure) { | |
358 | $submitResult = $this->callAPIFailure('mailing', 'submit', $params); | |
359 | $this->assertRegExp($expectedFailure, $submitResult['error_message']); | |
360 | } | |
361 | else { | |
362 | $submitResult = $this->callAPIAndDocument('mailing', 'submit', $params, __FUNCTION__, __FILE__); | |
363 | $this->assertTrue(is_numeric($submitResult['id'])); | |
364 | $this->assertTrue(is_numeric($submitResult['values'][$id]['scheduled_id'])); | |
365 | $this->assertEquals($params['scheduled_date'], $submitResult['values'][$id]['scheduled_date']); | |
366 | } | |
367 | $this->assertDBQuery($expectedJobCount, 'SELECT count(*) FROM civicrm_mailing_job WHERE mailing_id = %1', array( | |
21dfd5f5 | 368 | 1 => array($id, 'Integer'), |
6818346a TO |
369 | )); |
370 | } | |
371 | ||
39010e60 EM |
372 | /** |
373 | * | |
374 | */ | |
ef643544 | 375 | public function testMailerStats() { |
376 | $result = $this->groupContactCreate($this->_groupID, 100); | |
377 | $this->assertEquals(100, $result['added']); //verify if 100 contacts are added for group | |
7811a84b | 378 | |
379 | //Create and send test mail first and change the mail job to live, | |
380 | //because stats api only works on live mail | |
ef643544 | 381 | $mail = $this->callAPISuccess('mailing', 'create', $this->_params); |
7811a84b | 382 | $params = array('mailing_id' => $mail['id'], 'test_email' => NULL, 'test_group' => $this->_groupID); |
ef643544 | 383 | $deliveredInfo = $this->callAPISuccess($this->_entity, 'send_test', $params); |
92915c55 | 384 | $deliveredIds = implode(',', array_keys($deliveredInfo['values'])); |
7811a84b | 385 | |
386 | //Change the test mail into live | |
387 | $sql = "UPDATE civicrm_mailing_job SET is_test = 0 WHERE mailing_id = {$mail['id']}"; | |
388 | CRM_Core_DAO::executeQuery($sql); | |
389 | ||
ef643544 | 390 | foreach (array('bounce', 'unsubscribe', 'opened') as $type) { |
391 | $sql = "CREATE TEMPORARY TABLE mail_{$type}_temp | |
392 | (event_queue_id int, time_stamp datetime, delivered_id int) | |
393 | SELECT event_queue_id, time_stamp, id | |
394 | FROM civicrm_mailing_event_delivered | |
395 | WHERE id IN ($deliveredIds) | |
396 | ORDER BY RAND() LIMIT 0,20;"; | |
ef643544 | 397 | CRM_Core_DAO::executeQuery($sql); |
7811a84b | 398 | |
ef643544 | 399 | $sql = "DELETE FROM civicrm_mailing_event_delivered WHERE id IN (SELECT delivered_id FROM mail_{$type}_temp);"; |
400 | CRM_Core_DAO::executeQuery($sql); | |
7811a84b | 401 | |
ef643544 | 402 | if ($type == 'unsubscribe') { |
403 | $sql = "INSERT INTO civicrm_mailing_event_{$type} (event_queue_id, time_stamp, org_unsubscribe) | |
404 | SELECT event_queue_id, time_stamp, 1 FROM mail_{$type}_temp"; | |
405 | } | |
406 | else { | |
407 | $sql = "INSERT INTO civicrm_mailing_event_{$type} (event_queue_id, time_stamp) | |
408 | SELECT event_queue_id, time_stamp FROM mail_{$type}_temp"; | |
409 | } | |
410 | CRM_Core_DAO::executeQuery($sql); | |
411 | } | |
7811a84b | 412 | |
ef643544 | 413 | $result = $this->callAPISuccess('mailing', 'stats', array('mailing_id' => $mail['id'])); |
414 | $expectedResult = array( | |
415 | 'Delivered' => 80, //since among 100 mails 20 has been bounced | |
416 | 'Bounces' => 20, | |
417 | 'Opened' => 20, | |
418 | 'Unique Clicks' => 0, | |
21dfd5f5 | 419 | 'Unsubscribers' => 20, |
ef643544 | 420 | ); |
ef643544 | 421 | $this->checkArrayEquals($expectedResult, $result['values'][$mail['id']]); |
422 | } | |
92915c55 | 423 | |
c43d01f3 | 424 | /** |
30d44db2 | 425 | * Test civicrm_mailing_delete |
c43d01f3 | 426 | */ |
427 | public function testMailerDeleteSuccess() { | |
e37b4b04 | 428 | $result = $this->callAPISuccess($this->_entity, 'create', $this->_params); |
ad4f6a9c | 429 | $this->callAPIAndDocument($this->_entity, 'delete', array('id' => $result['id']), __FUNCTION__, __FILE__); |
e37b4b04 | 430 | $this->assertAPIDeleted($this->_entity, $result['id']); |
c43d01f3 | 431 | } |
6a488035 TO |
432 | |
433 | //@ todo tests below here are all failure tests which are not hugely useful - need success tests | |
434 | ||
435 | //------------ civicrm_mailing_event_bounce methods------------ | |
436 | ||
437 | /** | |
438 | * Test civicrm_mailing_event_bounce with wrong params. | |
e37b4b04 | 439 | * Note that tests like this are slightly better than no test but an |
440 | * api function cannot be considered supported / 'part of the api' without a | |
441 | * success test | |
6a488035 TO |
442 | */ |
443 | public function testMailerBounceWrongParams() { | |
444 | $params = array( | |
445 | 'job_id' => 'Wrong ID', | |
446 | 'event_queue_id' => 'Wrong ID', | |
447 | 'hash' => 'Wrong Hash', | |
448 | 'body' => 'Body...', | |
6a488035 TO |
449 | 'time_stamp' => '20111109212100', |
450 | ); | |
ad4f6a9c | 451 | $this->callAPIFailure('mailing_event', 'bounce', $params, |
30d44db2 | 452 | 'Queue event could not be found' |
453 | ); | |
6a488035 TO |
454 | } |
455 | ||
456 | //----------- civicrm_mailing_event_confirm methods ----------- | |
457 | ||
458 | /** | |
459 | * Test civicrm_mailing_event_confirm with wrong params. | |
e37b4b04 | 460 | * Note that tests like this are slightly better than no test but an |
461 | * api function cannot be considered supported / 'part of the api' without a | |
462 | * success test | |
6a488035 TO |
463 | */ |
464 | public function testMailerConfirmWrongParams() { | |
465 | $params = array( | |
466 | 'contact_id' => 'Wrong ID', | |
467 | 'subscribe_id' => 'Wrong ID', | |
468 | 'hash' => 'Wrong Hash', | |
469 | 'event_subscribe_id' => '123', | |
470 | 'time_stamp' => '20111111010101', | |
ef170fe0 | 471 | ); |
ad4f6a9c | 472 | $this->callAPIFailure('mailing_event', 'confirm', $params, |
e37b4b04 | 473 | 'Confirmation failed' |
474 | ); | |
6a488035 TO |
475 | } |
476 | ||
477 | //---------- civicrm_mailing_event_reply methods ----------- | |
478 | ||
479 | /** | |
480 | * Test civicrm_mailing_event_reply with wrong params. | |
e37b4b04 | 481 | * |
482 | * Note that tests like this are slightly better than no test but an | |
483 | * api function cannot be considered supported / 'part of the api' without a | |
484 | * success test | |
6a488035 TO |
485 | */ |
486 | public function testMailerReplyWrongParams() { | |
487 | $params = array( | |
488 | 'job_id' => 'Wrong ID', | |
489 | 'event_queue_id' => 'Wrong ID', | |
490 | 'hash' => 'Wrong Hash', | |
491 | 'bodyTxt' => 'Body...', | |
492 | 'replyTo' => $this->_email, | |
493 | 'time_stamp' => '20111111010101', | |
ef170fe0 | 494 | ); |
ad4f6a9c | 495 | $this->callAPIFailure('mailing_event', 'reply', $params, |
e37b4b04 | 496 | 'Queue event could not be found' |
30d44db2 | 497 | ); |
6a488035 TO |
498 | } |
499 | ||
500 | ||
501 | //----------- civicrm_mailing_event_forward methods ---------- | |
502 | ||
503 | /** | |
504 | * Test civicrm_mailing_event_forward with wrong params. | |
e37b4b04 | 505 | * Note that tests like this are slightly better than no test but an |
506 | * api function cannot be considered supported / 'part of the api' without a | |
507 | * success test | |
6a488035 TO |
508 | */ |
509 | public function testMailerForwardWrongParams() { | |
510 | $params = array( | |
511 | 'job_id' => 'Wrong ID', | |
512 | 'event_queue_id' => 'Wrong ID', | |
513 | 'hash' => 'Wrong Hash', | |
514 | 'email' => $this->_email, | |
515 | 'time_stamp' => '20111111010101', | |
ef170fe0 | 516 | ); |
ad4f6a9c | 517 | $this->callAPIFailure('mailing_event', 'forward', $params, |
e37b4b04 | 518 | 'Queue event could not be found' |
519 | ); | |
6a488035 TO |
520 | } |
521 | ||
6818346a TO |
522 | /** |
523 | * @return array|int | |
524 | */ | |
525 | public function createDraftMailing() { | |
526 | $createParams = $this->_params; | |
527 | $createParams['api.mailing_job.create'] = 0; // note: exact match to API default | |
528 | $createResult = $this->callAPISuccess('mailing', 'create', $createParams, __FUNCTION__, __FILE__); | |
529 | $this->assertTrue(is_numeric($createResult['id'])); | |
530 | $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_mailing_job WHERE mailing_id = %1', array( | |
21dfd5f5 | 531 | 1 => array($createResult['id'], 'Integer'), |
6818346a TO |
532 | )); |
533 | return $createResult['id']; | |
534 | } | |
535 | ||
6c6e6187 | 536 | //----------- civicrm_mailing_create ---------- |
6a488035 TO |
537 | |
538 | } |