3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * File for the CiviCRM APIv3 job functions
15 * @package CiviCRM_APIv3
18 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * @version $Id: Job.php 30879 2010-11-22 15:45:55Z shot $
24 * Class api_v3_JobTest
28 class api_v3_JobProcessMailingTest
extends CiviUnitTestCase
{
29 protected $_apiversion = 3;
31 public $DBResetRequired = FALSE;
32 public $_entity = 'Job';
37 protected $defaultSettings;
44 public function setUp() {
45 $this->cleanupMailingTest();
48 CRM_Mailing_BAO_MailingJob
::$mailsProcessed = 0;
49 $this->_groupID
= $this->groupCreate();
50 $this->_email
= 'test@test.test';
52 'subject' => 'Accidents in cars cause children',
53 'body_text' => 'BEWARE children need regular infusions of toys. Santa knows your {domain.address}. There is no {action.optOutUrl}.',
54 'name' => 'mailing name',
56 'groups' => ['include' => [$this->_groupID
]],
57 'scheduled_date' => 'now',
59 $this->defaultSettings
= [
60 // int, #mailings to send
62 // int, #contacts to receive mailing
64 // int, #concurrent cron jobs
66 // int, #times to spawn all the workers
68 // int, #extra seconds each cron job should hold lock
70 // int, max# recipients to send in a given cron run
71 'mailerBatchLimit' => 0,
72 // int, max# concurrent jobs
74 // int, max# recipients in each job
76 // int, microseconds separating messages
77 'mailThrottleTime' => 0,
79 $this->_mut
= new CiviMailUtils($this, TRUE);
80 $this->callAPISuccess('mail_settings', 'get', ['api.mail_settings.create' => ['domain' => 'chaos.org']]);
85 public function tearDown() {
86 //$this->_mut->clearMessages();
88 CRM_Utils_Hook
::singleton()->reset();
90 CRM_Mailing_BAO_MailingJob
::$mailsProcessed = 0;
91 //$this->cleanupMailingTest();
95 public function testBasic() {
96 $this->createContactsInGroup(10, $this->_groupID
);
97 Civi
::settings()->add([
98 'mailerBatchLimit' => 2,
100 $this->callAPISuccess('mailing', 'create', $this->_params
);
101 $this->_mut
->assertRecipients([]);
102 $this->callAPISuccess('job', 'process_mailing', []);
103 $this->_mut
->assertRecipients($this->getRecipients(1, 2));
107 * Test what happens when a contact is set to decesaed
109 public function testDecesasedRecepient() {
110 $contactID = $this->individualCreate(['first_name' => 'test dead recipeint', 'email' => 'mailtestdead@civicrm.org']);
111 $this->callAPISuccess('group_contact', 'create', [
112 'contact_id' => $contactID,
113 'group_id' => $this->_groupID
,
116 $this->createContactsInGroup(2, $this->_groupID
);
117 Civi
::settings()->add([
118 'mailerBatchLimit' => 2,
120 $mailing = $this->callAPISuccess('mailing', 'create', $this->_params
);
121 $this->assertEquals(3, $this->callAPISuccess('MailingRecipients', 'get', ['mailing_id' => $mailing['id']])['count']);
122 $this->_mut
->assertRecipients([]);
123 $this->callAPISuccess('Contact', 'create', ['id' => $contactID, 'is_deceased' => 1, 'contact_type' => 'Individual']);
124 $this->callAPISuccess('job', 'process_mailing', []);
125 // Check that the deceased contact is not found in the mailing.
126 $this->_mut
->assertRecipients($this->getRecipients(1, 2));
131 * Test pause and resume on Mailing.
133 public function testPauseAndResumeMailing() {
134 $this->createContactsInGroup(10, $this->_groupID
);
135 Civi
::settings()->add([
136 'mailerBatchLimit' => 2,
138 $this->_mut
->clearMessages();
139 //Create a test mailing and check if the status is set to Scheduled.
140 $result = $this->callAPISuccess('mailing', 'create', $this->_params
);
141 $jobs = $this->callAPISuccess('mailing_job', 'get', ['mailing_id' => $result['id']]);
142 $this->assertEquals('Scheduled', $jobs['values'][$jobs['id']]['status']);
145 CRM_Mailing_BAO_MailingJob
::pause($result['id']);
146 $jobs = $this->callAPISuccess('mailing_job', 'get', ['mailing_id' => $result['id']]);
147 $this->assertEquals('Paused', $jobs['values'][$jobs['id']]['status']);
149 //Verify if Paused mailing isn't considered in process_mailing job.
150 $this->callAPISuccess('job', 'process_mailing', []);
151 //Check if mail log is empty.
152 $this->_mut
->assertMailLogEmpty();
153 $jobs = $this->callAPISuccess('mailing_job', 'get', ['mailing_id' => $result['id']]);
154 $this->assertEquals('Paused', $jobs['values'][$jobs['id']]['status']);
156 //Resume should set the status back to Scheduled.
157 CRM_Mailing_BAO_MailingJob
::resume($result['id']);
158 $jobs = $this->callAPISuccess('mailing_job', 'get', ['mailing_id' => $result['id']]);
159 $this->assertEquals('Scheduled', $jobs['values'][$jobs['id']]['status']);
161 //Execute the job and it should send the mailing to the recipients now.
162 $this->callAPISuccess('job', 'process_mailing', []);
163 $this->_mut
->assertRecipients($this->getRecipients(1, 2));
164 // Ensure that loading the report produces no errors.
165 $report = CRM_Mailing_BAO_Mailing
::report($result['id']);
166 // dev/mailing#56 dev/mailing#57 Ensure that for completed mailings the jobs array is not empty.
167 $this->assertTrue(!empty($report['jobs']));
168 // Ensure that mailing name is correctly stored in the report.
169 $this->assertEquals('mailing name', $report['mailing']['name']);
173 * Test mail when in non-production environment.
176 public function testMailNonProductionRun() {
177 // Test in non-production mode.
179 'environment' => 'Staging',
181 $this->callAPISuccess('Setting', 'create', $params);
182 //Assert if outbound mail is disabled.
183 $mailingBackend = Civi
::settings()->get('mailing_backend');
184 $this->assertEquals($mailingBackend['outBound_option'], CRM_Mailing_Config
::OUTBOUND_OPTION_DISABLED
);
186 $this->createContactsInGroup(10, $this->_groupID
);
187 Civi
::settings()->add([
188 'mailerBatchLimit' => 2,
190 $this->callAPISuccess('mailing', 'create', $this->_params
);
191 $this->_mut
->assertRecipients([]);
192 $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.");
194 // Test with runInNonProductionEnvironment param.
195 $this->callAPISuccess('job', 'process_mailing', ['runInNonProductionEnvironment' => TRUE]);
196 $this->_mut
->assertRecipients($this->getRecipients(1, 2));
198 $jobId = $this->callAPISuccessGetValue('Job', [
200 'api_action' => "group_rebuild",
202 $this->callAPISuccess('Job', 'create', [
204 'parameters' => "runInNonProductionEnvironment=TRUE",
206 $jobManager = new CRM_Core_JobManager();
207 $jobManager->executeJobById($jobId);
209 //Assert if outbound mail is still disabled.
210 $mailingBackend = Civi
::settings()->get('mailing_backend');
211 $this->assertEquals($mailingBackend['outBound_option'], CRM_Mailing_Config
::OUTBOUND_OPTION_DISABLED
);
213 // Test in production mode.
215 'environment' => 'Production',
217 $this->callAPISuccess('Setting', 'create', $params);
218 $this->callAPISuccess('job', 'process_mailing', []);
219 $this->_mut
->assertRecipients($this->getRecipients(1, 2));
222 public function concurrencyExamples() {
225 // Launch 3 workers, but mailerJobsMax limits us to 1 worker.
230 // FIXME: lockHold is unrealistic/unrepresentative. In reality, this situation fails because
231 // the data.* locks trample the worker.* locks. However, setting lockHold allows us to
232 // approximate the behavior of what would happen *if* the lock-implementation didn't suffer
233 // trampling effects.
235 'mailerBatchLimit' => 4,
236 'mailerJobsMax' => 1,
239 // 2 jobs which produce 0 messages
241 // 1 job which produces 4 messages
247 // Launch 3 workers, but mailerJobsMax limits us to 2 workers.
253 // FIXME: lockHold is unrealistic/unrepresentative. In reality, this situation fails because
254 // the data.* locks trample the worker.* locks. However, setting lockHold allows us to
255 // approximate the behavior of what would happen *if* the lock-implementation didn't suffer
256 // trampling effects.
258 'mailerBatchLimit' => 5,
259 'mailerJobsMax' => 2,
263 // 1 job which produce 0 messages
265 // 2 jobs which produce 5 messages
272 // Launch 3 workers and saturate them (mailerJobsMax=3)
278 'mailerBatchLimit' => 6,
279 'mailerJobsMax' => 3,
283 // 3 jobs which produce 6 messages
290 // Launch 4 workers and saturate them (mailerJobsMax=0)
296 'mailerBatchLimit' => 6,
297 'mailerJobsMax' => 0,
301 // 3 jobs which produce 6 messages
303 // 1 job which produces 2 messages
310 // Launch 1 worker, 3 times in a row. Deliver everything.
317 'mailerBatchLimit' => 7,
321 // 1 job which produces 7 messages
323 // 1 job which produces 3 messages
325 // 1 job which produces 0 messages
332 // Launch 2 worker, 3 times in a row. Deliver everything.
339 'mailerBatchLimit' => 3,
343 // 3 jobs which produce 3 messages
345 // 1 job which produces 1 messages
347 // 2 jobs which produce 0 messages
354 // For two mailings, launch 1 worker, 5 times in a row. Deliver everything.
362 'mailerBatchLimit' => 6,
366 // x6 => x4+x2 => x6 => x2 => x0
367 // 3 jobs which produce 6 messages
369 // 1 job which produces 2 messages
371 // 1 job which produces 0 messages
382 * Setup various mail configuration options (eg $mailerBatchLimit,
383 * $mailerJobMax) and spawn multiple worker threads ($workers).
384 * Allow the threads to complete. (Optionally, repeat the above
385 * process.) Finally, check to see if the right number of
386 * jobs delivered the right number of messages.
388 * @param array $settings
389 * An array of settings (eg mailerBatchLimit, workers). See comments
390 * for $this->defaultSettings.
391 * @param array $expectedTallies
392 * A listing of the number cron-runs keyed by their size.
393 * For example, array(10=>2) means that there 2 cron-runs
394 * which delivered 10 messages each.
395 * @param int $expectedTotal
396 * The total number of contacts for whom messages should have
398 * @dataProvider concurrencyExamples
400 public function testConcurrency($settings, $expectedTallies, $expectedTotal) {
401 $settings = array_merge($this->defaultSettings
, $settings);
403 $this->createContactsInGroup($settings['recipients'], $this->_groupID
);
404 Civi
::settings()->add(CRM_Utils_Array
::subset($settings, [
410 for ($i = 0; $i < $settings['mailings']; $i++
) {
411 $this->callAPISuccess('mailing', 'create', $this->_params
);
414 $this->_mut
->assertRecipients([]);
417 for ($iterationId = 0; $iterationId < $settings['iterations']; $iterationId++
) {
418 $apiCalls = $this->createExternalAPI();
419 $apiCalls->addEnv(['CIVICRM_CRON_HOLD' => $settings['lockHold']]);
420 for ($workerId = 0; $workerId < $settings['workers']; $workerId++
) {
421 $apiCalls->addCall('job', 'process_mailing', []);
424 $this->assertEquals($settings['workers'], $apiCalls->getRunningCount());
427 $allApiResults = array_merge($allApiResults, $apiCalls->getResults());
430 $actualTallies = $this->tallyApiResults($allApiResults);
431 $this->assertEquals($expectedTallies, $actualTallies, 'API tallies should match.' . print_r([
432 'expectedTallies' => $expectedTallies,
433 'actualTallies' => $actualTallies,
434 'apiResults' => $allApiResults,
436 $this->_mut
->assertRecipients($this->getRecipients(1, $expectedTotal / $settings['mailings'], 'nul.example.com', $settings['mailings']));
437 $this->assertEquals(0, $apiCalls->getRunningCount());
441 * Create contacts in group.
444 * @param int $groupID
445 * @param string $domain
447 public function createContactsInGroup($count, $groupID, $domain = 'nul.example.com') {
448 for ($i = 1; $i <= $count; $i++
) {
449 $contactID = $this->individualCreate(['first_name' => $count, 'email' => 'mail' . $i . '@' . $domain]);
450 $this->callAPISuccess('group_contact', 'create', [
451 'contact_id' => $contactID,
452 'group_id' => $groupID,
459 * Construct the list of email addresses for $count recipients.
463 * @param string $domain
464 * @param int $mailings
468 public function getRecipients($start, $count, $domain = 'nul.example.com', $mailings = 1) {
470 for ($m = 0; $m < $mailings; $m++
) {
471 for ($i = $start; $i < ($start +
$count); $i++
) {
472 $recipients[][0] = 'mail' . $i . '@' . $domain;
478 protected function cleanupMailingTest() {
479 $this->quickCleanup([
481 'civicrm_mailing_job',
482 'civicrm_mailing_spool',
483 'civicrm_mailing_group',
484 'civicrm_mailing_recipients',
485 'civicrm_mailing_event_queue',
486 'civicrm_mailing_event_bounce',
487 'civicrm_mailing_event_delivered',
489 'civicrm_group_contact',
495 * Categorize results based on (a) whether they succeeded
496 * and (b) the number of messages sent.
498 * @param array $apiResults
500 * One key 'error' for all failures.
501 * A separate key for each distinct quantity.
503 protected function tallyApiResults($apiResults) {
505 foreach ($apiResults as $apiResult) {
506 $key = !empty($apiResult['is_error']) ?
'error' : $apiResult['values']['processed'];
507 $ret[$key] = !empty($ret[$key]) ?
1 +
$ret[$key] : 1;