Merge pull request #14891 from eileenmcnaughton/saved_search
[civicrm-core.git] / tests / phpunit / api / v3 / JobProcessMailingTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * File for the CiviCRM APIv3 job functions
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_Job
33 *
34 * @copyright CiviCRM LLC (c) 2004-2019
35 * @version $Id: Job.php 30879 2010-11-22 15:45:55Z shot $
36 *
37 */
38
39 /**
40 * Class api_v3_JobTest
41 * @group headless
42 * @group civimail
43 */
44 class api_v3_JobProcessMailingTest extends CiviUnitTestCase {
45 protected $_apiversion = 3;
46
47 public $DBResetRequired = FALSE;
48 public $_entity = 'Job';
49 public $_params = [];
50 private $_groupID;
51 private $_email;
52
53 protected $defaultSettings;
54
55 /**
56 * @var CiviMailUtils
57 */
58 private $_mut;
59
60 public function setUp() {
61 $this->cleanupMailingTest();
62 parent::setUp();
63 // DGW
64 CRM_Mailing_BAO_MailingJob::$mailsProcessed = 0;
65 $this->_groupID = $this->groupCreate();
66 $this->_email = 'test@test.test';
67 $this->_params = [
68 'subject' => 'Accidents in cars cause children',
69 'body_text' => 'BEWARE children need regular infusions of toys. Santa knows your {domain.address}. There is no {action.optOutUrl}.',
70 'name' => 'mailing name',
71 'created_id' => 1,
72 'groups' => ['include' => [$this->_groupID]],
73 'scheduled_date' => 'now',
74 ];
75 $this->defaultSettings = [
76 // int, #mailings to send
77 'mailings' => 1,
78 // int, #contacts to receive mailing
79 'recipients' => 20,
80 // int, #concurrent cron jobs
81 'workers' => 1,
82 // int, #times to spawn all the workers
83 'iterations' => 1,
84 // int, #extra seconds each cron job should hold lock
85 'lockHold' => 0,
86 // int, max# recipients to send in a given cron run
87 'mailerBatchLimit' => 0,
88 // int, max# concurrent jobs
89 'mailerJobsMax' => 0,
90 // int, max# recipients in each job
91 'mailerJobSize' => 0,
92 // int, microseconds separating messages
93 'mailThrottleTime' => 0,
94 ];
95 $this->_mut = new CiviMailUtils($this, TRUE);
96 $this->callAPISuccess('mail_settings', 'get', ['api.mail_settings.create' => ['domain' => 'chaos.org']]);
97 }
98
99 /**
100 */
101 public function tearDown() {
102 //$this->_mut->clearMessages();
103 $this->_mut->stop();
104 CRM_Utils_Hook::singleton()->reset();
105 // DGW
106 CRM_Mailing_BAO_MailingJob::$mailsProcessed = 0;
107 //$this->cleanupMailingTest();
108 parent::tearDown();
109 }
110
111 public function testBasic() {
112 $this->createContactsInGroup(10, $this->_groupID);
113 Civi::settings()->add([
114 'mailerBatchLimit' => 2,
115 ]);
116 $this->callAPISuccess('mailing', 'create', $this->_params);
117 $this->_mut->assertRecipients([]);
118 $this->callAPISuccess('job', 'process_mailing', []);
119 $this->_mut->assertRecipients($this->getRecipients(1, 2));
120 }
121
122 /**
123 * Test what happens when a contact is set to decesaed
124 */
125 public function testDecesasedRecepient() {
126 $contactID = $this->individualCreate(['first_name' => 'test dead recipeint', 'email' => 'mailtestdead@civicrm.org']);
127 $this->callAPISuccess('group_contact', 'create', [
128 'contact_id' => $contactID,
129 'group_id' => $this->_groupID,
130 'status' => 'Added',
131 ]);
132 $this->createContactsInGroup(2, $this->_groupID);
133 Civi::settings()->add([
134 'mailerBatchLimit' => 2,
135 ]);
136 $mailing = $this->callAPISuccess('mailing', 'create', $this->_params);
137 $this->assertEquals(3, $this->callAPISuccess('MailingRecipients', 'get', ['mailing_id' => $mailing['id']])['count']);
138 $this->_mut->assertRecipients([]);
139 $this->callAPISuccess('Contact', 'create', ['id' => $contactID, 'is_deceased' => 1, 'contact_type' => 'Individual']);
140 $this->callAPISuccess('job', 'process_mailing', []);
141 // Check that the deceased contact is not found in the mailing.
142 $this->_mut->assertRecipients($this->getRecipients(1, 2));
143
144 }
145
146 /**
147 * Test pause and resume on Mailing.
148 */
149 public function testPauseAndResumeMailing() {
150 $this->createContactsInGroup(10, $this->_groupID);
151 Civi::settings()->add([
152 'mailerBatchLimit' => 2,
153 ]);
154 $this->_mut->clearMessages();
155 //Create a test mailing and check if the status is set to Scheduled.
156 $result = $this->callAPISuccess('mailing', 'create', $this->_params);
157 $jobs = $this->callAPISuccess('mailing_job', 'get', ['mailing_id' => $result['id']]);
158 $this->assertEquals('Scheduled', $jobs['values'][$jobs['id']]['status']);
159
160 //Pause the mailing.
161 CRM_Mailing_BAO_MailingJob::pause($result['id']);
162 $jobs = $this->callAPISuccess('mailing_job', 'get', ['mailing_id' => $result['id']]);
163 $this->assertEquals('Paused', $jobs['values'][$jobs['id']]['status']);
164
165 //Verify if Paused mailing isn't considered in process_mailing job.
166 $this->callAPISuccess('job', 'process_mailing', []);
167 //Check if mail log is empty.
168 $this->_mut->assertMailLogEmpty();
169 $jobs = $this->callAPISuccess('mailing_job', 'get', ['mailing_id' => $result['id']]);
170 $this->assertEquals('Paused', $jobs['values'][$jobs['id']]['status']);
171
172 //Resume should set the status back to Scheduled.
173 CRM_Mailing_BAO_MailingJob::resume($result['id']);
174 $jobs = $this->callAPISuccess('mailing_job', 'get', ['mailing_id' => $result['id']]);
175 $this->assertEquals('Scheduled', $jobs['values'][$jobs['id']]['status']);
176
177 //Execute the job and it should send the mailing to the recipients now.
178 $this->callAPISuccess('job', 'process_mailing', []);
179 $this->_mut->assertRecipients($this->getRecipients(1, 2));
180 }
181
182 /**
183 * Test mail when in non-production environment.
184 *
185 */
186 public function testMailNonProductionRun() {
187 // Test in non-production mode.
188 $params = [
189 'environment' => 'Staging',
190 ];
191 $this->callAPISuccess('Setting', 'create', $params);
192 //Assert if outbound mail is disabled.
193 $mailingBackend = Civi::settings()->get('mailing_backend');
194 $this->assertEquals($mailingBackend['outBound_option'], CRM_Mailing_Config::OUTBOUND_OPTION_DISABLED);
195
196 $this->createContactsInGroup(10, $this->_groupID);
197 Civi::settings()->add([
198 'mailerBatchLimit' => 2,
199 ]);
200 $this->callAPISuccess('mailing', 'create', $this->_params);
201 $this->_mut->assertRecipients([]);
202 $this->callAPIFailure('job', 'process_mailing', "Failure in api call for job process_mailing: Job has not been executed as it is a non-production environment.");
203
204 // Test with runInNonProductionEnvironment param.
205 $this->callAPISuccess('job', 'process_mailing', ['runInNonProductionEnvironment' => TRUE]);
206 $this->_mut->assertRecipients($this->getRecipients(1, 2));
207
208 $jobId = $this->callAPISuccessGetValue('Job', [
209 'return' => "id",
210 'api_action' => "group_rebuild",
211 ]);
212 $this->callAPISuccess('Job', 'create', [
213 'id' => $jobId,
214 'parameters' => "runInNonProductionEnvironment=TRUE",
215 ]);
216 $jobManager = new CRM_Core_JobManager();
217 $jobManager->executeJobById($jobId);
218
219 //Assert if outbound mail is still disabled.
220 $mailingBackend = Civi::settings()->get('mailing_backend');
221 $this->assertEquals($mailingBackend['outBound_option'], CRM_Mailing_Config::OUTBOUND_OPTION_DISABLED);
222
223 // Test in production mode.
224 $params = [
225 'environment' => 'Production',
226 ];
227 $this->callAPISuccess('Setting', 'create', $params);
228 $this->callAPISuccess('job', 'process_mailing', []);
229 $this->_mut->assertRecipients($this->getRecipients(1, 2));
230 }
231
232 public function concurrencyExamples() {
233 $es = [];
234
235 // Launch 3 workers, but mailerJobsMax limits us to 1 worker.
236 $es[0] = [
237 [
238 'recipients' => 20,
239 'workers' => 3,
240 // FIXME: lockHold is unrealistic/unrepresentative. In reality, this situation fails because
241 // the data.* locks trample the worker.* locks. However, setting lockHold allows us to
242 // approximate the behavior of what would happen *if* the lock-implementation didn't suffer
243 // trampling effects.
244 'lockHold' => 10,
245 'mailerBatchLimit' => 4,
246 'mailerJobsMax' => 1,
247 ],
248 [
249 // 2 jobs which produce 0 messages
250 0 => 2,
251 // 1 job which produces 4 messages
252 4 => 1,
253 ],
254 4,
255 ];
256
257 // Launch 3 workers, but mailerJobsMax limits us to 2 workers.
258 $es[1] = [
259 // Settings.
260 [
261 'recipients' => 20,
262 'workers' => 3,
263 // FIXME: lockHold is unrealistic/unrepresentative. In reality, this situation fails because
264 // the data.* locks trample the worker.* locks. However, setting lockHold allows us to
265 // approximate the behavior of what would happen *if* the lock-implementation didn't suffer
266 // trampling effects.
267 'lockHold' => 10,
268 'mailerBatchLimit' => 5,
269 'mailerJobsMax' => 2,
270 ],
271 // Tallies.
272 [
273 // 1 job which produce 0 messages
274 0 => 1,
275 // 2 jobs which produce 5 messages
276 5 => 2,
277 ],
278 // Total sent.
279 10,
280 ];
281
282 // Launch 3 workers and saturate them (mailerJobsMax=3)
283 $es[2] = [
284 // Settings.
285 [
286 'recipients' => 20,
287 'workers' => 3,
288 'mailerBatchLimit' => 6,
289 'mailerJobsMax' => 3,
290 ],
291 // Tallies.
292 [
293 // 3 jobs which produce 6 messages
294 6 => 3,
295 ],
296 // Total sent.
297 18,
298 ];
299
300 // Launch 4 workers and saturate them (mailerJobsMax=0)
301 $es[3] = [
302 // Settings.
303 [
304 'recipients' => 20,
305 'workers' => 4,
306 'mailerBatchLimit' => 6,
307 'mailerJobsMax' => 0,
308 ],
309 // Tallies.
310 [
311 // 3 jobs which produce 6 messages
312 6 => 3,
313 // 1 job which produces 2 messages
314 2 => 1,
315 ],
316 // Total sent.
317 20,
318 ];
319
320 // Launch 1 worker, 3 times in a row. Deliver everything.
321 $es[4] = [
322 // Settings.
323 [
324 'recipients' => 10,
325 'workers' => 1,
326 'iterations' => 3,
327 'mailerBatchLimit' => 7,
328 ],
329 // Tallies.
330 [
331 // 1 job which produces 7 messages
332 7 => 1,
333 // 1 job which produces 3 messages
334 3 => 1,
335 // 1 job which produces 0 messages
336 0 => 1,
337 ],
338 // Total sent.
339 10,
340 ];
341
342 // Launch 2 worker, 3 times in a row. Deliver everything.
343 $es[5] = [
344 // Settings.
345 [
346 'recipients' => 10,
347 'workers' => 2,
348 'iterations' => 3,
349 'mailerBatchLimit' => 3,
350 ],
351 // Tallies.
352 [
353 // 3 jobs which produce 3 messages
354 3 => 3,
355 // 1 job which produces 1 messages
356 1 => 1,
357 // 2 jobs which produce 0 messages
358 0 => 2,
359 ],
360 // Total sent.
361 10,
362 ];
363
364 // For two mailings, launch 1 worker, 5 times in a row. Deliver everything.
365 $es[6] = [
366 // Settings.
367 [
368 'mailings' => 2,
369 'recipients' => 10,
370 'workers' => 1,
371 'iterations' => 5,
372 'mailerBatchLimit' => 6,
373 ],
374 // Tallies.
375 [
376 // x6 => x4+x2 => x6 => x2 => x0
377 // 3 jobs which produce 6 messages
378 6 => 3,
379 // 1 job which produces 2 messages
380 2 => 1,
381 // 1 job which produces 0 messages
382 0 => 1,
383 ],
384 // Total sent.
385 20,
386 ];
387
388 return $es;
389 }
390
391 /**
392 * Setup various mail configuration options (eg $mailerBatchLimit,
393 * $mailerJobMax) and spawn multiple worker threads ($workers).
394 * Allow the threads to complete. (Optionally, repeat the above
395 * process.) Finally, check to see if the right number of
396 * jobs delivered the right number of messages.
397 *
398 * @param array $settings
399 * An array of settings (eg mailerBatchLimit, workers). See comments
400 * for $this->defaultSettings.
401 * @param array $expectedTallies
402 * A listing of the number cron-runs keyed by their size.
403 * For example, array(10=>2) means that there 2 cron-runs
404 * which delivered 10 messages each.
405 * @param int $expectedTotal
406 * The total number of contacts for whom messages should have
407 * been sent.
408 * @dataProvider concurrencyExamples
409 */
410 public function testConcurrency($settings, $expectedTallies, $expectedTotal) {
411 $settings = array_merge($this->defaultSettings, $settings);
412
413 $this->createContactsInGroup($settings['recipients'], $this->_groupID);
414 Civi::settings()->add(CRM_Utils_Array::subset($settings, [
415 'mailerBatchLimit',
416 'mailerJobsMax',
417 'mailThrottleTime',
418 ]));
419
420 for ($i = 0; $i < $settings['mailings']; $i++) {
421 $this->callAPISuccess('mailing', 'create', $this->_params);
422 }
423
424 $this->_mut->assertRecipients([]);
425
426 $allApiResults = [];
427 for ($iterationId = 0; $iterationId < $settings['iterations']; $iterationId++) {
428 $apiCalls = $this->createExternalAPI();
429 $apiCalls->addEnv(['CIVICRM_CRON_HOLD' => $settings['lockHold']]);
430 for ($workerId = 0; $workerId < $settings['workers']; $workerId++) {
431 $apiCalls->addCall('job', 'process_mailing', []);
432 }
433 $apiCalls->start();
434 $this->assertEquals($settings['workers'], $apiCalls->getRunningCount());
435
436 $apiCalls->wait();
437 $allApiResults = array_merge($allApiResults, $apiCalls->getResults());
438 }
439
440 $actualTallies = $this->tallyApiResults($allApiResults);
441 $this->assertEquals($expectedTallies, $actualTallies, 'API tallies should match.' . print_r([
442 'expectedTallies' => $expectedTallies,
443 'actualTallies' => $actualTallies,
444 'apiResults' => $allApiResults,
445 ], TRUE));
446 $this->_mut->assertRecipients($this->getRecipients(1, $expectedTotal / $settings['mailings'], 'nul.example.com', $settings['mailings']));
447 $this->assertEquals(0, $apiCalls->getRunningCount());
448 }
449
450 /**
451 * Create contacts in group.
452 *
453 * @param int $count
454 * @param int $groupID
455 * @param string $domain
456 */
457 public function createContactsInGroup($count, $groupID, $domain = 'nul.example.com') {
458 for ($i = 1; $i <= $count; $i++) {
459 $contactID = $this->individualCreate(['first_name' => $count, 'email' => 'mail' . $i . '@' . $domain]);
460 $this->callAPISuccess('group_contact', 'create', [
461 'contact_id' => $contactID,
462 'group_id' => $groupID,
463 'status' => 'Added',
464 ]);
465 }
466 }
467
468 /**
469 * Construct the list of email addresses for $count recipients.
470 *
471 * @param int $start
472 * @param int $count
473 * @param string $domain
474 * @param int $mailings
475 *
476 * @return array
477 */
478 public function getRecipients($start, $count, $domain = 'nul.example.com', $mailings = 1) {
479 $recipients = [];
480 for ($m = 0; $m < $mailings; $m++) {
481 for ($i = $start; $i < ($start + $count); $i++) {
482 $recipients[][0] = 'mail' . $i . '@' . $domain;
483 }
484 }
485 return $recipients;
486 }
487
488 protected function cleanupMailingTest() {
489 $this->quickCleanup([
490 'civicrm_mailing',
491 'civicrm_mailing_job',
492 'civicrm_mailing_spool',
493 'civicrm_mailing_group',
494 'civicrm_mailing_recipients',
495 'civicrm_mailing_event_queue',
496 'civicrm_mailing_event_bounce',
497 'civicrm_mailing_event_delivered',
498 'civicrm_group',
499 'civicrm_group_contact',
500 'civicrm_contact',
501 ]);
502 }
503
504 /**
505 * Categorize results based on (a) whether they succeeded
506 * and (b) the number of messages sent.
507 *
508 * @param array $apiResults
509 * @return array
510 * One key 'error' for all failures.
511 * A separate key for each distinct quantity.
512 */
513 protected function tallyApiResults($apiResults) {
514 $ret = [];
515 foreach ($apiResults as $apiResult) {
516 $key = !empty($apiResult['is_error']) ? 'error' : $apiResult['values']['processed'];
517 $ret[$key] = !empty($ret[$key]) ? 1 + $ret[$key] : 1;
518 }
519 return $ret;
520 }
521
522 }