From: Seamus Lee Date: Wed, 22 Feb 2017 20:25:22 +0000 (+1100) Subject: Add support for some legacy handling of group param as well that maybe out in the... X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=b6b28d93f3066546cca4c37ebe9a64c69091496b;p=civicrm-core.git Add support for some legacy handling of group param as well that maybe out in the wild --- diff --git a/api/v3/Contact.php b/api/v3/Contact.php index 6a930e8ca2..a01fdeabcf 100644 --- a/api/v3/Contact.php +++ b/api/v3/Contact.php @@ -393,9 +393,13 @@ function _civicrm_api3_contact_get_supportanomalies(&$params, &$options) { // handle format like 'group' => array('title1', 'title2'). elseif (is_array($groups)) { foreach ($groups as $k => &$group) { - if (array_search($group, $allGroups)) { + if (!is_numeric($group) && array_search($group, $allGroups)) { $group = array_search($group, $allGroups); } + if (!is_numeric($k) && array_search($k, $allGroups)) { + unset($groups[$k]); + $groups[array_search($k, $allGroups)] = $group; + } } } elseif (!is_numeric($groups) && array_search($groups, $allGroups)) { diff --git a/tests/phpunit/api/v3/ContactTest.php b/tests/phpunit/api/v3/ContactTest.php index 815dd35318..30b5c9d81e 100644 --- a/tests/phpunit/api/v3/ContactTest.php +++ b/tests/phpunit/api/v3/ContactTest.php @@ -3266,4 +3266,73 @@ class api_v3_ContactTest extends CiviUnitTestCase { $this->callAPISuccess('contact', 'delete', array('id' => $created_contact_id, 'skip_undelete' => TRUE)); } + /** + * CRM-20144 Verify that passing title of group works as well as id + * Tests the following formats + * contact.get group=array('title1' => 1) + * contact.get group=array('titke1' => 1, 'title2' => 1) + * contact.get group=array('id1' => 1) + * contact.get group=array('id1' => 1, id2 => 1) + */ + public function testContactGetWithGroupTitleMultipleGroupsLegacyFormat() { + // 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']; + $createdGroupsTitles = $createdGroupsIds = array(); + // 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']; + $createdGroupsIds[] = $create_group['id']; + $createdGroupTitles[] = $title; + // 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' => array($createdGroupTitles[0] => 1), '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']); + foreach ($createdGroupsIds as $id) { + $this->assertContains((string) $id, $contact_groups); + } + $contact_get2 = $this->callAPISuccess('contact', 'get', array('group' => array($createdGroupTitles[0] => 1, $createdGroupTitles[1] => 1), 'return' => 'group')); + $this->assertEquals(1, $contact_get2['count']); + $this->assertEquals($created_contact_id, $contact_get2['id']); + $contact_groups = explode(',', $contact_get2['values'][$created_contact_id]['groups']); + foreach ($createdGroupsIds as $id) { + $this->assertContains((string) $id, $contact_groups); + } + $contact_get3 = $this->callAPISuccess('contact', 'get', array('group' => array($createdGroupsIds[0] => 1), 'return' => 'group')); + $this->assertEquals($created_contact_id, $contact_get3['id']); + $contact_groups3 = explode(',', $contact_get3['values'][$created_contact_id]['groups']); + foreach ($createdGroupsIds as $id) { + $this->assertContains((string) $id, $contact_groups2); + } + $contact_get4 = $this->callAPISuccess('contact', 'get', array('group' => array($createdGroupsIds[0] => 1, $createdGroupsIds[1] => 1), 'return' => 'group')); + $this->assertEquals($created_contact_id, $contact_get4['id']); + $contact_groups3 = explode(',', $contact_get4['values'][$created_contact_id]['groups']); + foreach ($createdGroupsIds as $id) { + $this->assertContains((string) $id, $contact_groups2); + } + foreach ($createdGroupsIds as $id) { + $this->callAPISuccess('group', 'delete', array('id' => $id)); + } + $this->callAPISuccess('contact', 'delete', array('id' => $created_contact_id, 'skip_undelete' => TRUE)); + } + }