add 'minute' as recurring unit
authorJon Goldberg <jon@megaphonetech.com>
Wed, 20 Oct 2021 17:13:03 +0000 (13:13 -0400)
committerJon Goldberg <jon@megaphonetech.com>
Wed, 20 Oct 2021 17:18:09 +0000 (13:18 -0400)
CRM/Admin/Form/ScheduleReminders.php
CRM/Core/SelectValues.php
tests/phpunit/CRM/Core/BAO/ActionScheduleTest.php
tests/phpunit/CRM/Core/BAO/RecurringEntityTest.php

index 40ae4aa356483dc8a56dde39947a64e6e3e351e4..8a4e7385788d506cac1e3e4276c88a6634ce2907 100644 (file)
@@ -390,6 +390,7 @@ class CRM_Admin_Form_ScheduleReminders extends CRM_Admin_Form {
       $defaults['is_active'] = 1;
       $defaults['mode'] = 'Email';
       $defaults['record_activity'] = 1;
+      $defaults['start_action_unit'] = 'hour';
     }
     else {
       $defaults = $this->_values;
index 2830c67782be2e23f4c27cf813afd08fe9b5bff5..9158b6f44be414d11fe002249d418de4e93f27a6 100644 (file)
@@ -900,6 +900,7 @@ class CRM_Core_SelectValues {
     // is for recurring payments and probably not good to re-use for recurring entities.
     // If something other than a hard-coded list is desired, add a new option_group.
     return [
+      'minute' => ts('minute', ['plural' => 'minutes', 'count' => $count]),
       'hour' => ts('hour', ['plural' => 'hours', 'count' => $count]),
       'day' => ts('day', ['plural' => 'days', 'count' => $count]),
       'week' => ts('week', ['plural' => 'weeks', 'count' => $count]),
index 07244a71c76d5f5b546c6287a79fc5448bd58b61..7d9b2a4d898c951b61885753e79a2d2aee8660bb 100644 (file)
@@ -169,6 +169,36 @@ class CRM_Core_BAO_ActionScheduleTest extends CiviUnitTestCase {
       'effective_start_date' => '2012-06-14 00:00:00',
       'effective_end_date' => '2012-06-15 00:00:00',
     ];
+    $this->fixtures['sched_activity_5minute'] = [
+      'name' => 'Five_Minute_Phone_Call_Notice',
+      'title' => 'Five Minute Phone Call Notice',
+      'limit_to' => '1',
+      'absolute_date' => NULL,
+      'body_html' => '<p>5 minutes (for {activity.subject})</p>',
+      'body_text' => '5 minutes (non-repeating) (for {activity.subject})',
+      'end_action' => NULL,
+      'end_date' => NULL,
+      'end_frequency_interval' => NULL,
+      'end_frequency_unit' => NULL,
+      'entity_status' => '1',
+      'entity_value' => '2',
+      'group_id' => NULL,
+      'is_active' => '1',
+      'is_repeat' => '0',
+      'mapping_id' => '1',
+      'msg_template_id' => NULL,
+      'recipient' => '2',
+      'recipient_listing' => NULL,
+      'recipient_manual' => NULL,
+      'record_activity' => 1,
+      'repetition_frequency_interval' => NULL,
+      'repetition_frequency_unit' => NULL,
+      'start_action_condition' => 'before',
+      'start_action_date' => 'activity_date_time',
+      'start_action_offset' => '5',
+      'start_action_unit' => 'minute',
+      'subject' => '5 minutes (about {activity.activity_type})',
+    ];
     $this->fixtures['sched_activity_1day_r'] = [
       'name' => 'One_Day_Phone_Call_Notice_R',
       'title' => 'One Day Phone Call Notice R',
@@ -1142,6 +1172,46 @@ class CRM_Core_BAO_ActionScheduleTest extends CiviUnitTestCase {
     }
   }
 
+  /**
+   * Test "minute" as a valid unit.
+   *
+   * @throws \API_Exception
+   * @throws \CRM_Core_Exception
+   */
+  public function testActivityDateTimeMinuteUnit(): void {
+    $this->createScheduleFromFixtures('sched_activity_5minute');
+
+    $activity = $this->createTestObject('CRM_Activity_DAO_Activity', $this->fixtures['phone_call']);
+    $contact = $this->callAPISuccess('contact', 'create', $this->fixtures['contact']);
+    $activity->subject = 'Test subject for phone_call';
+    $activity->save();
+
+    $source['contact_id'] = $contact['id'];
+    $source['activity_id'] = $activity->id;
+    $source['record_type_id'] = 2;
+    $activityContact = $this->createTestObject('CRM_Activity_DAO_ActivityContact', $source);
+    $activityContact->save();
+
+    $this->assertCronRuns([
+      [
+        // 15 minutes before phone call time, no email
+        'time' => '2012-06-15 9:45:00',
+        'recipients' => [],
+        'subjects' => [],
+      ],
+      [
+        // 3 minutes before, an email
+        'time' => '2012-06-15 9:57:00',
+        'recipients' => [['test-member@example.com']],
+      ],
+      [
+        // Run cron again; message already sent
+        'time' => '',
+        'recipients' => [],
+      ],
+    ]);
+  }
+
   /**
    * Test schedule creation on repeatable schedule.
    *
index 6c42982d6bd7d1a1efd8d494fe35921fae27a0f9..effa449831f491d542df12b7aff5b1d527c90806 100644 (file)
@@ -366,4 +366,40 @@ class CRM_Core_BAO_RecurringEntityTest extends CiviUnitTestCase {
     }
   }
 
+  /**
+   * Testing Activity Generation through Entity Recursion with minute units.
+   */
+  public function testRecurringEntityGenerationWithMinuteUnit() {
+    // Create original activity
+    $activityDateTime = '2021-11-11 15:00:00';
+    $activityId = $this->activityCreate([
+      'activity_date_time' => $activityDateTime,
+      'subject' => 'minute unit test',
+    ])['id'];
+
+    // Create recurring activities.
+    $recursion = new CRM_Core_BAO_RecurringEntity();
+    $recursion->entity_id = $activityId;
+    $recursion->entity_table = 'civicrm_activity';
+    $recursion->dateColumns = ['activity_date_time'];
+    $recursion->schedule = [
+      'entity_value' => $activityId,
+      'start_action_date' => $activityDateTime,
+      'repetition_frequency_unit' => 'minute',
+      'repetition_frequency_interval' => 20,
+      'start_action_offset' => 4,
+    ];
+
+    $recursion->generate();
+
+    $activities = \Civi\Api4\Activity::get()
+      ->addSelect('activity_date_time')
+      ->addWhere('subject', '=', 'minute unit test')
+      ->execute()
+      ->column('activity_date_time');
+    $this->assertEquals(5, count($activities));
+    $expectedTimestamps = ['2021-11-11 15:00:00', '2021-11-11 15:20:00', '2021-11-11 15:40:00', '2021-11-11 16:00:00', '2021-11-11 16:20:00'];
+    $this->assertEquals($expectedTimestamps, $activities);
+  }
+
 }