From 309f98a98e2cebe05c88bb9555c9587f6938d417 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Tue, 21 Feb 2017 20:44:06 +1100 Subject: [PATCH] CRM-20144 Fix contact.get api break where groups no longer returned if using title in params --- CRM/Contact/BAO/Query.php | 13 ++++++++- tests/phpunit/api/v3/ContactTest.php | 43 ++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index d7e47b6520..fe4a03b77b 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -2942,6 +2942,18 @@ class CRM_Contact_BAO_Query { $regularGroupIDs = $smartGroupIDs = array(); foreach ((array) $value as $id) { + if (!is_numeric($id)) { + $check = civicrm_api3('group', 'get', array('name' => $id, 'options' => array('limit' => 1), 'sequential' => 1)); + if (!empty($check['values'])) { + $id = $check['values'][0]['id']; + } + else { + $check = civicrm_api3('group', 'get', array('title' => $id, 'options' => array('limit' => 1), 'sequential' => 1)); + if (!empty($check['values'])) { + $id = $check['values'][0]['id']; + } + } + } if (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $id, 'saved_search_id')) { $smartGroupIDs[] = $id; } @@ -2966,7 +2978,6 @@ class CRM_Contact_BAO_Query { else { $statii[] = "'Added'"; } - $groupClause = array(); if (count($regularGroupIDs) || empty($value)) { $groupIds = implode(',', (array) $regularGroupIDs); diff --git a/tests/phpunit/api/v3/ContactTest.php b/tests/phpunit/api/v3/ContactTest.php index 6bbcacc7e6..4ed26e123a 100644 --- a/tests/phpunit/api/v3/ContactTest.php +++ b/tests/phpunit/api/v3/ContactTest.php @@ -3166,4 +3166,47 @@ class api_v3_ContactTest extends CiviUnitTestCase { } } + /** + * CRM-20144 Verify that passing title of group works as well as id + */ + public function testContactGetWithGroupTitle() { + // Set up a contact, asser that they were created. + $contact_params = array( + 'contact_type' => 'Individual', + 'first_name' => 'Test2', + 'last_name' => 'Groupmember', + 'email' => 'test@example.org', + ); + $create_contact = $this->callApiSuccess('Contact', 'create', $contact_params); + $created_contact_id = $create_contact['id']; + // Set up multiple groups, add the contact to the groups. + $test_groups = array('Test group C', 'Test group D'); + foreach ($test_groups as $title) { + $group_params = array( + 'title' => $title, + 'created_id' => $created_contact_id, + ); + $create_group = $this->callApiSuccess('Group', 'create', $group_params); + $created_group_id = $create_group['id']; + + // Add contact to the new group. + $group_contact_params = array( + 'contact_id' => $created_contact_id, + 'group_id' => $create_group['id'], + ); + $create_group_contact = $this->callApiSuccess('GroupContact', 'create', $group_contact_params); + $contact_get = $this->callAPISuccess('contact', 'get', array('group' => $title, 'return' => 'group')); + $this->assertEquals(1, $contact_get['count']); + $this->assertEquals($created_contact_id, $contact_get['id']); + $contact_groups = explode(',', $contact_get['values'][$created_contact_id]['groups']); + $this->assertContains((string) $create_group['id'], $contact_groups); + $contact_get2 = $this->callAPISuccess('contact', 'get', array('group' => $created_group_id, 'return' => 'group')); + $this->assertEquals($created_contact_id, $contact_get2['id']); + $contact_groups2 = explode(',', $contact_get2['values'][$created_contact_id]['groups']); + $this->assertContains((string) $create_group['id'], $contact_groups2); + $this->callAPISuccess('group', 'delete', array('id' => $created_group_id)); + } + $this->callAPISuccess('contact', 'delete', array('id' => $created_contact_id, 'skip_undelete' => TRUE)); + } + } -- 2.25.1