From 9a1407dca96457d47fc3be2d9dc76348118ea896 Mon Sep 17 00:00:00 2001 From: demeritcowboy Date: Wed, 7 Apr 2021 09:10:04 -0400 Subject: [PATCH] is_active should default to true if missing --- CRM/Core/BAO/UFGroup.php | 23 ++++++-------- tests/phpunit/api/v3/UFGroupTest.php | 47 ++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 14 deletions(-) diff --git a/CRM/Core/BAO/UFGroup.php b/CRM/Core/BAO/UFGroup.php index a4f56910d8..d34bec68ed 100644 --- a/CRM/Core/BAO/UFGroup.php +++ b/CRM/Core/BAO/UFGroup.php @@ -1439,21 +1439,16 @@ class CRM_Core_BAO_UFGroup extends CRM_Core_DAO_UFGroup { $params['id'] = $ids['ufgroup']; CRM_Core_Error::deprecatedWarning('ids parameter is deprecated'); } - $fields = [ - 'is_active', - 'add_captcha', - 'is_map', - 'is_update_dupe', - 'is_edit_link', - 'is_uf_link', - 'is_cms_user', - ]; - foreach ($fields as $field) { - $params[$field] = CRM_Utils_Array::value($field, $params, FALSE); - } - $params['limit_listings_group_id'] = $params['group'] ?? NULL; - $params['add_to_group_id'] = $params['add_contact_to_group'] ?? NULL; + // Convert parameter names but don't overwrite existing data on updates + // unless explicitly specified. And allow setting to null, so use + // array_key_exists. i.e. we need to treat missing and empty separately. + if (array_key_exists('group', $params)) { + $params['limit_listings_group_id'] = $params['group']; + } + if (array_key_exists('add_contact_to_group', $params)) { + $params['add_to_group_id'] = $params['add_contact_to_group']; + } //CRM-15427 if (!empty($params['group_type']) && is_array($params['group_type'])) { diff --git a/tests/phpunit/api/v3/UFGroupTest.php b/tests/phpunit/api/v3/UFGroupTest.php index dd3c5dcb74..62284634e4 100644 --- a/tests/phpunit/api/v3/UFGroupTest.php +++ b/tests/phpunit/api/v3/UFGroupTest.php @@ -100,6 +100,21 @@ class api_v3_UFGroupTest extends CiviUnitTestCase { } } + /** + * Test what happens if you don't set is_active + * @param int $version + * @dataProvider versionThreeAndFour + */ + public function testUpdateUFGroupActiveMissing($version) { + $this->_apiversion = $version; + $this->callAPISuccess('uf_group', 'create', [ + 'title' => 'Edited Test Profile', + 'id' => $this->_ufGroupId, + ]); + $result = $this->callAPISuccess('uf_group', 'getsingle', ['id' => $this->_ufGroupId]); + $this->assertSame($this->_apiversion === 3 ? '1' : TRUE, $result['is_active']); + } + /** * @param int $version * @dataProvider versionThreeAndFour @@ -227,4 +242,36 @@ class api_v3_UFGroupTest extends CiviUnitTestCase { $this->assertEquals(0, $this->callAPISuccess('uf_group', 'getcount', $params), "in line " . __LINE__); } + /** + * Test creating a profile with an "Add contact to group" group set and + * then removing it. It should get removed. + * It's complicated by the fact that the parameter name is not the same as + * the field name. + * @param int $version + * @dataProvider versionThreeAndFour + */ + public function testUFGroupUnsetAddToGroup($version) { + $this->_apiversion = $version; + + // sanity check + $this->assertNotEmpty($this->params['add_contact_to_group']); + + $ufGroup = $this->callAPISuccess('uf_group', 'create', $this->params); + $result = $this->callAPISuccess('uf_group', 'getsingle', ['id' => $ufGroup['id']]); + $this->assertEquals($this->params['add_contact_to_group'], $result['add_to_group_id']); + + // now remove it + $this->callAPISuccess('uf_group', 'create', [ + 'id' => $ufGroup['id'], + 'add_contact_to_group' => '', + ]); + $result = $this->callAPISuccess('uf_group', 'getsingle', ['id' => $ufGroup['id']]); + if ($this->_apiversion === 3) { + $this->assertFalse(array_key_exists('add_to_group_id', $result)); + } + else { + $this->assertNull($result['add_to_group_id']); + } + } + } -- 2.25.1