dev/core#382 Ensure that no db errors are generated when trying to update a civicrm...
authorSeamus Lee <seamuslee001@gmail.com>
Thu, 6 Sep 2018 04:58:08 +0000 (14:58 +1000)
committerSeamus Lee <seamuslee001@gmail.com>
Fri, 7 Sep 2018 21:47:53 +0000 (07:47 +1000)
Only Pass necessary params to GroupOrganization::add function

CRM/Contact/BAO/Group.php
tests/phpunit/CRM/Contact/BAO/GroupTest.php

index a5374fe7a68f22a222d37627518621b6b9e49773..1cc2bd19d05b2c50ca7f8fa43c98e39ac77346b0 100644 (file)
@@ -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);
     }
 
index 5aa7183348e5d474c9be66f685f13a196b40e6db..279c24fbc9239cdfc53033ef1b04123042547e46 100644 (file)
@@ -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);
+  }
+
 }