From 4c29a32a275aba24232555a3f18e9563584b3070 Mon Sep 17 00:00:00 2001 From: colemanw Date: Wed, 19 Jul 2023 19:55:55 -0400 Subject: [PATCH] ScheduledReminders - Handle legacy input for limit_to --- CRM/Core/BAO/ActionSchedule.php | 16 +++++++++++++++- api/v3/utils.php | 5 +++++ tests/phpunit/api/v3/ActionScheduleTest.php | 17 +++++++++++++++-- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/CRM/Core/BAO/ActionSchedule.php b/CRM/Core/BAO/ActionSchedule.php index 31c440ddac..131885510e 100644 --- a/CRM/Core/BAO/ActionSchedule.php +++ b/CRM/Core/BAO/ActionSchedule.php @@ -16,11 +16,12 @@ */ use Civi\ActionSchedule\Event\MappingRegisterEvent; +use Civi\Core\HookInterface; /** * This class contains functions for managing Scheduled Reminders */ -class CRM_Core_BAO_ActionSchedule extends CRM_Core_DAO_ActionSchedule { +class CRM_Core_BAO_ActionSchedule extends CRM_Core_DAO_ActionSchedule implements HookInterface { /** * @param array $filters @@ -169,6 +170,19 @@ FROM civicrm_action_schedule cas return self::writeRecord($params); } + /** + * @param \Civi\Core\Event\PreEvent $event + * @implements hook_civicrm_pre + */ + public static function self_hook_civicrm_pre(\Civi\Core\Event\PreEvent $event) { + if (in_array($event->action, ['create', 'edit'])) { + if (isset($event->params['limit_to']) && in_array($event->params['limit_to'], [0, '0', FALSE], TRUE)) { + CRM_Core_Error::deprecatedWarning('Deprecated value "0" is no longer a valid option for ActionSchedule.limit_to; changed to "2".'); + $event->params['limit_to'] = 2; + } + } + } + /** * Retrieve DB object and copy to defaults array. * diff --git a/api/v3/utils.php b/api/v3/utils.php index aaf363636c..6548801c89 100644 --- a/api/v3/utils.php +++ b/api/v3/utils.php @@ -2065,6 +2065,11 @@ function _civicrm_api3_validate_integer(&$params, $fieldName, &$fieldInfo, $enti // https://lab.civicrm.org/dev/rc/-/issues/14 $fieldValue = 1; } + if ($fieldName === 'limit_to' && in_array($fieldValue, [0, '0'], TRUE)) { + // https://github.com/civicrm/civicrm-core/pull/26881 + // FALSE will bypass the below validation and then the BAO will change it to 2 with a deprecation notice + $fieldValue = FALSE; + } if (strpos(($op ?? ''), 'NULL') !== FALSE || strpos(($op ?? ''), 'EMPTY') !== FALSE) { return; } diff --git a/tests/phpunit/api/v3/ActionScheduleTest.php b/tests/phpunit/api/v3/ActionScheduleTest.php index 860d9cccbe..9473b17878 100644 --- a/tests/phpunit/api/v3/ActionScheduleTest.php +++ b/tests/phpunit/api/v3/ActionScheduleTest.php @@ -32,8 +32,7 @@ * @group headless */ class api_v3_ActionScheduleTest extends CiviUnitTestCase { - protected $_params; - protected $_params2; + protected $_entity = 'action_schedule'; /** @@ -127,7 +126,21 @@ class api_v3_ActionScheduleTest extends CiviUnitTestCase { $this->assertEquals($actionSchedule['values'][$actionSchedule['id']]['start_action_offset'], $params['start_action_offset']); $newCount = CRM_Core_DAO::singleValueQuery('select count(*) from civicrm_action_schedule'); $this->assertEquals($oldCount + 1, $newCount); + } + public function testDeprecatedLimitToValue(): void { + $params = [ + 'title' => 'Hello', + 'limit_to' => 0, + 'entity_value' => 'Meeting', + 'entity_status' => 'Scheduled', + 'mapping_id' => CRM_Activity_ActionMapping::ACTIVITY_MAPPING_ID, + 'start_action_date' => 'activity_date_time', + 'body_html' => 'Test description', + 'subject' => 'Test subject', + ]; + $actionSchedule = $this->callAPIFailure('action_schedule', 'create', $params); + $this->assertStringContainsString('ActionSchedule.limit_to', $actionSchedule['error_message']); } } -- 2.25.1