ScheduledReminders - Handle legacy input for limit_to
authorcolemanw <coleman@civicrm.org>
Wed, 19 Jul 2023 23:55:55 +0000 (19:55 -0400)
committercolemanw <coleman@civicrm.org>
Thu, 20 Jul 2023 19:10:18 +0000 (15:10 -0400)
CRM/Core/BAO/ActionSchedule.php
api/v3/utils.php
tests/phpunit/api/v3/ActionScheduleTest.php

index 31c440ddacc7a5b2987ed13ed37c2e95aa0a2f6b..131885510efefbb7db1ee723692a9c0f8c063ee1 100644 (file)
  */
 
 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.
    *
index aaf363636c45c8bc75cc249e7a2017400032e615..6548801c898b44f21cda8df263d625645a729057 100644 (file)
@@ -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;
   }
index 860d9cccbeea1ce3246df9973f6e93351629c49e..9473b17878c1846f53a36e1749675e3fdf23cbe8 100644 (file)
@@ -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']);
   }
 
 }