From eed051b5d2ec460e1fd57b353801951cd32847c7 Mon Sep 17 00:00:00 2001 From: eileen Date: Fri, 19 Feb 2021 19:18:04 +1300 Subject: [PATCH] APIv3 - Legacy support for campaign_id fields which used to have a pseudoconstant --- api/v3/Generic.php | 19 +++++++++++++++++-- api/v3/utils.php | 2 +- tests/phpunit/api/v3/MailingTest.php | 5 +++-- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/api/v3/Generic.php b/api/v3/Generic.php index 0eef89a7a2..586d34f5b9 100644 --- a/api/v3/Generic.php +++ b/api/v3/Generic.php @@ -419,8 +419,23 @@ function civicrm_api3_generic_getoptions($apiRequest) { CRM_Core_DAO::buildOptionsContext($context); unset($apiRequest['params']['context'], $apiRequest['params']['field'], $apiRequest['params']['condition']); - $baoName = _civicrm_api3_get_BAO($apiRequest['entity']); - $options = $baoName::buildOptions($fieldName, $context, $apiRequest['params']); + // Legacy support for campaign_id fields which used to have a pseudoconstant + if ($fieldName === 'campaign_id') { + $campaignParams = [ + 'select' => ['id', 'name', 'title'], + 'options' => ['limit' => 0], + ]; + if ($context === 'match' || $context === 'create') { + $campaignParams['is_active'] = 1; + } + $labelField = $context === 'validate' ? 'name' : 'title'; + $keyField = $context === 'match' ? 'name' : 'id'; + $options = array_column(civicrm_api3('Campaign', 'get', $campaignParams)['values'], $labelField, $keyField); + } + else { + $baoName = _civicrm_api3_get_BAO($apiRequest['entity']); + $options = $baoName::buildOptions($fieldName, $context, $apiRequest['params']); + } if ($options === FALSE) { return civicrm_api3_create_error("The field '{$fieldName}' has no associated option list."); } diff --git a/api/v3/utils.php b/api/v3/utils.php index 6b8712d44b..f0db700317 100644 --- a/api/v3/utils.php +++ b/api/v3/utils.php @@ -2062,7 +2062,7 @@ function _civicrm_api3_validate_integer(&$params, $fieldName, &$fieldInfo, $enti $fieldValue = NULL; } } - if (!empty($fieldInfo['pseudoconstant']) || !empty($fieldInfo['options'])) { + if (!empty($fieldInfo['pseudoconstant']) || !empty($fieldInfo['options']) || $fieldName === 'campaign_id') { $additional_lookup_params = []; if (strtolower($entity) == 'address' && $fieldName == 'state_province_id') { $country_id = _civicrm_api3_resolve_country_id($params); diff --git a/tests/phpunit/api/v3/MailingTest.php b/tests/phpunit/api/v3/MailingTest.php index 748a73278a..bbf99310ab 100644 --- a/tests/phpunit/api/v3/MailingTest.php +++ b/tests/phpunit/api/v3/MailingTest.php @@ -79,8 +79,9 @@ class api_v3_MailingTest extends CiviUnitTestCase { /** * Test civicrm_mailing_create. */ - public function testMailerCreateSuccess() { - $result = $this->callAPIAndDocument('mailing', 'create', $this->_params + ['scheduled_date' => 'now'], __FUNCTION__, __FILE__); + public function testMailerCreateSuccess(): void { + $this->callAPISuccess('Campaign', 'create', ['name' => 'big campaign', 'title' => 'abc']); + $result = $this->callAPIAndDocument('mailing', 'create', $this->_params + ['scheduled_date' => 'now', 'campaign_id' => 'big campaign'], __FUNCTION__, __FILE__); $jobs = $this->callAPISuccess('mailing_job', 'get', ['mailing_id' => $result['id']]); $this->assertEquals(1, $jobs['count']); // return isn't working on this in getAndCheck so lets not check it for now -- 2.25.1