From: Seamus Lee Date: Thu, 6 Sep 2018 04:58:08 +0000 (+1000) Subject: dev/core#382 Ensure that no db errors are generated when trying to update a civicrm... X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=1321b65beced9dd703734dc3c96204de6e12f6ab;p=civicrm-core.git dev/core#382 Ensure that no db errors are generated when trying to update a civicrm group linked to an organization when the group id doesn't match the id of the relevant row in civicrm_group_organization Only Pass necessary params to GroupOrganization::add function --- diff --git a/CRM/Contact/BAO/Group.php b/CRM/Contact/BAO/Group.php index a5374fe7a6..1cc2bd19d0 100644 --- a/CRM/Contact/BAO/Group.php +++ b/CRM/Contact/BAO/Group.php @@ -448,8 +448,11 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group { } if (!empty($params['organization_id'])) { - $groupOrg = $params; - $groupOrg['group_id'] = $group->id; + // dev/core#382 Keeping the id here can cause db errors as it tries to update the wrong record in the Organization table + $groupOrg = [ + 'group_id' => $group->id, + 'organization_id' => $params['organization_id'], + ]; CRM_Contact_BAO_GroupOrganization::add($groupOrg); } diff --git a/tests/phpunit/CRM/Contact/BAO/GroupTest.php b/tests/phpunit/CRM/Contact/BAO/GroupTest.php index 5aa7183348..279c24fbc9 100644 --- a/tests/phpunit/CRM/Contact/BAO/GroupTest.php +++ b/tests/phpunit/CRM/Contact/BAO/GroupTest.php @@ -199,4 +199,42 @@ class CRM_Contact_BAO_GroupTest extends CiviUnitTestCase { } } + /** + * Ensure that when updating a group with a linked organisation record even tho that record's id doesn't match the group id no db error is produced + */ + public function testGroupUpdateWithOrganization() { + $params = array( + 'name' => uniqid(), + 'title' => 'Group A', + 'description' => 'Group One', + 'visibility' => 'User and User Admin Only', + 'is_active' => 1, + ); + $group1 = CRM_Contact_BAO_Group::create($params); + + $domain1 = $this->callAPISuccess('Domain', 'get', ['id' => 1]); + $params2 = array( + 'name' => uniqid(), + 'title' => 'Group B', + 'description' => 'Group Two', + 'visibility' => 'User and User Admin Only', + 'is_active' => 1, + 'organization_id' => $domain1['values'][1]['contact_id'], + ); + $group2 = CRM_Contact_BAO_Group::create($params2); + + $domain2 = $this->callAPISuccess('Domain', 'get', ['id' => 2]); + $params3 = array( + 'name' => uniqid(), + 'title' => 'Group C', + 'description' => 'Group Three', + 'visibility' => 'User and User Admin Only', + 'is_active' => 1, + 'organization_id' => $domain2['values'][2]['contact_id'], + ); + $group3 = CRM_Contact_BAO_Group::create($params3); + $params2['id'] = $group2->id; + $testUpdate = CRM_Contact_BAO_Group::create($params2); + } + }