From 58159c3be3f1c3f18d23283a4d7ba82d88ef5919 Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 28 Oct 2019 21:09:29 +1300 Subject: [PATCH] dev/core#1282, 1324 unit test for bug --- CRM/Core/BAO/CustomField.php | 3 +++ CRM/Core/BAO/UFGroup.php | 8 ++++---- api/v3/Profile.php | 3 ++- tests/phpunit/api/v3/ProfileTest.php | 26 ++++++++++++++++++++++++++ 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index f0687c82e7..2f7910611b 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -159,6 +159,9 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { CRM_Utils_Hook::post(($op === 'add' ? 'create' : 'edit'), 'CustomField', $customField->id, $customField); CRM_Utils_System::flushCache(); + // Flush caches is not aggressive about clearing the specific cache we know we want to clear + // so do it manually. Ideally we wouldn't need to clear others... + Civi::cache('metadata')->clear(); return $customField; } diff --git a/CRM/Core/BAO/UFGroup.php b/CRM/Core/BAO/UFGroup.php index fca57c0d9c..00654a3c20 100644 --- a/CRM/Core/BAO/UFGroup.php +++ b/CRM/Core/BAO/UFGroup.php @@ -741,8 +741,8 @@ class CRM_Core_BAO_UFGroup extends CRM_Core_DAO_UFGroup { * @return mixed */ protected static function getCustomFields($ctype) { - static $customFieldCache = []; - if (!isset($customFieldCache[$ctype])) { + $cacheKey = 'uf_group_custom_fields_' . $ctype; + if (!Civi::cache('metadata')->has($cacheKey)) { $customFields = CRM_Core_BAO_CustomField::getFieldsForImport($ctype, FALSE, FALSE, FALSE, TRUE, TRUE); // hack to add custom data for components @@ -752,9 +752,9 @@ class CRM_Core_BAO_UFGroup extends CRM_Core_DAO_UFGroup { } $addressCustomFields = CRM_Core_BAO_CustomField::getFieldsForImport('Address'); $customFields = array_merge($customFields, $addressCustomFields); - $customFieldCache[$ctype] = [$customFields, $addressCustomFields]; + Civi::cache('metadata')->set($cacheKey, [$customFields, $addressCustomFields]); } - return $customFieldCache[$ctype]; + return Civi::cache('metadata')->get($cacheKey); } /** diff --git a/api/v3/Profile.php b/api/v3/Profile.php index 1d6cfe80b9..e5fe71f802 100644 --- a/api/v3/Profile.php +++ b/api/v3/Profile.php @@ -47,8 +47,9 @@ * Associative array of property name/value. * pairs to get profile field values * - * @throws API_Exception * @return array + * @throws \CRM_Core_Exception + * @throws API_Exception */ function civicrm_api3_profile_get($params) { $nonStandardLegacyBehaviour = is_numeric($params['profile_id']) ? TRUE : FALSE; diff --git a/tests/phpunit/api/v3/ProfileTest.php b/tests/phpunit/api/v3/ProfileTest.php index 31e8cbf89b..9c9945ed6f 100644 --- a/tests/phpunit/api/v3/ProfileTest.php +++ b/tests/phpunit/api/v3/ProfileTest.php @@ -32,6 +32,7 @@ * @group headless */ class api_v3_ProfileTest extends CiviUnitTestCase { + use CRMTraits_Custom_CustomDataTrait; protected $_profileID = 0; @@ -94,6 +95,8 @@ class api_v3_ProfileTest extends CiviUnitTestCase { /** * Check with success. + * + * @throws \CRM_Core_Exception */ public function testProfileGet() { $profileFieldValues = $this->_createIndividualContact(); @@ -109,6 +112,29 @@ class api_v3_ProfileTest extends CiviUnitTestCase { } } + /** + * Test retrieving a profile with an address custom field in it. + * + * We are checking that there is no error. + * + * @throws \CRM_Core_Exception + */ + public function testProfileGetWithAddressCustomData() { + $this->_createIndividualContact(); + $this->entity = 'Address'; + $this->createCustomGroupWithFieldOfType(['extends' => 'Address']); + $this->callAPISuccess('UFField', 'create', [ + 'uf_group_id' => $this->_profileID, + 'field_name' => $this->getCustomFieldName('text'), + 'visibility' => 'Public Pages and Listings', + 'label' => 'My custom field', + 'field_type' => 'Contact', + ]); + $this->callAPISuccess('Address', 'get', ['contact_id' => $this->_contactID, 'api.Address.create' => [$this->getCustomFieldName('text') => 'my field']]); + $result = $this->callAPISuccess('Profile', 'get', ['profile_id' => $this->_profileID, 'contact_id' => $this->_contactID])['values']; + // $this->assertEquals('my field', $result[$this->getCustomFieldName('text')]); + } + /** * Test getting multiple profiles. */ -- 2.25.1