From 0c66b30c77e71993f64df19a77bbeaf679ea472e Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Wed, 22 Feb 2017 11:00:39 +1100 Subject: [PATCH] Tidy up checks as per discussion with Eileen and add new test of array format --- api/v3/Contact.php | 5 ++- tests/phpunit/api/v3/ContactTest.php | 57 ++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/api/v3/Contact.php b/api/v3/Contact.php index 2f41854d06..6a930e8ca2 100644 --- a/api/v3/Contact.php +++ b/api/v3/Contact.php @@ -380,14 +380,17 @@ function _civicrm_api3_contact_get_supportanomalies(&$params, &$options) { $groups = $params['group']; $allGroups = CRM_Core_PseudoConstant::group(); if (is_array($groups) && in_array(key($groups), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) { + // Get the groups array. $groupsArray = $groups[key($groups)]; - foreach ($groupsArray as $group => &$ignore) { + foreach ($groupsArray as &$group) { if (!is_numeric($group) && array_search($group, $allGroups)) { $group = array_search($group, $allGroups); } } + // Now reset the $groups array with the ids not the titles. $groups[key($groups)] = $groupsArray; } + // handle format like 'group' => array('title1', 'title2'). elseif (is_array($groups)) { foreach ($groups as $k => &$group) { if (array_search($group, $allGroups)) { diff --git a/tests/phpunit/api/v3/ContactTest.php b/tests/phpunit/api/v3/ContactTest.php index 4ed26e123a..815dd35318 100644 --- a/tests/phpunit/api/v3/ContactTest.php +++ b/tests/phpunit/api/v3/ContactTest.php @@ -3168,6 +3168,9 @@ class api_v3_ContactTest extends CiviUnitTestCase { /** * CRM-20144 Verify that passing title of group works as well as id + * Tests the following formats + * contact.get group='title1' + * contact.get group=id1 */ public function testContactGetWithGroupTitle() { // Set up a contact, asser that they were created. @@ -3209,4 +3212,58 @@ 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', title1) + * contact.get group=array('IN' => array('title1', 'title2) + */ + public function testContactGetWithGroupTitleMultipleGroups() { + // 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' => $createdGroupTitles, '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('IN' => $createdGroupTitles), 'return' => 'group')); + $this->assertEquals($created_contact_id, $contact_get2['id']); + $contact_groups2 = explode(',', $contact_get2['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)); + } + } -- 2.25.1