From 9ad9e2311f645b6b715710b85a5b5a10c20c776a Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 10 Aug 2022 10:58:36 -0400 Subject: [PATCH] Fix regression causing custom groups to reset to 'Contact' when updated Fixes dev/core#3794 --- CRM/Core/BAO/CustomGroup.php | 11 ++-- .../phpunit/api/v4/Custom/CustomGroupTest.php | 54 +++++++++++++++++++ 2 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 tests/phpunit/api/v4/Custom/CustomGroupTest.php diff --git a/CRM/Core/BAO/CustomGroup.php b/CRM/Core/BAO/CustomGroup.php index 3b6e9269cf..9dc9cf3273 100644 --- a/CRM/Core/BAO/CustomGroup.php +++ b/CRM/Core/BAO/CustomGroup.php @@ -29,9 +29,8 @@ class CRM_Core_BAO_CustomGroup extends CRM_Core_DAO_CustomGroup implements \Civi } /** - * Takes an associative array and creates a custom group object. - * - * This function is invoked from within the web form layer and also from the api layer + * FIXME: This function is too complex because it's trying to handle both api-style inputs and + * quickform inputs. Needs to be deprecated and broken up. * * @param array $params * (reference) an assoc array of name/value pairs. @@ -40,8 +39,10 @@ class CRM_Core_BAO_CustomGroup extends CRM_Core_DAO_CustomGroup implements \Civi * @throws \Exception */ public static function create(&$params) { - // This is the database default - $params += ['extends' => 'Contact']; + // FIXME: This is needed by the form parsing code below + if (empty($params['id'])) { + $params += ['extends' => 'Contact']; + } // create custom group dao, populate fields and then save. $group = new CRM_Core_DAO_CustomGroup(); if (isset($params['title'])) { diff --git a/tests/phpunit/api/v4/Custom/CustomGroupTest.php b/tests/phpunit/api/v4/Custom/CustomGroupTest.php new file mode 100644 index 0000000000..7e85a43c2c --- /dev/null +++ b/tests/phpunit/api/v4/Custom/CustomGroupTest.php @@ -0,0 +1,54 @@ +createTestRecord('CustomGroup', [ + 'extends' => 'Contribution', + 'weight' => 1, + ]); + $customGroup2 = $this->createTestRecord('CustomGroup', [ + 'extends' => 'Contribution', + ]); + + CustomGroup::update(FALSE) + ->addValue('weight', 1) + ->addWhere('id', '=', $customGroup2['id']) + ->execute(); + + $groups = CustomGroup::get(FALSE) + ->addWhere('id', 'IN', [$customGroup1['id'], $customGroup2['id']]) + ->addOrderBy('id') + ->execute(); + + $this->assertEquals(1, $groups[1]['weight']); + $this->assertEquals(2, $groups[0]['weight']); + $this->assertEquals('Contribution', $groups[0]['extends']); + $this->assertEquals('Contribution', $groups[1]['extends']); + } + +} -- 2.25.1