From ef8b0f5bb27930243ba0a7a8067b501b710caf38 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Thu, 11 Aug 2016 06:35:35 +1000 Subject: [PATCH] CRM-19209 fix regression found when parents param is empty and CRM-18591 fix issue where group_type param in api was strange fixed in master --- CRM/Contact/BAO/Group.php | 7 ++++-- CRM/Group/Form/Edit.php | 11 ++++++++++ tests/phpunit/api/v3/GroupTest.php | 34 ++++++++++++++++++++++++++++++ xml/schema/Contact/Group.xml | 3 +++ 4 files changed, 53 insertions(+), 2 deletions(-) diff --git a/CRM/Contact/BAO/Group.php b/CRM/Contact/BAO/Group.php index a470843154..6fca737351 100644 --- a/CRM/Contact/BAO/Group.php +++ b/CRM/Contact/BAO/Group.php @@ -368,9 +368,12 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group { if (isset($params['group_type'])) { if (is_array($params['group_type'])) { $params['group_type'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, - array_keys($params['group_type']) + $params['group_type'] ) . CRM_Core_DAO::VALUE_SEPARATOR; } + else { + $params['group_type'] = CRM_Core_DAO::VALUE_SEPARATOR . $params['group_type'] . CRM_Core_DAO::VALUE_SEPARATOR; + } } else { $params['group_type'] = ''; @@ -389,7 +392,7 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group { // CRM-19068. // Validate parents parameter when creating group. - if (isset($params['parents'])) { + if (!empty($params['parents'])) { if (is_array($params['parents'])) { foreach ($params['parents'] as $parent => $dc) { CRM_Utils_Type::validate($parent, 'Integer'); diff --git a/CRM/Group/Form/Edit.php b/CRM/Group/Form/Edit.php index a6733dea09..0557549f96 100644 --- a/CRM/Group/Form/Edit.php +++ b/CRM/Group/Form/Edit.php @@ -395,6 +395,17 @@ WHERE title = %1 $params['is_reserved'] = CRM_Utils_Array::value('is_reserved', $params, FALSE); + $groupTypeIds = array(); + $groupType = CRM_Utils_Array::value('group_type', $params); + if (is_array($groupType)) { + foreach ($groupType as $type => $selected) { + if ($selected) { + $groupTypeIds[] = $type; + } + } + } + $params['group_type'] = $groupTypeIds; + $customFields = CRM_Core_BAO_CustomField::getFields('Group'); $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, $customFields, diff --git a/tests/phpunit/api/v3/GroupTest.php b/tests/phpunit/api/v3/GroupTest.php index 311e1f93bc..8f126b1361 100644 --- a/tests/phpunit/api/v3/GroupTest.php +++ b/tests/phpunit/api/v3/GroupTest.php @@ -128,6 +128,40 @@ class api_v3_GroupTest extends CiviUnitTestCase { } } + /** + * Test Group create with Group Type and Parent + */ + public function testGroupCreateWithTypeAndParent() { + $params = array( + 'name' => 'Test Group type', + 'title' => 'Test Group Type', + 'description' => 'Test Group with Group Type', + 'is_active' => 1, + //check for empty parent + 'parents' => "", + 'visibility' => 'Public Pages', + 'group_type' => array(1, 2), + ); + $result = $this->callAPISuccess('Group', 'create', $params); + $group = $result['values'][$result['id']]; + $this->assertEquals($group['name'], "Test Group type"); + $this->assertEquals($group['is_active'], 1); + $this->assertEquals($group['parents'], ""); + $this->assertEquals($group['group_type'], $params['group_type']); + //assert single value for group_type and parent + $params = array_merge($params, array( + 'name' => 'Test Group 2', + 'title' => 'Test Group 2', + 'group_type' => 2, + 'parents' => $result['id'], + ) + ); + $result = $this->callAPISuccess('Group', 'create', $params); + $group = $result["values"][$result['id']]; + $this->assertEquals($group['group_type'], array($params['group_type'])); + $this->assertEquals($group['parents'], $params['parents']); + } + public function testGetNonExistingGroup() { $params = array(); $params['title'] = 'No such group Exist'; diff --git a/xml/schema/Contact/Group.xml b/xml/schema/Contact/Group.xml index 5f09e4147e..fb9c1bd671 100644 --- a/xml/schema/Contact/Group.xml +++ b/xml/schema/Contact/Group.xml @@ -119,6 +119,9 @@ Group Type 128 FK to group type + + group_type + 1.9 -- 2.25.1