test for target contacts on Bulk Email for mailing in batches
authordemeritcowboy <demeritcowboy@hotmail.com>
Thu, 24 Sep 2020 18:23:45 +0000 (14:23 -0400)
committerdemeritcowboy <demeritcowboy@hotmail.com>
Thu, 24 Sep 2020 18:23:45 +0000 (14:23 -0400)
tests/phpunit/api/v3/JobProcessMailingTest.php

index c45c4c55c8265e5ae00ad5335804e087e903ff3c..98e8438d2d2966846c4cf558cef1ef96c968bc4b 100644 (file)
@@ -470,6 +470,49 @@ class api_v3_JobProcessMailingTest extends CiviUnitTestCase {
     $this->assertEquals(0, $apiCalls->getRunningCount());
   }
 
+  /**
+   * Check that the earlier iterations's activity targets on the recorded
+   * BulkEmail activity don't get wiped out by subsequent iterations when
+   * using batches.
+   */
+  public function testBatchActivityTargets() {
+    $loggedInUserId = $this->createLoggedInUser();
+
+    \Civi::settings()->set('mailerBatchLimit', 2);
+
+    // We have such small batches we need to lower this interval to get it
+    // to create the activity.
+    $old_sync_interval = \Civi::settings()->get('civimail_sync_interval');
+    \Civi::settings()->set('civimail_sync_interval', 1);
+
+    $this->createContactsInGroup(6, $this->_groupID);
+    $mailing = $this->callAPISuccess('mailing', 'create', $this->_params + ['scheduled_id' => $loggedInUserId]);
+    $this->callAPISuccess('job', 'process_mailing', []);
+    $bulkEmailActivity = $this->callAPISuccess('Activity', 'getsingle', [
+      'source_record_id' => $mailing['id'],
+      'activity_type_id' => 'Bulk Email',
+      'return' => ['target_contact_id'],
+    ]);
+    // After first batch, should have two targets
+    $this->assertCount(2, $bulkEmailActivity['target_contact_id']);
+
+    // Because we're running in script mode need to reset this static
+    // to get it to process the other batches.
+    CRM_Mailing_BAO_MailingJob::$mailsProcessed = 0;
+
+    $this->callAPISuccess('job', 'process_mailing', []);
+    $bulkEmailActivity = $this->callAPISuccess('Activity', 'getsingle', [
+      'source_record_id' => $mailing['id'],
+      'activity_type_id' => 'Bulk Email',
+      'return' => ['target_contact_id'],
+    ]);
+    // After second batch, should have four targets
+    $this->assertCount(4, $bulkEmailActivity['target_contact_id']);
+
+    // restore setting
+    \Civi::settings()->set('civimail_sync_interval', $old_sync_interval);
+  }
+
   /**
    * Create contacts in group.
    *