Skip to content

Commit

Permalink
Fix regression causing custom groups to reset to 'Contact' when updated
Browse files Browse the repository at this point in the history
Fixes dev/core#3794
  • Loading branch information
colemanw committed Aug 10, 2022
1 parent 68c12d8 commit 9ad9e23
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
11 changes: 6 additions & 5 deletions CRM/Core/BAO/CustomGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ public static function self_hook_civicrm_post(\Civi\Core\Event\PostEvent $e): vo
}

/**
* 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.
Expand All @@ -40,8 +39,10 @@ public static function self_hook_civicrm_post(\Civi\Core\Event\PostEvent $e): vo
* @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'])) {
Expand Down
54 changes: 54 additions & 0 deletions tests/phpunit/api/v4/Custom/CustomGroupTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

/**
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/


namespace api\v4\Custom;

use Civi\Api4\CustomGroup;

/**
* @group headless
*/
class CustomGroupTest extends CustomTestBase {

public function testUpdateCustomGroup() {
$customGroup1 = $this->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']);
}

}

0 comments on commit 9ad9e23

Please sign in to comment.