/**
  * Check mailing is sent
  */
 function testProcessMailing()
 {
     $this->createContactsInGroup(10, $this->_groupID);
     CRM_Core_Config::singleton()->mailerBatchLimit = 2;
     $this->callAPISuccess('mailing', 'create', $this->_params);
     $this->callAPISuccess('job', 'process_mailing', array());
     $this->_mut->assertRecipients($this->getRecipients(1, 2));
 }
 /**
  * Setup various mail configuration options (eg $mailerBatchLimit,
  * $mailerJobMax) and spawn multiple worker threads ($workers).
  * Allow the threads to complete. (Optionally, repeat the above
  * process.) Finally, check to see if the right number of
  * jobs delivered the right number of messages.
  *
  * @param array $settings
  *   An array of settings (eg mailerBatchLimit, workers). See comments
  *   for $this->defaultSettings.
  * @param array $expectedTallies
  *    A listing of the number cron-runs keyed by their size.
  *    For example, array(10=>2) means that there 2 cron-runs
  *    which delivered 10 messages each.
  * @param int $expectedTotal
  *    The total number of contacts for whom messages should have
  *    been sent.
  * @dataProvider concurrencyExamples
  */
 public function testConcurrency($settings, $expectedTallies, $expectedTotal)
 {
     $settings = array_merge($this->defaultSettings, $settings);
     $this->createContactsInGroup($settings['recipients'], $this->_groupID);
     Civi::settings()->add(CRM_Utils_Array::subset($settings, array('mailerBatchLimit', 'mailerJobsMax', 'mailThrottleTime')));
     for ($i = 0; $i < $settings['mailings']; $i++) {
         $this->callAPISuccess('mailing', 'create', $this->_params);
     }
     $this->_mut->assertRecipients(array());
     $allApiResults = array();
     for ($iterationId = 0; $iterationId < $settings['iterations']; $iterationId++) {
         $apiCalls = $this->createExternalAPI();
         $apiCalls->addEnv(array('CIVICRM_CRON_HOLD' => $settings['lockHold']));
         for ($workerId = 0; $workerId < $settings['workers']; $workerId++) {
             $apiCalls->addCall('job', 'process_mailing', array());
         }
         $apiCalls->start();
         $this->assertEquals($settings['workers'], $apiCalls->getRunningCount());
         $apiCalls->wait();
         $allApiResults = array_merge($allApiResults, $apiCalls->getResults());
     }
     $actualTallies = $this->tallyApiResults($allApiResults);
     $this->assertEquals($expectedTallies, $actualTallies, 'API tallies should match.' . print_r(array('expectedTallies' => $expectedTallies, 'actualTallies' => $actualTallies, 'apiResults' => $allApiResults), TRUE));
     $this->_mut->assertRecipients($this->getRecipients(1, $expectedTotal / $settings['mailings'], 'nul.example.com', $settings['mailings']));
     $this->assertEquals(0, $apiCalls->getRunningCount());
 }
 /**
  * Run a series of cron jobs and make an assertion about email deliveries.
  *
  * @param array $cronRuns
  *   array specifying when to run cron and what messages to expect; each item is an array with keys:
  *   - time: string, e.g. '2012-06-15 21:00:01'
  *   - recipients: array(array(string)), list of email addresses which should receive messages
  */
 public function assertCronRuns($cronRuns)
 {
     foreach ($cronRuns as $cronRun) {
         CRM_Utils_Time::setTime($cronRun['time']);
         $this->callAPISuccess('job', 'send_reminder', array());
         $this->mut->assertRecipients($cronRun['recipients']);
         if (array_key_exists('subjects', $cronRun)) {
             $this->mut->assertSubjects($cronRun['subjects']);
         }
         $this->mut->clearMessages();
     }
 }