is_active should default to true if missing
authordemeritcowboy <demeritcowboy@hotmail.com>
Wed, 7 Apr 2021 13:10:04 +0000 (09:10 -0400)
committerdemeritcowboy <demeritcowboy@hotmail.com>
Wed, 7 Apr 2021 21:39:53 +0000 (17:39 -0400)
CRM/Core/BAO/UFGroup.php
tests/phpunit/api/v3/UFGroupTest.php

index a4f56910d88f410401d25e58733cd9507d5cd3de..d34bec68ed0fd12f45806d0b8a8f9ee94b166034 100644 (file)
@@ -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'])) {
index dd3c5dcb7442b23f3ffb4e00d7f29f450c2cd50d..62284634e4ae357114eb17b5b8fcc57164dc4c02 100644 (file)
@@ -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']);
+    }
+  }
+
 }