From 33ad8852d36c2fd3a7deb7bc40b09fdc6aa11006 Mon Sep 17 00:00:00 2001 From: Jon Goldberg Date: Wed, 20 Oct 2021 13:13:03 -0400 Subject: [PATCH] add 'minute' as recurring unit --- CRM/Admin/Form/ScheduleReminders.php | 1 + CRM/Core/SelectValues.php | 1 + .../CRM/Core/BAO/ActionScheduleTest.php | 70 +++++++++++++++++++ .../CRM/Core/BAO/RecurringEntityTest.php | 36 ++++++++++ 4 files changed, 108 insertions(+) diff --git a/CRM/Admin/Form/ScheduleReminders.php b/CRM/Admin/Form/ScheduleReminders.php index 40ae4aa356..8a4e738578 100644 --- a/CRM/Admin/Form/ScheduleReminders.php +++ b/CRM/Admin/Form/ScheduleReminders.php @@ -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; diff --git a/CRM/Core/SelectValues.php b/CRM/Core/SelectValues.php index 2830c67782..9158b6f44b 100644 --- a/CRM/Core/SelectValues.php +++ b/CRM/Core/SelectValues.php @@ -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]), diff --git a/tests/phpunit/CRM/Core/BAO/ActionScheduleTest.php b/tests/phpunit/CRM/Core/BAO/ActionScheduleTest.php index 07244a71c7..7d9b2a4d89 100644 --- a/tests/phpunit/CRM/Core/BAO/ActionScheduleTest.php +++ b/tests/phpunit/CRM/Core/BAO/ActionScheduleTest.php @@ -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' => '

5 minutes (for {activity.subject})

', + '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. * diff --git a/tests/phpunit/CRM/Core/BAO/RecurringEntityTest.php b/tests/phpunit/CRM/Core/BAO/RecurringEntityTest.php index 6c42982d6b..effa449831 100644 --- a/tests/phpunit/CRM/Core/BAO/RecurringEntityTest.php +++ b/tests/phpunit/CRM/Core/BAO/RecurringEntityTest.php @@ -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); + } + } -- 2.25.1